@virmator/lint 14.26.0 → 14.27.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 +36 -0
- package/dist/rules/error-name-as-class-field.lint.d.ts +3 -0
- package/dist/rules/error-name-as-class-field.lint.js +165 -0
- package/dist/rules/no-raw-date.lint.d.ts +3 -0
- package/dist/rules/no-raw-date.lint.js +42 -0
- package/dist/rules/no-readonly-primitive.lint.d.ts +3 -0
- package/dist/rules/no-readonly-primitive.lint.js +68 -0
- package/dist/rules/no-relative-import-outside-package.lint.js +5 -2
- package/dist/rules/no-should-in-test-description.lint.d.ts +3 -0
- package/dist/rules/no-should-in-test-description.lint.js +84 -0
- package/dist/rules/no-underscore-variable.lint.d.ts +3 -0
- package/dist/rules/no-underscore-variable.lint.js +60 -0
- package/dist/rules/prefer-arrow-object-block-body.lint.d.ts +3 -0
- package/dist/rules/prefer-arrow-object-block-body.lint.js +51 -0
- package/dist/rules/prefer-ensure-error.lint.d.ts +3 -0
- package/dist/rules/prefer-ensure-error.lint.js +74 -0
- package/dist/rules/prefer-if-else-chain.lint.js +5 -0
- package/dist/rules/prefer-map-enum-to-object.lint.d.ts +3 -0
- package/dist/rules/prefer-map-enum-to-object.lint.js +68 -0
- package/dist/rules/prefer-params-object.lint.d.ts +3 -0
- package/dist/rules/prefer-params-object.lint.js +117 -0
- package/dist/rules/prefer-protected-over-private.lint.d.ts +3 -0
- package/dist/rules/prefer-protected-over-private.lint.js +53 -0
- package/dist/rules/prefer-remove-duplicates.lint.d.ts +3 -0
- package/dist/rules/prefer-remove-duplicates.lint.js +142 -0
- package/dist/rules/prefer-to-sorted.lint.d.ts +3 -0
- package/dist/rules/prefer-to-sorted.lint.js +58 -0
- package/package.json +2 -2
- package/src/rules/error-name-as-class-field.lint.ts +211 -0
- package/src/rules/no-raw-date.lint.ts +52 -0
- package/src/rules/no-readonly-primitive.lint.ts +85 -0
- package/src/rules/no-relative-import-outside-package.lint.ts +7 -2
- package/src/rules/no-should-in-test-description.lint.ts +106 -0
- package/src/rules/no-underscore-variable.lint.ts +73 -0
- package/src/rules/prefer-arrow-object-block-body.lint.ts +66 -0
- package/src/rules/prefer-ensure-error.lint.ts +91 -0
- package/src/rules/prefer-if-else-chain.lint.ts +5 -0
- package/src/rules/prefer-map-enum-to-object.lint.ts +86 -0
- package/src/rules/prefer-params-object.lint.ts +190 -0
- package/src/rules/prefer-protected-over-private.lint.ts +71 -0
- package/src/rules/prefer-remove-duplicates.lint.ts +225 -0
- package/src/rules/prefer-to-sorted.lint.ts +79 -0
|
@@ -6,10 +6,22 @@ import sonarJsEslint from 'eslint-plugin-sonarjs';
|
|
|
6
6
|
import eslintPluginUnicorn from 'eslint-plugin-unicorn';
|
|
7
7
|
import globals from 'globals';
|
|
8
8
|
import tsEslint from 'typescript-eslint';
|
|
9
|
+
import errorNameAsClassFieldRule from '../src/rules/error-name-as-class-field.lint.js';
|
|
10
|
+
import noRawDateRule from '../src/rules/no-raw-date.lint.js';
|
|
11
|
+
import noReadonlyPrimitiveRule from '../src/rules/no-readonly-primitive.lint.js';
|
|
9
12
|
import noRelativeImportOutsidePackageRule from '../src/rules/no-relative-import-outside-package.lint.js';
|
|
13
|
+
import noShouldInTestDescriptionRule from '../src/rules/no-should-in-test-description.lint.js';
|
|
14
|
+
import noUnderscoreVariableRule from '../src/rules/no-underscore-variable.lint.js';
|
|
15
|
+
import preferArrowObjectBlockBodyRule from '../src/rules/prefer-arrow-object-block-body.lint.js';
|
|
16
|
+
import preferEnsureErrorRule from '../src/rules/prefer-ensure-error.lint.js';
|
|
10
17
|
import preferIfElseChainRule from '../src/rules/prefer-if-else-chain.lint.js';
|
|
18
|
+
import preferMapEnumToObjectRule from '../src/rules/prefer-map-enum-to-object.lint.js';
|
|
19
|
+
import preferParamsObjectRule from '../src/rules/prefer-params-object.lint.js';
|
|
11
20
|
import preferParseUrlRule from '../src/rules/prefer-parse-url.lint.js';
|
|
21
|
+
import preferProtectedOverPrivateRule from '../src/rules/prefer-protected-over-private.lint.js';
|
|
22
|
+
import preferRemoveDuplicatesRule from '../src/rules/prefer-remove-duplicates.lint.js';
|
|
12
23
|
import preferSingleQuotesRule from '../src/rules/prefer-single-quotes.lint.js';
|
|
24
|
+
import preferToSortedRule from '../src/rules/prefer-to-sorted.lint.js';
|
|
13
25
|
import requireImportExtensionsRule from '../src/rules/require-import-extensions.lint.js';
|
|
14
26
|
|
|
15
27
|
export const globalVars = {
|
|
@@ -60,10 +72,22 @@ export function defineEslintConfig(repoDir: string) {
|
|
|
60
72
|
unicorn: eslintPluginUnicorn,
|
|
61
73
|
'@virmator': {
|
|
62
74
|
rules: {
|
|
75
|
+
'error-name-as-class-field': errorNameAsClassFieldRule,
|
|
76
|
+
'no-raw-date': noRawDateRule,
|
|
77
|
+
'no-readonly-primitive': noReadonlyPrimitiveRule,
|
|
63
78
|
'no-relative-import-outside-package': noRelativeImportOutsidePackageRule,
|
|
79
|
+
'no-should-in-test-description': noShouldInTestDescriptionRule,
|
|
80
|
+
'no-underscore-variable': noUnderscoreVariableRule,
|
|
81
|
+
'prefer-arrow-object-block-body': preferArrowObjectBlockBodyRule,
|
|
82
|
+
'prefer-ensure-error': preferEnsureErrorRule,
|
|
64
83
|
'prefer-if-else-chain': preferIfElseChainRule,
|
|
84
|
+
'prefer-map-enum-to-object': preferMapEnumToObjectRule,
|
|
85
|
+
'prefer-params-object': preferParamsObjectRule,
|
|
65
86
|
'prefer-parse-url': preferParseUrlRule,
|
|
87
|
+
'prefer-protected-over-private': preferProtectedOverPrivateRule,
|
|
88
|
+
'prefer-remove-duplicates': preferRemoveDuplicatesRule,
|
|
66
89
|
'prefer-single-quotes': preferSingleQuotesRule,
|
|
90
|
+
'prefer-to-sorted': preferToSortedRule,
|
|
67
91
|
'require-import-extensions': requireImportExtensionsRule,
|
|
68
92
|
},
|
|
69
93
|
},
|
|
@@ -166,10 +190,22 @@ export function defineEslintConfig(repoDir: string) {
|
|
|
166
190
|
'@jsdoc/no-undefined-types': 'error',
|
|
167
191
|
'@typescript-eslint/await-thenable': 'error',
|
|
168
192
|
'@typescript-eslint/no-unused-vars': 'error',
|
|
193
|
+
'@virmator/error-name-as-class-field': 'error',
|
|
194
|
+
'@virmator/no-raw-date': 'error',
|
|
195
|
+
'@virmator/no-readonly-primitive': 'error',
|
|
169
196
|
'@virmator/no-relative-import-outside-package': 'error',
|
|
197
|
+
'@virmator/no-should-in-test-description': 'error',
|
|
198
|
+
'@virmator/no-underscore-variable': 'error',
|
|
199
|
+
'@virmator/prefer-arrow-object-block-body': 'error',
|
|
200
|
+
'@virmator/prefer-ensure-error': 'error',
|
|
170
201
|
'@virmator/prefer-if-else-chain': 'error',
|
|
202
|
+
'@virmator/prefer-map-enum-to-object': 'error',
|
|
203
|
+
'@virmator/prefer-params-object': 'error',
|
|
171
204
|
'@virmator/prefer-parse-url': 'error',
|
|
205
|
+
'@virmator/prefer-protected-over-private': 'error',
|
|
206
|
+
'@virmator/prefer-remove-duplicates': 'error',
|
|
172
207
|
'@virmator/prefer-single-quotes': 'error',
|
|
208
|
+
'@virmator/prefer-to-sorted': 'error',
|
|
173
209
|
'@virmator/require-import-extensions': 'error',
|
|
174
210
|
'no-lonely-if': 'error',
|
|
175
211
|
curly: 'error',
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
const functionNodeTypes = [
|
|
2
|
+
'FunctionExpression',
|
|
3
|
+
'FunctionDeclaration',
|
|
4
|
+
'ArrowFunctionExpression',
|
|
5
|
+
];
|
|
6
|
+
/**
|
|
7
|
+
* Determine whether a class extends an `Error`-like superclass: an `Identifier` whose name ends in
|
|
8
|
+
* `Error` (e.g. `Error`, `MyCustomError`).
|
|
9
|
+
*/
|
|
10
|
+
function extendsErrorLike(node) {
|
|
11
|
+
return (!!node.superClass &&
|
|
12
|
+
node.superClass.type === 'Identifier' &&
|
|
13
|
+
node.superClass.name.endsWith('Error'));
|
|
14
|
+
}
|
|
15
|
+
function isClassNode(node) {
|
|
16
|
+
return node.type === 'ClassDeclaration' || node.type === 'ClassExpression';
|
|
17
|
+
}
|
|
18
|
+
/** Determine whether an assignment target is the `this.name` member expression. */
|
|
19
|
+
function isThisNameMember(node) {
|
|
20
|
+
return (node.type === 'MemberExpression' &&
|
|
21
|
+
node.object.type === 'ThisExpression' &&
|
|
22
|
+
!node.computed &&
|
|
23
|
+
node.property.type === 'Identifier' &&
|
|
24
|
+
node.property.name === 'name');
|
|
25
|
+
}
|
|
26
|
+
/** Determine whether a class already declares a (non-computed) `name` member. */
|
|
27
|
+
function hasNameMember(node) {
|
|
28
|
+
return node.body.body.some((member) => (member.type === 'PropertyDefinition' || member.type === 'MethodDefinition') &&
|
|
29
|
+
!member.computed &&
|
|
30
|
+
member.key.type === 'Identifier' &&
|
|
31
|
+
member.key.name === 'name');
|
|
32
|
+
}
|
|
33
|
+
const rule = {
|
|
34
|
+
meta: {
|
|
35
|
+
type: 'suggestion',
|
|
36
|
+
fixable: 'code',
|
|
37
|
+
messages: {
|
|
38
|
+
errorNameAsClassField: 'Override `name` as a class field (`public override readonly name = ...`) instead of assigning `this.name` in the constructor.',
|
|
39
|
+
missingErrorNameField: 'Declare `name` as a class field (`public override readonly name = ...`) on every `Error` subclass so instances are identifiable in logs.',
|
|
40
|
+
},
|
|
41
|
+
schema: [],
|
|
42
|
+
},
|
|
43
|
+
create(context) {
|
|
44
|
+
/**
|
|
45
|
+
* Classes whose own instance has `this.name` assigned somewhere in their body (constructor,
|
|
46
|
+
* method, or nested arrow). The missing-`name` check skips these: such a class manages
|
|
47
|
+
* `name` imperatively, so inserting a `readonly` field could conflict (an arrow's `this` is
|
|
48
|
+
* the instance, so its `this.name = ...` would write to the new `readonly` field). Only the
|
|
49
|
+
* _nearest_ enclosing class is recorded: a `this.name` inside a nested class targets that
|
|
50
|
+
* inner instance, not the outer one, so it must not suppress the outer class's report.
|
|
51
|
+
*/
|
|
52
|
+
const classesAssigningThisName = new Set();
|
|
53
|
+
function checkMissingNameField(node) {
|
|
54
|
+
if (!extendsErrorLike(node) ||
|
|
55
|
+
hasNameMember(node) ||
|
|
56
|
+
classesAssigningThisName.has(node)) {
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
const className = node.id?.name;
|
|
60
|
+
context.report({
|
|
61
|
+
node: node.id ?? node,
|
|
62
|
+
messageId: 'missingErrorNameField',
|
|
63
|
+
/**
|
|
64
|
+
* Only autofix when the class name is derivable (named declarations and named class
|
|
65
|
+
* expressions). Anonymous class expressions are reported without a fix.
|
|
66
|
+
*/
|
|
67
|
+
fix: className
|
|
68
|
+
? (fixer) => {
|
|
69
|
+
const openBrace = context.sourceCode.getFirstToken(node.body);
|
|
70
|
+
if (!openBrace) {
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
return fixer.insertTextAfter(openBrace, `\n public override readonly name = '${className}';`);
|
|
74
|
+
}
|
|
75
|
+
: undefined,
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
return {
|
|
79
|
+
AssignmentExpression(node) {
|
|
80
|
+
if (!isThisNameMember(node.left)) {
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
const ancestors = context.sourceCode.getAncestors(node);
|
|
84
|
+
const nearestEnclosingClass = ancestors.findLast(isClassNode);
|
|
85
|
+
if (nearestEnclosingClass) {
|
|
86
|
+
classesAssigningThisName.add(nearestEnclosingClass);
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Gate the report on the assignment's _closest enclosing function_: it must be the
|
|
90
|
+
* class constructor itself. Tracking only a constructor-depth flag would wrongly
|
|
91
|
+
* flag `this.name` assignments inside nested functions/methods declared within the
|
|
92
|
+
* constructor body (e.g. a nested `class Inner { method() { this.name = 'x'; }
|
|
93
|
+
* }`).
|
|
94
|
+
*/
|
|
95
|
+
const enclosingFunctionIndex = ancestors.findLastIndex((ancestor) => functionNodeTypes.includes(ancestor.type));
|
|
96
|
+
if (enclosingFunctionIndex < 0) {
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
const methodDefinition = ancestors[enclosingFunctionIndex - 1];
|
|
100
|
+
if (methodDefinition?.type !== 'MethodDefinition' ||
|
|
101
|
+
methodDefinition.kind !== 'constructor') {
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
const classBody = ancestors[enclosingFunctionIndex - 2];
|
|
105
|
+
const classNode = ancestors[enclosingFunctionIndex - 3];
|
|
106
|
+
if (!classBody ||
|
|
107
|
+
!classNode ||
|
|
108
|
+
!isClassNode(classNode) ||
|
|
109
|
+
!extendsErrorLike(classNode)) {
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
const enclosingFunction = ancestors[enclosingFunctionIndex];
|
|
113
|
+
const statement = node.parent;
|
|
114
|
+
/**
|
|
115
|
+
* Only offer an autofix when the conversion is provably safe: the assignment is a
|
|
116
|
+
* standalone top-level statement of the constructor body (not nested in a
|
|
117
|
+
* conditional/loop, so no control flow is lost), its value is a literal (so the
|
|
118
|
+
* field initializer cannot capture a constructor parameter or local, which run
|
|
119
|
+
* after field initialization), and the class does not already declare a `name`
|
|
120
|
+
* member (otherwise inserting a field would duplicate it). Anything else is
|
|
121
|
+
* reported without a fix.
|
|
122
|
+
*/
|
|
123
|
+
const canAutofix = statement.type === 'ExpressionStatement' &&
|
|
124
|
+
statement.parent.type === 'BlockStatement' &&
|
|
125
|
+
statement.parent.parent === enclosingFunction &&
|
|
126
|
+
node.right.type === 'Literal' &&
|
|
127
|
+
!hasNameMember(classNode);
|
|
128
|
+
context.report({
|
|
129
|
+
node,
|
|
130
|
+
messageId: 'errorNameAsClassField',
|
|
131
|
+
fix: canAutofix
|
|
132
|
+
? (fixer) => {
|
|
133
|
+
const openBrace = context.sourceCode.getFirstToken(classBody);
|
|
134
|
+
const tokenBeforeStatement = context.sourceCode.getTokenBefore(statement);
|
|
135
|
+
const statementRange = statement.range;
|
|
136
|
+
/**
|
|
137
|
+
* Bail out (report without fixing) when required tokens are missing,
|
|
138
|
+
* or when a comment sits between the previous token and the
|
|
139
|
+
* assignment: such a comment falls inside the removal range and would
|
|
140
|
+
* be silently deleted (e.g. `super(); // keep`).
|
|
141
|
+
*/
|
|
142
|
+
if (!openBrace ||
|
|
143
|
+
!tokenBeforeStatement ||
|
|
144
|
+
!statementRange ||
|
|
145
|
+
context.sourceCode.getCommentsBefore(statement).length > 0) {
|
|
146
|
+
return null;
|
|
147
|
+
}
|
|
148
|
+
const valueText = context.sourceCode.getText(node.right);
|
|
149
|
+
return [
|
|
150
|
+
fixer.insertTextAfter(openBrace, `\n public override readonly name = ${valueText};`),
|
|
151
|
+
fixer.removeRange([
|
|
152
|
+
tokenBeforeStatement.range[1],
|
|
153
|
+
statementRange[1],
|
|
154
|
+
]),
|
|
155
|
+
];
|
|
156
|
+
}
|
|
157
|
+
: undefined,
|
|
158
|
+
});
|
|
159
|
+
},
|
|
160
|
+
'ClassDeclaration:exit': checkMissingNameField,
|
|
161
|
+
'ClassExpression:exit': checkMissingNameField,
|
|
162
|
+
};
|
|
163
|
+
},
|
|
164
|
+
};
|
|
165
|
+
export default rule;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
const rule = {
|
|
2
|
+
meta: {
|
|
3
|
+
type: 'problem',
|
|
4
|
+
messages: {
|
|
5
|
+
rawDate: 'Do not use `new Date`. Use date-vir utilities (e.g. `createFullDate`, `getNowInUtcTimezone`, `createUtcFullDate`) instead.',
|
|
6
|
+
},
|
|
7
|
+
schema: [],
|
|
8
|
+
},
|
|
9
|
+
create(context) {
|
|
10
|
+
return {
|
|
11
|
+
NewExpression(node) {
|
|
12
|
+
const callee = node.callee;
|
|
13
|
+
/**
|
|
14
|
+
* Catch `new Date()` and the global-namespace forms `new globalThis.Date()` and
|
|
15
|
+
* `new globalThis['Date']()`, since all construct the built-in Date. A `new
|
|
16
|
+
* namespace.Date()` on some other object is left alone: that is a different,
|
|
17
|
+
* user-defined constructor. Dynamic access through a variable (e.g. `new
|
|
18
|
+
* globalThis[propertyName]()`) is not statically determinable and is intentionally
|
|
19
|
+
* not flagged.
|
|
20
|
+
*/
|
|
21
|
+
const isGlobalThisDate = callee.type === 'MemberExpression' &&
|
|
22
|
+
callee.object.type === 'Identifier' &&
|
|
23
|
+
callee.object.name === 'globalThis' &&
|
|
24
|
+
((!callee.computed &&
|
|
25
|
+
callee.property.type === 'Identifier' &&
|
|
26
|
+
callee.property.name === 'Date') ||
|
|
27
|
+
(callee.computed &&
|
|
28
|
+
callee.property.type === 'Literal' &&
|
|
29
|
+
callee.property.value === 'Date'));
|
|
30
|
+
const constructsRawDate = (callee.type === 'Identifier' && callee.name === 'Date') || isGlobalThisDate;
|
|
31
|
+
if (!constructsRawDate) {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
context.report({
|
|
35
|
+
node,
|
|
36
|
+
messageId: 'rawDate',
|
|
37
|
+
});
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
export default rule;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Primitive keyword type node names (plus `TSLiteralType` for literal types like `'a'` or `42`)
|
|
3
|
+
* that have no meaningful `Readonly<>` wrapping.
|
|
4
|
+
*/
|
|
5
|
+
const primitiveTypeNodeTypes = [
|
|
6
|
+
'TSStringKeyword',
|
|
7
|
+
'TSNumberKeyword',
|
|
8
|
+
'TSBooleanKeyword',
|
|
9
|
+
'TSBigIntKeyword',
|
|
10
|
+
'TSSymbolKeyword',
|
|
11
|
+
'TSNullKeyword',
|
|
12
|
+
'TSUndefinedKeyword',
|
|
13
|
+
'TSLiteralType',
|
|
14
|
+
];
|
|
15
|
+
const rule = {
|
|
16
|
+
meta: {
|
|
17
|
+
type: 'suggestion',
|
|
18
|
+
fixable: 'code',
|
|
19
|
+
messages: {
|
|
20
|
+
noReadonlyPrimitive: 'Do not wrap a primitive type in `Readonly<>` - it has no effect. Use the bare primitive type.',
|
|
21
|
+
},
|
|
22
|
+
schema: [],
|
|
23
|
+
},
|
|
24
|
+
create(context) {
|
|
25
|
+
return {
|
|
26
|
+
TSTypeReference(node) {
|
|
27
|
+
const tsNode = node;
|
|
28
|
+
if (tsNode.typeName.type !== 'Identifier' || tsNode.typeName.name !== 'Readonly') {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* `@typescript-eslint/parser` exposes a type reference's type arguments as
|
|
33
|
+
* `typeArguments` on current versions but historically used `typeParameters`; read
|
|
34
|
+
* both so the rule is resilient across parser versions (matching
|
|
35
|
+
* `no-readonly-inputs`).
|
|
36
|
+
*/
|
|
37
|
+
const typeArguments = tsNode.typeArguments || tsNode.typeParameters;
|
|
38
|
+
if (!typeArguments || typeArguments.params.length !== 1) {
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
const innerType = typeArguments.params[0];
|
|
42
|
+
if (!innerType) {
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Unwrap `TSParenthesizedType` if present so `Readonly<(string)>` is flagged and
|
|
47
|
+
* fixed to the bare `string` (matching `prefer-array-element-type`).
|
|
48
|
+
*/
|
|
49
|
+
const innerTypeRecord = innerType;
|
|
50
|
+
const unwrappedInnerType = innerTypeRecord.type === 'TSParenthesizedType' && innerTypeRecord.typeAnnotation
|
|
51
|
+
? innerTypeRecord.typeAnnotation
|
|
52
|
+
: innerType;
|
|
53
|
+
if (!primitiveTypeNodeTypes.includes(unwrappedInnerType.type)) {
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
const innerTypeText = context.sourceCode.getText(unwrappedInnerType);
|
|
57
|
+
context.report({
|
|
58
|
+
node,
|
|
59
|
+
messageId: 'noReadonlyPrimitive',
|
|
60
|
+
fix(fixer) {
|
|
61
|
+
return fixer.replaceText(node, innerTypeText);
|
|
62
|
+
},
|
|
63
|
+
});
|
|
64
|
+
},
|
|
65
|
+
};
|
|
66
|
+
},
|
|
67
|
+
};
|
|
68
|
+
export default rule;
|
|
@@ -31,7 +31,7 @@ function isRelativeImport(specifier) {
|
|
|
31
31
|
specifier.startsWith('./') ||
|
|
32
32
|
specifier.startsWith('../'));
|
|
33
33
|
}
|
|
34
|
-
function isWithinDir(target, baseDir) {
|
|
34
|
+
function isWithinDir({ target, baseDir }) {
|
|
35
35
|
if (target === baseDir) {
|
|
36
36
|
return true;
|
|
37
37
|
}
|
|
@@ -52,7 +52,10 @@ function checkImport(context, importPath, node) {
|
|
|
52
52
|
return;
|
|
53
53
|
}
|
|
54
54
|
const resolvedImport = resolve(fileDir, importPath);
|
|
55
|
-
if (isWithinDir(
|
|
55
|
+
if (isWithinDir({
|
|
56
|
+
target: resolvedImport,
|
|
57
|
+
baseDir: packageInfo.dir,
|
|
58
|
+
})) {
|
|
56
59
|
return;
|
|
57
60
|
}
|
|
58
61
|
context.report({
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
const testFunctionNames = [
|
|
2
|
+
'it',
|
|
3
|
+
'test',
|
|
4
|
+
'describe',
|
|
5
|
+
];
|
|
6
|
+
/**
|
|
7
|
+
* Extracts the base identifier name of a test call's callee, supporting both bare identifiers
|
|
8
|
+
* (`it`) and member expressions (`it.only`, `test.skip`).
|
|
9
|
+
*/
|
|
10
|
+
function getCalleeBaseName(callee) {
|
|
11
|
+
if (callee.type === 'Identifier') {
|
|
12
|
+
return callee.name;
|
|
13
|
+
}
|
|
14
|
+
else if (callee.type === 'MemberExpression' && callee.object.type === 'Identifier') {
|
|
15
|
+
return callee.object.name;
|
|
16
|
+
}
|
|
17
|
+
return undefined;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Reads a statically-known string from a node, supporting both string literals and template
|
|
21
|
+
* literals with no interpolations. Returns `undefined` for anything dynamic.
|
|
22
|
+
*/
|
|
23
|
+
function getStaticStringValue(node) {
|
|
24
|
+
if (node?.type === 'Literal' && typeof node.value === 'string') {
|
|
25
|
+
return node.value;
|
|
26
|
+
}
|
|
27
|
+
else if (node?.type === 'TemplateLiteral' &&
|
|
28
|
+
node.expressions.length === 0 &&
|
|
29
|
+
node.quasis[0]) {
|
|
30
|
+
return node.quasis[0].value.cooked ?? undefined;
|
|
31
|
+
}
|
|
32
|
+
return undefined;
|
|
33
|
+
}
|
|
34
|
+
function startsWithShould(value) {
|
|
35
|
+
return value != undefined && /^should\b/i.test(value);
|
|
36
|
+
}
|
|
37
|
+
/** Whether a property's key is the `it` description key used by `itCases`-style test tables. */
|
|
38
|
+
function isItDescriptionKey(property) {
|
|
39
|
+
if (property.computed) {
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
return ((property.key.type === 'Identifier' && property.key.name === 'it') ||
|
|
43
|
+
(property.key.type === 'Literal' && property.key.value === 'it'));
|
|
44
|
+
}
|
|
45
|
+
const rule = {
|
|
46
|
+
meta: {
|
|
47
|
+
type: 'suggestion',
|
|
48
|
+
messages: {
|
|
49
|
+
noShould: "Write the test description in declarative present tense without a leading 'should' (e.g. 'strips maiden name' not 'should strip maiden name').",
|
|
50
|
+
},
|
|
51
|
+
schema: [],
|
|
52
|
+
},
|
|
53
|
+
create(context) {
|
|
54
|
+
return {
|
|
55
|
+
CallExpression(node) {
|
|
56
|
+
const baseName = getCalleeBaseName(node.callee);
|
|
57
|
+
if (!baseName || !testFunctionNames.includes(baseName)) {
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
const firstArgument = node.arguments[0];
|
|
61
|
+
if (!firstArgument ||
|
|
62
|
+
firstArgument.type === 'SpreadElement' ||
|
|
63
|
+
!startsWithShould(getStaticStringValue(firstArgument))) {
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
context.report({
|
|
67
|
+
node: firstArgument,
|
|
68
|
+
messageId: 'noShould',
|
|
69
|
+
});
|
|
70
|
+
},
|
|
71
|
+
Property(node) {
|
|
72
|
+
if (!isItDescriptionKey(node) ||
|
|
73
|
+
!startsWithShould(getStaticStringValue(node.value))) {
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
context.report({
|
|
77
|
+
node: node.value,
|
|
78
|
+
messageId: 'noShould',
|
|
79
|
+
});
|
|
80
|
+
},
|
|
81
|
+
};
|
|
82
|
+
},
|
|
83
|
+
};
|
|
84
|
+
export default rule;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Recursively reports any binding `Identifier` named exactly `_` reachable from a binding pattern.
|
|
3
|
+
* Walks into destructuring patterns (object/array), default values (`AssignmentPattern`), and rest
|
|
4
|
+
* elements (`RestElement`) so placeholders like `{a: _}`, `[_]`, `_ = 1`, and `..._` are caught.
|
|
5
|
+
* Only binding positions are visited - object-pattern property keys are skipped (only the value
|
|
6
|
+
* side is a binding), and non-binding nodes (e.g. member-expression assignment targets) are
|
|
7
|
+
* ignored.
|
|
8
|
+
*/
|
|
9
|
+
function reportUnderscoresInPattern(context, node) {
|
|
10
|
+
if (!node) {
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
if (node.type === 'Identifier') {
|
|
14
|
+
if (node.name === '_') {
|
|
15
|
+
context.report({
|
|
16
|
+
node,
|
|
17
|
+
messageId: 'underscoreVariable',
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
else if (node.type === 'AssignmentPattern') {
|
|
22
|
+
reportUnderscoresInPattern(context, node.left);
|
|
23
|
+
}
|
|
24
|
+
else if (node.type === 'RestElement') {
|
|
25
|
+
reportUnderscoresInPattern(context, node.argument);
|
|
26
|
+
}
|
|
27
|
+
else if (node.type === 'ArrayPattern') {
|
|
28
|
+
node.elements.forEach((element) => reportUnderscoresInPattern(context, element));
|
|
29
|
+
}
|
|
30
|
+
else if (node.type === 'ObjectPattern') {
|
|
31
|
+
node.properties.forEach((property) => {
|
|
32
|
+
reportUnderscoresInPattern(context, property.type === 'RestElement' ? property.argument : property.value);
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
const rule = {
|
|
37
|
+
meta: {
|
|
38
|
+
type: 'suggestion',
|
|
39
|
+
messages: {
|
|
40
|
+
underscoreVariable: 'Do not use a standalone `_` as a variable or parameter name; give it a brief descriptive name (e.g. `unusedReason`).',
|
|
41
|
+
},
|
|
42
|
+
schema: [],
|
|
43
|
+
},
|
|
44
|
+
create(context) {
|
|
45
|
+
return {
|
|
46
|
+
VariableDeclarator(node) {
|
|
47
|
+
reportUnderscoresInPattern(context, node.id);
|
|
48
|
+
},
|
|
49
|
+
'FunctionDeclaration, FunctionExpression, ArrowFunctionExpression'(node) {
|
|
50
|
+
node.params.forEach((param) => {
|
|
51
|
+
reportUnderscoresInPattern(context, param);
|
|
52
|
+
});
|
|
53
|
+
},
|
|
54
|
+
CatchClause(node) {
|
|
55
|
+
reportUnderscoresInPattern(context, node.param);
|
|
56
|
+
},
|
|
57
|
+
};
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
export default rule;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
const rule = {
|
|
2
|
+
meta: {
|
|
3
|
+
type: 'suggestion',
|
|
4
|
+
fixable: 'code',
|
|
5
|
+
messages: {
|
|
6
|
+
preferBlockBody: 'Use a block body with an explicit `return` instead of the parenthesized object shorthand `() => ({...})`.',
|
|
7
|
+
},
|
|
8
|
+
schema: [],
|
|
9
|
+
},
|
|
10
|
+
create(context) {
|
|
11
|
+
const sourceCode = context.sourceCode;
|
|
12
|
+
return {
|
|
13
|
+
ArrowFunctionExpression(node) {
|
|
14
|
+
/**
|
|
15
|
+
* `() => ({ ... })` parses with an `ObjectExpression` body; the wrapping parens are
|
|
16
|
+
* not represented as a node. A block body (`() => { ... }`) parses as a
|
|
17
|
+
* `BlockStatement` and must not be flagged.
|
|
18
|
+
*/
|
|
19
|
+
if (node.body.type !== 'ObjectExpression') {
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
context.report({
|
|
23
|
+
node: node.body,
|
|
24
|
+
messageId: 'preferBlockBody',
|
|
25
|
+
fix(fixer) {
|
|
26
|
+
const objectText = sourceCode.getText(node.body);
|
|
27
|
+
/**
|
|
28
|
+
* The object body is always wrapped in parentheses in source (the parser
|
|
29
|
+
* requires them to disambiguate from a block body), but those parens are
|
|
30
|
+
* not AST nodes. Expand the replacement to swallow the surrounding `(` and
|
|
31
|
+
* `)` tokens so the rewrite does not leave stray parentheses behind.
|
|
32
|
+
*/
|
|
33
|
+
const openParen = sourceCode.getTokenBefore(node.body);
|
|
34
|
+
const closeParen = sourceCode.getTokenAfter(node.body);
|
|
35
|
+
if (!openParen ||
|
|
36
|
+
!closeParen ||
|
|
37
|
+
openParen.value !== '(' ||
|
|
38
|
+
closeParen.value !== ')') {
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
return fixer.replaceTextRange([
|
|
42
|
+
openParen.range[0],
|
|
43
|
+
closeParen.range[1],
|
|
44
|
+
], `{ return ${objectText}; }`);
|
|
45
|
+
},
|
|
46
|
+
});
|
|
47
|
+
},
|
|
48
|
+
};
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
export default rule;
|