Skip to content

Commit f5496c7

Browse files
fiskersindresorhus
andauthored
Refactor: remove eslint-ast-utils package (#1048)
Co-authored-by: Sindre Sorhus <[email protected]>
1 parent 77bcdc6 commit f5496c7

File tree

5 files changed

+54
-9
lines changed

5 files changed

+54
-9
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
"dependencies": {
3838
"ci-info": "^2.0.0",
3939
"clean-regexp": "^1.0.0",
40-
"eslint-ast-utils": "^1.1.0",
4140
"eslint-template-visitor": "^2.2.2",
4241
"eslint-utils": "^2.1.0",
4342
"import-modules": "^2.1.0",

rules/prefer-reflect-apply.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
2-
const astUtils = require('eslint-ast-utils');
32
const getDocumentationUrl = require('./utils/get-documentation-url');
43
const isLiteralValue = require('./utils/is-literal-value');
4+
const getPropertyName = require('./utils/get-property-name');
55

66
const MESSAGE_ID = 'prefer-reflect-apply';
77
const messages = {
@@ -26,7 +26,7 @@ const getReflectApplyCall = (sourceCode, target, receiver, argumentsList) => (
2626

2727
const fixDirectApplyCall = (node, sourceCode) => {
2828
if (
29-
astUtils.getPropertyName(node.callee) === 'apply' &&
29+
getPropertyName(node.callee) === 'apply' &&
3030
node.arguments.length === 2 &&
3131
isApplySignature(node.arguments[0], node.arguments[1])
3232
) {
@@ -41,9 +41,9 @@ const fixDirectApplyCall = (node, sourceCode) => {
4141

4242
const fixFunctionPrototypeCall = (node, sourceCode) => {
4343
if (
44-
astUtils.getPropertyName(node.callee) === 'call' &&
45-
astUtils.getPropertyName(node.callee.object) === 'apply' &&
46-
astUtils.getPropertyName(node.callee.object.object) === 'prototype' &&
44+
getPropertyName(node.callee) === 'call' &&
45+
getPropertyName(node.callee.object) === 'apply' &&
46+
getPropertyName(node.callee.object.object) === 'prototype' &&
4747
node.callee.object.object.object &&
4848
node.callee.object.object.object.type === 'Identifier' &&
4949
node.callee.object.object.object.name === 'Function' &&

rules/prevent-abbreviations.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use strict';
22
const path = require('path');
3-
const astUtils = require('eslint-ast-utils');
43
const {defaultsDeep, upperFirst, lowerFirst} = require('lodash');
54

65
const getDocumentationUrl = require('./utils/get-documentation-url');
@@ -432,6 +431,16 @@ const shouldFix = variable => {
432431
return !getVariableIdentifiers(variable).some(identifier => isExportedIdentifier(identifier));
433432
};
434433

434+
const isStaticRequire = node => Boolean(
435+
node &&
436+
node.callee &&
437+
node.callee.type === 'Identifier' &&
438+
node.callee.name === 'require' &&
439+
node.arguments.length === 1 &&
440+
node.arguments[0].type === 'Literal' &&
441+
typeof node.arguments[0].value === 'string'
442+
);
443+
435444
const isDefaultOrNamespaceImportName = identifier => {
436445
if (
437446
identifier.parent.type === 'ImportDefaultSpecifier' &&
@@ -459,7 +468,7 @@ const isDefaultOrNamespaceImportName = identifier => {
459468
if (
460469
identifier.parent.type === 'VariableDeclarator' &&
461470
identifier.parent.id === identifier &&
462-
astUtils.isStaticRequire(identifier.parent.init)
471+
isStaticRequire(identifier.parent.init)
463472
) {
464473
return true;
465474
}

rules/utils/get-property-name.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'use strict';
2+
const {getStaticValue} = require('eslint-utils');
3+
4+
/**
5+
Get the property value of a `MemberExpression` node.
6+
7+
@param {Node} node - The `MemberExpression` node.
8+
@param {Scope} [scope] - The scope to start finding the variable. Optional. If this scope was given, it tries to resolve identifier references which are in the given node as much as possible.
9+
*/
10+
function getPropertyName(node, scope) {
11+
const {type, property, computed} = node;
12+
/* istanbul ignore next */
13+
if (type !== 'MemberExpression') {
14+
return;
15+
}
16+
17+
if (!computed) {
18+
if (property.type === 'Identifier') {
19+
return property.name;
20+
}
21+
22+
/* istanbul ignore next: It could be `PrivateIdentifier`(ESTree) or `PrivateName`(Babel) when it's in `class` */
23+
return;
24+
}
25+
26+
const result = getStaticValue(property, scope);
27+
return result && result.value;
28+
}
29+
30+
module.exports = getPropertyName;

test/prefer-reflect-apply.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ test({
2222
'foo.apply;',
2323
'apply;',
2424
'Reflect.apply(foo, null);',
25-
'Reflect.apply(foo, null, [bar]);'
25+
'Reflect.apply(foo, null, [bar]);',
26+
// Currently, we are not passing `scope` to `getStaticValue`, so the method is unknown
27+
'const apply = "apply"; foo[apply](null, [42]);'
2628
],
2729
invalid: [
2830
{
@@ -74,6 +76,11 @@ test({
7476
code: 'Function.prototype.apply.call(foo, this, arguments);',
7577
output: 'Reflect.apply(foo, this, arguments);',
7678
errors
79+
},
80+
{
81+
code: 'foo["apply"](null, [42]);',
82+
output: 'Reflect.apply(foo, null, [42]);',
83+
errors
7784
}
7885
]
7986
});

0 commit comments

Comments
 (0)