Skip to content

Commit 9179afe

Browse files
authored
Remove eslint-template-visitor dependency (#1665)
1 parent 03b0946 commit 9179afe

File tree

5 files changed

+31
-44
lines changed

5 files changed

+31
-44
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
"@babel/helper-validator-identifier": "^7.15.7",
5050
"ci-info": "^3.3.0",
5151
"clean-regexp": "^1.0.0",
52-
"eslint-template-visitor": "^2.3.2",
5352
"eslint-utils": "^3.0.0",
5453
"esquery": "^1.4.0",
5554
"indent-string": "^4.0.0",

rules/import-style.js

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22
const {defaultsDeep} = require('lodash');
33
const {getStringIfConstant} = require('eslint-utils');
4-
const eslintTemplateVisitor = require('eslint-template-visitor');
54
const {callExpressionSelector} = require('./selectors/index.js');
65

76
const MESSAGE_ID = 'importStyle';
@@ -131,24 +130,18 @@ const defaultStyles = {
131130
},
132131
};
133132

134-
const templates = eslintTemplateVisitor({
135-
parserOptions: {
136-
sourceType: 'module',
137-
ecmaVersion: 2018,
138-
},
139-
});
140-
141-
const variableDeclarationVariable = templates.variableDeclarationVariable();
142-
const assignmentTargetVariable = templates.variable();
143-
const moduleNameVariable = templates.variable();
144-
145-
const assignedDynamicImportTemplate = templates.template`async () => {
146-
${variableDeclarationVariable} ${assignmentTargetVariable} = await import(${moduleNameVariable});
147-
}`.narrow('BlockStatement > :has(AwaitExpression)');
133+
const assignedDynamicImportSelector = [
134+
'VariableDeclarator',
135+
'[init.type="AwaitExpression"]',
136+
'[init.argument.type="ImportExpression"]',
137+
].join('');
148138

149-
const assignedRequireTemplate = templates.template`
150-
${variableDeclarationVariable} ${assignmentTargetVariable} = require(${moduleNameVariable});
151-
`;
139+
const assignedRequireSelector = [
140+
'VariableDeclarator',
141+
'[init.type="CallExpression"]',
142+
'[init.callee.type="Identifier"]',
143+
'[init.callee.name="require"]',
144+
].join('');
152145

153146
/** @param {import('eslint').Rule.RuleContext} context */
154147
const create = context => {
@@ -235,9 +228,9 @@ const create = context => {
235228
report(node, moduleName, actualImportStyles, allowedImportStyles);
236229
},
237230

238-
[assignedDynamicImportTemplate](node) {
239-
const assignmentTargetNode = assignedDynamicImportTemplate.context.getMatch(assignmentTargetVariable);
240-
const moduleNameNode = assignedDynamicImportTemplate.context.getMatch(moduleNameVariable);
231+
[assignedDynamicImportSelector](node) {
232+
const assignmentTargetNode = node.id;
233+
const moduleNameNode = node.init.argument.source;
241234
const moduleName = getStringIfConstant(moduleNameNode, context.getScope());
242235

243236
if (!moduleName) {
@@ -288,9 +281,9 @@ const create = context => {
288281
report(node, moduleName, actualImportStyles, allowedImportStyles, true);
289282
},
290283

291-
[assignedRequireTemplate](node) {
292-
const assignmentTargetNode = assignedRequireTemplate.context.getMatch(assignmentTargetVariable);
293-
const moduleNameNode = assignedRequireTemplate.context.getMatch(moduleNameVariable);
284+
[assignedRequireSelector](node) {
285+
const assignmentTargetNode = node.id;
286+
const moduleNameNode = node.init.arguments[0];
294287
const moduleName = getStringIfConstant(moduleNameNode, context.getScope());
295288

296289
if (!moduleName) {
@@ -305,7 +298,7 @@ const create = context => {
305298
};
306299
}
307300

308-
return templates.visitor(visitor);
301+
return visitor;
309302
};
310303

311304
const schema = {

rules/prefer-string-slice.js

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
2-
const eslintTemplateVisitor = require('eslint-template-visitor');
32
const {getParenthesizedText} = require('./utils/parentheses.js');
3+
const {methodCallSelector} = require('./selectors/index.js');
44
const isNumber = require('./utils/is-number.js');
55

66
const MESSAGE_ID_SUBSTR = 'substr';
@@ -10,13 +10,8 @@ const messages = {
1010
[MESSAGE_ID_SUBSTRING]: 'Prefer `String#slice()` over `String#substring()`.',
1111
};
1212

13-
const templates = eslintTemplateVisitor();
14-
15-
const objectVariable = templates.variable();
16-
const argumentsVariable = templates.spreadVariable();
17-
18-
const substrCallTemplate = templates.template`${objectVariable}.substr(${argumentsVariable})`;
19-
const substringCallTemplate = templates.template`${objectVariable}.substring(${argumentsVariable})`;
13+
const substrCallSelector = methodCallSelector({method: 'substr', includeOptionalMember: true, includeOptionalCall: true});
14+
const substringCallSelector = methodCallSelector({method: 'substring', includeOptionalMember: true, includeOptionalCall: true});
2015

2116
const isLiteralNumber = node => node && node.type === 'Literal' && typeof node.value === 'number';
2217

@@ -43,10 +38,10 @@ const isLengthProperty = node => (
4338
const create = context => {
4439
const sourceCode = context.getSourceCode();
4540

46-
return templates.visitor({
47-
[substrCallTemplate](node) {
48-
const objectNode = substrCallTemplate.context.getMatch(objectVariable);
49-
const argumentNodes = substrCallTemplate.context.getMatch(argumentsVariable);
41+
return {
42+
[substrCallSelector](node) {
43+
const objectNode = node.callee.object;
44+
const argumentNodes = node.arguments;
5045

5146
const problem = {
5247
node,
@@ -110,9 +105,9 @@ const create = context => {
110105
context.report(problem);
111106
},
112107

113-
[substringCallTemplate](node) {
114-
const objectNode = substringCallTemplate.context.getMatch(objectVariable);
115-
const argumentNodes = substringCallTemplate.context.getMatch(argumentsVariable);
108+
[substringCallSelector](node) {
109+
const objectNode = node.callee.object;
110+
const argumentNodes = node.arguments;
116111

117112
const problem = {
118113
node,
@@ -177,7 +172,7 @@ const create = context => {
177172

178173
context.report(problem);
179174
},
180-
});
175+
};
181176
};
182177

183178
/** @type {import('eslint').Rule.RuleModule} */

test/snapshots/import-style.mjs.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Generated by [AVA](https://avajs.dev).
3131
3232
`␊
3333
> 1 | const util = require('util')␊
34-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use named import for module \`util\`.␊
34+
| ^^^^^^^^^^^^^^^^^^^^^^ Use named import for module \`util\`.␊
3535
`
3636

3737
## Invalid #4
@@ -74,6 +74,6 @@ Generated by [AVA](https://avajs.dev).
7474
`␊
7575
1 | async () => {␊
7676
> 2 | const {red} = await import('chalk');␊
77-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use default import for module \`chalk\`.␊
77+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use default import for module \`chalk\`.␊
7878
3 | }␊
7979
`
4 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)