Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: parse tsconfig as jsonc, resolves #32. #33

Merged
merged 1 commit into from
Dec 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading