Skip to content

Commit

Permalink
JS-236 Add eslint-plugin-sonarjs compatibility with ESLint 9 (#4758)
Browse files Browse the repository at this point in the history
  • Loading branch information
vdiez authored Jul 23, 2024
1 parent 9ca7d86 commit 1f76567
Show file tree
Hide file tree
Showing 25 changed files with 815 additions and 429 deletions.
10 changes: 8 additions & 2 deletions .cirrus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ npmrc_script_definition: &NPMRC_SCRIPT_DEFINITION
npmrc_script:
- cp .cirrus/npmrc $CIRRUS_WORKING_DIR/.npmrc
- cp .cirrus/npmrc $CIRRUS_WORKING_DIR/packages/jsts/src/rules/.npmrc
- cp .cirrus/npmrc $CIRRUS_WORKING_DIR/its/eslint-plugin-sonarjs/.npmrc
- cp .cirrus/npmrc $CIRRUS_WORKING_DIR/its/eslint8-plugin-sonarjs/.npmrc
- cp .cirrus/npmrc $CIRRUS_WORKING_DIR/its/eslint9-plugin-sonarjs/.npmrc

win_ssd_and_clone:
&WIN_SSD_AND_CLONE # copy&paste from https://github.com/SonarSource/sonar-cpp/blob/a8c6f1e45a12393508682a013ac7ee35eb92bece/.cirrus.yml#L45
Expand Down Expand Up @@ -306,8 +307,13 @@ eslint_plugin_test_task:
- source set_maven_build_version $BUILD_NUMBER
- cd packages/jsts/src/rules
- npm run build
- cd ../../../../its/eslint-plugin-sonarjs
- cd ../../../../its/eslint8-plugin-sonarjs
- npm run build
- npx tsc --noEmit # check typings for tseslint.config.ts
- npm run test
- cd ../eslint9-plugin-sonarjs
- npm run build
- npx tsc --noEmit # check typings for tseslint.config.ts
- npm run test

css_ruling_task:
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,5 @@ Desktop.ini

# eslint-plugin-sonarjs
eslint-plugin-sonarjs-*.tgz
its/eslint-plugin-sonarjs/package-lock.json
its/eslint8-plugin-sonarjs/package-lock.json
its/eslint9-plugin-sonarjs/package-lock.json
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ test('should work with CommonJS config', async t => {
encoding: 'utf-8',
});
const output = result.stdout;
console.log(output);
const errorLines = output.split('\n').filter(line => line.includes('error'));
assert(errorLines.length > 4);
});
Expand All @@ -18,6 +19,18 @@ test('should work with ECMAScript modules config', async t => {
encoding: 'utf-8',
});
const output = result.stdout;
console.log(output);
const errorLines = output.split('\n').filter(line => line.includes('error'));
assert(errorLines.length > 4);
});

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);
});
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
{
"name": "eslint-plugin-sonarjs-tests",
"name": "eslint8-plugin-sonarjs-tests",
"description": "Test suite for eslint-plugin-sonarjs",
"scripts": {
"build": "npm install && cp ../../packages/jsts/src/rules/lib/eslint-plugin-sonarjs-* ./plugin.tgz && npm i ./plugin.tgz --no-save && rm -f plugin.tgz",
"test": "node index.test.js"
},
"devDependencies": {
"cross-spawn": "7.0.3",
"eslint": "8.57.0"
"eslint": "8.57.0",
"typescript": "5.5.4",
"typescript-eslint": "7.16.1"
}
}
9 changes: 9 additions & 0 deletions its/eslint8-plugin-sonarjs/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"compilerOptions": {
"module": "NodeNext",
"target": "es6",
"allowJs": true,
"moduleResolution": "NodeNext"
},
"files": ["tseslint.config.mjs"]
}
8 changes: 8 additions & 0 deletions its/eslint8-plugin-sonarjs/tseslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// @ts-check

import plugin from 'eslint-plugin-sonarjs';
import tseslint from 'typescript-eslint';

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

export default tseslint.config(plugin.configs.recommended);
12 changes: 12 additions & 0 deletions its/eslint9-plugin-sonarjs/eslint.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const plugin = require('eslint-plugin-sonarjs');

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

module.exports = [
{
files: ['./*.js'],
languageOptions: { sourceType: 'commonjs' },
},
plugin.configs.recommended,
{ rules: { 'sonarjs/accessor-pairs': 'error' } },
];
12 changes: 12 additions & 0 deletions its/eslint9-plugin-sonarjs/eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import plugin from 'eslint-plugin-sonarjs';

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

export default [
{
files: ['./*.js'],
languageOptions: { sourceType: 'commonjs' },
},
plugin.configs.recommended,
{ rules: { 'sonarjs/accessor-pairs': 'error' } },
];
43 changes: 43 additions & 0 deletions its/eslint9-plugin-sonarjs/file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
function S3776(foo) {
if (foo.bar) {
if (foo.baz) {
if (foo.qux) {
if (foo.fred) {
if (foo.thud) {
if (foo.abc) {
foo.bcd();
}
}
}
}
}
}
}

function S2703(foo) {
if (x == 0) {
x = 42;
}
}

function S2376() {
class C {
set m(a) {
this.a = a;
}
}
}

/*
function S125() {
class C {
set m(a) {
this.a = a;
}
}
}
*/

// if (something) {}
36 changes: 36 additions & 0 deletions its/eslint9-plugin-sonarjs/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const { test } = require('node:test');
const assert = require('node:assert');
const spawn = require('cross-spawn');

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);
});

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);
});

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);
});
14 changes: 14 additions & 0 deletions its/eslint9-plugin-sonarjs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "eslint9-plugin-sonarjs-tests",
"description": "Test suite for eslint-plugin-sonarjs",
"scripts": {
"build": "npm install && cp ../../packages/jsts/src/rules/lib/eslint-plugin-sonarjs-* ./plugin.tgz && npm i ./plugin.tgz --no-save && rm -f plugin.tgz",
"test": "node index.test.js"
},
"devDependencies": {
"cross-spawn": "7.0.3",
"eslint": "8.57.0",
"typescript": "5.5.4",
"typescript-eslint": "7.16.1"
}
}
9 changes: 9 additions & 0 deletions its/eslint9-plugin-sonarjs/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"compilerOptions": {
"module": "NodeNext",
"target": "es6",
"allowJs": true,
"moduleResolution": "NodeNext"
},
"files": ["tseslint.config.mjs"]
}
8 changes: 8 additions & 0 deletions its/eslint9-plugin-sonarjs/tseslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// @ts-check

import plugin from 'eslint-plugin-sonarjs';
import tseslint from 'typescript-eslint';

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

export default tseslint.config(plugin.configs.recommended);
42 changes: 21 additions & 21 deletions its/ruling/src/test/expected/jsts/ag-grid/typescript-S2933.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@
17
],
"ag-grid:src/ts/componentProvider.ts": [
56,
59,
62,
65,
69
57,
60,
63,
66,
70
],
"ag-grid:src/ts/context/beanStub.ts": [
10
Expand Down Expand Up @@ -118,16 +118,16 @@
20
],
"ag-grid:src/ts/filter/baseFilter.ts": [
68,
71,
74,
80,
230
69,
72,
75,
81,
231
],
"ag-grid:src/ts/filter/dateFilter.ts": [
28,
31,
34
29,
32,
35
],
"ag-grid:src/ts/filter/filterManager.ts": [
22,
Expand All @@ -144,18 +144,18 @@
33
],
"ag-grid:src/ts/filter/floatingFilter.ts": [
98
99
],
"ag-grid:src/ts/filter/floatingFilterWrapper.ts": [
25,
61
],
"ag-grid:src/ts/filter/numberFilter.ts": [
20,
26
21,
27
],
"ag-grid:src/ts/filter/textFilter.ts": [
20
21
],
"ag-grid:src/ts/focusedCellController.ts": [
16,
Expand Down Expand Up @@ -429,10 +429,10 @@
34
],
"ag-grid:src/ts/headerRendering/standardMenu.ts": [
14,
16,
18,
20
15,
17,
19,
21
],
"ag-grid:src/ts/layout/borderLayout.ts": [
5,
Expand Down
Loading

0 comments on commit 1f76567

Please sign in to comment.