eslint-plugin-complete
Version:
An ESLint plugin that contains useful rules.
68 lines (67 loc) • 2.32 kB
JavaScript
/**
* Returns false for trailing comments like:
*
* ```ts
* const abc = 123; // Foo
* ```
*/
export function isCommentOnOwnLine(sourceCode, comment) {
const startLine = comment.loc.start.line;
const endLine = comment.loc.end.line;
const previousToken = sourceCode.getTokenBefore(comment);
const previousTokenEndLine = previousToken === null ? null : previousToken.loc.end.line;
const nextToken = sourceCode.getTokenAfter(comment);
const nextTokenStartLine = nextToken === null ? null : nextToken.loc.start.line;
return startLine !== previousTokenEndLine && endLine !== nextTokenStartLine;
}
export function isEnumBlockLabel(text) {
text = text.trim();
return (
// e.g. CollectibleType.SAD_ONION
/^\w+\.\w+$/.test(text)
// e.g. CollectibleType.SAD_ONION (1)
|| /^\w+\.\w+ \(\d+\)$/.test(text)
// e.g. CacheFlag.FIRE_DELAY (1 << 1)
|| /^\w+\.\w+ \(\d+ << \d+\)$/.test(text)
// e.g. 1
|| /^\d+$/.test(text)
// e.g. 1.0
|| /^\d+\.\d+$/.test(text)
// e.g. 1 << 1
|| /^\d+ << \d+$/.test(text)
// e.g. 1, 2, 3, 4, 5
|| /^\d+, \d+$/.test(text)
|| /^\d+, \d+, \d+$/.test(text)
|| /^(?:\d+, ){3}\d+$/.test(text)
|| /^(?:\d+, ){4}\d+$/.test(text)
// e.g. 1.0, 2.0, 3.0, 4.0, 5.0
|| /^\d+\.\d+, \d+\.\d+$/.test(text)
|| /^(?:\d+\.\d+, ){2}\d+\.\d+$/.test(text)
|| /^(?:\d+\.\d+, ){3}\d+\.\d+$/.test(text)
|| /^(?:\d+\.\d+, ){4}\d+\.\d+$/.test(text));
}
/**
* A "separator" line is a line with all hyphens like the following:
*
* ```ts
* // ----------------
* // Getter functions
* // ----------------
* ```
*/
export function isSeparatorLine(text) {
return /^\s*-+\s*$/.test(text);
}
export function isSpecialComment(text) {
text = text.trim();
return (text.startsWith("eslint-")
|| text.startsWith("prettier-")
|| text.startsWith("cspell:")
|| text.startsWith("ts-prune-") // e.g. ts-prune-ignore-next
|| text.startsWith("@ts-")
|| text.startsWith("@template-") // e.g. @template-customization-start (complete-cli directives)
|| text.startsWith("TODO:")
|| text.startsWith("FIXME:")
|| text === "TODO"
|| text === "FIXME");
}