From 0e2458a066d281294a7e534cbba9e02e98d88379 Mon Sep 17 00:00:00 2001 From: "Alexander S." Date: Fri, 8 Nov 2024 18:01:01 +0100 Subject: [PATCH] refactor: move duplicate constants between `scripts` and `src` to `src` (#237) --- scripts/constants.ts | 68 ++-------------------------- scripts/traverse-rules.ts | 6 ++- src/build-from-oxlint-config.spec.ts | 2 +- src/build-from-oxlint-config.ts | 26 ++--------- src/constants.ts | 66 +++++++++++++++++++++++++++ 5 files changed, 78 insertions(+), 90 deletions(-) create mode 100644 src/constants.ts diff --git a/scripts/constants.ts b/scripts/constants.ts index ad40951..edd3ea8 100644 --- a/scripts/constants.ts +++ b/scripts/constants.ts @@ -1,4 +1,5 @@ import path from 'node:path'; +import { aliasPluginNames } from '../src/constants.js'; const __dirname = new URL('.', import.meta.url).pathname; @@ -9,72 +10,9 @@ export const SPARSE_CLONE_DIRECTORY = 'crates/oxc_linter/src'; // these are the rules that don't have a direct equivalent in the eslint rules export const ignoreScope = new Set(['oxc', 'deepscan', 'security']); -// these are the mappings from the scope in the rules.rs to the eslint scope -// only used for the scopes where the directory structure doesn't reflect the eslint scope -// such as `typescript` vs `@typescript-eslint` or others. Eslint as a scope is an exception, -// as eslint doesn't have a scope. -// There is a duplicate in src/build-from-oxlint-config.ts, for clean builds we manage it in 2 files. -// In the future we can generate maybe this constant into src folder -export const scopeMaps = { - eslint: '', - typescript: '@typescript-eslint', - nextjs: '@next/next', -}; - -// Some typescript-eslint rules are re-implemented version of eslint rules. -// e.g. no-array-constructor, max-params, etc... -// Since oxlint supports these rules under eslint/* and it also supports TS, -// we should override these to make implementation status up-to-date. -export const typescriptRulesExtendEslintRules = [ - 'block-spacing', - 'brace-style', - 'class-methods-use-this', - 'comma-dangle', - 'comma-spacing', - 'default-param-last', - 'func-call-spacing', - 'indent', - 'init-declarations', - 'key-spacing', - 'keyword-spacing', - 'lines-around-comment', - 'lines-between-class-members', - 'max-params', - 'no-array-constructor', - 'no-dupe-class-members', - 'no-empty-function', - 'no-extra-parens', - 'no-extra-semi', - 'no-invalid-this', - 'no-loop-func', - 'no-loss-of-precision', - 'no-magic-numbers', - 'no-redeclare', - 'no-restricted-imports', - 'no-shadow', - 'no-unused-expressions', - 'no-unused-vars', - 'no-use-before-define', - 'no-useless-constructor', - 'object-curly-spacing', - 'padding-line-between-statements', - 'quotes', - 'semi', - 'space-before-blocks', - 'space-before-function-paren', - 'space-infix-ops', -]; - -// All rules from `eslint-plugin-react-hooks` -// Since oxlint supports these rules under react/*, we need to remap them. -export const reactHookRulesInsideReactScope = [ - 'rules-of-hooks', - 'exhaustive-deps', -]; - export function convertScope(scope: string) { - return Reflect.has(scopeMaps, scope) - ? scopeMaps[scope as 'eslint'] + return Reflect.has(aliasPluginNames, scope) + ? aliasPluginNames[scope as 'eslint'] : scope.replace('_', '-'); } diff --git a/scripts/traverse-rules.ts b/scripts/traverse-rules.ts index 1ada495..9e2f633 100644 --- a/scripts/traverse-rules.ts +++ b/scripts/traverse-rules.ts @@ -3,11 +3,13 @@ import path from 'node:path'; import { ignoreScope, prefixScope, - reactHookRulesInsideReactScope, SPARSE_CLONE_DIRECTORY, TARGET_DIRECTORY, - typescriptRulesExtendEslintRules, } from './constants.js'; +import { + reactHookRulesInsideReactScope, + typescriptRulesExtendEslintRules, +} from '../src/constants.js'; // Recursive function to read files in a directory, this currently assumes that the directory // structure is semi-consistent within the oxc_linter crate diff --git a/src/build-from-oxlint-config.spec.ts b/src/build-from-oxlint-config.spec.ts index f7918d6..3c97cb3 100644 --- a/src/build-from-oxlint-config.spec.ts +++ b/src/build-from-oxlint-config.spec.ts @@ -6,7 +6,7 @@ import { import fs from 'node:fs'; import { execSync } from 'node:child_process'; import type { Linter } from 'eslint'; -import { typescriptRulesExtendEslintRules } from '../scripts/constants.js'; +import { typescriptRulesExtendEslintRules } from './constants.js'; describe('buildFromOxlintConfig', () => { describe('rule values', () => { diff --git a/src/build-from-oxlint-config.ts b/src/build-from-oxlint-config.ts index 7c45ea1..5d00f85 100644 --- a/src/build-from-oxlint-config.ts +++ b/src/build-from-oxlint-config.ts @@ -2,28 +2,10 @@ import fs from 'node:fs'; import configByCategory from './generated/configs-by-category.js'; import type { Linter } from 'eslint'; import JSONCParser from 'jsonc-parser'; - -// these are the mappings from the scope in the rules.rs to the eslint scope -// only used for the scopes where the directory structure doesn't reflect the eslint scope -// such as `typescript` vs `@typescript-eslint` or others. Eslint as a scope is an exception, -// as eslint doesn't have a scope. -// look here: -const aliasPluginNames: Record = { - eslint: '', - typescript: '@typescript-eslint', - nextjs: '@next/next', - - // only in build-config - react_perf: 'react-perf', - jsx_a11y: 'jsx-a11y', -}; - -// All rules from `eslint-plugin-react-hooks` -// Since oxlint supports these rules under react/*, we need to remap them. -export const reactHookRulesInsideReactScope = [ - 'rules-of-hooks', - 'exhaustive-deps', -]; +import { + aliasPluginNames, + reactHookRulesInsideReactScope, +} from './constants.js'; const allRulesObjects = Object.values(configByCategory).map( (config) => config.rules diff --git a/src/constants.ts b/src/constants.ts new file mode 100644 index 0000000..d96d059 --- /dev/null +++ b/src/constants.ts @@ -0,0 +1,66 @@ +// these are the mappings from the scope in the rules.rs to the eslint scope +// only used for the scopes where the directory structure doesn't reflect the eslint scope +// such as `typescript` vs `@typescript-eslint` or others. Eslint as a scope is an exception, +// as eslint doesn't have a scope. +// look here: +export const aliasPluginNames: Record = { + // for scripts/generate and src/build-from-oxlint-config + eslint: '', + typescript: '@typescript-eslint', + nextjs: '@next/next', + + // only for src/build-from-oxlint-config + react_perf: 'react-perf', + jsx_a11y: 'jsx-a11y', +}; + +// Some typescript-eslint rules are re-implemented version of eslint rules. +// e.g. no-array-constructor, max-params, etc... +// Since oxlint supports these rules under eslint/* and it also supports TS, +// we should override these to make implementation status up-to-date. +export const typescriptRulesExtendEslintRules = [ + 'block-spacing', + 'brace-style', + 'class-methods-use-this', + 'comma-dangle', + 'comma-spacing', + 'default-param-last', + 'func-call-spacing', + 'indent', + 'init-declarations', + 'key-spacing', + 'keyword-spacing', + 'lines-around-comment', + 'lines-between-class-members', + 'max-params', + 'no-array-constructor', + 'no-dupe-class-members', + 'no-empty-function', + 'no-extra-parens', + 'no-extra-semi', + 'no-invalid-this', + 'no-loop-func', + 'no-loss-of-precision', + 'no-magic-numbers', + 'no-redeclare', + 'no-restricted-imports', + 'no-shadow', + 'no-unused-expressions', + 'no-unused-vars', + 'no-use-before-define', + 'no-useless-constructor', + 'object-curly-spacing', + 'padding-line-between-statements', + 'quotes', + 'semi', + 'space-before-blocks', + 'space-before-function-paren', + 'space-infix-ops', +]; + +// All rules from `eslint-plugin-react-hooks` +// Since oxlint supports these rules under react/*, we need to remap them. +export const reactHookRulesInsideReactScope = [ + 'rules-of-hooks', + 'exhaustive-deps', +];