Skip to content

Commit 074e6f3

Browse files
committed
Update getScope for max compatibility
1 parent 73e9ebe commit 074e6f3

7 files changed

+40
-36
lines changed

package-lock.json

+8-23
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rules/detect-child-process.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ module.exports = {
2424
},
2525
},
2626
create(context) {
27-
const sourceCode = context.sourceCode;
27+
const sourceCode = context.sourceCode || context.getSourceCode();
2828
return {
2929
CallExpression: function (node) {
3030
if (node.callee.name === 'require') {
@@ -42,19 +42,21 @@ module.exports = {
4242
return;
4343
}
4444

45+
const scope = sourceCode.getScope ? sourceCode.getScope(node) : context.getScope();
46+
4547
// Reports non-literal `exec()` calls.
4648
if (
4749
!node.arguments.length ||
4850
isStaticExpression({
4951
node: node.arguments[0],
50-
scope: sourceCode.getScope(node.arguments[0]),
52+
scope,
5153
})
5254
) {
5355
return;
5456
}
5557
const pathInfo = getImportAccessPath({
5658
node: node.callee,
57-
scope: sourceCode.getScope(node.callee),
59+
scope,
5860
packageNames: childProcessPackageNames,
5961
});
6062
const fnName = pathInfo && pathInfo.path.length === 1 && pathInfo.path[0];

rules/detect-non-literal-fs-filename.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,18 @@ module.exports = {
2727
},
2828
},
2929
create(context) {
30-
const sourceCode = context.sourceCode;
30+
const sourceCode = context.sourceCode || context.getSourceCode();
3131
return {
32-
CallExpression: function (node) {
32+
CallExpression(node) {
3333
// don't check require. If all arguments are Literals, it's surely safe!
3434
if ((node.callee.type === 'Identifier' && node.callee.name === 'require') || node.arguments.every((argument) => argument.type === 'Literal')) {
3535
return;
3636
}
3737

38+
const scope = sourceCode.getScope ? sourceCode.getScope(node) : context.getScope();
3839
const pathInfo = getImportAccessPath({
3940
node: node.callee,
40-
scope: sourceCode.getScope(node.callee),
41+
scope,
4142
packageNames: fsPackageNames,
4243
});
4344
if (!pathInfo) {
@@ -80,7 +81,8 @@ module.exports = {
8081
continue;
8182
}
8283
const argument = node.arguments[index];
83-
if (isStaticExpression({ node: argument, scope: sourceCode.getScope(argument) })) {
84+
85+
if (isStaticExpression({ node: argument, scope })) {
8486
continue;
8587
}
8688
indices.push(index);

rules/detect-non-literal-regexp.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,20 @@ module.exports = {
2222
},
2323
},
2424
create(context) {
25+
const sourceCode = context.sourceCode || context.getSourceCode();
26+
2527
return {
26-
NewExpression: function (node) {
28+
NewExpression(node) {
2729
if (node.callee.name === 'RegExp') {
2830
const args = node.arguments;
31+
const scope = sourceCode.getScope ? sourceCode.getScope(node) : context.getScope();
32+
2933
if (
3034
args &&
3135
args.length > 0 &&
3236
!isStaticExpression({
3337
node: args[0],
34-
scope: context.sourceCode.getScope(args[0]),
38+
scope,
3539
})
3640
) {
3741
return context.report({ node: node, message: 'Found non-literal argument to RegExp Constructor' });

rules/detect-non-literal-require.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,20 @@ module.exports = {
2222
},
2323
},
2424
create(context) {
25+
const sourceCode = context.sourceCode || context.getSourceCode();
26+
2527
return {
26-
CallExpression: function (node) {
28+
CallExpression(node) {
2729
if (node.callee.name === 'require') {
2830
const args = node.arguments;
31+
const scope = sourceCode.getScope ? sourceCode.getScope(node) : context.getScope();
32+
2933
if (
3034
args &&
3135
args.length > 0 &&
3236
!isStaticExpression({
3337
node: args[0],
34-
scope: context.sourceCode.getScope(args[0]),
38+
scope,
3539
})
3640
) {
3741
return context.report({ node: node, message: 'Found non-literal argument in require' });

test/utils/import-utils.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,18 @@ function getGetImportAccessPathResult(code) {
1010
const result = [];
1111
const testRule = {
1212
create(context) {
13+
const sourceCode = context.sourceCode || context.getSourceCode();
1314
return {
1415
'Identifier[name = target]'(node) {
1516
let expr = node;
1617
if (node.parent.type === 'MemberExpression' && node.parent.property === node) {
1718
expr = node.parent;
1819
}
20+
const scope = sourceCode.getScope ? sourceCode.getScope(node) : context.getScope();
21+
1922
const info = getImportAccessPath({
2023
node: expr,
21-
scope: context.sourceCode.getScope(expr),
24+
scope,
2225
packageNames: ['target', 'target-foo', 'target-bar'],
2326
});
2427
if (!info) return;

test/utils/is-static-expression.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,17 @@ function getIsStaticExpressionResult(code) {
1414
const result = [];
1515
const testRule = {
1616
create(context) {
17+
const sourceCode = context.sourceCode || context.getSourceCode();
18+
1719
return {
1820
'CallExpression[callee.name = target]'(node) {
21+
const scope = sourceCode.getScope ? sourceCode.getScope(node) : context.getScope();
22+
1923
result.push(
2024
...node.arguments.map((expr) =>
2125
isStaticExpression({
2226
node: expr,
23-
scope: context.sourceCode.getScope(expr),
27+
scope,
2428
})
2529
)
2630
);

0 commit comments

Comments
 (0)