From ff1492b883bfe73cd872cf127937679961abcf99 Mon Sep 17 00:00:00 2001 From: KCM Date: Sat, 16 Dec 2023 10:11:52 -0600 Subject: [PATCH] fix: parse tsconfig as jsonc, resolves #32. (#33) --- package-lock.json | 24 +++++++++--------------- package.json | 6 +++--- src/init.js | 15 ++++++++++----- test/__fixtures__/plain/tsconfig.json | 2 +- test/integration.js | 2 +- 5 files changed, 24 insertions(+), 25 deletions(-) diff --git a/package-lock.json b/package-lock.json index 68bc114..0bedf35 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,19 +1,19 @@ { "name": "@knighted/duel", - "version": "1.0.3", + "version": "1.0.4", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@knighted/duel", - "version": "1.0.3", + "version": "1.0.4", "license": "MIT", "dependencies": { "@knighted/specifier": "^1.0.1", "find-up": "^6.3.0", "glob": "^10.3.3", - "read-pkg-up": "^10.0.0", - "strip-json-comments": "^5.0.1" + "jsonc-parser": "^3.2.0", + "read-pkg-up": "^10.0.0" }, "bin": { "duel": "dist/duel.js" @@ -4042,6 +4042,11 @@ "node": ">=6" } }, + "node_modules/jsonc-parser": { + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/jsonc-parser/-/jsonc-parser-3.2.0.tgz", + "integrity": "sha512-gfFQZrcTc8CnKXp6Y4/CBT3fTc0OVuDofpre4aEeEpSBPV5X5v4+Vmx+8snU7RLPrNHPKSgLxGo9YuQzz20o+w==" + }, "node_modules/levn": { "version": "0.4.1", "resolved": "https://registry.npmjs.org/levn/-/levn-0.4.1.tgz", @@ -4858,17 +4863,6 @@ "node": ">=8" } }, - "node_modules/strip-json-comments": { - "version": "5.0.1", - "resolved": "https://registry.npmjs.org/strip-json-comments/-/strip-json-comments-5.0.1.tgz", - "integrity": "sha512-0fk9zBqO67Nq5M/m45qHCJxylV/DhBlIOVExqgOMiCCrzrhU6tCibRXNqE3jwJLftzE9SNuZtYbpzcO+i9FiKw==", - "engines": { - "node": ">=14.16" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - }, "node_modules/supports-color": { "version": "5.5.0", "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-5.5.0.tgz", diff --git a/package.json b/package.json index 63450c1..81186c4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@knighted/duel", - "version": "1.0.3", + "version": "1.0.4", "description": "TypeScript dual packages.", "type": "module", "main": "dist", @@ -66,8 +66,8 @@ "@knighted/specifier": "^1.0.1", "find-up": "^6.3.0", "glob": "^10.3.3", - "read-pkg-up": "^10.0.0", - "strip-json-comments": "^5.0.1" + "jsonc-parser": "^3.2.0", + "read-pkg-up": "^10.0.0" }, "prettier": { "arrowParens": "avoid", diff --git a/src/init.js b/src/init.js index aa86501..56c06db 100644 --- a/src/init.js +++ b/src/init.js @@ -2,9 +2,9 @@ import { cwd } from 'node:process' import { parseArgs } from 'node:util' import { resolve, join, dirname } from 'node:path' import { stat, readFile } from 'node:fs/promises' -import stripJsonComments from 'strip-json-comments' import { readPackageUp } from 'read-pkg-up' +import JSONC from 'jsonc-parser' import { logError, log } from './util.js' @@ -123,11 +123,16 @@ const init = async args => { if (stats.isFile()) { let tsconfig = null + const errors = [] + const jsonText = (await readFile(configPath)).toString() - try { - tsconfig = JSON.parse(stripJsonComments((await readFile(configPath)).toString())) - } catch (err) { - logError(`The config file found at ${configPath} is not parsable as JSON.`) + tsconfig = JSONC.parse(jsonText, errors, { + disallowComments: false, + allowTrailingComma: true, + }) + + if (errors.length) { + logError(`The config file found at ${configPath} is not parsable as JSONC.`) return false } diff --git a/test/__fixtures__/plain/tsconfig.json b/test/__fixtures__/plain/tsconfig.json index 0ce5bb5..019adf7 100644 --- a/test/__fixtures__/plain/tsconfig.json +++ b/test/__fixtures__/plain/tsconfig.json @@ -7,7 +7,7 @@ "declaration": true, "strict": false, "outDir": "dist", - "lib": ["DOM", "ES2015"] + "lib": ["DOM", "ES2015"], // dangle comma }, "include": ["src"] } diff --git a/test/integration.js b/test/integration.js index 5d95ea0..777dfef 100644 --- a/test/integration.js +++ b/test/integration.js @@ -70,7 +70,7 @@ describe('duel', () => { const spy = t.mock.method(global.console, 'log') await duel(['-p', 'test/__fixtures__/esmProject/tsconfig.not.json']) - assert.ok(spy.mock.calls[0].arguments[1].endsWith('not parsable as JSON.')) + assert.ok(spy.mock.calls[0].arguments[1].endsWith('not parsable as JSONC.')) }) it('reports errors when using deprecated --target-extension', async t => {