From 5ccf629faf6707f27936faad9e2532550f8d6470 Mon Sep 17 00:00:00 2001 From: Igor Date: Mon, 3 Jun 2024 12:41:14 +0100 Subject: [PATCH] Make javascript linting task compatible with eslint 9+ --- .gitignore | 2 +- .../components/year-field-completion/index.js | 2 +- mtp_common/build_tasks/tasks.py | 2 +- .../mtp_common/build_tasks/eslint.config.cjs | 73 +++++++++++++++++++ .../mtp_common/build_tasks/eslintrc.json | 70 ------------------ .../mtp_common/build_tasks/package.json | 2 + package.json | 2 + 7 files changed, 80 insertions(+), 73 deletions(-) create mode 100644 mtp_common/templates/mtp_common/build_tasks/eslint.config.cjs delete mode 100644 mtp_common/templates/mtp_common/build_tasks/eslintrc.json diff --git a/.gitignore b/.gitignore index 8c0dfd11..9b600dce 100644 --- a/.gitignore +++ b/.gitignore @@ -17,7 +17,7 @@ npm-debug.log package-lock.json # app-specific -.eslintrc.json +/eslint.config.cjs .sass-lint.yml mtp_common/assets/ diff --git a/mtp_common/assets-src/mtp_common/components/year-field-completion/index.js b/mtp_common/assets-src/mtp_common/components/year-field-completion/index.js index aae2e821..9f746241 100644 --- a/mtp_common/assets-src/mtp_common/components/year-field-completion/index.js +++ b/mtp_common/assets-src/mtp_common/components/year-field-completion/index.js @@ -8,7 +8,7 @@ export var YearFieldCompletion = { init: function (selector) { $(selector || this.selector).blur(function () { var $input = $(this); - var currentYear = (new Date()).getFullYear(); + var currentYear = new Date().getFullYear(); var century = 100 * Math.floor(currentYear / 100); var eraBoundary = parseInt($input.data('era-boundary'), 10); if (isNaN(eraBoundary)) { diff --git a/mtp_common/build_tasks/tasks.py b/mtp_common/build_tasks/tasks.py index dee29435..6eea17b7 100644 --- a/mtp_common/build_tasks/tasks.py +++ b/mtp_common/build_tasks/tasks.py @@ -251,7 +251,7 @@ def lint_config(context: Context): """ Generates javasript and stylesheet linting configuration files """ - context.write_template('eslintrc.json', path='.eslintrc.json') + context.write_template('eslint.config.cjs') context.write_template('sass-lint.yml', path='.sass-lint.yml') diff --git a/mtp_common/templates/mtp_common/build_tasks/eslint.config.cjs b/mtp_common/templates/mtp_common/build_tasks/eslint.config.cjs new file mode 100644 index 00000000..0d834fb7 --- /dev/null +++ b/mtp_common/templates/mtp_common/build_tasks/eslint.config.cjs @@ -0,0 +1,73 @@ +var globals = require('globals'); +var eslintJs = require('@eslint/js'); + +module.exports = [ + eslintJs.configs.recommended, + { + languageOptions: { + sourceType: 'module', + globals: { + ...globals.commonjs, + ...globals.es2015, + ...globals.browser, + ...globals.jquery, + django: 'readonly', + Sentry: 'readonly', + }, + }, + rules: { + 'indent': ['error', 2, {SwitchCase: 1}], + 'complexity': ['error', 20], + 'eol-last': ['error'], + 'max-len': ['error', 120], + 'radix': ['error', 'always'], + 'semi': ['error'], + 'semi-spacing': ['error'], + 'semi-style': ['error', 'last'], + + 'array-bracket-spacing': ['warn'], + 'brace-style': ['warn', '1tbs'], + 'camelcase': ['warn'], + 'comma-spacing': ['warn'], + 'comma-style': ['warn'], + 'consistent-return': ['warn'], + 'curly': ['warn', 'all'], + 'eqeqeq': ['warn'], + 'func-call-spacing': ['warn'], + 'guard-for-in': ['warn'], + 'init-declarations': ['warn'], + 'key-spacing': ['warn'], + 'keyword-spacing': ['warn'], + 'linebreak-style': ['warn', 'unix'], + 'quotes': ['warn', 'single'], + 'no-eval': ['warn'], + 'no-extra-parens': ['warn'], + 'no-implicit-coercion': ['warn'], + 'no-implicit-globals': ['warn'], + 'no-implied-eval': ['warn'], + 'no-labels': ['warn'], + 'no-lone-blocks': ['warn'], + 'no-lonely-if': ['warn'], + 'no-multi-spaces': ['warn'], + 'no-multi-str': ['warn'], + 'no-return-assign': ['warn'], + 'no-self-compare': ['warn'], + 'no-trailing-spaces': ['warn'], + 'no-unsafe-negation': ['warn'], + 'no-unused-expressions': ['warn'], + 'no-use-before-define': ['warn'], + 'no-useless-call': ['warn'], + 'no-useless-concat': ['warn'], + 'no-useless-escape': ['warn'], + 'no-void': ['warn'], + 'no-whitespace-before-property': ['warn'], + 'no-with': ['warn'], + 'object-property-newline': ['warn'], + 'space-before-function-paren': ['warn'], + 'space-infix-ops': ['warn'], + 'space-unary-ops': ['warn'], + 'spaced-comment': ['warn'], + 'wrap-iife': ['warn'], + }, + }, +]; diff --git a/mtp_common/templates/mtp_common/build_tasks/eslintrc.json b/mtp_common/templates/mtp_common/build_tasks/eslintrc.json deleted file mode 100644 index bc401069..00000000 --- a/mtp_common/templates/mtp_common/build_tasks/eslintrc.json +++ /dev/null @@ -1,70 +0,0 @@ -{ - "env": { - "browser": true, - "commonjs": true, - "es6": true, - "jquery": true - }, - "parserOptions": { - "sourceType": "module" - }, - "extends": "eslint:recommended", - "globals": { - "django": true, - "Sentry": true - }, - "rules": { - "indent": ["error", 2, {"SwitchCase": 1}], - "complexity": ["error", 20], - "eol-last": ["error"], - "max-len": ["error", 120], - "radix": ["error", "always"], - "semi": ["error"], - "semi-spacing": ["error"], - "semi-style": ["error", "last"], - - "array-bracket-spacing": ["warn"], - "brace-style": ["warn", "1tbs"], - "camelcase": ["warn"], - "comma-spacing": ["warn"], - "comma-style": ["warn"], - "consistent-return": ["warn"], - "curly": ["warn", "all"], - "eqeqeq": ["warn"], - "func-call-spacing": ["warn"], - "guard-for-in": ["warn"], - "init-declarations": ["warn"], - "key-spacing": ["warn"], - "keyword-spacing": ["warn"], - "linebreak-style": ["warn", "unix"], - "quotes": ["warn", "single"], - "no-eval": ["warn"], - "no-extra-parens": ["warn"], - "no-implicit-coercion": ["warn"], - "no-implicit-globals": ["warn"], - "no-implied-eval": ["warn"], - "no-labels": ["warn"], - "no-lone-blocks": ["warn"], - "no-lonely-if": ["warn"], - "no-multi-spaces": ["warn"], - "no-multi-str": ["warn"], - "no-return-assign": ["warn"], - "no-self-compare": ["warn"], - "no-trailing-spaces": ["warn"], - "no-unsafe-negation": ["warn"], - "no-unused-expressions": ["warn"], - "no-use-before-define": ["warn"], - "no-useless-call": ["warn"], - "no-useless-concat": ["warn"], - "no-useless-escape": ["warn"], - "no-void": ["warn"], - "no-whitespace-before-property": ["warn"], - "no-with": ["warn"], - "object-property-newline": ["warn"], - "space-before-function-paren": ["warn"], - "space-infix-ops": ["warn"], - "space-unary-ops": ["warn"], - "spaced-comment": ["warn"], - "wrap-iife": ["warn"] - } -} diff --git a/mtp_common/templates/mtp_common/build_tasks/package.json b/mtp_common/templates/mtp_common/build_tasks/package.json index 56572cc4..0d8d14ab 100644 --- a/mtp_common/templates/mtp_common/build_tasks/package.json +++ b/mtp_common/templates/mtp_common/build_tasks/package.json @@ -16,7 +16,9 @@ "license": "MIT", "devDependencies": { "browser-sync": "~3.0", + "@eslint/js": "~9.4", "eslint": "~9.4", + "globals": "~15.3", "sass-lint": "~1.13.1" }, "dependencies": { diff --git a/package.json b/package.json index cb09d170..f1a66175 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,9 @@ }, "license": "MIT", "devDependencies": { + "@eslint/js": "~9.4", "eslint": "~9.4", + "globals": "~15.3", "sass-lint": "~1.13.1" } }