Skip to content

Commit dbb2347

Browse files
authored
Remove esquery (#2825)
1 parent c3479c0 commit dbb2347

File tree

3 files changed

+33
-41
lines changed

3 files changed

+33
-41
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@
6363
"ci-info": "^4.3.1",
6464
"clean-regexp": "^1.0.0",
6565
"core-js-compat": "^3.46.0",
66-
"esquery": "^1.6.0",
6766
"find-up-simple": "^1.0.1",
6867
"globals": "^16.4.0",
6968
"indent-string": "^5.0.0",

rules/isolated-functions.js

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,11 @@
11
import globals from 'globals';
2-
import esquery from 'esquery';
32
import {functionTypes} from './ast/index.js';
43

54
const MESSAGE_ID_EXTERNALLY_SCOPED_VARIABLE = 'externally-scoped-variable';
65
const messages = {
76
[MESSAGE_ID_EXTERNALLY_SCOPED_VARIABLE]: 'Variable {{name}} not defined in scope of isolated function. Function is isolated because: {{reason}}.',
87
};
98

10-
const parsedEsquerySelectors = new Map();
11-
const parseEsquerySelector = selector => {
12-
if (!parsedEsquerySelectors.has(selector)) {
13-
parsedEsquerySelectors.set(selector, esquery.parse(selector));
14-
}
15-
16-
return parsedEsquerySelectors.get(selector);
17-
};
18-
199
/** @type {{functions: string[], selectors: string[], comments: string[], overrideGlobals?: import('eslint').Linter.Globals}} */
2010
const defaultOptions = {
2111
functions: ['makeSynchronous'],
@@ -40,14 +30,16 @@ const create = context => {
4030
...context.languageOptions.globals,
4131
...options.overrideGlobals,
4232
};
33+
const checked = new WeakSet();
4334

4435
/** @param {import('estree').Node} node */
45-
const checkForExternallyScopedVariables = node => {
46-
let reason = reasonForBeingIsolatedFunction(node);
47-
if (!reason) {
36+
const checkForExternallyScopedVariables = (node, reason) => {
37+
if (checked.has(node) || !functionTypes.includes(node.type)) {
4838
return;
4939
}
5040

41+
checked.add(node);
42+
5143
const nodeScope = sourceCode.getScope(node);
5244

5345
// `through`: "The array of references which could not be resolved in this scope" https://eslint.org/docs/latest/extend/scope-manager-interface#scope-interface
@@ -136,20 +128,29 @@ const create = context => {
136128
) {
137129
return `callee of function named ${JSON.stringify(node.parent.callee.name)}`;
138130
}
131+
};
139132

140-
if (options.selectors.length > 0) {
141-
const ancestors = sourceCode.getAncestors(node);
142-
const matchedSelector = options.selectors.find(selector => esquery.matches(node, parseEsquerySelector(selector), ancestors));
143-
if (matchedSelector) {
144-
return `matches selector ${JSON.stringify(matchedSelector)}`;
133+
context.onExit(
134+
functionTypes,
135+
node => {
136+
const reason = reasonForBeingIsolatedFunction(node);
137+
if (!reason) {
138+
return;
145139
}
146-
}
147-
};
148140

149-
context.on(
150-
functionTypes.map(type => `${type}:exit`),
151-
checkForExternallyScopedVariables,
141+
return checkForExternallyScopedVariables(node, reason);
142+
},
152143
);
144+
145+
for (const selector of options.selectors) {
146+
context.onExit(
147+
selector,
148+
node => {
149+
const reason = `matches selector ${JSON.stringify(selector)}`;
150+
return checkForExternallyScopedVariables(node, reason);
151+
},
152+
);
153+
}
153154
};
154155

155156
/** @type {import('json-schema').JSONSchema7[]} */

rules/template-indent.js

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import stripIndent from 'strip-indent';
22
import indentString from 'indent-string';
3-
import esquery from 'esquery';
43
import {replaceTemplateElement} from './fix/index.js';
54
import {isTaggedTemplateLiteral} from './ast/index.js';
65
import {isNodeMatches} from './utils/index.js';
@@ -11,15 +10,6 @@ const messages = {
1110
[MESSAGE_ID_IMPROPERLY_INDENTED_TEMPLATE]: 'Templates should be properly indented.',
1211
};
1312

14-
const parsedEsquerySelectors = new Map();
15-
const parseEsquerySelector = selector => {
16-
if (!parsedEsquerySelectors.has(selector)) {
17-
parsedEsquerySelectors.set(selector, esquery.parse(selector));
18-
}
19-
20-
return parsedEsquerySelectors.get(selector);
21-
};
22-
2313
/** @param {import('eslint').Rule.RuleContext} context */
2414
const create = context => {
2515
const {sourceCode} = context;
@@ -32,9 +22,16 @@ const create = context => {
3222
};
3323

3424
options.comments = options.comments.map(comment => comment.toLowerCase());
25+
const checked = new WeakSet();
3526

3627
/** @param {import('@babel/core').types.TemplateLiteral} node */
3728
const getProblem = node => {
29+
if (node.type !== 'TemplateLiteral' || checked.has(node)) {
30+
return;
31+
}
32+
33+
checked.add(node);
34+
3835
const delimiter = '__PLACEHOLDER__' + Math.random();
3936
const joined = node.quasis
4037
.map(quasi => {
@@ -114,13 +111,6 @@ const create = context => {
114111
return true;
115112
}
116113

117-
if (options.selectors.length > 0) {
118-
const ancestors = sourceCode.getAncestors(node).toReversed();
119-
if (options.selectors.some(selector => esquery.matches(node, parseEsquerySelector(selector), ancestors))) {
120-
return true;
121-
}
122-
}
123-
124114
return false;
125115
};
126116

@@ -131,6 +121,8 @@ const create = context => {
131121

132122
return getProblem(node);
133123
});
124+
125+
context.on(options.selectors, /** @param {import('@babel/core').types.TemplateLiteral} node */ node => getProblem(node));
134126
};
135127

136128
/** @type {import('json-schema').JSONSchema7[]} */

0 commit comments

Comments
 (0)