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: strip dangling comma #31

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
64 changes: 63 additions & 1 deletion src/tsconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ export function readFileSync (filename: string): any {
* Parse `tsconfig.json` file.
*/
export function parse (contents: string, filename: string) {
const data = stripComments(stripBom(contents))
const data = stripDanglingComma(stripComments(stripBom(contents)))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const data = stripDanglingComma(stripComments(stripBom(contents)))
const data = stripDanglingCommas(stripComments(stripBom(contents)))


// A tsconfig.json file is permitted to be completely empty.
if (/^\s*$/.test(data)) {
Expand Down Expand Up @@ -230,3 +230,65 @@ function isFile (stats: fs.Stats | void) {
function isDirectory (stats: fs.Stats | void) {
return stats ? (stats as fs.Stats).isDirectory() : false
}

/**
* replace dangling commas from pseudo-json string with single space
*
* limitations:
* - pseudo-json must not contain comments, use strip-json-comments before
* - only a single dangling comma before } or ] is removed
* stripDanglingComma('[1,2,]') === '[1,2 ]
* stripDanglingComma('[1,2,,]') === '[1,2, ]
*
* implementation heavily inspired by strip-json-comments
*/
function stripDanglingComma (jsonString: string) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
function stripDanglingComma (jsonString: string) {
function stripDanglingCommas (jsonString: string) {

/**
* Check if char at qoutePosition is escaped by an odd number of backslashes preceding it
*/
function isEscaped (jsonString: string, quotePosition: number) {
let index = quotePosition - 1
let backslashCount = 0

while (jsonString[index] === '\\') {
index -= 1
backslashCount += 1
}

return Boolean(backslashCount % 2)
}
let insideString = false
let offset = 0
let result = ''
let danglingCommaPos = null
for (let i = 0; i < jsonString.length; i++) {
const currentCharacter = jsonString[i]

if (currentCharacter === '"') {
const escaped = isEscaped(jsonString, i)
if (!escaped) {
insideString = !insideString
}
}

if (insideString) {
danglingCommaPos = null
continue
}
if (currentCharacter === ',') {
danglingCommaPos = i
continue
}
if (danglingCommaPos) {
if (currentCharacter === '}' || currentCharacter === ']') {
result += jsonString.slice(offset, danglingCommaPos) + ' '
offset = danglingCommaPos + 1
danglingCommaPos = null
} else if (!currentCharacter.match(/\s/)) {
danglingCommaPos = null
}
}
}

return result + jsonString.substring(offset)
}
10 changes: 6 additions & 4 deletions tests/valid/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
"removeComments": true,
"preserveConstEnums": true,
"outDir": "dist",
"sourceMap": true
},
"sourceMap": true, // regular dangling comma
} ,
// These are the files to compile with.
"files": [
"./src/foo.ts" /* Use `foo.ts` */
"./src/foo.ts" /* Use `foo.ts` */ , // dangling comma surrounded with whitespace and comments
]
}
// dangling comma on next line
,
}