From 956a97ce24e4b908bb6d44a03e47eee61f38c6d1 Mon Sep 17 00:00:00 2001 From: Mehdy Dara Date: Tue, 5 Mar 2024 14:35:56 +0100 Subject: [PATCH] Tweak * Add eslint-plugin-import * Reactivate rule promise/no-return-wrap * Use override for TS files * Remove override for test files in TS * Require node 18 * Add CI with Github * Fix test * Bump deps --- .github/workflows/main.yml | 22 ++++++++++++++++++++ .travis.yml | 6 ------ index.js | 41 ++++++++++++++++++++++---------------- package.json | 25 ++++++++++++----------- test/_x.ts | 0 test/test.js | 26 +++++++++++++----------- tsconfig.json | 3 +++ 7 files changed, 76 insertions(+), 47 deletions(-) create mode 100644 .github/workflows/main.yml delete mode 100644 .travis.yml create mode 100644 test/_x.ts diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..a023591 --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,22 @@ +name: CI +on: + - push + - pull_request +jobs: + test: + name: Node.js ${{ matrix.node-version }} + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + node-version: + - 20 + - 18 + - 16 + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version: ${{ matrix.node-version }} + - run: npm install + - run: npm test diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 2b0624f..0000000 --- a/.travis.yml +++ /dev/null @@ -1,6 +0,0 @@ -language: node_js - -node_js: - - '12' - - '10' - - '8' diff --git a/index.js b/index.js index f0172ff..04cef35 100644 --- a/index.js +++ b/index.js @@ -10,28 +10,35 @@ ruleNamingConventionOverride[1].format = [ ]; module.exports = { - plugins: ['promise'], - extends: ['xo', 'xo-typescript/space', 'plugin:promise/recommended'], - rules: { - 'capitalized-comments': 'off', - 'promise/no-return-wrap': 'off', - 'no-console': 'error', - // Override naming convention rule to allow `snake_case`. - '@typescript-eslint/naming-convention': ruleNamingConventionOverride, - // Override this rule to allow usage of null and undefined. - '@typescript-eslint/ban-types': ruleBanTypeOverride, - // Disable this rule because we need interface and type. - '@typescript-eslint/consistent-type-definitions': 'off', - }, overrides: [ { files: [ - 'test/**/*.ts', + '*.ts', ], + plugins: ['promise', 'import'], + extends: ['xo', 'xo-typescript/space', 'plugin:promise/recommended'], rules: { - '@typescript-eslint/no-unsafe-call': 'off', - '@typescript-eslint/no-unsafe-member-access': 'off', - '@typescript-eslint/no-unsafe-assignment': 'off', + 'capitalized-comments': 'off', + 'no-console': 'error', + // Override naming convention rule to allow `snake_case`. + '@typescript-eslint/naming-convention': ruleNamingConventionOverride, + // Override this rule to allow usage of null and undefined. + '@typescript-eslint/ban-types': ruleBanTypeOverride, + // Disable this rule because we need interface and type. + '@typescript-eslint/consistent-type-definitions': 'off', + 'import/extensions': [ + 'error', + 'ignorePackages', + ], + 'import/no-duplicates': 'error', + 'import/order': [ + 'error', + { + alphabetize: { + order: 'asc', + }, + }, + ], }, }, ], diff --git a/package.json b/package.json index 6279d89..97359e5 100644 --- a/package.json +++ b/package.json @@ -7,10 +7,10 @@ "author": { "name": "Radio France", "email": "opensource@radiofrance.com", - "url": "www.radiofrance.fr" + "url": "https://www.radiofrance.fr" }, "engines": { - "node": ">=8" + "node": ">=18" }, "scripts": { "lint": "eslint .", @@ -46,22 +46,23 @@ "simple" ], "dependencies": { - "@typescript-eslint/eslint-plugin": "^6.9.0", - "@typescript-eslint/parser": "^6.9.0", - "eslint-config-xo": "^0.43.1", - "eslint-config-xo-typescript": "^1.0.1", + "@typescript-eslint/eslint-plugin": ">=7.0.2", + "@typescript-eslint/parser": ">=7.0.2", + "eslint-config-xo": "^0.44.0", + "eslint-config-xo-typescript": "^3.0.0", + "eslint-plugin-import": "^2.29.1", "eslint-plugin-promise": "^6.0.0", - "typescript": ">=5.0.2" + "typescript": ">=5.0.0" }, "devDependencies": { - "ava": "^3.7.1", + "ava": "^6.1.2", "eslint": "^8.52.0" }, "peerDependencies": { - "@typescript-eslint/eslint-plugin": ">=6.0.0", - "@typescript-eslint/parser": ">=6.0.0", - "eslint": ">=8.0.0", - "typescript": ">=5.0.2" + "@typescript-eslint/eslint-plugin": ">=7.0.2", + "@typescript-eslint/parser": ">=7.0.2", + "eslint": ">=8.56.0", + "typescript": ">=5.0.0" }, "eslintConfig": { "extends": "xo", diff --git a/test/_x.ts b/test/_x.ts new file mode 100644 index 0000000..e69de29 diff --git a/test/test.js b/test/test.js index 21aeafe..15368b4 100644 --- a/test/test.js +++ b/test/test.js @@ -1,27 +1,29 @@ -const path = require('path'); const test = require('ava'); -const eslint = require('eslint'); +const {ESLint} = require('eslint'); -const config = '../index.js'; +const config = require('../index.js'); const hasRule = (errors, ruleId) => errors.some(x => x.ruleId === ruleId); -function runEslint(string, config) { - const linter = new eslint.CLIEngine({ +async function runEslint(string, config) { + const eslint = new ESLint({ useEslintrc: false, - configFile: path.join(__dirname, config), + overrideConfig: config, }); - return linter.executeOnText(string, path.join(__dirname, '../_x.ts')).results[0].messages; + const [firstResult] = await eslint.lintText(string, {filePath: 'test/_x.ts'}); + + return firstResult.messages; } -// Cant be fixed due https://github.com/typescript-eslint/typescript-eslint/issues/885 -test.failing('main', t => { - const errors = runEslint('const foo: number = 5;', config); +test('should throw error no-inferrable-types', async t => { + const errors = await runEslint('const foo: number = 5;\n', config); t.true(hasRule(errors, '@typescript-eslint/no-inferrable-types'), JSON.stringify(errors)); + t.is(errors.length, 1); }); -test.failing('main error no-console', t => { - const errors = runEslint('\'use strict\';\nconst x = true;\n\nif (x) {\n console.log();\n}\n', config); +test('should throw error no-console', async t => { + const errors = await runEslint('\'use strict\';\nconst x = true;\n\nif (x) {\n console.log();\n}\n', config); t.true(hasRule(errors, 'no-console'), JSON.stringify(errors)); + t.is(errors.length, 1); }); diff --git a/tsconfig.json b/tsconfig.json index 1bbe77a..495c881 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,4 +1,7 @@ { + "compilerOptions": { + "strict": true, + }, "include": [ "test" ]