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

@@ -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