Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JS-236 Add eslint-plugin-sonarjs compatibility with ESLint 9 #4758

Merged
merged 16 commits into from
Jul 23, 2024
Merged
12 changes: 10 additions & 2 deletions .cirrus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@
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,15 @@
- 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 eslint file.js || true # to check output in CI
vdiez marked this conversation as resolved.
Show resolved Hide resolved
- npx tsc --noEmit # check typings for tseslint.config.ts
- npm run test
vdiez marked this conversation as resolved.
Show resolved Hide resolved
- cd ../eslint9-plugin-sonarjs
- npm run build
- npx eslint file.js || true # to check output in CI
- npx tsc --noEmit # check typings for tseslint.config.ts
- npm run test

css_ruling_task:
Expand Down Expand Up @@ -359,7 +367,7 @@
path: 'its/perf/target/perf.txt'
cleanup_before_cache_script: cleanup_maven_repository

promote_task:

Check warning on line 370 in .cirrus.yml

View check run for this annotation

Cirrus CI / Build Parsing Results

.cirrus.yml#L370

task "promote" depends on task "ws_scan", but their only_if conditions are different

Check warning on line 370 in .cirrus.yml

View check run for this annotation

Cirrus CI / Build Parsing Results

.cirrus.yml#L370

task "promote" depends on task "ws_scan", but their only_if conditions are different
depends_on:
- ws_scan
- build_win
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.
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,13 @@ test('should work with ECMAScript modules config', async t => {
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;
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
vdiez marked this conversation as resolved.
Show resolved Hide resolved

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) {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should you clean this up or is it part of S125? same for original file

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why use this one?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needed to call npx from spawn in Windows


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;
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;
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;
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
Loading