From b1b3df5a31d18dd5e6ec3d6cf36fb88a8b13ca7c Mon Sep 17 00:00:00 2001 From: Sysix Date: Wed, 30 Oct 2024 20:26:13 +0100 Subject: [PATCH] feat: add buildFromOxlintConfig --- package.json | 3 +++ pnpm-lock.yaml | 4 ++++ src/build-from-oxlint-config.spec.ts | 13 ++++++------- src/build-from-oxlint-config.ts | 5 +++-- 4 files changed, 16 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index 4b55784..fe03430 100644 --- a/package.json +++ b/package.json @@ -82,5 +82,8 @@ }, "volta": { "node": "20.14.0" + }, + "dependencies": { + "jsonc-parser": "^3.3.1" } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 5859e25..f225d41 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -7,6 +7,10 @@ settings: importers: .: + dependencies: + jsonc-parser: + specifier: ^3.3.1 + version: 3.3.1 devDependencies: '@eslint/js': specifier: ^9.13.0 diff --git a/src/build-from-oxlint-config.spec.ts b/src/build-from-oxlint-config.spec.ts index 0161d18..b77c784 100644 --- a/src/build-from-oxlint-config.spec.ts +++ b/src/build-from-oxlint-config.spec.ts @@ -130,14 +130,15 @@ const createConfigFileAndBuildFromIt = ( }; describe('buildFromOxlintConfigFile', () => { - it('successfully parse oxlint config', () => { + it('successfully parse oxlint json config', () => { const rules = createConfigFileAndBuildFromIt( 'success-config.json', - JSON.stringify({ - rules: { - 'no-await-loop': 'error', + `{ + "rules": { + // hello world + "no-await-loop": "error", }, - }) + }` ); expect(rules.length).toBe(1); @@ -317,8 +318,6 @@ describe('integration test with oxlint', () => { expect(eslintRules.length).toBe(1); expect(eslintRules[0].rules).not.toBeUndefined(); - console.log(eslintRules[0].rules!); - let expectedCount = oxlintRulesCount ?? 0; // special mapping for ts alias rules diff --git a/src/build-from-oxlint-config.ts b/src/build-from-oxlint-config.ts index 99264d0..0f06d8c 100644 --- a/src/build-from-oxlint-config.ts +++ b/src/build-from-oxlint-config.ts @@ -1,6 +1,7 @@ import fs from 'node:fs'; import configByCategory from './configs-by-category.js'; import type { Linter } from 'eslint'; +import JSONCParser from 'jsonc-parser'; // these are the mappings from the scope in the rules.rs to the eslint scope // only used for the scopes where the directory structure doesn't reflect the eslint scope @@ -41,10 +42,10 @@ const getConfigContent = ( oxlintConfigFile: string ): OxlintConfig | undefined => { try { - const buffer = fs.readFileSync(oxlintConfigFile, 'utf8'); + const content = fs.readFileSync(oxlintConfigFile, 'utf8'); try { - const configContent = JSON.parse(buffer); + const configContent = JSONCParser.parse(content); if (typeof configContent !== 'object' || Array.isArray(configContent)) { throw new TypeError('not an valid config file');