| 1 | /*
|
|---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
|---|
| 3 | Author Natsu @xiaoxiaojx
|
|---|
| 4 |
|
|---|
| 5 | This file contains code ported from strip-json-comments:
|
|---|
| 6 | https://github.com/sindresorhus/strip-json-comments
|
|---|
| 7 | Original license: MIT
|
|---|
| 8 | Original author: Sindre Sorhus
|
|---|
| 9 | */
|
|---|
| 10 |
|
|---|
| 11 | "use strict";
|
|---|
| 12 |
|
|---|
| 13 | /**
|
|---|
| 14 | * @typedef {object} StripJsonCommentsOptions
|
|---|
| 15 | * @property {boolean=} whitespace Replace comments with whitespace
|
|---|
| 16 | * @property {boolean=} trailingCommas Strip trailing commas
|
|---|
| 17 | */
|
|---|
| 18 |
|
|---|
| 19 | const singleComment = Symbol("singleComment");
|
|---|
| 20 | const multiComment = Symbol("multiComment");
|
|---|
| 21 |
|
|---|
| 22 | /**
|
|---|
| 23 | * Strip without whitespace (returns empty string)
|
|---|
| 24 | * @param {string} _string Unused
|
|---|
| 25 | * @param {number} _start Unused
|
|---|
| 26 | * @param {number} _end Unused
|
|---|
| 27 | * @returns {string} Empty string for all input
|
|---|
| 28 | */
|
|---|
| 29 | const stripWithoutWhitespace = (_string, _start, _end) => "";
|
|---|
| 30 |
|
|---|
| 31 | /**
|
|---|
| 32 | * Replace all characters except ASCII spaces, tabs and line endings with regular spaces to ensure valid JSON output.
|
|---|
| 33 | * @param {string} string String to process
|
|---|
| 34 | * @param {number} start Start index
|
|---|
| 35 | * @param {number} end End index
|
|---|
| 36 | * @returns {string} Processed string with comments replaced by whitespace
|
|---|
| 37 | */
|
|---|
| 38 | const stripWithWhitespace = (string, start, end) =>
|
|---|
| 39 | string.slice(start, end).replace(/[^ \t\r\n]/g, " ");
|
|---|
| 40 |
|
|---|
| 41 | /**
|
|---|
| 42 | * Check if a quote is escaped
|
|---|
| 43 | * @param {string} jsonString JSON string
|
|---|
| 44 | * @param {number} quotePosition Position of the quote
|
|---|
| 45 | * @returns {boolean} True if the quote at the given position is escaped
|
|---|
| 46 | */
|
|---|
| 47 | const isEscaped = (jsonString, quotePosition) => {
|
|---|
| 48 | let index = quotePosition - 1;
|
|---|
| 49 | let backslashCount = 0;
|
|---|
| 50 |
|
|---|
| 51 | while (jsonString[index] === "\\") {
|
|---|
| 52 | index -= 1;
|
|---|
| 53 | backslashCount += 1;
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | return Boolean(backslashCount % 2);
|
|---|
| 57 | };
|
|---|
| 58 |
|
|---|
| 59 | /**
|
|---|
| 60 | * Strip comments from JSON string
|
|---|
| 61 | * @param {string} jsonString JSON string with potential comments
|
|---|
| 62 | * @param {StripJsonCommentsOptions} options Options
|
|---|
| 63 | * @returns {string} JSON string without comments
|
|---|
| 64 | */
|
|---|
| 65 | function stripJsonComments(
|
|---|
| 66 | jsonString,
|
|---|
| 67 | { whitespace = true, trailingCommas = false } = {},
|
|---|
| 68 | ) {
|
|---|
| 69 | if (typeof jsonString !== "string") {
|
|---|
| 70 | throw new TypeError(
|
|---|
| 71 | `Expected argument \`jsonString\` to be a \`string\`, got \`${typeof jsonString}\``,
|
|---|
| 72 | );
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | const strip = whitespace ? stripWithWhitespace : stripWithoutWhitespace;
|
|---|
| 76 |
|
|---|
| 77 | let isInsideString = false;
|
|---|
| 78 | /** @type {false | typeof singleComment | typeof multiComment} */
|
|---|
| 79 | let isInsideComment = false;
|
|---|
| 80 | let offset = 0;
|
|---|
| 81 | let buffer = "";
|
|---|
| 82 | let result = "";
|
|---|
| 83 | let commaIndex = -1;
|
|---|
| 84 |
|
|---|
| 85 | for (let index = 0; index < jsonString.length; index++) {
|
|---|
| 86 | const currentCharacter = jsonString[index];
|
|---|
| 87 | const nextCharacter = jsonString[index + 1];
|
|---|
| 88 |
|
|---|
| 89 | if (!isInsideComment && currentCharacter === '"') {
|
|---|
| 90 | // Enter or exit string
|
|---|
| 91 | const escaped = isEscaped(jsonString, index);
|
|---|
| 92 | if (!escaped) {
|
|---|
| 93 | isInsideString = !isInsideString;
|
|---|
| 94 | }
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | if (isInsideString) {
|
|---|
| 98 | continue;
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | if (!isInsideComment && currentCharacter + nextCharacter === "//") {
|
|---|
| 102 | // Enter single-line comment
|
|---|
| 103 | buffer += jsonString.slice(offset, index);
|
|---|
| 104 | offset = index;
|
|---|
| 105 | isInsideComment = singleComment;
|
|---|
| 106 | index++;
|
|---|
| 107 | } else if (
|
|---|
| 108 | isInsideComment === singleComment &&
|
|---|
| 109 | currentCharacter + nextCharacter === "\r\n"
|
|---|
| 110 | ) {
|
|---|
| 111 | // Exit single-line comment via \r\n
|
|---|
| 112 | index++;
|
|---|
| 113 | isInsideComment = false;
|
|---|
| 114 | buffer += strip(jsonString, offset, index);
|
|---|
| 115 | offset = index;
|
|---|
| 116 | continue;
|
|---|
| 117 | } else if (isInsideComment === singleComment && currentCharacter === "\n") {
|
|---|
| 118 | // Exit single-line comment via \n
|
|---|
| 119 | isInsideComment = false;
|
|---|
| 120 | buffer += strip(jsonString, offset, index);
|
|---|
| 121 | offset = index;
|
|---|
| 122 | } else if (!isInsideComment && currentCharacter + nextCharacter === "/*") {
|
|---|
| 123 | // Enter multiline comment
|
|---|
| 124 | buffer += jsonString.slice(offset, index);
|
|---|
| 125 | offset = index;
|
|---|
| 126 | isInsideComment = multiComment;
|
|---|
| 127 | index++;
|
|---|
| 128 | continue;
|
|---|
| 129 | } else if (
|
|---|
| 130 | isInsideComment === multiComment &&
|
|---|
| 131 | currentCharacter + nextCharacter === "*/"
|
|---|
| 132 | ) {
|
|---|
| 133 | // Exit multiline comment
|
|---|
| 134 | index++;
|
|---|
| 135 | isInsideComment = false;
|
|---|
| 136 | buffer += strip(jsonString, offset, index + 1);
|
|---|
| 137 | offset = index + 1;
|
|---|
| 138 | continue;
|
|---|
| 139 | } else if (trailingCommas && !isInsideComment) {
|
|---|
| 140 | if (commaIndex !== -1) {
|
|---|
| 141 | if (currentCharacter === "}" || currentCharacter === "]") {
|
|---|
| 142 | // Strip trailing comma
|
|---|
| 143 | buffer += jsonString.slice(offset, index);
|
|---|
| 144 | result += strip(buffer, 0, 1) + buffer.slice(1);
|
|---|
| 145 | buffer = "";
|
|---|
| 146 | offset = index;
|
|---|
| 147 | commaIndex = -1;
|
|---|
| 148 | } else if (
|
|---|
| 149 | currentCharacter !== " " &&
|
|---|
| 150 | currentCharacter !== "\t" &&
|
|---|
| 151 | currentCharacter !== "\r" &&
|
|---|
| 152 | currentCharacter !== "\n"
|
|---|
| 153 | ) {
|
|---|
| 154 | // Hit non-whitespace following a comma; comma is not trailing
|
|---|
| 155 | buffer += jsonString.slice(offset, index);
|
|---|
| 156 | offset = index;
|
|---|
| 157 | commaIndex = -1;
|
|---|
| 158 | }
|
|---|
| 159 | } else if (currentCharacter === ",") {
|
|---|
| 160 | // Flush buffer prior to this point, and save new comma index
|
|---|
| 161 | result += buffer + jsonString.slice(offset, index);
|
|---|
| 162 | buffer = "";
|
|---|
| 163 | offset = index;
|
|---|
| 164 | commaIndex = index;
|
|---|
| 165 | }
|
|---|
| 166 | }
|
|---|
| 167 | }
|
|---|
| 168 |
|
|---|
| 169 | const remaining =
|
|---|
| 170 | isInsideComment === singleComment
|
|---|
| 171 | ? strip(jsonString, offset, jsonString.length)
|
|---|
| 172 | : jsonString.slice(offset);
|
|---|
| 173 |
|
|---|
| 174 | return result + buffer + remaining;
|
|---|
| 175 | }
|
|---|
| 176 |
|
|---|
| 177 | module.exports = stripJsonComments;
|
|---|