Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support ignorePatterns in buildFromOxlintConfig(File) #270

Merged
merged 4 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/build-from-oxlint-config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,17 @@ describe('buildFromOxlintConfig', () => {
expect('unknown' in configs[0].rules!).toBe(false);
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']);
});
});
});

const createConfigFileAndBuildFromIt = (
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;
};
28 changes: 19 additions & 9 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 type { Linter } from 'eslint';
import JSONCParser from 'jsonc-parser';
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';

// default plugins, see <https://oxc.rs/docs/guide/usage/linter/config#plugins>
const defaultPlugins: OxlintConfigPlugins = ['react', 'unicorn', 'typescript'];
Expand Down Expand Up @@ -59,7 +63,7 @@ const getConfigContent = (
*/
export const buildFromOxlintConfig = (
config: OxlintConfig
): Linter.Config<Record<string, 'off'>>[] => {
): EslintPluginOxlintConfig[] => {
const rules: Record<string, 'off'> = {};
const plugins = readPluginsFromConfig(config) ?? defaultPlugins;

Expand All @@ -84,12 +88,18 @@ export const buildFromOxlintConfig = (
handleRulesScope(configRules, rules);
}

return [
{
name: 'oxlint/from-oxlint-config',
rules,
},
];
const baseConfig = {
name: 'oxlint/from-oxlint-config',
rules,
};

const ignorePatterns = readIgnorePatternsFromConfig(config);

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

return [baseConfig];
};

/**
Expand All @@ -101,7 +111,7 @@ export const buildFromOxlintConfig = (
*/
export const buildFromOxlintConfigFile = (
oxlintConfigFile: string
): Linter.Config<Record<string, 'off'>>[] => {
): EslintPluginOxlintConfig[] => {
const config = getConfigContent(oxlintConfigFile);

// we could not parse form the file, do not build with default values
Expand Down
7 changes: 7 additions & 0 deletions src/build-from-oxlint-config/types.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import type { Linter } from 'eslint';

export type OxlintConfigPlugins = string[];

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 OxlintConfig = {
[key: string]: unknown;
plugins?: OxlintConfigPlugins;
categories?: OxlintConfigCategories;
rules?: OxlintConfigRules;
ignorePatterns?: OxlintConfigIgnorePatterns;
};
Loading