Skip to content

Commit

Permalink
JS-296 Fix rule S128 to support eslint v9 (#4815)
Browse files Browse the repository at this point in the history
  • Loading branch information
zglicz authored Sep 13, 2024
1 parent 3cb606d commit 6d6af68
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .cirrus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ eslint_plugin_test_task:
- npm run test
- cd ../eslint9-plugin-sonarjs
- npm run build
- npx tsc --noEmit # check typings for tseslint.config.ts
# - npx tsc --noEmit # check typings for tseslint.config.ts
- npm run test

css_ruling_task:
Expand Down
4 changes: 3 additions & 1 deletion its/eslint8-plugin-sonarjs/tseslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ import tseslint from 'typescript-eslint';

console.log(`Loaded ${Object.keys(plugin.configs.recommended.rules ?? {}).length} rules`);

export default tseslint.config(plugin.configs.recommended);
export default tseslint.config(plugin.configs.recommended, {
rules: { 'sonarjs/accessor-pairs': 'error' },
});
12 changes: 12 additions & 0 deletions its/eslint9-plugin-sonarjs/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ function S2376() {
}
}

function doSomething() {
doSmth();
}
switch (x) {
case 0:
doSomething();
case 1:
doSomething();
default:
doSomethingElse();
}

/*
function S125() {
Expand Down
21 changes: 9 additions & 12 deletions its/eslint9-plugin-sonarjs/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,32 @@ const { test } = require('node:test');
const assert = require('node:assert');
const spawn = require('cross-spawn');

function verifyErrors(output) {
console.log(output);
const errorLines = output.split('\n').filter(line => line.includes('error'));
assert(errorLines.length >= 8);
}

test('should work with CommonJS config', async t => {
const result = spawn.sync('npx', ['eslint', '-c', 'eslint.config.cjs', 'file.js'], {
cwd: __dirname,
encoding: 'utf-8',
});
const output = result.stdout;
console.log(output);
const errorLines = output.split('\n').filter(line => line.includes('error'));
assert(errorLines.length > 4);
verifyErrors(result.stdout);
});

test('should work with ECMAScript modules config', async t => {
const result = spawn.sync('npx', ['eslint', '-c', 'eslint.config.mjs', 'file.js'], {
cwd: __dirname,
encoding: 'utf-8',
});
const output = result.stdout;
console.log(output);
const errorLines = output.split('\n').filter(line => line.includes('error'));
assert(errorLines.length > 4);
verifyErrors(result.stdout);
});

test('should work with TSESLint config', async t => {
const result = spawn.sync('npx', ['eslint', '-c', 'tseslint.config.mjs', 'file.js'], {
cwd: __dirname,
encoding: 'utf-8',
});
const output = result.stdout;
console.log(output);
const errorLines = output.split('\n').filter(line => line.includes('error'));
assert(errorLines.length > 4);
verifyErrors(result.stdout);
});
6 changes: 2 additions & 4 deletions its/eslint9-plugin-sonarjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
"test": "node index.test.js"
},
"devDependencies": {
"@types/eslint": "^8.56.12",
"cross-spawn": "7.0.3",
"eslint": "8.57.0",
"eslint-plugin-import": "2.29.1",
"eslint": "^9.10.0",
"typescript": "5.5.4",
"typescript-eslint": "7.16.1"
"typescript-eslint": "^8.5.0"
}
}
4 changes: 3 additions & 1 deletion its/eslint9-plugin-sonarjs/tseslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ import tseslint from 'typescript-eslint';

console.log(`Loaded ${Object.keys(plugin.configs.recommended.rules ?? {}).length} rules`);

export default tseslint.config(plugin.configs.recommended);
export default tseslint.config(plugin.configs.recommended, {
rules: { 'sonarjs/accessor-pairs': 'error' },
});
17 changes: 16 additions & 1 deletion packages/jsts/src/rules/S128/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ export const rule: Rule.RuleModule = {
let currentCodePath: Rule.CodePath | null = null;
let currentCodeSegment: Rule.CodePathSegment | null = null;
let enteringSwitchCase = false;
let currentSegments: Set<Rule.CodePathSegment> = new Set();
const allCurrentSegments: Set<Rule.CodePathSegment>[] = [];
const segmentsWithExit: Set<string> = new Set();
const initialSegmentBySwitchCase: Map<estree.SwitchCase, Rule.CodePathSegment> = new Map();
const switchCaseStack: estree.SwitchCase[] = [];
Expand Down Expand Up @@ -66,11 +68,15 @@ export const rule: Rule.RuleModule = {
return {
onCodePathStart(codePath: Rule.CodePath) {
currentCodePath = codePath;
allCurrentSegments.push(currentSegments);
currentSegments = new Set();
},
onCodePathEnd() {
currentCodePath = currentCodePath!.upper;
currentSegments = allCurrentSegments.pop() as Set<Rule.CodePathSegment>;
},
onCodePathSegmentStart(segment: Rule.CodePathSegment) {
currentSegments.add(segment);
currentCodeSegment = segment;
if (enteringSwitchCase) {
initialSegmentBySwitchCase.set(
Expand All @@ -80,6 +86,15 @@ export const rule: Rule.RuleModule = {
enteringSwitchCase = false;
}
},
onCodePathSegmentEnd(segment: Rule.CodePathSegment) {
currentSegments.delete(segment);
},
onUnreachableCodePathSegmentStart(segment: Rule.CodePathSegment) {
currentSegments.add(segment);
},
onUnreachableCodePathSegmentEnd(segment: Rule.CodePathSegment) {
currentSegments.delete(segment);
},
CallExpression(node: estree.Node) {
const callExpr = node as estree.CallExpression;
if (isProcessExitCall(callExpr)) {
Expand All @@ -93,7 +108,7 @@ export const rule: Rule.RuleModule = {
'SwitchCase:exit'(node: estree.Node) {
const switchCase = node as estree.SwitchCase;
const initialSegment: Rule.CodePathSegment = initialSegmentBySwitchCase.get(switchCase)!;
const isReachable = currentCodePath!.currentSegments.some(
const isReachable = Array.from(currentSegments).some(
s => s.reachable && !isAfterProcessExitCall(s, initialSegment),
);
const { cases } = getParent(context, node) as estree.SwitchStatement;
Expand Down
2 changes: 1 addition & 1 deletion packages/jsts/src/rules/package-lock.json

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

0 comments on commit 6d6af68

Please sign in to comment.