Skip to content

Commit ac50ab4

Browse files
nzakasaladdin-add
andauthored
fix: Ensure everything works with ESLint v9 (#145)
Co-authored-by: 唯然 <[email protected]>
1 parent df1b606 commit ac50ab4

20 files changed

+486
-439
lines changed

package-lock.json

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

package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,13 @@
4646
"safe-regex": "^2.1.1"
4747
},
4848
"devDependencies": {
49-
"@eslint/js": "^8.51.0",
49+
"@eslint/js": "^9.0.0",
5050
"changelog": "1.3.0",
51-
"eslint": "^8.51.0",
51+
"eslint": "^9.0.0",
5252
"eslint-config-nodesecurity": "^1.3.1",
5353
"eslint-config-prettier": "^8.5.0",
5454
"eslint-doc-generator": "^1.7.0",
55-
"eslint-plugin-eslint-plugin": "^5.1.1",
55+
"eslint-plugin-eslint-plugin": "^5.5.1",
5656
"lint-staged": "^12.3.7",
5757
"markdownlint-cli": "^0.32.2",
5858
"mocha": "^9.2.2",

rules/detect-bidi-characters.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ module.exports = {
7878
url: 'https://github.com/eslint-community/eslint-plugin-security/blob/main/docs/rules/detect-bidi-characters.md',
7979
},
8080
},
81-
create: function (context) {
81+
create(context) {
8282
return {
8383
Program: function (node) {
8484
report({

rules/detect-buffer-noassert.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ module.exports = {
6161
write,
6262
},
6363
},
64-
create: function (context) {
64+
create(context) {
6565
return {
6666
MemberExpression: function (node) {
6767
let index;

rules/detect-child-process.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ module.exports = {
2323
url: 'https://github.com/eslint-community/eslint-plugin-security/blob/main/docs/rules/detect-child-process.md',
2424
},
2525
},
26-
create: function (context) {
26+
create(context) {
27+
const sourceCode = context.sourceCode || context.getSourceCode();
2728
return {
2829
CallExpression: function (node) {
2930
if (node.callee.name === 'require') {
@@ -41,19 +42,21 @@ module.exports = {
4142
return;
4243
}
4344

45+
const scope = sourceCode.getScope ? sourceCode.getScope(node) : context.getScope();
46+
4447
// Reports non-literal `exec()` calls.
4548
if (
4649
!node.arguments.length ||
4750
isStaticExpression({
4851
node: node.arguments[0],
49-
scope: context.getScope(),
52+
scope,
5053
})
5154
) {
5255
return;
5356
}
5457
const pathInfo = getImportAccessPath({
5558
node: node.callee,
56-
scope: context.getScope(),
59+
scope,
5760
packageNames: childProcessPackageNames,
5861
});
5962
const fnName = pathInfo && pathInfo.path.length === 1 && pathInfo.path[0];

rules/detect-disable-mustache-escape.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module.exports = {
1010
url: 'https://github.com/eslint-community/eslint-plugin-security/blob/main/docs/rules/detect-disable-mustache-escape.md',
1111
},
1212
},
13-
create: function (context) {
13+
create(context) {
1414
return {
1515
AssignmentExpression: function (node) {
1616
if (node.operator === '=') {

rules/detect-new-buffer.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module.exports = {
1010
url: 'https://github.com/eslint-community/eslint-plugin-security/blob/main/docs/rules/detect-new-buffer.md',
1111
},
1212
},
13-
create: function (context) {
13+
create(context) {
1414
return {
1515
NewExpression: function (node) {
1616
if (node.callee.name === 'Buffer' && node.arguments[0] && node.arguments[0].type !== 'Literal') {

rules/detect-no-csrf-before-method-override.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ module.exports = {
1919
url: 'https://github.com/eslint-community/eslint-plugin-security/blob/main/docs/rules/detect-no-csrf-before-method-override.md',
2020
},
2121
},
22-
create: function (context) {
22+
create(context) {
2323
let csrf = false;
2424

2525
return {

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

+7-4
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,19 @@ module.exports = {
2626
url: 'https://github.com/eslint-community/eslint-plugin-security/blob/main/docs/rules/detect-non-literal-fs-filename.md',
2727
},
2828
},
29-
create: function (context) {
29+
create(context) {
30+
const sourceCode = context.sourceCode || context.getSourceCode();
3031
return {
31-
CallExpression: function (node) {
32+
CallExpression(node) {
3233
// don't check require. If all arguments are Literals, it's surely safe!
3334
if ((node.callee.type === 'Identifier' && node.callee.name === 'require') || node.arguments.every((argument) => argument.type === 'Literal')) {
3435
return;
3536
}
3637

38+
const scope = sourceCode.getScope ? sourceCode.getScope(node) : context.getScope();
3739
const pathInfo = getImportAccessPath({
3840
node: node.callee,
39-
scope: context.getScope(),
41+
scope,
4042
packageNames: fsPackageNames,
4143
});
4244
if (!pathInfo) {
@@ -79,7 +81,8 @@ module.exports = {
7981
continue;
8082
}
8183
const argument = node.arguments[index];
82-
if (isStaticExpression({ node: argument, scope: context.getScope() })) {
84+
85+
if (isStaticExpression({ node: argument, scope })) {
8386
continue;
8487
}
8588
indices.push(index);

rules/detect-non-literal-regexp.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,21 @@ module.exports = {
2121
url: 'https://github.com/eslint-community/eslint-plugin-security/blob/main/docs/rules/detect-non-literal-regexp.md',
2222
},
2323
},
24-
create: function (context) {
24+
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.getScope(),
38+
scope,
3539
})
3640
) {
3741
return context.report({ node: node, message: 'Found non-literal argument to RegExp Constructor' });

rules/detect-non-literal-require.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,21 @@ module.exports = {
2121
url: 'https://github.com/eslint-community/eslint-plugin-security/blob/main/docs/rules/detect-non-literal-require.md',
2222
},
2323
},
24-
create: function (context) {
24+
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.getScope(),
38+
scope,
3539
})
3640
) {
3741
return context.report({ node: node, message: 'Found non-literal argument in require' });

rules/detect-object-injection.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ module.exports = {
6161
url: 'https://github.com/eslint-community/eslint-plugin-security/blob/main/docs/rules/detect-object-injection.md',
6262
},
6363
},
64-
create: function (context) {
64+
create(context) {
6565
return {
6666
MemberExpression: function (node) {
6767
if (node.computed === true) {

rules/detect-possible-timing-attacks.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ module.exports = {
3232
url: 'https://github.com/eslint-community/eslint-plugin-security/blob/main/docs/rules/detect-possible-timing-attacks.md',
3333
},
3434
},
35-
create: function (context) {
35+
create(context) {
3636
return {
3737
IfStatement: function (node) {
3838
if (node.test && node.test.type === 'BinaryExpression') {

rules/detect-pseudoRandomBytes.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ module.exports = {
1919
url: 'https://github.com/eslint-community/eslint-plugin-security/blob/main/docs/rules/detect-pseudoRandomBytes.md',
2020
},
2121
},
22-
create: function (context) {
22+
create(context) {
2323
return {
2424
MemberExpression: function (node) {
2525
if (node.property.name === 'pseudoRandomBytes') {

rules/detect-unsafe-regex.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ module.exports = {
2525
url: 'https://github.com/eslint-community/eslint-plugin-security/blob/main/docs/rules/detect-unsafe-regex.md',
2626
},
2727
},
28-
create: function (context) {
28+
create(context) {
2929
return {
3030
Literal: function (node) {
3131
const token = context.getSourceCode().getTokens(node)[0];

test/rules/detect-child-process.js

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
'use strict';
22

33
const RuleTester = require('eslint').RuleTester;
4-
const tester = new RuleTester({
5-
parserOptions: {
6-
ecmaVersion: 6,
7-
sourceType: 'module',
8-
},
9-
});
4+
const tester = new RuleTester();
105

116
const ruleName = 'detect-child-process';
127
const rule = require(`../../rules/${ruleName}`);

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

+21-16
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
'use strict';
22

33
const RuleTester = require('eslint').RuleTester;
4-
const tester = new RuleTester({
5-
parserOptions: {
6-
ecmaVersion: 13,
7-
sourceType: 'module',
8-
},
9-
});
4+
const tester = new RuleTester();
105

116
const ruleName = 'detect-non-literal-fs-filename';
127

@@ -33,8 +28,10 @@ tester.run(ruleName, require(`../../rules/${ruleName}`), {
3328
const index = await fsp.readFile(path.resolve(__dirname, './index.html'), 'utf-8');
3429
const key = fs.readFileSync(path.join(__dirname, './ssl.key'));
3530
await fsp.writeFile(path.resolve(__dirname, './sitemap.xml'), sitemap);`,
36-
globals: {
37-
__dirname: 'readonly',
31+
languageOptions: {
32+
globals: {
33+
__dirname: 'readonly',
34+
},
3835
},
3936
},
4037
{
@@ -43,16 +40,20 @@ tester.run(ruleName, require(`../../rules/${ruleName}`), {
4340
import path from 'path';
4441
const dirname = path.dirname(__filename)
4542
const key = fs.readFileSync(path.resolve(dirname, './index.html'));`,
46-
globals: {
47-
__filename: 'readonly',
43+
languageOptions: {
44+
globals: {
45+
__filename: 'readonly',
46+
},
4847
},
4948
},
5049
{
5150
code: `
5251
import fs from 'fs';
5352
const key = fs.readFileSync(\`\${process.cwd()}/path/to/foo.json\`);`,
54-
globals: {
55-
process: 'readonly',
53+
languageOptions: {
54+
globals: {
55+
process: 'readonly',
56+
},
5657
},
5758
},
5859
`
@@ -65,8 +66,10 @@ tester.run(ruleName, require(`../../rules/${ruleName}`), {
6566
code: `
6667
import fs from 'fs';
6768
const pkg = fs.readFileSync(require.resolve('eslint/package.json'), 'utf-8');`,
68-
globals: {
69-
require: 'readonly',
69+
languageOptions: {
70+
globals: {
71+
require: 'readonly',
72+
},
7073
},
7174
},
7275
],
@@ -191,8 +194,10 @@ tester.run(ruleName, require(`../../rules/${ruleName}`), {
191194
import fs from 'fs';
192195
import path from 'path';
193196
const key = fs.readFileSync(path.resolve(__dirname, foo));`,
194-
globals: {
195-
__filename: 'readonly',
197+
languageOptions: {
198+
globals: {
199+
__filename: 'readonly',
200+
},
196201
},
197202
errors: [{ message: 'Found readFileSync from package "fs" with non literal argument at index 0' }],
198203
},

test/rules/detect-non-literal-require.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const RuleTester = require('eslint').RuleTester;
44

5-
const tester = new RuleTester({ parserOptions: { ecmaVersion: 6 } });
5+
const tester = new RuleTester({ languageOptions: { sourceType: 'commonjs' } });
66

77
const ruleName = 'detect-non-literal-require';
88

@@ -17,8 +17,10 @@ tester.run(ruleName, require(`../../rules/${ruleName}`), {
1717
},
1818
{
1919
code: "const utils = require(__dirname + '/utils');",
20-
globals: {
21-
__dirname: 'readonly',
20+
languageOptions: {
21+
globals: {
22+
__dirname: 'readonly',
23+
},
2224
},
2325
},
2426
],

test/utils/import-utils.js

+13-7
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,20 @@ const Linter = require('eslint').Linter;
88
function getGetImportAccessPathResult(code) {
99
const linter = new Linter();
1010
const result = [];
11-
linter.defineRule('test-rule', {
11+
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.getScope(),
24+
scope,
2225
packageNames: ['target', 'target-foo', 'target-bar'],
2326
});
2427
if (!info) return;
@@ -30,15 +33,18 @@ function getGetImportAccessPathResult(code) {
3033
},
3134
};
3235
},
33-
});
36+
};
3437

3538
const linterResult = linter.verify(code, {
36-
parserOptions: {
37-
ecmaVersion: 6,
38-
sourceType: 'module',
39+
plugins: {
40+
test: {
41+
rules: {
42+
'test-rule': testRule,
43+
},
44+
},
3945
},
4046
rules: {
41-
'test-rule': 'error',
47+
'test/test-rule': 'error',
4248
},
4349
});
4450
deepStrictEqual(linterResult, []);

0 commit comments

Comments
 (0)