Skip to content

Commit

Permalink
fix: Properly parse tsconfig.json
Browse files Browse the repository at this point in the history
tsconfig.json cannot be parsed with JSON.parse properly. It can have comments
and trailing comma which are not legal in JSON. Use json5 library instead.
  • Loading branch information
zermelo-wisen committed Mar 1, 2024
1 parent def2a65 commit 36cef0f
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@
"acorn-walk": "^8.2.0",
"astring": "^1.8.6",
"chalk": "<5",
"json5": "^2.2.3",
"meriyah": "^4.3.7",
"source-map-js": "^1.0.2",
"strip-json-comments": "^3",
"yaml": "^2.3.4"
},
"workspaces": [
Expand Down
4 changes: 2 additions & 2 deletions src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { dirname, join, resolve } from "node:path";
import { kill, pid } from "node:process";
import { pathToFileURL } from "node:url";

import stripJsonComments from "strip-json-comments";
import json5 from "json5";
import YAML from "yaml";

import config from "./config";
Expand Down Expand Up @@ -85,7 +85,7 @@ function isTsEsmLoaderNeeded(cmd: string, args: string[]) {
const tsConfigSource = readTsConfigUp(config.root);
if (tsConfigSource == null) return false;

const tsConfig: unknown = JSON.parse(stripJsonComments(tsConfigSource));
const tsConfig: unknown = json5.parse(tsConfigSource);
// Check if ts-node is configured and has esm set to true
return (
tsConfig != null &&
Expand Down
2 changes: 1 addition & 1 deletion test/typescript-esm/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
"rootDir": "src" /* Specify the root folder within your source files. */,
"outDir": "dist",
"types": ["node"],
"sourceMap": true
"sourceMap": true, /* Trailing comma needs to be parsed correctly */
}
}
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2733,14 +2733,14 @@ __metadata:
eslint-plugin-prettier: ^5.0.1
fast-glob: ^3.3.1
jest: ^29.6.2
json5: ^2.2.3
meriyah: ^4.3.7
next: ^14.0.4
prettier: ^3.0.2
react: ^18
react-dom: ^18
semantic-release: ^22.0.5
source-map-js: ^1.0.2
strip-json-comments: ^3
tmp: ^0.2.1
ts-node: ^10.9.1
type-fest: ^4.3.2
Expand Down Expand Up @@ -6206,7 +6206,7 @@ __metadata:
languageName: node
linkType: hard

"json5@npm:^2.2.2":
"json5@npm:^2.2.2, json5@npm:^2.2.3":
version: 2.2.3
resolution: "json5@npm:2.2.3"
bin:
Expand Down Expand Up @@ -9422,7 +9422,7 @@ __metadata:
languageName: node
linkType: hard

"strip-json-comments@npm:3.1.1, strip-json-comments@npm:^3, strip-json-comments@npm:^3.1.1":
"strip-json-comments@npm:3.1.1, strip-json-comments@npm:^3.1.1":
version: 3.1.1
resolution: "strip-json-comments@npm:3.1.1"
checksum: 492f73e27268f9b1c122733f28ecb0e7e8d8a531a6662efbd08e22cccb3f9475e90a1b82cab06a392f6afae6d2de636f977e231296400d0ec5304ba70f166443
Expand Down

0 comments on commit 36cef0f

Please sign in to comment.