fix: comments being stripped out of json request body (#4402)

This commit is contained in:
Andrew Bastin
2024-10-04 23:41:40 +05:30
committed by GitHub
parent fdb250a608
commit 8e91078f43
3 changed files with 13 additions and 17 deletions

View File

@@ -67,6 +67,7 @@
"insomnia-importers": "3.6.0",
"io-ts": "2.2.20",
"js-yaml": "4.1.0",
"jsonc-parser": "3.3.1",
"jsonpath-plus": "7.2.0",
"lodash-es": "4.17.21",
"lossless-json": "3.0.2",

View File

@@ -1,6 +1,7 @@
import { convertIndexToLineCh } from "../utils"
import { LinterDefinition, LinterResult } from "./linter"
import jsoncParse from "~/helpers/jsoncParse"
import { stripComments } from "jsonc-parser"
const linter: LinterDefinition = (text) => {
try {
@@ -23,23 +24,8 @@ const linter: LinterDefinition = (text) => {
* @param jsonString The JSON string with comments.
* @returns The JSON string without comments.
*/
const singleLineCommentPattern = /\/\/.*$/gm
const multiLineCommentPattern = /\/\*[\s\S]*?\*\//gm
export function removeComments(jsonString: string): string {
// Remove single-line comments
jsonString = jsonString.replace(singleLineCommentPattern, "")
// Remove multi-line comments
jsonString = jsonString.replace(multiLineCommentPattern, "")
jsonString = removeTrailingCommas(jsonString)
return jsonString
}
export function removeTrailingCommas(jsonString: string): string {
return jsonString.replace(/,(?=\s*?[\]}])/g, "")
return stripComments(jsonString)
}
export default linter