Skip to content

Commit

Permalink
refactor: split buildFromOxlintConfig into multiple files
Browse files Browse the repository at this point in the history
  • Loading branch information
Sysix committed Nov 29, 2024
1 parent 20c61d1 commit 023e6f7
Show file tree
Hide file tree
Showing 9 changed files with 315 additions and 294 deletions.
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 @@ -2,7 +2,7 @@ import { describe, expect, it } from 'vitest';
import {
buildFromOxlintConfig,
buildFromOxlintConfigFile,
} from './build-from-oxlint-config.js';
} from './build-from-oxlint-config/index.js';
import fs from 'node:fs';
import { execSync } from 'node:child_process';
import type { Linter } from 'eslint';
Expand Down
292 changes: 0 additions & 292 deletions src/build-from-oxlint-config.ts

This file was deleted.

57 changes: 57 additions & 0 deletions src/build-from-oxlint-config/categories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { aliasPluginNames } from '../constants.js';
import configByCategory from '../generated/configs-by-category.js';
import {
OxlintConfig,
OxlintConfigCategories,
OxlintConfigPlugins,
} from './types.js';
import { isObject } from './utils.js';

/**
* appends all rules which are enabled by a plugin and falls into a specific category
*/
export const handleCategoriesScope = (
plugins: OxlintConfigPlugins,
categories: OxlintConfigCategories,
rules: Record<string, 'off'>
): void => {
for (const category in categories) {
const configName = `flat/${category}`;

// category is not enabled or not in found categories
if (categories[category] === 'off' || !(configName in configByCategory)) {
continue;
}

// @ts-expect-error -- come on TS, we are checking if the configName exists in the configByCategory
const possibleRules = configByCategory[configName].rules;

// iterate to each rule to check if the rule can be appended, because the plugin is activated
for (const rule of Object.keys(possibleRules)) {
for (const plugin of plugins) {
const pluginPrefix =
plugin in aliasPluginNames ? aliasPluginNames[plugin] : plugin;

// the rule has no prefix, so it is a eslint one
if (pluginPrefix === '' && !rule.includes('/')) {
rules[rule] = 'off';
// other rules with a prefix like @typescript-eslint/
} else if (rule.startsWith(`${pluginPrefix}/`)) {
rules[rule] = 'off';
}
}
}
}
};

/**
* tries to return the "categories" section from the config.
* it returns `undefined` when not found or invalid.
*/
export const readCategoriesFromConfig = (
config: OxlintConfig
): OxlintConfigCategories | undefined => {
return 'categories' in config && isObject(config.categories)
? (config.categories as OxlintConfigCategories)
: undefined;
};
Loading

0 comments on commit 023e6f7

Please sign in to comment.