Skip to content

Commit

Permalink
fix: parse tsconfig as jsonc, resolves #32. (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
knightedcodemonkey authored Dec 16, 2023
1 parent 328c183 commit ff1492b
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 25 deletions.
24 changes: 9 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@knighted/duel",
"version": "1.0.3",
"version": "1.0.4",
"description": "TypeScript dual packages.",
"type": "module",
"main": "dist",
Expand Down Expand Up @@ -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",
Expand Down
15 changes: 10 additions & 5 deletions src/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion test/__fixtures__/plain/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"declaration": true,
"strict": false,
"outDir": "dist",
"lib": ["DOM", "ES2015"]
"lib": ["DOM", "ES2015"], // dangle comma
},
"include": ["src"]
}
2 changes: 1 addition & 1 deletion test/integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down

0 comments on commit ff1492b

Please sign in to comment.