mirror of
https://github.com/react/react-native-devtools-frontend.git
synced 2026-09-28 21:24:26 +08:00
This CL adds the eslint-plugin-jsdoc plugin and enables just one rule, `jsdoc/check-alignment`, which ensures that all the lines in a comment are correctly aligned. For example: ``` /** * foo * bar */ ``` Would fail the check and be corrected to: ``` /** * foo * bar */ ``` DISABLE_THIRD_PARTY_CHECK=deliberate install + use of eslint-plugin-jsdoc node_module Bug: none Change-Id: I969983cd550653654a334ede3409b8c850467245 Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/4110906 Commit-Queue: Andres Olivares <andoli@chromium.org> Reviewed-by: Andres Olivares <andoli@chromium.org> Auto-Submit: Jack Franklin <jacktfranklin@chromium.org> Commit-Queue: Jack Franklin <jacktfranklin@chromium.org>
121 lines
3.0 KiB
JavaScript
121 lines
3.0 KiB
JavaScript
/* eslint-disable prefer-named-capture-group -- Temporary */
|
|
import {
|
|
parse as commentParser,
|
|
tokenizers
|
|
} from 'comment-parser';
|
|
|
|
const {
|
|
name: nameTokenizer,
|
|
tag: tagTokenizer,
|
|
type: typeTokenizer,
|
|
description: descriptionTokenizer
|
|
} = tokenizers;
|
|
|
|
export const hasSeeWithLink = (spec) => {
|
|
return spec.tag === 'see' && (/\{@link.+?\}/u).test(spec.source[0].source);
|
|
};
|
|
|
|
export const defaultNoTypes = [
|
|
'default', 'defaultvalue', 'description', 'example',
|
|
'file', 'fileoverview', 'license',
|
|
'overview', 'see', 'summary'
|
|
];
|
|
|
|
export const defaultNoNames = [
|
|
'access', 'author',
|
|
'default', 'defaultvalue',
|
|
'description',
|
|
'example', 'exception', 'file', 'fileoverview',
|
|
'kind',
|
|
'license', 'overview',
|
|
'return', 'returns',
|
|
'since', 'summary',
|
|
'throws',
|
|
'version', 'variation'
|
|
];
|
|
|
|
const optionalBrackets = /^\[(?<name>[^=]*)=[^\]]*\]/u;
|
|
const preserveTypeTokenizer = typeTokenizer('preserve');
|
|
const preserveDescriptionTokenizer = descriptionTokenizer('preserve');
|
|
const plainNameTokenizer = nameTokenizer();
|
|
|
|
const getTokenizers = ({
|
|
noTypes = defaultNoTypes,
|
|
noNames = defaultNoNames
|
|
} = {}) => {
|
|
// trim
|
|
return [
|
|
// Tag
|
|
tagTokenizer(),
|
|
|
|
// Type
|
|
(spec) => {
|
|
if (noTypes.includes(spec.tag)) {
|
|
return spec;
|
|
}
|
|
|
|
return preserveTypeTokenizer(spec);
|
|
},
|
|
|
|
// Name
|
|
(spec) => {
|
|
if (spec.tag === 'template') {
|
|
// const preWS = spec.postTag;
|
|
const remainder = spec.source[0].tokens.description;
|
|
|
|
const pos = remainder.search(/(?<![\s,])\s/u);
|
|
|
|
let name = pos === -1 ? remainder : remainder.slice(0, pos);
|
|
const extra = remainder.slice(pos);
|
|
let postName = '', description = '', lineEnd = '';
|
|
if (pos > -1) {
|
|
[, postName, description, lineEnd] = extra.match(/(\s*)([^\r]*)(\r)?/u);
|
|
}
|
|
|
|
if (optionalBrackets.test(name)) {
|
|
name = name.match(optionalBrackets)?.groups?.name;
|
|
spec.optional = true;
|
|
} else {
|
|
spec.optional = false;
|
|
}
|
|
|
|
spec.name = name;
|
|
const {tokens} = spec.source[0];
|
|
tokens.name = name;
|
|
tokens.postName = postName;
|
|
tokens.description = description;
|
|
tokens.lineEnd = lineEnd || '';
|
|
|
|
return spec;
|
|
}
|
|
|
|
if (noNames.includes(spec.tag) || hasSeeWithLink(spec)) {
|
|
return spec;
|
|
}
|
|
|
|
return plainNameTokenizer(spec);
|
|
},
|
|
|
|
// Description
|
|
(spec) => {
|
|
return preserveDescriptionTokenizer(spec);
|
|
}
|
|
];
|
|
};
|
|
|
|
/**
|
|
* Accepts a comment token and converts it into `comment-parser` AST.
|
|
* @param {PlainObject} commentNode
|
|
* @param {string} [indent=""] Whitespace
|
|
* @returns {PlainObject}
|
|
*/
|
|
const parseComment = (commentNode, indent = '') => {
|
|
// Preserve JSDoc block start/end indentation.
|
|
return commentParser(`${indent}/*${commentNode.value}*/`, {
|
|
// @see https://github.com/yavorskiy/comment-parser/issues/21
|
|
tokenizers: getTokenizers()
|
|
})[0];
|
|
};
|
|
|
|
export {getTokenizers, parseComment};
|