Skip to content

Commit

Permalink
Merge branch 'main' into feat-support-overrides-in-buildfromoxlintconfig
Browse files Browse the repository at this point in the history
  • Loading branch information
Sysix committed Dec 6, 2024
2 parents 6ee28c3 + 9bf4277 commit 0b57eb9
Show file tree
Hide file tree
Showing 11 changed files with 142 additions and 55 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "eslint-plugin-oxlint",
"version": "0.14.0",
"version": "0.14.1",
"description": "Turn off all rules already supported by oxlint",
"type": "module",
"types": "./dist/index.d.ts",
Expand Down Expand Up @@ -66,7 +66,7 @@
"husky": "^9.1.6",
"lint-staged": "^15.2.10",
"memfs": "^4.14.0",
"oxlint": "^0.14.0",
"oxlint": "^0.14.1",
"prettier": "^3.3.3",
"scule": "^1.3.0",
"shelljs": "^0.8.5",
Expand Down
74 changes: 37 additions & 37 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions src/__snapshots__/configs.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,15 @@ exports[`contains all the oxlint rules 1`] = `
"disallowArithmeticOperators": false,
},
],
"no-unused-expressions": [
0,
{
"allowShortCircuit": false,
"allowTaggedTemplates": false,
"allowTernary": false,
"enforceForJSX": false,
},
],
"no-unused-labels": [
0,
],
Expand Down Expand Up @@ -979,6 +988,9 @@ exports[`contains all the oxlint rules 1`] = `
0,
"always",
],
"react-hooks/rules-of-hooks": [
0,
],
"react-perf/jsx-no-jsx-as-prop": [
0,
],
Expand Down Expand Up @@ -1433,5 +1445,13 @@ exports[`contains all the oxlint rules 1`] = `
"vitest/require-local-test-context-for-concurrent-snapshots": [
0,
],
"yoda": [
0,
"never",
{
"exceptRange": false,
"onlyEquality": false,
},
],
}
`;
12 changes: 12 additions & 0 deletions src/build-from-oxlint-config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,17 @@ describe('buildFromOxlintConfig', () => {
expect('@next/next/no-img-element' in configs[0].rules!).toBe(false);
});

describe('ignorePattern Property', () => {
it('should append ignorePatterns to eslint v9 ignore property', () => {
const configs = buildFromOxlintConfig({
ignorePatterns: ['./tests/.*ts'],
});

expect(configs.length).toBe(1);
expect(configs[0].ignores).toStrictEqual(['./tests/.*ts']);
});
});

describe('overrides', () => {
it('supports simple files + rules overrides', () => {
const configs = buildFromOxlintConfig({
Expand Down Expand Up @@ -260,6 +271,7 @@ describe('buildFromOxlintConfig', () => {
});
});
});

const createConfigFileAndBuildFromIt = (
filename: string,
content: string
Expand Down
26 changes: 26 additions & 0 deletions src/build-from-oxlint-config/ignore-patterns.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import {
EslintPluginOxlintConfig,
OxlintConfig,
OxlintConfigIgnorePatterns,
} from './types.js';

/**
* adds the ignore patterns to the config
*/
export const handleIgnorePatternsScope = (
ignorePatterns: OxlintConfigIgnorePatterns,
config: EslintPluginOxlintConfig
): void => {
config.ignores = ignorePatterns;
};
/**
* tries to return the "ignorePattern" section from the config.
* it returns `undefined` when not found or invalid.
*/
export const readIgnorePatternsFromConfig = (
config: OxlintConfig
): OxlintConfigIgnorePatterns | undefined => {
return 'ignorePatterns' in config && Array.isArray(config.ignorePatterns)
? (config.ignorePatterns as OxlintConfigIgnorePatterns)
: undefined;
};
32 changes: 21 additions & 11 deletions src/build-from-oxlint-config/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs from 'node:fs';
import JSONCParser from 'jsonc-parser';
import type {
EslintPluginOxLintConfig,
import {
EslintPluginOxlintConfig,
OxlintConfig,
OxlintConfigCategories,
OxlintConfigPlugins,
Expand All @@ -13,6 +13,10 @@ import {
readCategoriesFromConfig,
} from './categories.js';
import { readPluginsFromConfig } from './plugins.js';
import {
handleIgnorePatternsScope,
readIgnorePatternsFromConfig,
} from './ignore-patterns.js';
import { handleOverridesScope, readOverridesFromConfig } from './overrides.js';

// default plugins, see <https://oxc.rs/docs/guide/usage/linter/config#plugins>
Expand Down Expand Up @@ -60,7 +64,7 @@ const getConfigContent = (
*/
export const buildFromOxlintConfig = (
config: OxlintConfig
): EslintPluginOxLintConfig[] => {
): EslintPluginOxlintConfig[] => {
const rules: Record<string, 'off'> = {};
const plugins = readPluginsFromConfig(config) ?? defaultPlugins;
const categories = readCategoriesFromConfig(config) ?? defaultCategories;
Expand All @@ -86,13 +90,19 @@ export const buildFromOxlintConfig = (
handleRulesScope(configRules, rules);
}

const overrides = readOverridesFromConfig(config);
const configs: EslintPluginOxLintConfig[] = [
{
name: 'oxlint/from-oxlint-config',
rules,
},
];
const baseConfig = {
name: 'oxlint/from-oxlint-config',
rules,
};

const ignorePatterns = readIgnorePatternsFromConfig(baseConfig);

if (ignorePatterns !== undefined) {
handleIgnorePatternsScope(ignorePatterns, baseConfig);
}

const configs = [baseConfig];
const overrides = readOverridesFromConfig(baseConfig);

if (overrides !== undefined) {
handleOverridesScope(overrides, configs, categories);
Expand All @@ -110,7 +120,7 @@ export const buildFromOxlintConfig = (
*/
export const buildFromOxlintConfigFile = (
oxlintConfigFile: string
): EslintPluginOxLintConfig[] => {
): EslintPluginOxlintConfig[] => {
const config = getConfigContent(oxlintConfigFile);

// we could not parse form the file, do not build with default values
Expand Down
6 changes: 3 additions & 3 deletions src/build-from-oxlint-config/overrides.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ import { handleCategoriesScope } from './categories.js';
import { readPluginsFromConfig } from './plugins.js';
import { handleRulesScope, readRulesFromConfig } from './rules.js';
import {
EslintPluginOxLintConfig,
EslintPluginOxlintConfig,
OxlintConfig,
OxlintConfigCategories,
OxlintConfigOverride,
} from './types.js';

export const handleOverridesScope = (
overrides: OxlintConfigOverride[],
configs: EslintPluginOxLintConfig[],
configs: EslintPluginOxlintConfig[],
baseCategories?: OxlintConfigCategories
): void => {
for (const overrideIndex in overrides) {
const override = overrides[overrideIndex];
const eslintRules: Record<string, 'off'> = {};
const eslintConfig: EslintPluginOxLintConfig = {
const eslintConfig: EslintPluginOxlintConfig = {
name: `oxlint/from-oxlint-config-override-${overrideIndex}`,
};

Expand Down
7 changes: 5 additions & 2 deletions src/build-from-oxlint-config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,20 @@ export type OxlintConfigCategories = Record<string, unknown>;

export type OxlintConfigRules = Record<string, unknown>;

export type OxlintConfigIgnorePatterns = string[];

export type EslintPluginOxlintConfig = Linter.Config<Record<string, 'off'>>;

export type OxlintConfigOverride = {
files: string[];
plugins?: OxlintConfigPlugins;
rules?: OxlintConfigRules;
};

export type EslintPluginOxLintConfig = Linter.Config<Record<string, 'off'>>;

export type OxlintConfig = {
[key: string]: unknown;
plugins?: OxlintConfigPlugins;
categories?: OxlintConfigCategories;
rules?: OxlintConfigRules;
ignorePatterns?: OxlintConfigIgnorePatterns;
};
6 changes: 6 additions & 0 deletions src/generated/configs-by-scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ const reactConfig = {
rules: rules.reactRules,
};

const reactHooksConfig = {
name: 'oxlint/react-hooks',
rules: rules.reactHooksRules,
};

const reactPerfConfig = {
name: 'oxlint/react-perf',
rules: rules.reactPerfRules,
Expand Down Expand Up @@ -77,6 +82,7 @@ const configByScope = {
'flat/node': nodeConfig,
'flat/promise': promiseConfig,
'flat/react': reactConfig,
'flat/react-hooks': reactHooksConfig,
'flat/react-perf': reactPerfConfig,
'flat/typescript': typescriptConfig,
'flat/unicorn': unicornConfig,
Expand Down
Loading

0 comments on commit 0b57eb9

Please sign in to comment.