@virmator/lint 14.33.0 → 14.35.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/configs/eslint.config.base.ts +39 -0
- package/dist/rules/known-file-suffixes.lint.d.ts +3 -0
- package/dist/rules/known-file-suffixes.lint.js +101 -0
- package/dist/rules/no-jsdoc-category.lint.d.ts +3 -0
- package/dist/rules/no-jsdoc-category.lint.js +144 -0
- package/dist/rules/prefer-function-keyword.lint.d.ts +3 -0
- package/dist/rules/prefer-function-keyword.lint.js +166 -0
- package/dist/rules/prefer-loose-nullish-equality.lint.d.ts +3 -0
- package/dist/rules/prefer-loose-nullish-equality.lint.js +118 -0
- package/dist/rules/prefer-protected-over-private.lint.js +6 -2
- package/dist/rules/single-cspell-words-comment.lint.d.ts +3 -0
- package/dist/rules/single-cspell-words-comment.lint.js +112 -0
- package/package.json +12 -12
- package/src/rules/known-file-suffixes.lint.ts +119 -0
- package/src/rules/no-jsdoc-category.lint.ts +209 -0
- package/src/rules/prefer-function-keyword.lint.ts +262 -0
- package/src/rules/prefer-loose-nullish-equality.lint.ts +151 -0
- package/src/rules/prefer-protected-over-private.lint.ts +6 -2
- package/src/rules/single-cspell-words-comment.lint.ts +167 -0
|
@@ -8,6 +8,8 @@ import globals from 'globals';
|
|
|
8
8
|
import tsEslint from 'typescript-eslint';
|
|
9
9
|
import assertionsInTestsRule from '../src/rules/assertions-in-tests.lint.js';
|
|
10
10
|
import errorNameAsClassFieldRule from '../src/rules/error-name-as-class-field.lint.js';
|
|
11
|
+
import knownFileSuffixesRule from '../src/rules/known-file-suffixes.lint.js';
|
|
12
|
+
import noJsdocCategoryRule from '../src/rules/no-jsdoc-category.lint.js';
|
|
11
13
|
import noRawDateRule from '../src/rules/no-raw-date.lint.js';
|
|
12
14
|
import noReadonlyPrimitiveRule from '../src/rules/no-readonly-primitive.lint.js';
|
|
13
15
|
import noRelativeImportOutsidePackageRule from '../src/rules/no-relative-import-outside-package.lint.js';
|
|
@@ -16,7 +18,9 @@ import noSwitchRule from '../src/rules/no-switch.lint.js';
|
|
|
16
18
|
import noUnderscoreVariableRule from '../src/rules/no-underscore-variable.lint.js';
|
|
17
19
|
import preferArrowObjectBlockBodyRule from '../src/rules/prefer-arrow-object-block-body.lint.js';
|
|
18
20
|
import preferEnsureErrorRule from '../src/rules/prefer-ensure-error.lint.js';
|
|
21
|
+
import preferFunctionKeywordRule from '../src/rules/prefer-function-keyword.lint.js';
|
|
19
22
|
import preferIfElseChainRule from '../src/rules/prefer-if-else-chain.lint.js';
|
|
23
|
+
import preferLooseNullishEqualityRule from '../src/rules/prefer-loose-nullish-equality.lint.js';
|
|
20
24
|
import preferMapEnumToObjectRule from '../src/rules/prefer-map-enum-to-object.lint.js';
|
|
21
25
|
import preferMultilineArrowBlockBodyRule from '../src/rules/prefer-multiline-arrow-block-body.lint.js';
|
|
22
26
|
import preferParamsObjectRule from '../src/rules/prefer-params-object.lint.js';
|
|
@@ -27,6 +31,22 @@ import preferSingleQuotesRule from '../src/rules/prefer-single-quotes.lint.js';
|
|
|
27
31
|
import preferToSortedRule from '../src/rules/prefer-to-sorted.lint.js';
|
|
28
32
|
import requireElseForTerminalBranchRule from '../src/rules/require-else-for-terminal-branch.lint.js';
|
|
29
33
|
import requireImportExtensionsRule from '../src/rules/require-import-extensions.lint.js';
|
|
34
|
+
import singleCspellWordsCommentRule from '../src/rules/single-cspell-words-comment.lint.js';
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* File name suffixes that any repo using this config may use. A repo with more of its own adds them
|
|
38
|
+
* by overriding `@virmator/known-file-suffixes` with this list spread into its own.
|
|
39
|
+
*/
|
|
40
|
+
export const defaultAllowedFileSuffixes = [
|
|
41
|
+
'config',
|
|
42
|
+
'e2e',
|
|
43
|
+
'element',
|
|
44
|
+
'error',
|
|
45
|
+
'example',
|
|
46
|
+
'mock',
|
|
47
|
+
'script',
|
|
48
|
+
'test',
|
|
49
|
+
];
|
|
30
50
|
|
|
31
51
|
export const globalVars = {
|
|
32
52
|
...globals.node,
|
|
@@ -78,6 +98,8 @@ export function defineEslintConfig(repoDir: string) {
|
|
|
78
98
|
rules: {
|
|
79
99
|
'assertions-in-tests': assertionsInTestsRule,
|
|
80
100
|
'error-name-as-class-field': errorNameAsClassFieldRule,
|
|
101
|
+
'known-file-suffixes': knownFileSuffixesRule,
|
|
102
|
+
'no-jsdoc-category': noJsdocCategoryRule,
|
|
81
103
|
'no-raw-date': noRawDateRule,
|
|
82
104
|
'no-readonly-primitive': noReadonlyPrimitiveRule,
|
|
83
105
|
'no-relative-import-outside-package': noRelativeImportOutsidePackageRule,
|
|
@@ -86,7 +108,9 @@ export function defineEslintConfig(repoDir: string) {
|
|
|
86
108
|
'no-underscore-variable': noUnderscoreVariableRule,
|
|
87
109
|
'prefer-arrow-object-block-body': preferArrowObjectBlockBodyRule,
|
|
88
110
|
'prefer-ensure-error': preferEnsureErrorRule,
|
|
111
|
+
'prefer-function-keyword': preferFunctionKeywordRule,
|
|
89
112
|
'prefer-if-else-chain': preferIfElseChainRule,
|
|
113
|
+
'prefer-loose-nullish-equality': preferLooseNullishEqualityRule,
|
|
90
114
|
'prefer-map-enum-to-object': preferMapEnumToObjectRule,
|
|
91
115
|
'prefer-multiline-arrow-block-body': preferMultilineArrowBlockBodyRule,
|
|
92
116
|
'prefer-params-object': preferParamsObjectRule,
|
|
@@ -97,6 +121,7 @@ export function defineEslintConfig(repoDir: string) {
|
|
|
97
121
|
'prefer-to-sorted': preferToSortedRule,
|
|
98
122
|
'require-else-for-terminal-branch': requireElseForTerminalBranchRule,
|
|
99
123
|
'require-import-extensions': requireImportExtensionsRule,
|
|
124
|
+
'single-cspell-words-comment': singleCspellWordsCommentRule,
|
|
100
125
|
},
|
|
101
126
|
},
|
|
102
127
|
},
|
|
@@ -201,6 +226,17 @@ export function defineEslintConfig(repoDir: string) {
|
|
|
201
226
|
'@typescript-eslint/no-unused-vars': 'error',
|
|
202
227
|
'@virmator/assertions-in-tests': 'error',
|
|
203
228
|
'@virmator/error-name-as-class-field': 'error',
|
|
229
|
+
'@virmator/known-file-suffixes': [
|
|
230
|
+
'error',
|
|
231
|
+
{
|
|
232
|
+
suffixes: defaultAllowedFileSuffixes,
|
|
233
|
+
},
|
|
234
|
+
],
|
|
235
|
+
/**
|
|
236
|
+
* `@virmator/no-jsdoc-category` is deliberately left off here: Typedoc categories
|
|
237
|
+
* are useful in published packages. Consumers that do not use Typedoc enable it
|
|
238
|
+
* themselves.
|
|
239
|
+
*/
|
|
204
240
|
'@virmator/no-raw-date': 'error',
|
|
205
241
|
'@virmator/no-readonly-primitive': 'error',
|
|
206
242
|
'@virmator/no-relative-import-outside-package': 'error',
|
|
@@ -209,7 +245,9 @@ export function defineEslintConfig(repoDir: string) {
|
|
|
209
245
|
'@virmator/no-underscore-variable': 'error',
|
|
210
246
|
'@virmator/prefer-arrow-object-block-body': 'error',
|
|
211
247
|
'@virmator/prefer-ensure-error': 'error',
|
|
248
|
+
'@virmator/prefer-function-keyword': 'error',
|
|
212
249
|
'@virmator/prefer-if-else-chain': 'error',
|
|
250
|
+
'@virmator/prefer-loose-nullish-equality': 'error',
|
|
213
251
|
'@virmator/prefer-map-enum-to-object': 'error',
|
|
214
252
|
'@virmator/prefer-multiline-arrow-block-body': 'error',
|
|
215
253
|
'@virmator/prefer-params-object': 'error',
|
|
@@ -220,6 +258,7 @@ export function defineEslintConfig(repoDir: string) {
|
|
|
220
258
|
'@virmator/prefer-to-sorted': 'error',
|
|
221
259
|
'@virmator/require-else-for-terminal-branch': 'error',
|
|
222
260
|
'@virmator/require-import-extensions': 'error',
|
|
261
|
+
'@virmator/single-cspell-words-comment': 'error',
|
|
223
262
|
'no-lonely-if': 'error',
|
|
224
263
|
curly: 'error',
|
|
225
264
|
'object-shorthand': 'error',
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { basename } from 'node:path';
|
|
2
|
+
const numberedSuffixRegExp = /-\d+$/;
|
|
3
|
+
/**
|
|
4
|
+
* The suffix segments of a file name: everything between its first segment (the name itself) and
|
|
5
|
+
* its extension. A leading dot is part of the name rather than a segment separator, so
|
|
6
|
+
* `.eslintrc.ts` has no suffixes.
|
|
7
|
+
*/
|
|
8
|
+
function extractSuffixes(filePath) {
|
|
9
|
+
const fileName = basename(filePath).replace(/^\.+/, '');
|
|
10
|
+
return fileName.split('.').slice(1, -1);
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Every chain of suffixes that ends at the given index, longest first: `['config', 'base']` at
|
|
14
|
+
* index `1` produces `'config.base'` then `'base'`. A dotted entry in the `suffixes` option
|
|
15
|
+
* therefore allows its last suffix only directly after the ones before it, so listing `config.base`
|
|
16
|
+
* allows `thing.config.base.ts` while still rejecting `thing.base.ts`.
|
|
17
|
+
*/
|
|
18
|
+
function listSuffixChains({ suffixes, suffixIndex, }) {
|
|
19
|
+
return suffixes.slice(0, suffixIndex + 1).map((currentSuffix, startIndex) => {
|
|
20
|
+
return suffixes.slice(startIndex, suffixIndex + 1).join('.');
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
const rule = {
|
|
24
|
+
meta: {
|
|
25
|
+
type: 'suggestion',
|
|
26
|
+
docs: {
|
|
27
|
+
description: 'Require file name suffixes to come from a known list.',
|
|
28
|
+
},
|
|
29
|
+
messages: {
|
|
30
|
+
unknownSuffix: "Unknown file name suffix `.{{suffix}}`. Known suffixes: {{knownSuffixes}}. Add this suffix to the rule's `suffixes` option if it is intentional.",
|
|
31
|
+
},
|
|
32
|
+
schema: [
|
|
33
|
+
{
|
|
34
|
+
type: 'object',
|
|
35
|
+
required: [
|
|
36
|
+
'suffixes',
|
|
37
|
+
],
|
|
38
|
+
properties: {
|
|
39
|
+
suffixes: {
|
|
40
|
+
type: 'array',
|
|
41
|
+
items: {
|
|
42
|
+
type: 'string',
|
|
43
|
+
},
|
|
44
|
+
},
|
|
45
|
+
/**
|
|
46
|
+
* Allows a `-<number>` counter on a suffix, for splitting a single slow test
|
|
47
|
+
* file into numbered companions (`my-thing.element-2.test.ts`).
|
|
48
|
+
*/
|
|
49
|
+
allowNumberedSuffixes: {
|
|
50
|
+
type: 'boolean',
|
|
51
|
+
},
|
|
52
|
+
},
|
|
53
|
+
additionalProperties: false,
|
|
54
|
+
},
|
|
55
|
+
],
|
|
56
|
+
},
|
|
57
|
+
create(context) {
|
|
58
|
+
const options = context.options[0] ?? {};
|
|
59
|
+
const knownSuffixes = options.suffixes ?? [];
|
|
60
|
+
return {
|
|
61
|
+
Program() {
|
|
62
|
+
const suffixes = extractSuffixes(context.filename).map((suffix) => {
|
|
63
|
+
return options.allowNumberedSuffixes
|
|
64
|
+
? suffix.replace(numberedSuffixRegExp, '')
|
|
65
|
+
: suffix;
|
|
66
|
+
});
|
|
67
|
+
suffixes.forEach((suffix, suffixIndex) => {
|
|
68
|
+
const isKnown = listSuffixChains({
|
|
69
|
+
suffixes,
|
|
70
|
+
suffixIndex,
|
|
71
|
+
}).some((suffixChain) => {
|
|
72
|
+
return knownSuffixes.includes(suffixChain);
|
|
73
|
+
});
|
|
74
|
+
if (isKnown) {
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
context.report({
|
|
78
|
+
/**
|
|
79
|
+
* The file name is the problem, so the report is anchored to its first
|
|
80
|
+
* line.
|
|
81
|
+
*/
|
|
82
|
+
loc: {
|
|
83
|
+
line: 1,
|
|
84
|
+
column: 0,
|
|
85
|
+
},
|
|
86
|
+
messageId: 'unknownSuffix',
|
|
87
|
+
data: {
|
|
88
|
+
suffix,
|
|
89
|
+
knownSuffixes: knownSuffixes
|
|
90
|
+
.map((knownSuffix) => {
|
|
91
|
+
return `.${knownSuffix}`;
|
|
92
|
+
})
|
|
93
|
+
.join(', '),
|
|
94
|
+
},
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
},
|
|
98
|
+
};
|
|
99
|
+
},
|
|
100
|
+
};
|
|
101
|
+
export default rule;
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
const categoryTagRegExp = /@category\b/;
|
|
2
|
+
/**
|
|
3
|
+
* A whole JSDoc comment holding nothing but the tag, and a comment line holding nothing but the
|
|
4
|
+
* tag. The tag value is limited to a single word so that a line with a second tag or a description
|
|
5
|
+
* on it is never matched (and so never deleted).
|
|
6
|
+
*/
|
|
7
|
+
const tagOnlyCommentRegExp = /^\/\*\*[\t ]*@category(?:[\t ]+[\w-]+)?[\t ]*\*\/$/;
|
|
8
|
+
const tagOnlyLineRegExp = /^[\t ]*(?:\*[\t ]*)?@category(?:[\t ]+[\w-]+)?[\t ]*$/;
|
|
9
|
+
function findCategoryTagLines({ commentText, commentStart, }) {
|
|
10
|
+
return commentText.split('\n').reduce((tagLines, lineText, lineIndex, allLines) => {
|
|
11
|
+
const lineStart = commentStart +
|
|
12
|
+
allLines.slice(0, lineIndex).reduce((total, previousLine) => {
|
|
13
|
+
return total + previousLine.length + 1;
|
|
14
|
+
}, 0);
|
|
15
|
+
const tagIndex = lineText.search(categoryTagRegExp);
|
|
16
|
+
if (tagIndex === -1) {
|
|
17
|
+
return tagLines;
|
|
18
|
+
}
|
|
19
|
+
return [
|
|
20
|
+
...tagLines,
|
|
21
|
+
{
|
|
22
|
+
lineStart,
|
|
23
|
+
lineEnd: lineStart + lineText.length,
|
|
24
|
+
tagStart: lineStart + tagIndex,
|
|
25
|
+
},
|
|
26
|
+
];
|
|
27
|
+
}, []);
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Whether the given comment is the only thing on its line, meaning the whole line can be removed
|
|
31
|
+
* along with the comment.
|
|
32
|
+
*/
|
|
33
|
+
function isAloneOnItsLine({ comment, sourceCode, }) {
|
|
34
|
+
const range = comment.range;
|
|
35
|
+
if (!range) {
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
const lineStart = sourceCode.text.lastIndexOf('\n', range[0]) + 1;
|
|
39
|
+
const nextNewLine = sourceCode.text.indexOf('\n', range[1]);
|
|
40
|
+
const lineEnd = nextNewLine === -1 ? sourceCode.text.length : nextNewLine;
|
|
41
|
+
return (sourceCode.text.slice(lineStart, range[0]).trim() === '' &&
|
|
42
|
+
sourceCode.text.slice(range[1], lineEnd).trim() === '');
|
|
43
|
+
}
|
|
44
|
+
function getWholeLineRange({ comment, sourceCode, }) {
|
|
45
|
+
const range = comment.range;
|
|
46
|
+
if (!range) {
|
|
47
|
+
return undefined;
|
|
48
|
+
}
|
|
49
|
+
const lineStart = sourceCode.text.lastIndexOf('\n', range[0]) + 1;
|
|
50
|
+
const nextNewLine = sourceCode.text.indexOf('\n', range[1]);
|
|
51
|
+
return [
|
|
52
|
+
lineStart,
|
|
53
|
+
nextNewLine === -1 ? sourceCode.text.length : nextNewLine + 1,
|
|
54
|
+
];
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Only a comment or line holding nothing but the tag can be deleted outright. Anything else (a
|
|
58
|
+
* description sharing the line, a tag value spanning lines) is reported without a fix so the author
|
|
59
|
+
* decides what the remaining comment should say.
|
|
60
|
+
*/
|
|
61
|
+
function createFix({ comment, commentText, sourceCode, tagLine, }) {
|
|
62
|
+
const range = comment.range;
|
|
63
|
+
if (range == undefined) {
|
|
64
|
+
return undefined;
|
|
65
|
+
}
|
|
66
|
+
else if (commentText.includes('\n')) {
|
|
67
|
+
const tagLineText = sourceCode.text.slice(tagLine.lineStart, tagLine.lineEnd);
|
|
68
|
+
if (!tagOnlyLineRegExp.test(tagLineText)) {
|
|
69
|
+
return undefined;
|
|
70
|
+
}
|
|
71
|
+
return (fixer) => {
|
|
72
|
+
return fixer.removeRange([
|
|
73
|
+
tagLine.lineStart,
|
|
74
|
+
tagLine.lineEnd + 1,
|
|
75
|
+
]);
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
else if (tagOnlyCommentRegExp.test(commentText.trim())) {
|
|
79
|
+
const wholeLineRange = getWholeLineRange({
|
|
80
|
+
comment,
|
|
81
|
+
sourceCode,
|
|
82
|
+
});
|
|
83
|
+
return (fixer) => {
|
|
84
|
+
return wholeLineRange &&
|
|
85
|
+
isAloneOnItsLine({
|
|
86
|
+
comment,
|
|
87
|
+
sourceCode,
|
|
88
|
+
})
|
|
89
|
+
? fixer.removeRange(wholeLineRange)
|
|
90
|
+
: fixer.removeRange(range);
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
else {
|
|
94
|
+
return undefined;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
const rule = {
|
|
98
|
+
meta: {
|
|
99
|
+
type: 'suggestion',
|
|
100
|
+
fixable: 'code',
|
|
101
|
+
docs: {
|
|
102
|
+
description: 'Disallow `@category` tags in JSDoc comments.',
|
|
103
|
+
},
|
|
104
|
+
messages: {
|
|
105
|
+
noCategory: 'Do not use `@category` JSDoc tags.',
|
|
106
|
+
},
|
|
107
|
+
schema: [],
|
|
108
|
+
},
|
|
109
|
+
create(context) {
|
|
110
|
+
const sourceCode = context.sourceCode;
|
|
111
|
+
return {
|
|
112
|
+
Program() {
|
|
113
|
+
sourceCode.getAllComments().forEach((comment) => {
|
|
114
|
+
const range = comment.range;
|
|
115
|
+
if (comment.type !== 'Block' || !range || !comment.value.startsWith('*')) {
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
const commentText = sourceCode.text.slice(range[0], range[1]);
|
|
119
|
+
const tagLines = findCategoryTagLines({
|
|
120
|
+
commentText,
|
|
121
|
+
commentStart: range[0],
|
|
122
|
+
});
|
|
123
|
+
tagLines.forEach((tagLine) => {
|
|
124
|
+
const fix = createFix({
|
|
125
|
+
comment,
|
|
126
|
+
commentText,
|
|
127
|
+
sourceCode,
|
|
128
|
+
tagLine,
|
|
129
|
+
});
|
|
130
|
+
context.report({
|
|
131
|
+
loc: {
|
|
132
|
+
start: sourceCode.getLocFromIndex(tagLine.tagStart),
|
|
133
|
+
end: sourceCode.getLocFromIndex(tagLine.lineEnd),
|
|
134
|
+
},
|
|
135
|
+
messageId: 'noCategory',
|
|
136
|
+
fix,
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
});
|
|
140
|
+
},
|
|
141
|
+
};
|
|
142
|
+
},
|
|
143
|
+
};
|
|
144
|
+
export default rule;
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
function isArrowFunctionExpression(node) {
|
|
2
|
+
return node.type === 'ArrowFunctionExpression';
|
|
3
|
+
}
|
|
4
|
+
function isIdentifierVariableDeclarator(node) {
|
|
5
|
+
return node.type === 'VariableDeclarator' && node.id.type === 'Identifier';
|
|
6
|
+
}
|
|
7
|
+
function hasTypeAnnotation({ id }) {
|
|
8
|
+
const typescriptId = id;
|
|
9
|
+
return typescriptId.typeAnnotation != undefined;
|
|
10
|
+
}
|
|
11
|
+
function getParameterText({ node, sourceCode, }) {
|
|
12
|
+
const firstParam = node.params.at(0);
|
|
13
|
+
const lastParam = node.params.at(-1);
|
|
14
|
+
if (!firstParam || !lastParam) {
|
|
15
|
+
return '()';
|
|
16
|
+
}
|
|
17
|
+
const openParen = sourceCode.getTokenBefore(firstParam);
|
|
18
|
+
const closeParen = sourceCode.getTokenAfter(lastParam);
|
|
19
|
+
if (openParen?.value === '(' && closeParen?.value === ')') {
|
|
20
|
+
return sourceCode.text.slice(openParen.range[0], closeParen.range[1]);
|
|
21
|
+
}
|
|
22
|
+
else if (node.params.length > 1) {
|
|
23
|
+
return undefined;
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
return `(${sourceCode.getText(firstParam)})`;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
function getFunctionText({ node, name, sourceCode, useFunctionKeyword, }) {
|
|
30
|
+
const parameterText = getParameterText({
|
|
31
|
+
node,
|
|
32
|
+
sourceCode,
|
|
33
|
+
});
|
|
34
|
+
if (!parameterText) {
|
|
35
|
+
return undefined;
|
|
36
|
+
}
|
|
37
|
+
const typeParameterText = node.typeParameters ? sourceCode.getText(node.typeParameters) : '';
|
|
38
|
+
const returnTypeText = node.returnType ? sourceCode.getText(node.returnType) : '';
|
|
39
|
+
const bodyText = node.body.type === 'BlockStatement'
|
|
40
|
+
? sourceCode.getText(node.body)
|
|
41
|
+
: `{ return ${sourceCode.getText(node.body)}; }`;
|
|
42
|
+
const asyncText = node.async ? 'async ' : '';
|
|
43
|
+
const functionKeywordText = useFunctionKeyword ? 'function ' : '';
|
|
44
|
+
return `${asyncText}${functionKeywordText}${name}${typeParameterText}${parameterText}${returnTypeText} ${bodyText}`;
|
|
45
|
+
}
|
|
46
|
+
function isPrototypeSetter({ property }) {
|
|
47
|
+
return (!property.computed &&
|
|
48
|
+
((property.key.type === 'Identifier' && property.key.name === '__proto__') ||
|
|
49
|
+
(property.key.type === 'Literal' && property.key.value === '__proto__')));
|
|
50
|
+
}
|
|
51
|
+
function getConstVariable({ node, }) {
|
|
52
|
+
const declarator = node.parent;
|
|
53
|
+
if (!isIdentifierVariableDeclarator(declarator) ||
|
|
54
|
+
declarator.init !== node ||
|
|
55
|
+
hasTypeAnnotation({
|
|
56
|
+
id: declarator.id,
|
|
57
|
+
})) {
|
|
58
|
+
return undefined;
|
|
59
|
+
}
|
|
60
|
+
const declaration = declarator.parent;
|
|
61
|
+
if (declaration.type !== 'VariableDeclaration' || declaration.kind !== 'const') {
|
|
62
|
+
return undefined;
|
|
63
|
+
}
|
|
64
|
+
return {
|
|
65
|
+
declarator,
|
|
66
|
+
declaration,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
function canUseFunctionDeclaration({ declaration, }) {
|
|
70
|
+
return (declaration.declarations.length === 1 &&
|
|
71
|
+
(declaration.parent.type === 'BlockStatement' ||
|
|
72
|
+
declaration.parent.type === 'ExportNamedDeclaration' ||
|
|
73
|
+
declaration.parent.type === 'Program'));
|
|
74
|
+
}
|
|
75
|
+
const rule = {
|
|
76
|
+
meta: {
|
|
77
|
+
type: 'suggestion',
|
|
78
|
+
fixable: 'code',
|
|
79
|
+
messages: {
|
|
80
|
+
useFunctionDeclaration: 'Use a `function` keyword function instead of storing an arrow function in a `const`.',
|
|
81
|
+
usePropertyMethod: 'Use object property method shorthand instead of storing an arrow function in a property.',
|
|
82
|
+
},
|
|
83
|
+
schema: [],
|
|
84
|
+
},
|
|
85
|
+
create(context) {
|
|
86
|
+
const sourceCode = context.sourceCode;
|
|
87
|
+
const arrowsThatCaptureThis = new Set();
|
|
88
|
+
function markArrowsThatCaptureThis(node) {
|
|
89
|
+
sourceCode
|
|
90
|
+
.getAncestors(node)
|
|
91
|
+
.toReversed()
|
|
92
|
+
.some((ancestor) => {
|
|
93
|
+
if (ancestor.type === 'ClassDeclaration' ||
|
|
94
|
+
ancestor.type === 'ClassExpression' ||
|
|
95
|
+
ancestor.type === 'FunctionDeclaration' ||
|
|
96
|
+
ancestor.type === 'FunctionExpression') {
|
|
97
|
+
return true;
|
|
98
|
+
}
|
|
99
|
+
if (isArrowFunctionExpression(ancestor)) {
|
|
100
|
+
arrowsThatCaptureThis.add(ancestor);
|
|
101
|
+
}
|
|
102
|
+
return false;
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
function checkArrowFunction(node) {
|
|
106
|
+
if (arrowsThatCaptureThis.has(node)) {
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
const constVariable = getConstVariable({
|
|
110
|
+
node,
|
|
111
|
+
});
|
|
112
|
+
if (constVariable) {
|
|
113
|
+
const functionText = getFunctionText({
|
|
114
|
+
node,
|
|
115
|
+
name: constVariable.declarator.id.name,
|
|
116
|
+
sourceCode,
|
|
117
|
+
useFunctionKeyword: true,
|
|
118
|
+
});
|
|
119
|
+
context.report({
|
|
120
|
+
node,
|
|
121
|
+
messageId: 'useFunctionDeclaration',
|
|
122
|
+
fix: functionText
|
|
123
|
+
? (fixer) => {
|
|
124
|
+
return canUseFunctionDeclaration({
|
|
125
|
+
declaration: constVariable.declaration,
|
|
126
|
+
})
|
|
127
|
+
? fixer.replaceText(constVariable.declaration, functionText)
|
|
128
|
+
: fixer.replaceText(node, functionText);
|
|
129
|
+
}
|
|
130
|
+
: undefined,
|
|
131
|
+
});
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
const property = node.parent;
|
|
135
|
+
if (property.type !== 'Property' ||
|
|
136
|
+
property.value !== node ||
|
|
137
|
+
property.kind !== 'init' ||
|
|
138
|
+
isPrototypeSetter({
|
|
139
|
+
property,
|
|
140
|
+
})) {
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
const propertyName = property.computed
|
|
144
|
+
? `[${sourceCode.getText(property.key)}]`
|
|
145
|
+
: sourceCode.getText(property.key);
|
|
146
|
+
const functionText = getFunctionText({
|
|
147
|
+
node,
|
|
148
|
+
name: propertyName,
|
|
149
|
+
sourceCode,
|
|
150
|
+
useFunctionKeyword: false,
|
|
151
|
+
});
|
|
152
|
+
context.report({
|
|
153
|
+
node,
|
|
154
|
+
messageId: 'usePropertyMethod',
|
|
155
|
+
fix: functionText
|
|
156
|
+
? (fixer) => fixer.replaceText(property, functionText)
|
|
157
|
+
: undefined,
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
return {
|
|
161
|
+
ThisExpression: markArrowsThatCaptureThis,
|
|
162
|
+
'ArrowFunctionExpression:exit': checkArrowFunction,
|
|
163
|
+
};
|
|
164
|
+
},
|
|
165
|
+
};
|
|
166
|
+
export default rule;
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
const looseOperatorByStrictOperator = {
|
|
2
|
+
'===': '==',
|
|
3
|
+
'!==': '!=',
|
|
4
|
+
};
|
|
5
|
+
function getComparedNullishKind(node) {
|
|
6
|
+
if (node.type === 'Identifier' && node.name === 'undefined') {
|
|
7
|
+
return 'undefined';
|
|
8
|
+
}
|
|
9
|
+
else if (node.type === 'Literal' && node.raw === 'null') {
|
|
10
|
+
return 'null';
|
|
11
|
+
}
|
|
12
|
+
else {
|
|
13
|
+
return undefined;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Which nullish members the given node's type includes. `undefined` is returned when the type
|
|
18
|
+
* cannot be determined at all, or when it is `any` or `unknown` (both of which could be either
|
|
19
|
+
* nullish value at run time).
|
|
20
|
+
*
|
|
21
|
+
* ESLint types `sourceCode.parserServices` as `any`, so the TypeScript program behind the
|
|
22
|
+
* typescript-eslint parser is necessarily reached without types here.
|
|
23
|
+
*/
|
|
24
|
+
function getNullishMembers({ node, parserServices, }) {
|
|
25
|
+
const program = parserServices?.program;
|
|
26
|
+
const tsNode = parserServices?.esTreeNodeToTSNodeMap?.get(node);
|
|
27
|
+
if (!program || !tsNode) {
|
|
28
|
+
return undefined;
|
|
29
|
+
}
|
|
30
|
+
const checker = program.getTypeChecker();
|
|
31
|
+
const type = checker.getTypeAtLocation(tsNode);
|
|
32
|
+
/**
|
|
33
|
+
* `TypeFlags` values are compared through `typeToString` rather than the numeric enum because
|
|
34
|
+
* TypeScript renumbers those flags between major versions.
|
|
35
|
+
*/
|
|
36
|
+
const typeNames = (type.isUnion() ? type.types : [type]).map((typePart) => {
|
|
37
|
+
return checker.typeToString(typePart);
|
|
38
|
+
});
|
|
39
|
+
if (typeNames.includes('any') || typeNames.includes('unknown')) {
|
|
40
|
+
return undefined;
|
|
41
|
+
}
|
|
42
|
+
return {
|
|
43
|
+
hasNull: typeNames.includes('null'),
|
|
44
|
+
/** A `void` value can only ever be `undefined` at run time. */
|
|
45
|
+
hasUndefined: typeNames.includes('undefined') || typeNames.includes('void'),
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
const rule = {
|
|
49
|
+
meta: {
|
|
50
|
+
type: 'suggestion',
|
|
51
|
+
fixable: 'code',
|
|
52
|
+
docs: {
|
|
53
|
+
description: 'Require loose equality when comparing against `null` or `undefined` and strict equality cannot distinguish them.',
|
|
54
|
+
},
|
|
55
|
+
messages: {
|
|
56
|
+
preferLooseEquality: 'Use `{{looseOperator}}` instead of `{{strictOperator}}` here: this value cannot be `{{otherNullish}}`, so the two behave identically.',
|
|
57
|
+
},
|
|
58
|
+
schema: [],
|
|
59
|
+
},
|
|
60
|
+
create(context) {
|
|
61
|
+
const sourceCode = context.sourceCode;
|
|
62
|
+
return {
|
|
63
|
+
BinaryExpression(node) {
|
|
64
|
+
const looseOperator = looseOperatorByStrictOperator[node.operator];
|
|
65
|
+
if (!looseOperator) {
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
const leftNullishKind = getComparedNullishKind(node.left);
|
|
69
|
+
const rightNullishKind = getComparedNullishKind(node.right);
|
|
70
|
+
/**
|
|
71
|
+
* A comparison between two nullish literals has no value side to check, and a
|
|
72
|
+
* comparison between two non-nullish sides is not this rule's business.
|
|
73
|
+
*/
|
|
74
|
+
if (!!leftNullishKind === !!rightNullishKind) {
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
const comparedNullishKind = leftNullishKind ?? rightNullishKind;
|
|
78
|
+
const valueNode = leftNullishKind ? node.right : node.left;
|
|
79
|
+
const nullishMembers = getNullishMembers({
|
|
80
|
+
node: valueNode,
|
|
81
|
+
parserServices: sourceCode.parserServices,
|
|
82
|
+
});
|
|
83
|
+
if (!nullishMembers) {
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Loose equality only matches strict equality when the value cannot hold the
|
|
88
|
+
* _other_ nullish value. A value that can be both `null` and `undefined` is the
|
|
89
|
+
* rare case where strict equality is load bearing, so it is left alone.
|
|
90
|
+
*/
|
|
91
|
+
const isEquivalentToStrictEquality = comparedNullishKind === 'undefined'
|
|
92
|
+
? nullishMembers.hasUndefined && !nullishMembers.hasNull
|
|
93
|
+
: nullishMembers.hasNull && !nullishMembers.hasUndefined;
|
|
94
|
+
if (!isEquivalentToStrictEquality) {
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
const operatorToken = sourceCode.getTokenAfter(node.left, {
|
|
98
|
+
filter(token) {
|
|
99
|
+
return token.type === 'Punctuator' && token.value === node.operator;
|
|
100
|
+
},
|
|
101
|
+
});
|
|
102
|
+
context.report({
|
|
103
|
+
node,
|
|
104
|
+
messageId: 'preferLooseEquality',
|
|
105
|
+
data: {
|
|
106
|
+
looseOperator,
|
|
107
|
+
strictOperator: node.operator,
|
|
108
|
+
otherNullish: comparedNullishKind === 'undefined' ? 'null' : 'undefined',
|
|
109
|
+
},
|
|
110
|
+
fix: operatorToken
|
|
111
|
+
? (fixer) => fixer.replaceText(operatorToken, looseOperator)
|
|
112
|
+
: undefined,
|
|
113
|
+
});
|
|
114
|
+
},
|
|
115
|
+
};
|
|
116
|
+
},
|
|
117
|
+
};
|
|
118
|
+
export default rule;
|
|
@@ -6,10 +6,14 @@ function findPrivateToken(node, sourceCode) {
|
|
|
6
6
|
*/
|
|
7
7
|
return lastDecorator
|
|
8
8
|
? sourceCode.getTokenAfter(lastDecorator, {
|
|
9
|
-
filter
|
|
9
|
+
filter(token) {
|
|
10
|
+
return token.value === 'private';
|
|
11
|
+
},
|
|
10
12
|
})
|
|
11
13
|
: sourceCode.getFirstToken(node, {
|
|
12
|
-
filter
|
|
14
|
+
filter(token) {
|
|
15
|
+
return token.value === 'private';
|
|
16
|
+
},
|
|
13
17
|
});
|
|
14
18
|
}
|
|
15
19
|
const rule = {
|