Skip to content

Commit

Permalink
refactor: move duplicate constants between scripts and src to `sr…
Browse files Browse the repository at this point in the history
…c` (#237)
  • Loading branch information
Sysix authored Nov 8, 2024
1 parent 27960a3 commit 0e2458a
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 90 deletions.
68 changes: 3 additions & 65 deletions scripts/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import path from 'node:path';
import { aliasPluginNames } from '../src/constants.js';

const __dirname = new URL('.', import.meta.url).pathname;

Expand All @@ -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('_', '-');
}

Expand Down
6 changes: 4 additions & 2 deletions scripts/traverse-rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/build-from-oxlint-config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
26 changes: 4 additions & 22 deletions src/build-from-oxlint-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: <https://github.com/oxc-project/oxc/blob/0b329516372a0353e9eb18e5bc0fbe63bce21fee/crates/oxc_linter/src/config/rules.rs#L285>
const aliasPluginNames: Record<string, string> = {
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
Expand Down
66 changes: 66 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -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: <https://github.com/oxc-project/oxc/blob/0b329516372a0353e9eb18e5bc0fbe63bce21fee/crates/oxc_linter/src/config/rules.rs#L285>
export const aliasPluginNames: Record<string, string> = {
// 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',
];

0 comments on commit 0e2458a

Please sign in to comment.