@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,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Check whether a node is a `new Error(...)` expression (a NewExpression whose callee is the
|
|
3
|
+
* Identifier `Error`).
|
|
4
|
+
*/
|
|
5
|
+
function isNewError(node) {
|
|
6
|
+
return (node.type === 'NewExpression' &&
|
|
7
|
+
node.callee.type === 'Identifier' &&
|
|
8
|
+
node.callee.name === 'Error');
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Check whether a node is an `x instanceof Error` expression and return the left operand if so.
|
|
12
|
+
* Returns `undefined` for any other shape.
|
|
13
|
+
*/
|
|
14
|
+
function getInstanceofErrorOperand(node) {
|
|
15
|
+
if (node.type === 'BinaryExpression' &&
|
|
16
|
+
node.operator === 'instanceof' &&
|
|
17
|
+
node.right.type === 'Identifier' &&
|
|
18
|
+
node.right.name === 'Error' &&
|
|
19
|
+
/** The left operand of `instanceof` is always an Expression, never a PrivateIdentifier. */
|
|
20
|
+
node.left.type !== 'PrivateIdentifier') {
|
|
21
|
+
return node.left;
|
|
22
|
+
}
|
|
23
|
+
return undefined;
|
|
24
|
+
}
|
|
25
|
+
const rule = {
|
|
26
|
+
meta: {
|
|
27
|
+
type: 'suggestion',
|
|
28
|
+
messages: {
|
|
29
|
+
preferEnsureError: 'Use `ensureErrorAndPrependMessage({{name}}, ...)` from `@augment-vir/common` instead of an `instanceof Error` ternary.',
|
|
30
|
+
},
|
|
31
|
+
schema: [],
|
|
32
|
+
},
|
|
33
|
+
create(context) {
|
|
34
|
+
return {
|
|
35
|
+
ConditionalExpression(node) {
|
|
36
|
+
/** Canonical form: `x instanceof Error ? x : new Error(...)`. */
|
|
37
|
+
const directOperand = getInstanceofErrorOperand(node.test);
|
|
38
|
+
if (directOperand?.type === 'Identifier' &&
|
|
39
|
+
node.consequent.type === 'Identifier' &&
|
|
40
|
+
directOperand.name === node.consequent.name &&
|
|
41
|
+
isNewError(node.alternate)) {
|
|
42
|
+
context.report({
|
|
43
|
+
node,
|
|
44
|
+
messageId: 'preferEnsureError',
|
|
45
|
+
data: {
|
|
46
|
+
name: directOperand.name,
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
/** Inverted form: `!(x instanceof Error) ? new Error(...) : x`. */
|
|
52
|
+
if (node.test.type === 'UnaryExpression' && node.test.operator === '!') {
|
|
53
|
+
const inner = node.test.argument.type === 'BinaryExpression'
|
|
54
|
+
? node.test.argument
|
|
55
|
+
: undefined;
|
|
56
|
+
const invertedOperand = inner ? getInstanceofErrorOperand(inner) : undefined;
|
|
57
|
+
if (invertedOperand?.type === 'Identifier' &&
|
|
58
|
+
node.alternate.type === 'Identifier' &&
|
|
59
|
+
invertedOperand.name === node.alternate.name &&
|
|
60
|
+
isNewError(node.consequent)) {
|
|
61
|
+
context.report({
|
|
62
|
+
node,
|
|
63
|
+
messageId: 'preferEnsureError',
|
|
64
|
+
data: {
|
|
65
|
+
name: invertedOperand.name,
|
|
66
|
+
},
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
},
|
|
71
|
+
};
|
|
72
|
+
},
|
|
73
|
+
};
|
|
74
|
+
export default rule;
|
|
@@ -4,6 +4,11 @@ function isTerminating(node) {
|
|
|
4
4
|
const last = body.at(-1);
|
|
5
5
|
return last != undefined && (last.type === 'ReturnStatement' || last.type === 'ThrowStatement');
|
|
6
6
|
}
|
|
7
|
+
/**
|
|
8
|
+
* A type predicate cannot reference a destructured binding, so this function must keep positional
|
|
9
|
+
* parameters to narrow `curr`.
|
|
10
|
+
*/
|
|
11
|
+
// eslint-disable-next-line @virmator/prefer-params-object
|
|
7
12
|
function isConsecutiveTerminatingIfs(curr, next) {
|
|
8
13
|
return (curr.type === 'IfStatement' &&
|
|
9
14
|
!curr.alternate &&
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Extract the method name from a non-computed MemberExpression callee. Returns `undefined` for
|
|
3
|
+
* computed access or non-Identifier properties.
|
|
4
|
+
*/
|
|
5
|
+
function getMethodName(node) {
|
|
6
|
+
if (!node.computed && node.property.type === 'Identifier') {
|
|
7
|
+
return node.property.name;
|
|
8
|
+
}
|
|
9
|
+
return undefined;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Whether a call's callee builds an object from an array of entries: `Object.fromEntries(...)`,
|
|
13
|
+
* `typedObjectFromEntries(...)`, or `arrayToObject(...)`. These are the only contexts where a
|
|
14
|
+
* `getEnumValues(...).map(...)` is constructing an object mapping (and thus where `mapEnumToObject`
|
|
15
|
+
* is the right replacement). A bare `.map(...)` that produces an array is left alone.
|
|
16
|
+
*/
|
|
17
|
+
function isObjectBuilderCallee(callee) {
|
|
18
|
+
if (callee.type === 'Identifier') {
|
|
19
|
+
return callee.name === 'typedObjectFromEntries' || callee.name === 'arrayToObject';
|
|
20
|
+
}
|
|
21
|
+
return (callee.type === 'MemberExpression' &&
|
|
22
|
+
!callee.computed &&
|
|
23
|
+
callee.object.type === 'Identifier' &&
|
|
24
|
+
callee.object.name === 'Object' &&
|
|
25
|
+
callee.property.type === 'Identifier' &&
|
|
26
|
+
callee.property.name === 'fromEntries');
|
|
27
|
+
}
|
|
28
|
+
const rule = {
|
|
29
|
+
meta: {
|
|
30
|
+
type: 'suggestion',
|
|
31
|
+
messages: {
|
|
32
|
+
preferMapEnumToObject: 'Use `mapEnumToObject` from `@augment-vir/common` instead of building an object from `getEnumValues(...).map(...)`.',
|
|
33
|
+
},
|
|
34
|
+
schema: [],
|
|
35
|
+
},
|
|
36
|
+
create(context) {
|
|
37
|
+
return {
|
|
38
|
+
CallExpression(node) {
|
|
39
|
+
if (node.callee.type !== 'MemberExpression' ||
|
|
40
|
+
getMethodName(node.callee) !== 'map') {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
const innerCall = node.callee.object;
|
|
44
|
+
if (innerCall.type !== 'CallExpression' ||
|
|
45
|
+
innerCall.callee.type !== 'Identifier' ||
|
|
46
|
+
innerCall.callee.name !== 'getEnumValues') {
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Only flag when the `.map(...)` result is fed straight into an object builder. A
|
|
51
|
+
* bare `getEnumValues(...).map(...)` producing an array is a legitimate pattern
|
|
52
|
+
* that `mapEnumToObject` cannot replace.
|
|
53
|
+
*/
|
|
54
|
+
const parent = node.parent;
|
|
55
|
+
if (parent.type !== 'CallExpression' ||
|
|
56
|
+
parent.arguments[0] !== node ||
|
|
57
|
+
!isObjectBuilderCallee(parent.callee)) {
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
context.report({
|
|
61
|
+
node,
|
|
62
|
+
messageId: 'preferMapEnumToObject',
|
|
63
|
+
});
|
|
64
|
+
},
|
|
65
|
+
};
|
|
66
|
+
},
|
|
67
|
+
};
|
|
68
|
+
export default rule;
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Excludes a leading `this` parameter (which is a type-only annotation in TypeScript, not a real
|
|
3
|
+
* positional argument).
|
|
4
|
+
*/
|
|
5
|
+
function getRealParams(params) {
|
|
6
|
+
const first = params[0];
|
|
7
|
+
if (first?.type === 'Identifier' && first.name === 'this') {
|
|
8
|
+
return params.slice(1);
|
|
9
|
+
}
|
|
10
|
+
return params;
|
|
11
|
+
}
|
|
12
|
+
function getTypeText(param, sourceCode) {
|
|
13
|
+
const identifier = param.type === 'Identifier'
|
|
14
|
+
? param
|
|
15
|
+
: param.type === 'AssignmentPattern' && param.left.type === 'Identifier'
|
|
16
|
+
? param.left
|
|
17
|
+
: undefined;
|
|
18
|
+
const typeAnnotation = identifier?.typeAnnotation;
|
|
19
|
+
if (!typeAnnotation) {
|
|
20
|
+
return undefined;
|
|
21
|
+
}
|
|
22
|
+
return sourceCode.getText(typeAnnotation.typeAnnotation);
|
|
23
|
+
}
|
|
24
|
+
function hasDuplicateType(params, sourceCode) {
|
|
25
|
+
const seen = new Set();
|
|
26
|
+
return params.some((param) => {
|
|
27
|
+
const typeText = getTypeText(param, sourceCode);
|
|
28
|
+
if (typeText == undefined) {
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
/** Normalize whitespace so `Array<string>` and `Array< string >` compare as equal. */
|
|
32
|
+
const key = typeText.replaceAll(/\s+/g, '');
|
|
33
|
+
if (seen.has(key)) {
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
seen.add(key);
|
|
37
|
+
return false;
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
function getFixableParam(param, sourceCode) {
|
|
41
|
+
const identifier = param.type === 'Identifier'
|
|
42
|
+
? param
|
|
43
|
+
: param.type === 'AssignmentPattern' && param.left.type === 'Identifier'
|
|
44
|
+
? param.left
|
|
45
|
+
: undefined;
|
|
46
|
+
if (!identifier?.typeAnnotation) {
|
|
47
|
+
return undefined;
|
|
48
|
+
}
|
|
49
|
+
const defaultText = param.type === 'AssignmentPattern' ? sourceCode.getText(param.right) : undefined;
|
|
50
|
+
return {
|
|
51
|
+
name: identifier.name,
|
|
52
|
+
typeText: sourceCode.getText(identifier.typeAnnotation.typeAnnotation),
|
|
53
|
+
defaultText,
|
|
54
|
+
optional: Boolean(identifier.optional) || defaultText != undefined,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
function buildParamsObjectFix(params, sourceCode) {
|
|
58
|
+
const fixableParams = params.map((param) => getFixableParam(param, sourceCode));
|
|
59
|
+
if (fixableParams.some((fixable) => fixable == undefined)) {
|
|
60
|
+
return undefined;
|
|
61
|
+
}
|
|
62
|
+
const definedParams = fixableParams.filter((fixable) => fixable != undefined);
|
|
63
|
+
const patternText = definedParams
|
|
64
|
+
.map((param) => param.defaultText == undefined ? param.name : `${param.name} = ${param.defaultText}`)
|
|
65
|
+
.join(', ');
|
|
66
|
+
const typeText = definedParams
|
|
67
|
+
.map((param) => `${param.name}${param.optional ? '?' : ''}: ${param.typeText}`)
|
|
68
|
+
.join('; ');
|
|
69
|
+
const firstParam = params[0];
|
|
70
|
+
const lastParam = params.at(-1);
|
|
71
|
+
if (!firstParam || !lastParam) {
|
|
72
|
+
return undefined;
|
|
73
|
+
}
|
|
74
|
+
const start = sourceCode.getRange(firstParam)[0];
|
|
75
|
+
const end = sourceCode.getRange(lastParam)[1];
|
|
76
|
+
return (fixer) => fixer.replaceTextRange([
|
|
77
|
+
start,
|
|
78
|
+
end,
|
|
79
|
+
], `{${patternText}}: Readonly<{${typeText}}>`);
|
|
80
|
+
}
|
|
81
|
+
const rule = {
|
|
82
|
+
meta: {
|
|
83
|
+
type: 'suggestion',
|
|
84
|
+
fixable: 'code',
|
|
85
|
+
messages: {
|
|
86
|
+
tooManyPositionalParams: 'Functions with more than 3 parameters must use a single destructured params object instead.',
|
|
87
|
+
duplicateParamType: 'Functions with multiple parameters of the same type must use a single destructured params object to avoid order-based mistakes.',
|
|
88
|
+
},
|
|
89
|
+
schema: [],
|
|
90
|
+
},
|
|
91
|
+
create(context) {
|
|
92
|
+
const sourceCode = context.sourceCode;
|
|
93
|
+
function check(node) {
|
|
94
|
+
const realParams = getRealParams(node.params);
|
|
95
|
+
const messageId = realParams.length > 3
|
|
96
|
+
? 'tooManyPositionalParams'
|
|
97
|
+
: hasDuplicateType(realParams, sourceCode)
|
|
98
|
+
? 'duplicateParamType'
|
|
99
|
+
: undefined;
|
|
100
|
+
if (!messageId) {
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
const fix = buildParamsObjectFix(realParams, sourceCode);
|
|
104
|
+
context.report({
|
|
105
|
+
node,
|
|
106
|
+
messageId,
|
|
107
|
+
fix: fix ?? null,
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
return {
|
|
111
|
+
FunctionDeclaration: check,
|
|
112
|
+
FunctionExpression: check,
|
|
113
|
+
ArrowFunctionExpression: check,
|
|
114
|
+
};
|
|
115
|
+
},
|
|
116
|
+
};
|
|
117
|
+
export default rule;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
function findPrivateToken(node, sourceCode) {
|
|
2
|
+
const lastDecorator = node.decorators?.at(-1);
|
|
3
|
+
/**
|
|
4
|
+
* Accessibility modifiers always appear after any decorators and before the member key, so
|
|
5
|
+
* starting the search after the final decorator avoids matching a `@private`-style decorator.
|
|
6
|
+
*/
|
|
7
|
+
return lastDecorator
|
|
8
|
+
? sourceCode.getTokenAfter(lastDecorator, {
|
|
9
|
+
filter: (token) => token.value === 'private',
|
|
10
|
+
})
|
|
11
|
+
: sourceCode.getFirstToken(node, {
|
|
12
|
+
filter: (token) => token.value === 'private',
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
const rule = {
|
|
16
|
+
meta: {
|
|
17
|
+
type: 'suggestion',
|
|
18
|
+
fixable: 'code',
|
|
19
|
+
messages: {
|
|
20
|
+
useProtected: 'Use the `protected` accessibility modifier instead of `private`.',
|
|
21
|
+
},
|
|
22
|
+
schema: [],
|
|
23
|
+
},
|
|
24
|
+
create(context) {
|
|
25
|
+
const sourceCode = context.sourceCode;
|
|
26
|
+
function check(node) {
|
|
27
|
+
if (node.accessibility !== 'private') {
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
context.report({
|
|
31
|
+
node,
|
|
32
|
+
messageId: 'useProtected',
|
|
33
|
+
fix(fixer) {
|
|
34
|
+
const privateToken = findPrivateToken(node, sourceCode);
|
|
35
|
+
if (!privateToken) {
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
return fixer.replaceText(privateToken, 'protected');
|
|
39
|
+
},
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
return {
|
|
43
|
+
MethodDefinition: check,
|
|
44
|
+
PropertyDefinition: check,
|
|
45
|
+
AccessorProperty: check,
|
|
46
|
+
TSAbstractMethodDefinition: check,
|
|
47
|
+
TSAbstractPropertyDefinition: check,
|
|
48
|
+
TSAbstractAccessorProperty: check,
|
|
49
|
+
TSParameterProperty: check,
|
|
50
|
+
};
|
|
51
|
+
},
|
|
52
|
+
};
|
|
53
|
+
export default rule;
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
const importSource = '@augment-vir/common';
|
|
2
|
+
/**
|
|
3
|
+
* Scan program imports for a usable `removeDuplicates` value import and/or a named value import
|
|
4
|
+
* from `@augment-vir/common` that the fixer can extend.
|
|
5
|
+
*/
|
|
6
|
+
function findRemoveDuplicatesImport(program) {
|
|
7
|
+
const body = program.body;
|
|
8
|
+
return body.reduce((accum, node) => {
|
|
9
|
+
if (node.type !== 'ImportDeclaration') {
|
|
10
|
+
return accum;
|
|
11
|
+
}
|
|
12
|
+
const importDecl = node;
|
|
13
|
+
if (importDecl.source.value !== importSource ||
|
|
14
|
+
importDecl.importKind === 'type') {
|
|
15
|
+
return accum;
|
|
16
|
+
}
|
|
17
|
+
const namedSpecifiers = importDecl.specifiers.filter((spec) => spec.type === 'ImportSpecifier' &&
|
|
18
|
+
spec.importKind !== 'type');
|
|
19
|
+
const removeDuplicatesSpecifier = namedSpecifiers.find((spec) => spec.type === 'ImportSpecifier' &&
|
|
20
|
+
spec.imported.type === 'Identifier' &&
|
|
21
|
+
spec.imported.name === 'removeDuplicates');
|
|
22
|
+
const localName = removeDuplicatesSpecifier?.type === 'ImportSpecifier'
|
|
23
|
+
? removeDuplicatesSpecifier.local.name
|
|
24
|
+
: undefined;
|
|
25
|
+
return {
|
|
26
|
+
usableLocalName: accum.usableLocalName || localName,
|
|
27
|
+
extensibleDeclaration: accum.extensibleDeclaration ??
|
|
28
|
+
(namedSpecifiers.length > 0 ? importDecl : undefined),
|
|
29
|
+
};
|
|
30
|
+
}, {
|
|
31
|
+
usableLocalName: undefined,
|
|
32
|
+
extensibleDeclaration: undefined,
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Walks the scope chain to determine whether `name` is already bound, so the fixer can avoid
|
|
37
|
+
* introducing a `removeDuplicates` import that would shadow or duplicate an existing binding.
|
|
38
|
+
*/
|
|
39
|
+
function isNameInScope(scope, name) {
|
|
40
|
+
if (!scope) {
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
return scope.set.has(name) || isNameInScope(scope.upper, name);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Returns the single argument of a `new Set(...)` expression when `node` is exactly `[...new
|
|
47
|
+
* Set(arg)]`: an array with one spread element wrapping a `new Set` call that has exactly one
|
|
48
|
+
* argument. Returns undefined for any near-miss shape.
|
|
49
|
+
*/
|
|
50
|
+
function getSetSpreadArgument(node) {
|
|
51
|
+
if (node.elements.length !== 1) {
|
|
52
|
+
return undefined;
|
|
53
|
+
}
|
|
54
|
+
const onlyElement = node.elements[0];
|
|
55
|
+
if (!onlyElement || onlyElement.type !== 'SpreadElement') {
|
|
56
|
+
return undefined;
|
|
57
|
+
}
|
|
58
|
+
const spreadArgument = onlyElement.argument;
|
|
59
|
+
if (spreadArgument.type !== 'NewExpression') {
|
|
60
|
+
return undefined;
|
|
61
|
+
}
|
|
62
|
+
const newExpression = spreadArgument;
|
|
63
|
+
if (newExpression.callee.type !== 'Identifier' ||
|
|
64
|
+
newExpression.callee.name !== 'Set' ||
|
|
65
|
+
newExpression.arguments.length !== 1) {
|
|
66
|
+
return undefined;
|
|
67
|
+
}
|
|
68
|
+
const setArgument = newExpression.arguments[0];
|
|
69
|
+
/** Spread args (e.g. `new Set(...items)`) are not a plain single value to dedupe. */
|
|
70
|
+
if (!setArgument || setArgument.type === 'SpreadElement') {
|
|
71
|
+
return undefined;
|
|
72
|
+
}
|
|
73
|
+
return setArgument;
|
|
74
|
+
}
|
|
75
|
+
const rule = {
|
|
76
|
+
meta: {
|
|
77
|
+
type: 'suggestion',
|
|
78
|
+
fixable: 'code',
|
|
79
|
+
messages: {
|
|
80
|
+
preferRemoveDuplicates: 'Use `removeDuplicates(...)` from `@augment-vir/common` instead of `[...new Set(...)]`.',
|
|
81
|
+
},
|
|
82
|
+
schema: [],
|
|
83
|
+
},
|
|
84
|
+
create(context) {
|
|
85
|
+
return {
|
|
86
|
+
ArrayExpression(node) {
|
|
87
|
+
const setArgument = getSetSpreadArgument(node);
|
|
88
|
+
if (!setArgument) {
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
context.report({
|
|
92
|
+
node,
|
|
93
|
+
messageId: 'preferRemoveDuplicates',
|
|
94
|
+
*fix(fixer) {
|
|
95
|
+
const program = context.sourceCode.ast;
|
|
96
|
+
const importResult = findRemoveDuplicatesImport(program);
|
|
97
|
+
/**
|
|
98
|
+
* Yield import-related fixes first (earlier source positions) before the
|
|
99
|
+
* array replacement (later position) to keep ranges in ascending order.
|
|
100
|
+
*/
|
|
101
|
+
if (!importResult.usableLocalName) {
|
|
102
|
+
/**
|
|
103
|
+
* A fresh `removeDuplicates` binding is required. If the name is
|
|
104
|
+
* already bound in scope by something else (a local declaration or an
|
|
105
|
+
* import from another module), applying the fix would shadow the wrong
|
|
106
|
+
* function or create a duplicate identifier, so report without an
|
|
107
|
+
* autofix.
|
|
108
|
+
*/
|
|
109
|
+
if (isNameInScope(context.sourceCode.getScope(node), 'removeDuplicates')) {
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
if (importResult.extensibleDeclaration) {
|
|
113
|
+
const namedSpecifiers = importResult.extensibleDeclaration.specifiers.filter((spec) => spec.type === 'ImportSpecifier');
|
|
114
|
+
const lastNamedSpecifier = namedSpecifiers[namedSpecifiers.length - 1];
|
|
115
|
+
if (lastNamedSpecifier) {
|
|
116
|
+
yield fixer.insertTextAfter(lastNamedSpecifier, ', removeDuplicates');
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
/**
|
|
121
|
+
* No extensible named import from the source: insert a fresh import
|
|
122
|
+
* anchored before the first existing import (or the first
|
|
123
|
+
* statement). This covers side-effect, default, and namespace
|
|
124
|
+
* imports of the same module, which can't be safely extended with a
|
|
125
|
+
* named specifier via text insertion.
|
|
126
|
+
*/
|
|
127
|
+
const anchorNode = program.body.find((bodyNode) => bodyNode.type === 'ImportDeclaration') ?? program.body[0];
|
|
128
|
+
if (anchorNode) {
|
|
129
|
+
yield fixer.insertTextBefore(anchorNode, `import {removeDuplicates} from '${importSource}';\n`);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
const callName = importResult.usableLocalName || 'removeDuplicates';
|
|
134
|
+
const argumentText = context.sourceCode.getText(setArgument);
|
|
135
|
+
yield fixer.replaceText(node, `${callName}(${argumentText})`);
|
|
136
|
+
},
|
|
137
|
+
});
|
|
138
|
+
},
|
|
139
|
+
};
|
|
140
|
+
},
|
|
141
|
+
};
|
|
142
|
+
export default rule;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
const rule = {
|
|
2
|
+
meta: {
|
|
3
|
+
type: 'suggestion',
|
|
4
|
+
fixable: 'code',
|
|
5
|
+
messages: {
|
|
6
|
+
preferToSorted: 'Use `.toSorted(...)` instead of `[...array].sort(...)` for a non-mutating sort.',
|
|
7
|
+
},
|
|
8
|
+
schema: [],
|
|
9
|
+
},
|
|
10
|
+
create(context) {
|
|
11
|
+
return {
|
|
12
|
+
CallExpression(node) {
|
|
13
|
+
const callee = node.callee;
|
|
14
|
+
if (callee.type !== 'MemberExpression' ||
|
|
15
|
+
callee.computed ||
|
|
16
|
+
callee.property.type !== 'Identifier' ||
|
|
17
|
+
callee.property.name !== 'sort') {
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
const object = callee.object;
|
|
21
|
+
if (object.type !== 'ArrayExpression' ||
|
|
22
|
+
object.elements.length !== 1 ||
|
|
23
|
+
object.elements[0]?.type !== 'SpreadElement') {
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
const spreadArgument = object.elements[0].argument;
|
|
27
|
+
context.report({
|
|
28
|
+
node,
|
|
29
|
+
messageId: 'preferToSorted',
|
|
30
|
+
fix(fixer) {
|
|
31
|
+
const argumentSourceTexts = node.arguments.map((argument) => context.sourceCode.getText(argument));
|
|
32
|
+
const sourceText = context.sourceCode.getText(spreadArgument);
|
|
33
|
+
/**
|
|
34
|
+
* Member access binds tighter than most expression forms, so a receiver
|
|
35
|
+
* like `await fn()` or `a ? b : c` must be wrapped in parentheses;
|
|
36
|
+
* otherwise `.toSorted(...)` would attach to the wrong sub-expression.
|
|
37
|
+
* Identifiers, member/call/array expressions are already tight enough to
|
|
38
|
+
* leave bare.
|
|
39
|
+
*/
|
|
40
|
+
const tightReceiverTypes = [
|
|
41
|
+
'Identifier',
|
|
42
|
+
'MemberExpression',
|
|
43
|
+
'CallExpression',
|
|
44
|
+
'NewExpression',
|
|
45
|
+
'ArrayExpression',
|
|
46
|
+
'ThisExpression',
|
|
47
|
+
];
|
|
48
|
+
const receiverText = tightReceiverTypes.includes(spreadArgument.type)
|
|
49
|
+
? sourceText
|
|
50
|
+
: `(${sourceText})`;
|
|
51
|
+
return fixer.replaceText(node, `${receiverText}.toSorted(${argumentSourceTexts.join(', ')})`);
|
|
52
|
+
},
|
|
53
|
+
});
|
|
54
|
+
},
|
|
55
|
+
};
|
|
56
|
+
},
|
|
57
|
+
};
|
|
58
|
+
export default rule;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@virmator/lint",
|
|
3
|
-
"version": "14.
|
|
3
|
+
"version": "14.27.0",
|
|
4
4
|
"description": "Default ESLint linting plugin for virmator.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"virmator",
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
"@augment-vir/assert": "^31.72.1",
|
|
36
36
|
"@augment-vir/common": "^31.72.1",
|
|
37
37
|
"@augment-vir/node": "^31.72.1",
|
|
38
|
-
"@virmator/core": "^14.
|
|
38
|
+
"@virmator/core": "^14.27.0",
|
|
39
39
|
"globals": "^17.6.0",
|
|
40
40
|
"jiti": "^2.7.0",
|
|
41
41
|
"mri": "^1.2.0"
|