Skip to content

Commit 1130fb2

Browse files
authored
Enforce context.on() (#2797)
1 parent bc61cfd commit 1130fb2

File tree

105 files changed

+2447
-2350
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

105 files changed

+2447
-2350
lines changed

eslint.dogfooding.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const config = [
1111
'n/no-unsupported-features/es-syntax',
1212
'eslint-plugin/require-meta-default-options',
1313
'@stylistic/max-len',
14+
'internal/prefer-context-on',
1415
]),
1516
{
1617
linterOptions: {

index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import createDeprecatedRules from './rules/utils/create-deprecated-rules.js';
22
import flatConfigBase from './configs/flat-config-base.js';
33
import * as rawRules from './rules/index.js';
4-
import {createRules} from './rules/rule/index.js';
4+
import {toEslintRules} from './rules/rule/index.js';
55
import packageJson from './package.json' with {type: 'json'};
66

7-
const rules = createRules(rawRules);
7+
const rules = toEslintRules(rawRules);
88

99
const deprecatedRules = createDeprecatedRules({
1010
// {ruleId: {message: string, replacedBy: string[]}}
@@ -68,7 +68,7 @@ const unicorn = {
6868
version: packageJson.version,
6969
},
7070
rules: {
71-
...createRules(rules),
71+
...rules,
7272
...deprecatedRules,
7373
},
7474
};

rules/better-regex.js

Lines changed: 87 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -20,99 +20,98 @@ const create = context => {
2020
ignoreList.push('charClassClassrangesMerge');
2121
}
2222

23-
return {
24-
Literal(node) {
25-
if (!isRegexLiteral(node)) {
26-
return;
27-
}
28-
29-
const {raw: original, regex} = node;
30-
// Regular Expressions with `u` and `v` flag are not well handled by `regexp-tree`
31-
// https://github.com/DmitrySoshnikov/regexp-tree/issues/162
32-
if (regex.flags.includes('u') || regex.flags.includes('v')) {
33-
return;
34-
}
35-
36-
let optimized = original;
37-
38-
try {
39-
optimized = regexpTree.optimize(original, undefined, {blacklist: ignoreList}).toString();
40-
} catch (error) {
41-
return {
42-
node,
43-
messageId: MESSAGE_ID_PARSE_ERROR,
44-
data: {
45-
original,
46-
error: error.message,
47-
},
48-
};
49-
}
50-
51-
if (original === optimized) {
52-
return;
53-
}
54-
55-
const problem = {
23+
context.on('Literal', node => {
24+
if (!isRegexLiteral(node)) {
25+
return;
26+
}
27+
28+
const {raw: original, regex} = node;
29+
// Regular Expressions with `u` and `v` flag are not well handled by `regexp-tree`
30+
// https://github.com/DmitrySoshnikov/regexp-tree/issues/162
31+
if (regex.flags.includes('u') || regex.flags.includes('v')) {
32+
return;
33+
}
34+
35+
let optimized = original;
36+
37+
try {
38+
optimized = regexpTree.optimize(original, undefined, {blacklist: ignoreList}).toString();
39+
} catch (error) {
40+
return {
5641
node,
57-
messageId: MESSAGE_ID,
42+
messageId: MESSAGE_ID_PARSE_ERROR,
5843
data: {
5944
original,
60-
optimized,
45+
error: error.message,
6146
},
6247
};
63-
64-
if (
65-
node.parent.type === 'MemberExpression'
66-
&& node.parent.object === node
67-
&& !node.parent.optional
68-
&& !node.parent.computed
69-
&& node.parent.property.type === 'Identifier'
70-
&& (
71-
node.parent.property.name === 'toString'
72-
|| node.parent.property.name === 'source'
73-
)
74-
) {
75-
return problem;
76-
}
77-
78-
return Object.assign(problem, {
79-
fix: fixer => fixer.replaceText(node, optimized),
80-
});
81-
},
82-
NewExpression(node) {
83-
if (!isNewExpression(node, {name: 'RegExp', minimumArguments: 1})) {
84-
return;
85-
}
86-
87-
const [patternNode, flagsNode] = node.arguments;
88-
89-
if (!isStringLiteral(patternNode)) {
90-
return;
91-
}
92-
93-
const oldPattern = patternNode.value;
94-
const flags = isStringLiteral(flagsNode)
95-
? flagsNode.value
96-
: '';
97-
98-
const newPattern = cleanRegexp(oldPattern, flags);
99-
100-
if (oldPattern !== newPattern) {
101-
return {
102-
node,
103-
messageId: MESSAGE_ID,
104-
data: {
105-
original: oldPattern,
106-
optimized: newPattern,
107-
},
108-
fix: fixer => fixer.replaceText(
109-
patternNode,
110-
escapeString(newPattern, patternNode.raw.charAt(0)),
111-
),
112-
};
113-
}
114-
},
115-
};
48+
}
49+
50+
if (original === optimized) {
51+
return;
52+
}
53+
54+
const problem = {
55+
node,
56+
messageId: MESSAGE_ID,
57+
data: {
58+
original,
59+
optimized,
60+
},
61+
};
62+
63+
if (
64+
node.parent.type === 'MemberExpression'
65+
&& node.parent.object === node
66+
&& !node.parent.optional
67+
&& !node.parent.computed
68+
&& node.parent.property.type === 'Identifier'
69+
&& (
70+
node.parent.property.name === 'toString'
71+
|| node.parent.property.name === 'source'
72+
)
73+
) {
74+
return problem;
75+
}
76+
77+
return Object.assign(problem, {
78+
fix: fixer => fixer.replaceText(node, optimized),
79+
});
80+
});
81+
82+
context.on('NewExpression', node => {
83+
if (!isNewExpression(node, {name: 'RegExp', minimumArguments: 1})) {
84+
return;
85+
}
86+
87+
const [patternNode, flagsNode] = node.arguments;
88+
89+
if (!isStringLiteral(patternNode)) {
90+
return;
91+
}
92+
93+
const oldPattern = patternNode.value;
94+
const flags = isStringLiteral(flagsNode)
95+
? flagsNode.value
96+
: '';
97+
98+
const newPattern = cleanRegexp(oldPattern, flags);
99+
100+
if (oldPattern !== newPattern) {
101+
return {
102+
node,
103+
messageId: MESSAGE_ID,
104+
data: {
105+
original: oldPattern,
106+
optimized: newPattern,
107+
},
108+
fix: fixer => fixer.replaceText(
109+
patternNode,
110+
escapeString(newPattern, patternNode.raw.charAt(0)),
111+
),
112+
};
113+
}
114+
});
116115
};
117116

118117
const schema = [

rules/catch-error-name.js

Lines changed: 52 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -47,60 +47,58 @@ const create = context => {
4747
|| name.endsWith(expectedName)
4848
|| name.endsWith(upperFirst(expectedName));
4949

50-
return {
51-
Identifier(node) {
52-
if (
53-
!(node.parent.type === 'CatchClause' && node.parent.param === node)
54-
&& !isPromiseCatchParameter(node)
55-
) {
56-
return;
57-
}
58-
59-
const originalName = node.name;
60-
61-
if (
62-
isNameAllowed(originalName)
63-
|| isNameAllowed(originalName.replaceAll(/_+$/g, ''))
64-
) {
65-
return;
66-
}
67-
68-
const scope = context.sourceCode.getScope(node);
69-
const variable = findVariable(scope, node);
70-
71-
// This was reported https://github.com/sindresorhus/eslint-plugin-unicorn/issues/1075#issuecomment-768072967
72-
// But can't reproduce, just ignore this case
73-
/* c8 ignore next 3 */
74-
if (!variable) {
75-
return;
76-
}
77-
78-
if (originalName === '_' && variable.references.length === 0) {
79-
return;
80-
}
81-
82-
const scopes = [
83-
variable.scope,
84-
...variable.references.map(({from}) => from),
85-
];
86-
const fixedName = getAvailableVariableName(expectedName, scopes);
87-
88-
const problem = {
89-
node,
90-
messageId: MESSAGE_ID,
91-
data: {
92-
originalName,
93-
fixedName: fixedName || expectedName,
94-
},
95-
};
96-
97-
if (fixedName) {
98-
problem.fix = fixer => renameVariable(variable, fixedName, context, fixer);
99-
}
100-
101-
return problem;
102-
},
103-
};
50+
context.on('Identifier', node => {
51+
if (
52+
!(node.parent.type === 'CatchClause' && node.parent.param === node)
53+
&& !isPromiseCatchParameter(node)
54+
) {
55+
return;
56+
}
57+
58+
const originalName = node.name;
59+
60+
if (
61+
isNameAllowed(originalName)
62+
|| isNameAllowed(originalName.replaceAll(/_+$/g, ''))
63+
) {
64+
return;
65+
}
66+
67+
const scope = context.sourceCode.getScope(node);
68+
const variable = findVariable(scope, node);
69+
70+
// This was reported https://github.com/sindresorhus/eslint-plugin-unicorn/issues/1075#issuecomment-768072967
71+
// But can't reproduce, just ignore this case
72+
/* c8 ignore next 3 */
73+
if (!variable) {
74+
return;
75+
}
76+
77+
if (originalName === '_' && variable.references.length === 0) {
78+
return;
79+
}
80+
81+
const scopes = [
82+
variable.scope,
83+
...variable.references.map(({from}) => from),
84+
];
85+
const fixedName = getAvailableVariableName(expectedName, scopes);
86+
87+
const problem = {
88+
node,
89+
messageId: MESSAGE_ID,
90+
data: {
91+
originalName,
92+
fixedName: fixedName || expectedName,
93+
},
94+
};
95+
96+
if (fixedName) {
97+
problem.fix = fixer => renameVariable(variable, fixedName, context, fixer);
98+
}
99+
100+
return problem;
101+
});
104102
};
105103

106104
const schema = [

rules/consistent-assert.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ const isAssertFunction = (specifier, moduleName) =>
3434
const NODE_PROTOCOL = 'node:';
3535

3636
/** @type {import('eslint').Rule.RuleModule['create']} */
37-
const create = context => ({
38-
* ImportDeclaration(importDeclaration) {
37+
const create = context => {
38+
context.on('ImportDeclaration', function * (importDeclaration) {
3939
if (!isValueImport(importDeclaration)) {
4040
return;
4141
}
@@ -78,8 +78,8 @@ const create = context => ({
7878
};
7979
}
8080
}
81-
},
82-
});
81+
});
82+
};
8383

8484
/** @type {import('eslint').Rule.RuleModule} */
8585
const config = {

rules/consistent-date-clone.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ const messages = {
77
};
88

99
/** @param {import('eslint').Rule.RuleContext} context */
10-
const create = context => ({
11-
NewExpression(newExpression) {
10+
const create = context => {
11+
context.on('NewExpression', newExpression => {
1212
if (!isNewExpression(newExpression, {name: 'Date', argumentsLength: 1})) {
1313
return;
1414
}
@@ -34,8 +34,8 @@ const create = context => ({
3434
messageId: MESSAGE_ID_ERROR,
3535
fix: fixer => removeMethodCall(fixer, callExpression, context),
3636
};
37-
},
38-
});
37+
});
38+
};
3939

4040
/** @type {import('eslint').Rule.RuleModule} */
4141
const config = {

0 commit comments

Comments
 (0)