Skip to content

Commit

Permalink
feat: add bildFromOxlintConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
Sysix committed Oct 24, 2024
1 parent 0d71ecc commit f289660
Show file tree
Hide file tree
Showing 7 changed files with 282 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/__mocks__/oxlint-simple-category-active.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"categories": {
"correctness": "error"
}
}
5 changes: 5 additions & 0 deletions src/__mocks__/oxlint-simple-category-deactive.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"categories": {
"correctness": "off"
}
}
5 changes: 5 additions & 0 deletions src/__mocks__/oxlint-simple-rule-active.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"eqeqeq": "error"
}
}
5 changes: 5 additions & 0 deletions src/__mocks__/oxlint-simple-rule-deactive.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"eqeqeq": "off"
}
}
130 changes: 130 additions & 0 deletions src/__snapshots__/build-from-oxlint-config.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`detects active categories and append its rules > simpleCategoryActive 1`] = `
{
"@typescript-eslint/no-dupe-class-members": "off",
"@typescript-eslint/no-duplicate-enum-values": "off",
"@typescript-eslint/no-extra-non-null-assertion": "off",
"@typescript-eslint/no-loss-of-precision": "off",
"@typescript-eslint/no-misused-new": "off",
"@typescript-eslint/no-non-null-asserted-optional-chain": "off",
"@typescript-eslint/no-this-alias": "off",
"@typescript-eslint/no-unsafe-declaration-merging": "off",
"@typescript-eslint/triple-slash-reference": "off",
"import/default": "off",
"import/named": "off",
"import/namespace": "off",
"jest/expect-expect": "off",
"jest/no-conditional-expect": "off",
"jest/no-disabled-tests": "off",
"jest/no-export": "off",
"jest/no-standalone-expect": "off",
"jest/require-to-throw-message": "off",
"jest/valid-describe-callback": "off",
"jest/valid-expect": "off",
"jest/valid-title": "off",
"jsdoc/check-property-names": "off",
"jsdoc/check-tag-names": "off",
"jsdoc/implements-on-classes": "off",
"jsdoc/no-defaults": "off",
"jsdoc/require-property": "off",
"jsdoc/require-property-description": "off",
"jsdoc/require-property-name": "off",
"jsdoc/require-property-type": "off",
"jsdoc/require-yields": "off",
"jsx-a11y/alt-text": "off",
"jsx-a11y/anchor-is-valid": "off",
"jsx-a11y/aria-activedescendant-has-tabindex": "off",
"jsx-a11y/aria-role": "off",
"jsx-a11y/autocomplete-valid": "off",
"jsx-a11y/click-events-have-key-events": "off",
"jsx-a11y/heading-has-content": "off",
"jsx-a11y/html-has-lang": "off",
"jsx-a11y/iframe-has-title": "off",
"jsx-a11y/img-redundant-alt": "off",
"jsx-a11y/label-has-associated-control": "off",
"jsx-a11y/lang": "off",
"jsx-a11y/media-has-caption": "off",
"jsx-a11y/mouse-events-have-key-events": "off",
"jsx-a11y/no-access-key": "off",
"jsx-a11y/no-distracting-elements": "off",
"jsx-a11y/prefer-tag-over-role": "off",
"jsx-a11y/role-has-required-aria-props": "off",
"jsx-a11y/role-supports-aria-props": "off",
"nextjs/google-font-display": "off",
"nextjs/google-font-preconnect": "off",
"nextjs/inline-script-id": "off",
"nextjs/next-script-for-ga": "off",
"nextjs/no-assign-module-variable": "off",
"nextjs/no-async-client-component": "off",
"nextjs/no-before-interactive-script-outside-document": "off",
"nextjs/no-css-tags": "off",
"nextjs/no-document-import-in-page": "off",
"nextjs/no-duplicate-head": "off",
"nextjs/no-head-element": "off",
"nextjs/no-head-import-in-document": "off",
"nextjs/no-img-element": "off",
"nextjs/no-page-custom-font": "off",
"nextjs/no-script-component-in-head": "off",
"nextjs/no-styled-jsx-in-document": "off",
"nextjs/no-sync-scripts": "off",
"nextjs/no-title-in-document-head": "off",
"nextjs/no-unwanted-polyfillio": "off",
"no-async-promise-executor": "off",
"no-caller": "off",
"no-class-assign": "off",
"no-cond-assign": "off",
"no-const-assign": "off",
"no-constant-binary-expression": "off",
"no-constant-condition": "off",
"no-control-regex": "off",
"no-delete-var": "off",
"no-dupe-class-members": "off",
"no-dupe-else-if": "off",
"no-dupe-keys": "off",
"no-duplicate-case": "off",
"no-empty-character-class": "off",
"no-empty-pattern": "off",
"no-ex-assign": "off",
"no-func-assign": "off",
"no-global-assign": "off",
"no-import-assign": "off",
"no-invalid-regexp": "off",
"no-irregular-whitespace": "off",
"no-loss-of-precision": "off",
"no-new-native-nonconstructor": "off",
"no-obj-calls": "off",
"no-self-assign": "off",
"no-setter-return": "off",
"no-shadow-restricted-names": "off",
"no-sparse-arrays": "off",
"no-this-before-super": "off",
"no-unsafe-finally": "off",
"no-unsafe-optional-chaining": "off",
"no-unused-private-class-members": "off",
"no-useless-catch": "off",
"no-useless-rename": "off",
"no-with": "off",
"promise/no-callback-in-promise": "off",
"promise/valid-params": "off",
"react/jsx-key": "off",
"react/jsx-no-duplicate-props": "off",
"react/jsx-no-target-blank": "off",
"react/jsx-no-undef": "off",
"react/no-children-prop": "off",
"react/no-danger-with-children": "off",
"react/no-direct-mutation-state": "off",
"react/no-find-dom-node": "off",
"react/no-is-mounted": "off",
"react/no-render-return-value": "off",
"react/no-string-refs": "off",
"react/void-dom-elements-no-children": "off",
"require-yield": "off",
"unicorn/no-await-in-promise-methods": "off",
"unicorn/no-document-cookie": "off",
"unicorn/no-empty-file": "off",
"unicorn/no-invalid-remove-event-listener": "off",
"unicorn/no-thenable": "off",
"vitest/no-conditional-tests": "off",
}
`;
54 changes: 54 additions & 0 deletions src/build-from-oxlint-config.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { expect, it } from 'vitest';
import buildFromOxlintConfig from './build-from-oxlint-config.js';
import path from 'node:path';

it('detect active rules inside "rules" scope', () => {
const rules = buildFromOxlintConfig(
path.resolve(
import.meta.dirname,
'__mocks__',
'oxlint-simple-rule-active.json'
)
);

expect(rules).toStrictEqual({
eqeqeq: 'off',
});
});

it('skip deactive rules inside "rules" scope', () => {
const rules = buildFromOxlintConfig(
path.resolve(
import.meta.dirname,
'__mocks__',
'oxlint-simple-rule-deactive.json'
)
);

expect(rules).toStrictEqual({});
});

it('detects active categories and append its rules', () => {
const rules = buildFromOxlintConfig(
path.resolve(
import.meta.dirname,
'__mocks__',
'oxlint-simple-category-active.json'
)
);

// snapshot because it can change with the next release
expect(rules).toMatchSnapshot('simpleCategoryActive');
});

it('skip deactive categories ', () => {
const rules = buildFromOxlintConfig(
path.resolve(
import.meta.dirname,
'__mocks__',
'oxlint-simple-category-deactive.json'
)
);

expect(rules).toStrictEqual({});
});
78 changes: 78 additions & 0 deletions src/build-from-oxlint-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import fs from 'node:fs';
import configByCategory from './configs-by-category.js';

const getConfigContent = (
oxlintConfigFile: string
): Record<string, unknown> => {
try {
const buffer = fs.readFileSync(oxlintConfigFile, 'utf8');

try {
const configContent = JSON.parse(buffer);

return configContent;
} catch {
console.error(
`eslint-plugin-oxlint: could not parse oxlint config file: ${oxlintConfigFile}`
);
return {};
}
} catch {
console.error(
`eslint-plugin-oxlint: could not find oxlint config file: ${oxlintConfigFile}`
);
return {};
}
};

const appendCategoriesScope = (
categories: Record<string, unknown>,
rules: Record<string, 'off'>
): void => {
for (const category in categories) {
const configName = `flat/${category}`;

if (categories[category] !== 'off' && configName in configByCategory) {
// @ts-ignore -- come on TS, we are checking if the configName exists in the configByCategory
Object.assign(rules, configByCategory[configName].rules);
}
}
};

const appendRulesScope = (
oxlintRules: Record<string, unknown>,
rules: Record<string, 'off'>
): void => {
for (const rule in oxlintRules) {
// is this rules not turned off
if (oxlintRules[rule] !== 'off') {
rules[rule] = 'off';
}
}
};

export default function buildFromOxlintConfig(
oxlintConfigFile: string
): Record<string, 'off'> {
const config = getConfigContent(oxlintConfigFile);
const rules: Record<string, 'off'> = {};

if (
'categories' in config &&
typeof config.categories === 'object' &&
config.categories !== null
) {
appendCategoriesScope(config.categories as Record<string, unknown>, rules);
}

// is there a rules objects in the json file
if (
'rules' in config &&
typeof config.rules === 'object' &&
config.rules !== null
) {
appendRulesScope(config.rules as Record<string, unknown>, rules);
}

return rules;
}

0 comments on commit f289660

Please sign in to comment.