-
Notifications
You must be signed in to change notification settings - Fork 14
Open
Labels
Description
Tell us about your environment
- Node Version: v14.15.4
- comment-json Version 4.1.0
Please show your use case / code slices / code link that could reproduce the issue
// index.js
const fs = require('fs-extra')
const jsonc = require('comment-json')
const vscodeWorkspaceString = fs
.readFileSync('sandbox.code-workspace')
.toString()
const parsed = jsonc.parse(vscodeWorkspaceString)
const needsSaveStr = jsonc.stringify(parsed, null, 2)
console.log(needsSaveStr)What did you expect to happen?
Excepted Result:
node index.js result is same as sandbox.code-workspace.
Actual Result:
{
"folders": [
{
"path": "."
}
],
"settings": {
"highlight-bad-chars.additionalUnicodeChars": [
"\b", // BACKSPACE
" ", // IDEOGRAPHIC SPACE
" ", // NO-BREAK SPACE
"", // LEFT-TO-RIGHT MARK
"", // RIGHT-TO-LEFT MARK
"゚", // 半濁点
"゙" // 濁点
],
"editor.renderControlCharacters": true,
"editor.renderWhitespace": "all",
"editor.renderLineHighlight": "all",
"editor.renderIndentGuides": true
}
}Are you willing to submit a pull request to fix this bug?
No.
Self resolved:
// index.js
const fs = require('fs-extra')
const jsonc = require('comment-json')
const escapeRegExp = require('escape-string-regexp')
const vscodeWorkspaceString = fs
.readFileSync('sandbox.code-workspace')
.toString()
const codepointValues = []
const parsed = jsonc.parse(vscodeWorkspaceString, (key, value) => {
if (key === 'highlight-bad-chars.additionalUnicodeChars') {
value.forEach((v, index) => {
const point = String(v).codePointAt(0)?.toString(16).toUpperCase()
if (point) {
value[index] = `\\u${point.padStart(4, '0')}`
codepointValues.push(value[index])
}
})
return value
}
return value
})
const needsSaveStr = jsonc
.stringify(parsed, null, 2)
.replace(
new RegExp(
'"(' +
codepointValues.map((v) => escapeRegExp(`\\${v}`)).join('|') +
')"',
'g',
),
(match, p1) => {
return `"${p1.slice(1)}"`
},
)
console.log(needsSaveStr)