@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
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import {type Rule} from 'eslint';
|
|
2
|
+
import type {ConditionalExpression, Expression, Node} from 'estree';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Check whether a node is a `new Error(...)` expression (a NewExpression whose callee is the
|
|
6
|
+
* Identifier `Error`).
|
|
7
|
+
*/
|
|
8
|
+
function isNewError(node: Readonly<Node>): boolean {
|
|
9
|
+
return (
|
|
10
|
+
node.type === 'NewExpression' &&
|
|
11
|
+
node.callee.type === 'Identifier' &&
|
|
12
|
+
node.callee.name === 'Error'
|
|
13
|
+
);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Check whether a node is an `x instanceof Error` expression and return the left operand if so.
|
|
18
|
+
* Returns `undefined` for any other shape.
|
|
19
|
+
*/
|
|
20
|
+
function getInstanceofErrorOperand(node: Readonly<Expression>): Expression | undefined {
|
|
21
|
+
if (
|
|
22
|
+
node.type === 'BinaryExpression' &&
|
|
23
|
+
node.operator === 'instanceof' &&
|
|
24
|
+
node.right.type === 'Identifier' &&
|
|
25
|
+
node.right.name === 'Error' &&
|
|
26
|
+
/** The left operand of `instanceof` is always an Expression, never a PrivateIdentifier. */
|
|
27
|
+
node.left.type !== 'PrivateIdentifier'
|
|
28
|
+
) {
|
|
29
|
+
return node.left;
|
|
30
|
+
}
|
|
31
|
+
return undefined;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const rule: Rule.RuleModule = {
|
|
35
|
+
meta: {
|
|
36
|
+
type: 'suggestion',
|
|
37
|
+
messages: {
|
|
38
|
+
preferEnsureError:
|
|
39
|
+
'Use `ensureErrorAndPrependMessage({{name}}, ...)` from `@augment-vir/common` instead of an `instanceof Error` ternary.',
|
|
40
|
+
},
|
|
41
|
+
schema: [],
|
|
42
|
+
},
|
|
43
|
+
create(context) {
|
|
44
|
+
return {
|
|
45
|
+
ConditionalExpression(node: ConditionalExpression & Rule.NodeParentExtension) {
|
|
46
|
+
/** Canonical form: `x instanceof Error ? x : new Error(...)`. */
|
|
47
|
+
const directOperand = getInstanceofErrorOperand(node.test);
|
|
48
|
+
if (
|
|
49
|
+
directOperand?.type === 'Identifier' &&
|
|
50
|
+
node.consequent.type === 'Identifier' &&
|
|
51
|
+
directOperand.name === node.consequent.name &&
|
|
52
|
+
isNewError(node.alternate)
|
|
53
|
+
) {
|
|
54
|
+
context.report({
|
|
55
|
+
node,
|
|
56
|
+
messageId: 'preferEnsureError',
|
|
57
|
+
data: {
|
|
58
|
+
name: directOperand.name,
|
|
59
|
+
},
|
|
60
|
+
});
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Inverted form: `!(x instanceof Error) ? new Error(...) : x`. */
|
|
65
|
+
if (node.test.type === 'UnaryExpression' && node.test.operator === '!') {
|
|
66
|
+
const inner =
|
|
67
|
+
node.test.argument.type === 'BinaryExpression'
|
|
68
|
+
? node.test.argument
|
|
69
|
+
: undefined;
|
|
70
|
+
const invertedOperand = inner ? getInstanceofErrorOperand(inner) : undefined;
|
|
71
|
+
if (
|
|
72
|
+
invertedOperand?.type === 'Identifier' &&
|
|
73
|
+
node.alternate.type === 'Identifier' &&
|
|
74
|
+
invertedOperand.name === node.alternate.name &&
|
|
75
|
+
isNewError(node.consequent)
|
|
76
|
+
) {
|
|
77
|
+
context.report({
|
|
78
|
+
node,
|
|
79
|
+
messageId: 'preferEnsureError',
|
|
80
|
+
data: {
|
|
81
|
+
name: invertedOperand.name,
|
|
82
|
+
},
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
},
|
|
87
|
+
};
|
|
88
|
+
},
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
export default rule;
|
|
@@ -11,6 +11,11 @@ function isTerminating(node: IfStatement): boolean {
|
|
|
11
11
|
return last != undefined && (last.type === 'ReturnStatement' || last.type === 'ThrowStatement');
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
+
/**
|
|
15
|
+
* A type predicate cannot reference a destructured binding, so this function must keep positional
|
|
16
|
+
* parameters to narrow `curr`.
|
|
17
|
+
*/
|
|
18
|
+
// eslint-disable-next-line @virmator/prefer-params-object
|
|
14
19
|
function isConsecutiveTerminatingIfs(
|
|
15
20
|
curr: AnyStatement,
|
|
16
21
|
next: AnyStatement,
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import {type Rule} from 'eslint';
|
|
2
|
+
import type {CallExpression, MemberExpression} from 'estree';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Extract the method name from a non-computed MemberExpression callee. Returns `undefined` for
|
|
6
|
+
* computed access or non-Identifier properties.
|
|
7
|
+
*/
|
|
8
|
+
function getMethodName(node: Readonly<MemberExpression>): string | undefined {
|
|
9
|
+
if (!node.computed && node.property.type === 'Identifier') {
|
|
10
|
+
return node.property.name;
|
|
11
|
+
}
|
|
12
|
+
return undefined;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Whether a call's callee builds an object from an array of entries: `Object.fromEntries(...)`,
|
|
17
|
+
* `typedObjectFromEntries(...)`, or `arrayToObject(...)`. These are the only contexts where a
|
|
18
|
+
* `getEnumValues(...).map(...)` is constructing an object mapping (and thus where `mapEnumToObject`
|
|
19
|
+
* is the right replacement). A bare `.map(...)` that produces an array is left alone.
|
|
20
|
+
*/
|
|
21
|
+
function isObjectBuilderCallee(callee: Readonly<CallExpression['callee']>): boolean {
|
|
22
|
+
if (callee.type === 'Identifier') {
|
|
23
|
+
return callee.name === 'typedObjectFromEntries' || callee.name === 'arrayToObject';
|
|
24
|
+
}
|
|
25
|
+
return (
|
|
26
|
+
callee.type === 'MemberExpression' &&
|
|
27
|
+
!callee.computed &&
|
|
28
|
+
callee.object.type === 'Identifier' &&
|
|
29
|
+
callee.object.name === 'Object' &&
|
|
30
|
+
callee.property.type === 'Identifier' &&
|
|
31
|
+
callee.property.name === 'fromEntries'
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const rule: Rule.RuleModule = {
|
|
36
|
+
meta: {
|
|
37
|
+
type: 'suggestion',
|
|
38
|
+
messages: {
|
|
39
|
+
preferMapEnumToObject:
|
|
40
|
+
'Use `mapEnumToObject` from `@augment-vir/common` instead of building an object from `getEnumValues(...).map(...)`.',
|
|
41
|
+
},
|
|
42
|
+
schema: [],
|
|
43
|
+
},
|
|
44
|
+
create(context) {
|
|
45
|
+
return {
|
|
46
|
+
CallExpression(node: CallExpression & Rule.NodeParentExtension) {
|
|
47
|
+
if (
|
|
48
|
+
node.callee.type !== 'MemberExpression' ||
|
|
49
|
+
getMethodName(node.callee) !== 'map'
|
|
50
|
+
) {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const innerCall = node.callee.object;
|
|
55
|
+
if (
|
|
56
|
+
innerCall.type !== 'CallExpression' ||
|
|
57
|
+
innerCall.callee.type !== 'Identifier' ||
|
|
58
|
+
innerCall.callee.name !== 'getEnumValues'
|
|
59
|
+
) {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Only flag when the `.map(...)` result is fed straight into an object builder. A
|
|
65
|
+
* bare `getEnumValues(...).map(...)` producing an array is a legitimate pattern
|
|
66
|
+
* that `mapEnumToObject` cannot replace.
|
|
67
|
+
*/
|
|
68
|
+
const parent = node.parent;
|
|
69
|
+
if (
|
|
70
|
+
parent.type !== 'CallExpression' ||
|
|
71
|
+
parent.arguments[0] !== node ||
|
|
72
|
+
!isObjectBuilderCallee(parent.callee)
|
|
73
|
+
) {
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
context.report({
|
|
78
|
+
node,
|
|
79
|
+
messageId: 'preferMapEnumToObject',
|
|
80
|
+
});
|
|
81
|
+
},
|
|
82
|
+
};
|
|
83
|
+
},
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
export default rule;
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
import {type Rule, type SourceCode} from 'eslint';
|
|
2
|
+
import type {
|
|
3
|
+
ArrowFunctionExpression,
|
|
4
|
+
FunctionDeclaration,
|
|
5
|
+
FunctionExpression,
|
|
6
|
+
Identifier,
|
|
7
|
+
Node,
|
|
8
|
+
Pattern,
|
|
9
|
+
} from 'estree';
|
|
10
|
+
|
|
11
|
+
type AnyFunctionNode = FunctionDeclaration | FunctionExpression | ArrowFunctionExpression;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* The typescript-eslint parser attaches `typeAnnotation` and `optional` to identifier nodes, but
|
|
15
|
+
* those properties are absent from the base estree types.
|
|
16
|
+
*/
|
|
17
|
+
type TypedIdentifier = Identifier & {
|
|
18
|
+
typeAnnotation?: {typeAnnotation: Node};
|
|
19
|
+
optional?: boolean;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
type FixableParam = {
|
|
23
|
+
name: string;
|
|
24
|
+
typeText: string;
|
|
25
|
+
defaultText: string | undefined;
|
|
26
|
+
optional: boolean;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Excludes a leading `this` parameter (which is a type-only annotation in TypeScript, not a real
|
|
31
|
+
* positional argument).
|
|
32
|
+
*/
|
|
33
|
+
function getRealParams(params: ReadonlyArray<Pattern>): ReadonlyArray<Pattern> {
|
|
34
|
+
const first = params[0];
|
|
35
|
+
if (first?.type === 'Identifier' && first.name === 'this') {
|
|
36
|
+
return params.slice(1);
|
|
37
|
+
}
|
|
38
|
+
return params;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function getTypeText(param: Pattern, sourceCode: Readonly<SourceCode>): string | undefined {
|
|
42
|
+
const identifier =
|
|
43
|
+
param.type === 'Identifier'
|
|
44
|
+
? (param as TypedIdentifier)
|
|
45
|
+
: param.type === 'AssignmentPattern' && param.left.type === 'Identifier'
|
|
46
|
+
? (param.left as TypedIdentifier)
|
|
47
|
+
: undefined;
|
|
48
|
+
|
|
49
|
+
const typeAnnotation = identifier?.typeAnnotation;
|
|
50
|
+
if (!typeAnnotation) {
|
|
51
|
+
return undefined;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return sourceCode.getText(typeAnnotation.typeAnnotation);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function hasDuplicateType(
|
|
58
|
+
params: ReadonlyArray<Pattern>,
|
|
59
|
+
sourceCode: Readonly<SourceCode>,
|
|
60
|
+
): boolean {
|
|
61
|
+
const seen = new Set<string>();
|
|
62
|
+
|
|
63
|
+
return params.some((param) => {
|
|
64
|
+
const typeText = getTypeText(param, sourceCode);
|
|
65
|
+
if (typeText == undefined) {
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Normalize whitespace so `Array<string>` and `Array< string >` compare as equal. */
|
|
70
|
+
const key = typeText.replaceAll(/\s+/g, '');
|
|
71
|
+
if (seen.has(key)) {
|
|
72
|
+
return true;
|
|
73
|
+
}
|
|
74
|
+
seen.add(key);
|
|
75
|
+
return false;
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function getFixableParam(
|
|
80
|
+
param: Pattern,
|
|
81
|
+
sourceCode: Readonly<SourceCode>,
|
|
82
|
+
): FixableParam | undefined {
|
|
83
|
+
const identifier =
|
|
84
|
+
param.type === 'Identifier'
|
|
85
|
+
? (param as TypedIdentifier)
|
|
86
|
+
: param.type === 'AssignmentPattern' && param.left.type === 'Identifier'
|
|
87
|
+
? (param.left as TypedIdentifier)
|
|
88
|
+
: undefined;
|
|
89
|
+
|
|
90
|
+
if (!identifier?.typeAnnotation) {
|
|
91
|
+
return undefined;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const defaultText =
|
|
95
|
+
param.type === 'AssignmentPattern' ? sourceCode.getText(param.right) : undefined;
|
|
96
|
+
|
|
97
|
+
return {
|
|
98
|
+
name: identifier.name,
|
|
99
|
+
typeText: sourceCode.getText(identifier.typeAnnotation.typeAnnotation),
|
|
100
|
+
defaultText,
|
|
101
|
+
optional: Boolean(identifier.optional) || defaultText != undefined,
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function buildParamsObjectFix(
|
|
106
|
+
params: ReadonlyArray<Pattern>,
|
|
107
|
+
sourceCode: Readonly<SourceCode>,
|
|
108
|
+
): Rule.ReportFixer | undefined {
|
|
109
|
+
const fixableParams = params.map((param) => getFixableParam(param, sourceCode));
|
|
110
|
+
if (fixableParams.some((fixable) => fixable == undefined)) {
|
|
111
|
+
return undefined;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const definedParams = fixableParams.filter((fixable) => fixable != undefined);
|
|
115
|
+
|
|
116
|
+
const patternText = definedParams
|
|
117
|
+
.map((param) =>
|
|
118
|
+
param.defaultText == undefined ? param.name : `${param.name} = ${param.defaultText}`,
|
|
119
|
+
)
|
|
120
|
+
.join(', ');
|
|
121
|
+
const typeText = definedParams
|
|
122
|
+
.map((param) => `${param.name}${param.optional ? '?' : ''}: ${param.typeText}`)
|
|
123
|
+
.join('; ');
|
|
124
|
+
|
|
125
|
+
const firstParam = params[0];
|
|
126
|
+
const lastParam = params.at(-1);
|
|
127
|
+
if (!firstParam || !lastParam) {
|
|
128
|
+
return undefined;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const start = sourceCode.getRange(firstParam)[0];
|
|
132
|
+
const end = sourceCode.getRange(lastParam)[1];
|
|
133
|
+
|
|
134
|
+
return (fixer) =>
|
|
135
|
+
fixer.replaceTextRange(
|
|
136
|
+
[
|
|
137
|
+
start,
|
|
138
|
+
end,
|
|
139
|
+
],
|
|
140
|
+
`{${patternText}}: Readonly<{${typeText}}>`,
|
|
141
|
+
);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const rule: Rule.RuleModule = {
|
|
145
|
+
meta: {
|
|
146
|
+
type: 'suggestion',
|
|
147
|
+
fixable: 'code',
|
|
148
|
+
messages: {
|
|
149
|
+
tooManyPositionalParams:
|
|
150
|
+
'Functions with more than 3 parameters must use a single destructured params object instead.',
|
|
151
|
+
duplicateParamType:
|
|
152
|
+
'Functions with multiple parameters of the same type must use a single destructured params object to avoid order-based mistakes.',
|
|
153
|
+
},
|
|
154
|
+
schema: [],
|
|
155
|
+
},
|
|
156
|
+
create(context) {
|
|
157
|
+
const sourceCode = context.sourceCode;
|
|
158
|
+
|
|
159
|
+
function check(node: AnyFunctionNode) {
|
|
160
|
+
const realParams = getRealParams(node.params);
|
|
161
|
+
|
|
162
|
+
const messageId =
|
|
163
|
+
realParams.length > 3
|
|
164
|
+
? 'tooManyPositionalParams'
|
|
165
|
+
: hasDuplicateType(realParams, sourceCode)
|
|
166
|
+
? 'duplicateParamType'
|
|
167
|
+
: undefined;
|
|
168
|
+
|
|
169
|
+
if (!messageId) {
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
const fix = buildParamsObjectFix(realParams, sourceCode);
|
|
174
|
+
|
|
175
|
+
context.report({
|
|
176
|
+
node,
|
|
177
|
+
messageId,
|
|
178
|
+
fix: fix ?? null,
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
return {
|
|
183
|
+
FunctionDeclaration: check,
|
|
184
|
+
FunctionExpression: check,
|
|
185
|
+
ArrowFunctionExpression: check,
|
|
186
|
+
};
|
|
187
|
+
},
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
export default rule;
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import {type Rule, type SourceCode} from 'eslint';
|
|
2
|
+
import type {Node} from 'estree';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The typescript-eslint parser attaches `accessibility` and `decorators` to class member nodes and
|
|
6
|
+
* constructor parameter properties, but those properties are absent from the base estree types.
|
|
7
|
+
*/
|
|
8
|
+
type AccessibilityNode = Node & {
|
|
9
|
+
accessibility?: 'private' | 'protected' | 'public';
|
|
10
|
+
decorators?: ReadonlyArray<Node>;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
function findPrivateToken(node: AccessibilityNode, sourceCode: Readonly<SourceCode>) {
|
|
14
|
+
const lastDecorator = node.decorators?.at(-1);
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Accessibility modifiers always appear after any decorators and before the member key, so
|
|
18
|
+
* starting the search after the final decorator avoids matching a `@private`-style decorator.
|
|
19
|
+
*/
|
|
20
|
+
return lastDecorator
|
|
21
|
+
? sourceCode.getTokenAfter(lastDecorator, {
|
|
22
|
+
filter: (token) => token.value === 'private',
|
|
23
|
+
})
|
|
24
|
+
: sourceCode.getFirstToken(node, {
|
|
25
|
+
filter: (token) => token.value === 'private',
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const rule: Rule.RuleModule = {
|
|
30
|
+
meta: {
|
|
31
|
+
type: 'suggestion',
|
|
32
|
+
fixable: 'code',
|
|
33
|
+
messages: {
|
|
34
|
+
useProtected: 'Use the `protected` accessibility modifier instead of `private`.',
|
|
35
|
+
},
|
|
36
|
+
schema: [],
|
|
37
|
+
},
|
|
38
|
+
create(context) {
|
|
39
|
+
const sourceCode = context.sourceCode;
|
|
40
|
+
|
|
41
|
+
function check(node: AccessibilityNode) {
|
|
42
|
+
if (node.accessibility !== 'private') {
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
context.report({
|
|
47
|
+
node,
|
|
48
|
+
messageId: 'useProtected',
|
|
49
|
+
fix(fixer) {
|
|
50
|
+
const privateToken = findPrivateToken(node, sourceCode);
|
|
51
|
+
if (!privateToken) {
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
return fixer.replaceText(privateToken, 'protected');
|
|
55
|
+
},
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return {
|
|
60
|
+
MethodDefinition: check,
|
|
61
|
+
PropertyDefinition: check,
|
|
62
|
+
AccessorProperty: check,
|
|
63
|
+
TSAbstractMethodDefinition: check,
|
|
64
|
+
TSAbstractPropertyDefinition: check,
|
|
65
|
+
TSAbstractAccessorProperty: check,
|
|
66
|
+
TSParameterProperty: check,
|
|
67
|
+
};
|
|
68
|
+
},
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
export default rule;
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
import {type Rule, type Scope} from 'eslint';
|
|
2
|
+
import type {ArrayExpression, Expression, ImportDeclaration} from 'estree';
|
|
3
|
+
|
|
4
|
+
const importSource = '@augment-vir/common';
|
|
5
|
+
|
|
6
|
+
type RemoveDuplicatesImportResult = {
|
|
7
|
+
/**
|
|
8
|
+
* The local name under which `removeDuplicates` is already imported from `@augment-vir/common`,
|
|
9
|
+
* if any. Covers both a direct import (`removeDuplicates`) and an aliased one
|
|
10
|
+
* (`removeDuplicates as rd` yields `rd`). The fixer emits a call using this exact name, so no
|
|
11
|
+
* new import is needed.
|
|
12
|
+
*/
|
|
13
|
+
usableLocalName: string | undefined;
|
|
14
|
+
/**
|
|
15
|
+
* A non-type value import from `@augment-vir/common` that has at least one named specifier, so
|
|
16
|
+
* the fixer can safely append `, removeDuplicates` after its last named specifier.
|
|
17
|
+
* Default-only, namespace, and side-effect imports are excluded because they can't be extended
|
|
18
|
+
* with a named specifier via plain text insertion.
|
|
19
|
+
*/
|
|
20
|
+
extensibleDeclaration: ImportDeclaration | undefined;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Scan program imports for a usable `removeDuplicates` value import and/or a named value import
|
|
25
|
+
* from `@augment-vir/common` that the fixer can extend.
|
|
26
|
+
*/
|
|
27
|
+
function findRemoveDuplicatesImport(program: Readonly<Rule.Node>): RemoveDuplicatesImportResult {
|
|
28
|
+
const body = (program as unknown as {body: ReadonlyArray<Rule.Node>}).body;
|
|
29
|
+
|
|
30
|
+
return body.reduce<RemoveDuplicatesImportResult>(
|
|
31
|
+
(accum, node) => {
|
|
32
|
+
if (node.type !== 'ImportDeclaration') {
|
|
33
|
+
return accum;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const importDecl = node as unknown as ImportDeclaration;
|
|
37
|
+
|
|
38
|
+
if (
|
|
39
|
+
importDecl.source.value !== importSource ||
|
|
40
|
+
(importDecl as unknown as {importKind?: string}).importKind === 'type'
|
|
41
|
+
) {
|
|
42
|
+
return accum;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const namedSpecifiers = importDecl.specifiers.filter(
|
|
46
|
+
(spec) =>
|
|
47
|
+
spec.type === 'ImportSpecifier' &&
|
|
48
|
+
(spec as unknown as {importKind?: string}).importKind !== 'type',
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
const removeDuplicatesSpecifier = namedSpecifiers.find(
|
|
52
|
+
(spec) =>
|
|
53
|
+
spec.type === 'ImportSpecifier' &&
|
|
54
|
+
spec.imported.type === 'Identifier' &&
|
|
55
|
+
spec.imported.name === 'removeDuplicates',
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
const localName =
|
|
59
|
+
removeDuplicatesSpecifier?.type === 'ImportSpecifier'
|
|
60
|
+
? removeDuplicatesSpecifier.local.name
|
|
61
|
+
: undefined;
|
|
62
|
+
|
|
63
|
+
return {
|
|
64
|
+
usableLocalName: accum.usableLocalName || localName,
|
|
65
|
+
extensibleDeclaration:
|
|
66
|
+
accum.extensibleDeclaration ??
|
|
67
|
+
(namedSpecifiers.length > 0 ? importDecl : undefined),
|
|
68
|
+
};
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
usableLocalName: undefined,
|
|
72
|
+
extensibleDeclaration: undefined,
|
|
73
|
+
},
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Walks the scope chain to determine whether `name` is already bound, so the fixer can avoid
|
|
79
|
+
* introducing a `removeDuplicates` import that would shadow or duplicate an existing binding.
|
|
80
|
+
*/
|
|
81
|
+
function isNameInScope(scope: Scope.Scope | null, name: string): boolean {
|
|
82
|
+
if (!scope) {
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return scope.set.has(name) || isNameInScope(scope.upper, name);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Returns the single argument of a `new Set(...)` expression when `node` is exactly `[...new
|
|
91
|
+
* Set(arg)]`: an array with one spread element wrapping a `new Set` call that has exactly one
|
|
92
|
+
* argument. Returns undefined for any near-miss shape.
|
|
93
|
+
*/
|
|
94
|
+
function getSetSpreadArgument(node: Readonly<ArrayExpression>): Expression | undefined {
|
|
95
|
+
if (node.elements.length !== 1) {
|
|
96
|
+
return undefined;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const onlyElement = node.elements[0];
|
|
100
|
+
|
|
101
|
+
if (!onlyElement || onlyElement.type !== 'SpreadElement') {
|
|
102
|
+
return undefined;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const spreadArgument = onlyElement.argument;
|
|
106
|
+
|
|
107
|
+
if (spreadArgument.type !== 'NewExpression') {
|
|
108
|
+
return undefined;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const newExpression = spreadArgument;
|
|
112
|
+
|
|
113
|
+
if (
|
|
114
|
+
newExpression.callee.type !== 'Identifier' ||
|
|
115
|
+
newExpression.callee.name !== 'Set' ||
|
|
116
|
+
newExpression.arguments.length !== 1
|
|
117
|
+
) {
|
|
118
|
+
return undefined;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const setArgument = newExpression.arguments[0];
|
|
122
|
+
|
|
123
|
+
/** Spread args (e.g. `new Set(...items)`) are not a plain single value to dedupe. */
|
|
124
|
+
if (!setArgument || setArgument.type === 'SpreadElement') {
|
|
125
|
+
return undefined;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
return setArgument;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const rule: Rule.RuleModule = {
|
|
132
|
+
meta: {
|
|
133
|
+
type: 'suggestion',
|
|
134
|
+
fixable: 'code',
|
|
135
|
+
messages: {
|
|
136
|
+
preferRemoveDuplicates:
|
|
137
|
+
'Use `removeDuplicates(...)` from `@augment-vir/common` instead of `[...new Set(...)]`.',
|
|
138
|
+
},
|
|
139
|
+
schema: [],
|
|
140
|
+
},
|
|
141
|
+
create(context) {
|
|
142
|
+
return {
|
|
143
|
+
ArrayExpression(node: ArrayExpression & Rule.NodeParentExtension) {
|
|
144
|
+
const setArgument = getSetSpreadArgument(node);
|
|
145
|
+
|
|
146
|
+
if (!setArgument) {
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
context.report({
|
|
151
|
+
node,
|
|
152
|
+
messageId: 'preferRemoveDuplicates',
|
|
153
|
+
*fix(fixer) {
|
|
154
|
+
const program = context.sourceCode.ast;
|
|
155
|
+
const importResult = findRemoveDuplicatesImport(
|
|
156
|
+
program as unknown as Rule.Node,
|
|
157
|
+
);
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Yield import-related fixes first (earlier source positions) before the
|
|
161
|
+
* array replacement (later position) to keep ranges in ascending order.
|
|
162
|
+
*/
|
|
163
|
+
if (!importResult.usableLocalName) {
|
|
164
|
+
/**
|
|
165
|
+
* A fresh `removeDuplicates` binding is required. If the name is
|
|
166
|
+
* already bound in scope by something else (a local declaration or an
|
|
167
|
+
* import from another module), applying the fix would shadow the wrong
|
|
168
|
+
* function or create a duplicate identifier, so report without an
|
|
169
|
+
* autofix.
|
|
170
|
+
*/
|
|
171
|
+
if (
|
|
172
|
+
isNameInScope(context.sourceCode.getScope(node), 'removeDuplicates')
|
|
173
|
+
) {
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
if (importResult.extensibleDeclaration) {
|
|
178
|
+
const namedSpecifiers =
|
|
179
|
+
importResult.extensibleDeclaration.specifiers.filter(
|
|
180
|
+
(spec) => spec.type === 'ImportSpecifier',
|
|
181
|
+
);
|
|
182
|
+
const lastNamedSpecifier =
|
|
183
|
+
namedSpecifiers[namedSpecifiers.length - 1];
|
|
184
|
+
|
|
185
|
+
if (lastNamedSpecifier) {
|
|
186
|
+
yield fixer.insertTextAfter(
|
|
187
|
+
lastNamedSpecifier as unknown as Rule.Node,
|
|
188
|
+
', removeDuplicates',
|
|
189
|
+
);
|
|
190
|
+
}
|
|
191
|
+
} else {
|
|
192
|
+
/**
|
|
193
|
+
* No extensible named import from the source: insert a fresh import
|
|
194
|
+
* anchored before the first existing import (or the first
|
|
195
|
+
* statement). This covers side-effect, default, and namespace
|
|
196
|
+
* imports of the same module, which can't be safely extended with a
|
|
197
|
+
* named specifier via text insertion.
|
|
198
|
+
*/
|
|
199
|
+
const anchorNode =
|
|
200
|
+
program.body.find(
|
|
201
|
+
(bodyNode) => bodyNode.type === 'ImportDeclaration',
|
|
202
|
+
) ?? program.body[0];
|
|
203
|
+
|
|
204
|
+
if (anchorNode) {
|
|
205
|
+
yield fixer.insertTextBefore(
|
|
206
|
+
anchorNode as unknown as Rule.Node,
|
|
207
|
+
`import {removeDuplicates} from '${importSource}';\n`,
|
|
208
|
+
);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
const callName = importResult.usableLocalName || 'removeDuplicates';
|
|
214
|
+
const argumentText = context.sourceCode.getText(
|
|
215
|
+
setArgument as unknown as Rule.Node,
|
|
216
|
+
);
|
|
217
|
+
yield fixer.replaceText(node, `${callName}(${argumentText})`);
|
|
218
|
+
},
|
|
219
|
+
});
|
|
220
|
+
},
|
|
221
|
+
};
|
|
222
|
+
},
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
export default rule;
|