Skip to content

keep codepoint #29

@rktyt

Description

@rktyt

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)
// sandbox.code-workspace
{
  "folders": [
    {
      "path": "."
    }
  ],
  "settings": {
    "highlight-bad-chars.additionalUnicodeChars": [
      "\u0008", // BACKSPACE
      "\u3000", // IDEOGRAPHIC SPACE
      "\u00A0", // NO-BREAK SPACE
      "\u200E", // LEFT-TO-RIGHT MARK
      "\u200F", // RIGHT-TO-LEFT MARK
      "\u309A", // 半濁点
      "\u3099" // 濁点
    ],
    "editor.renderControlCharacters": true,
    "editor.renderWhitespace": "all",
    "editor.renderLineHighlight": "all",
    "editor.renderIndentGuides": true
  }
}

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions