Skip to content

Commit

Permalink
Update getScope for max compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
nzakas committed Apr 10, 2024
1 parent 73e9ebe commit 074e6f3
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 36 deletions.
31 changes: 8 additions & 23 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions rules/detect-child-process.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module.exports = {
},
},
create(context) {
const sourceCode = context.sourceCode;
const sourceCode = context.sourceCode || context.getSourceCode();
return {
CallExpression: function (node) {
if (node.callee.name === 'require') {
Expand All @@ -42,19 +42,21 @@ module.exports = {
return;
}

const scope = sourceCode.getScope ? sourceCode.getScope(node) : context.getScope();

// Reports non-literal `exec()` calls.
if (
!node.arguments.length ||
isStaticExpression({
node: node.arguments[0],
scope: sourceCode.getScope(node.arguments[0]),
scope,
})
) {
return;
}
const pathInfo = getImportAccessPath({
node: node.callee,
scope: sourceCode.getScope(node.callee),
scope,
packageNames: childProcessPackageNames,
});
const fnName = pathInfo && pathInfo.path.length === 1 && pathInfo.path[0];
Expand Down
10 changes: 6 additions & 4 deletions rules/detect-non-literal-fs-filename.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,18 @@ module.exports = {
},
},
create(context) {
const sourceCode = context.sourceCode;
const sourceCode = context.sourceCode || context.getSourceCode();
return {
CallExpression: function (node) {
CallExpression(node) {
// don't check require. If all arguments are Literals, it's surely safe!
if ((node.callee.type === 'Identifier' && node.callee.name === 'require') || node.arguments.every((argument) => argument.type === 'Literal')) {
return;
}

const scope = sourceCode.getScope ? sourceCode.getScope(node) : context.getScope();
const pathInfo = getImportAccessPath({
node: node.callee,
scope: sourceCode.getScope(node.callee),
scope,
packageNames: fsPackageNames,
});
if (!pathInfo) {
Expand Down Expand Up @@ -80,7 +81,8 @@ module.exports = {
continue;
}
const argument = node.arguments[index];
if (isStaticExpression({ node: argument, scope: sourceCode.getScope(argument) })) {

if (isStaticExpression({ node: argument, scope })) {
continue;
}
indices.push(index);
Expand Down
8 changes: 6 additions & 2 deletions rules/detect-non-literal-regexp.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,20 @@ module.exports = {
},
},
create(context) {
const sourceCode = context.sourceCode || context.getSourceCode();

return {
NewExpression: function (node) {
NewExpression(node) {
if (node.callee.name === 'RegExp') {
const args = node.arguments;
const scope = sourceCode.getScope ? sourceCode.getScope(node) : context.getScope();

if (
args &&
args.length > 0 &&
!isStaticExpression({
node: args[0],
scope: context.sourceCode.getScope(args[0]),
scope,
})
) {
return context.report({ node: node, message: 'Found non-literal argument to RegExp Constructor' });
Expand Down
8 changes: 6 additions & 2 deletions rules/detect-non-literal-require.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,20 @@ module.exports = {
},
},
create(context) {
const sourceCode = context.sourceCode || context.getSourceCode();

return {
CallExpression: function (node) {
CallExpression(node) {
if (node.callee.name === 'require') {
const args = node.arguments;
const scope = sourceCode.getScope ? sourceCode.getScope(node) : context.getScope();

if (
args &&
args.length > 0 &&
!isStaticExpression({
node: args[0],
scope: context.sourceCode.getScope(args[0]),
scope,
})
) {
return context.report({ node: node, message: 'Found non-literal argument in require' });
Expand Down
5 changes: 4 additions & 1 deletion test/utils/import-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,18 @@ function getGetImportAccessPathResult(code) {
const result = [];
const testRule = {
create(context) {
const sourceCode = context.sourceCode || context.getSourceCode();
return {
'Identifier[name = target]'(node) {
let expr = node;
if (node.parent.type === 'MemberExpression' && node.parent.property === node) {
expr = node.parent;
}
const scope = sourceCode.getScope ? sourceCode.getScope(node) : context.getScope();

const info = getImportAccessPath({
node: expr,
scope: context.sourceCode.getScope(expr),
scope,
packageNames: ['target', 'target-foo', 'target-bar'],
});
if (!info) return;
Expand Down
6 changes: 5 additions & 1 deletion test/utils/is-static-expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@ function getIsStaticExpressionResult(code) {
const result = [];
const testRule = {
create(context) {
const sourceCode = context.sourceCode || context.getSourceCode();

return {
'CallExpression[callee.name = target]'(node) {
const scope = sourceCode.getScope ? sourceCode.getScope(node) : context.getScope();

result.push(
...node.arguments.map((expr) =>
isStaticExpression({
node: expr,
scope: context.sourceCode.getScope(expr),
scope,
})
)
);
Expand Down

0 comments on commit 074e6f3

Please sign in to comment.