Skip to content

Commit

Permalink
fix: support jsonc in extended config (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
privatenumber authored Aug 12, 2022
1 parent 8d81d8a commit 2ca792b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
11 changes: 2 additions & 9 deletions src/parse-tsconfig/resolve-extends.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,10 @@ import path from 'path';
import fs from 'fs';
import Module from 'module';
import { findUp } from '../utils/find-up';
import { readJsonc } from '../utils/read-jsonc';

const { existsSync } = fs;

const safeJsonParse = (jsonString: string) => {
try {
return JSON.parse(jsonString);
} catch {}
};

const getPnpApi = () => {
const { findPnpApi } = Module;

Expand All @@ -19,9 +14,7 @@ const getPnpApi = () => {
};

function resolveFromPackageJsonPath(packageJsonPath: string) {
const packageJson = safeJsonParse(
fs.readFileSync(packageJsonPath, 'utf8'),
);
const packageJson = readJsonc(packageJsonPath);

return path.join(
packageJsonPath,
Expand Down
6 changes: 6 additions & 0 deletions src/utils/read-jsonc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import fs from 'fs';
import { parse } from 'jsonc-parser';

export const readJsonc = (
jsonPath: string,
) => parse(fs.readFileSync(jsonPath, 'utf8'));
23 changes: 23 additions & 0 deletions tests/specs/parse-tsconfig/extends/extends.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,29 @@ export default testSuite(({ describe }) => {
await fixture.rm();
});

test('jsonc', async () => {
const fixture = await createFixture({
'file.ts': '',
'tsconfig.base.json': `{
// comment
"compilerOptions": {
"jsx": "react", // dangling comma
},
}`,
'tsconfig.json': tsconfigJson({
extends: './tsconfig.base.json',
}),
});

const expectedTsconfig = await getTscTsconfig(fixture.path);
delete expectedTsconfig.files;

const tsconfig = getTsconfig(fixture.path);
expect(tsconfig!.config).toStrictEqual(expectedTsconfig);

await fixture.rm();
});

test('references is ignored', async () => {
const fixture = await createFixture({
'tsconfig.base.json': tsconfigJson({
Expand Down

0 comments on commit 2ca792b

Please sign in to comment.