Skip to content

Commit

Permalink
feat: support ignorePatterns in buildFromOxlintConfig(File)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sysix committed Nov 29, 2024
1 parent f877bfe commit 1effdfe
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 6 deletions.
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;
};
22 changes: 16 additions & 6 deletions src/build-from-oxlint-config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 @@ -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 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;
};

0 comments on commit 1effdfe

Please sign in to comment.