@kirill.konshin/lint 0.5.5 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +38 -3
- package/commands/spec-author.md +39 -0
- package/dist/configs/base.d.ts +4 -3
- package/dist/configs/base.js +61 -74
- package/dist/configs/imports.d.ts +3 -2
- package/dist/configs/imports.js +95 -146
- package/dist/configs/nx.d.ts +4 -3
- package/dist/configs/nx.js +31 -41
- package/dist/configs/prettier.d.ts +3 -2
- package/dist/configs/prettier.js +19 -16
- package/dist/configs/promise.d.ts +3 -2
- package/dist/configs/promise.js +16 -16
- package/dist/configs/react.d.ts +4 -3
- package/dist/configs/react.js +178 -217
- package/dist/configs/storybook.d.ts +4 -3
- package/dist/configs/storybook.js +22 -22
- package/dist/configs/tailwind.d.ts +4 -3
- package/dist/configs/tailwind.js +67 -91
- package/dist/configs/testing.d.ts +7 -6
- package/dist/configs/testing.js +112 -146
- package/dist/configs/turbo.d.ts +4 -3
- package/dist/configs/turbo.js +19 -27
- package/dist/configs/typescriptTypeAware.d.ts +4 -3
- package/dist/configs/typescriptTypeAware.js +99 -95
- package/dist/configs/unicorn.d.ts +3 -2
- package/dist/configs/unicorn.js +34 -41
- package/dist/exts.d.ts +2 -1
- package/dist/exts.js +10 -11
- package/dist/index.d.ts +44 -42
- package/dist/index.js +53 -85
- package/dist/lib.d.ts +21 -21
- package/dist/lib.js +261 -301
- package/dist/lintPrepare.d.ts +20 -17
- package/dist/lintPrepare.js +341 -371
- package/dist/lintStaged.d.ts +4 -3
- package/dist/lintStaged.js +25 -22
- package/dist/prettier.d.ts +4 -3
- package/dist/prettier.js +16 -17
- package/dist/restrictions.d.ts +11 -10
- package/dist/restrictions.js +18 -22
- package/dist/yarn.cjs +79 -98
- package/dist/yarn.d.cts +3 -2
- package/package.json +24 -8
- package/rules/agent.md +2 -0
- package/rules/openspec.md +1 -1
- package/skills/spec-steward/SKILL.md +3 -11
- package/skills/spec-steward/references/audit.md +18 -43
- package/skills/spec-steward/references/contract.md +4 -4
- package/skills/spec-steward/references/criteria.md +4 -4
- package/skills/spec-steward/references/review-format.md +6 -6
- package/skills/spec-steward/references/wiring.md +2 -2
- package/skills/spec-steward/references/worker.md +67 -0
- package/skills/spec-steward/scripts/lib/corpus.mjs +1 -1
- package/skills/spec-steward/scripts/lib/evidence.mjs +2 -147
- package/skills/spec-steward/scripts/lib/wire.mjs +6 -4
- package/skills/spec-steward/scripts/steward.mjs +30 -91
- package/skills/spec-steward/sweeps.json +49 -0
- package/skills/spec-tools/SKILL.md +46 -0
- package/skills/spec-tools/references/contract.md +111 -0
- package/skills/spec-tools/references/gitlab.md +122 -0
- package/skills/spec-verify/SKILL.md +153 -0
- package/spec-tools/dist/audit-DkZZulq6.mjs +130 -0
- package/spec-tools/dist/auditChanged-CNGjlake.mjs +1 -0
- package/spec-tools/dist/auditChanged-CZCvMgyZ.mjs +618 -0
- package/spec-tools/dist/auditReport-xgq4wK2i.mjs +458 -0
- package/spec-tools/dist/auditScope-CBuBtfPG.mjs +104 -0
- package/spec-tools/dist/changeGates-DdeQKfle.mjs +333 -0
- package/spec-tools/dist/changeGates-uy4Ti8rK.mjs +1 -0
- package/spec-tools/dist/ci-BUewHJS3.mjs +143 -0
- package/spec-tools/dist/ci-CFBzn8Of.mjs +1 -0
- package/spec-tools/dist/cli.mjs +93 -0
- package/spec-tools/dist/comment-szasMMg2.mjs +57 -0
- package/spec-tools/dist/files-Bppsb34B.mjs +1 -0
- package/spec-tools/dist/files-QlzY687_.mjs +38 -0
- package/spec-tools/dist/html-BMTkpbUN.mjs +1766 -0
- package/spec-tools/dist/jsonrepair-Dlm5dEen.mjs +742 -0
- package/spec-tools/dist/repo-CjIoUPYl.mjs +16 -0
- package/spec-tools/dist/repo-DUcnJw3c.mjs +1 -0
- package/spec-tools/dist/run-BElA9du3.mjs +67 -0
- package/spec-tools/dist/specDiff-8G56jMYn.mjs +123 -0
- package/spec-tools/dist/steward-DeieyOcj.mjs +34 -0
- package/spec-tools/dist/stewardAudit-BRMM0lrD.mjs +1 -0
- package/spec-tools/dist/stewardAudit-v0vOMfLN.mjs +271 -0
- package/spec-tools/dist/tier-Cn0P7juM.mjs +1 -0
- package/spec-tools/dist/tier-c3s_mA_I.mjs +65 -0
- package/spec-tools/dist/util-wnxhsnWO.mjs +49 -0
- package/spec-tools/dist/verdict-BFfN9PO8.mjs +58 -0
- package/spec-tools/dist/workers-BcIgckeq.mjs +417 -0
- package/spec-tools/dist/workflowEvidence-DyBj8EdG.mjs +1 -0
- package/spec-tools/dist/workflowEvidence-oui6ghPI.mjs +78 -0
- package/dist/configs/base.d.ts.map +0 -1
- package/dist/configs/imports.d.ts.map +0 -1
- package/dist/configs/nx.d.ts.map +0 -1
- package/dist/configs/prettier.d.ts.map +0 -1
- package/dist/configs/promise.d.ts.map +0 -1
- package/dist/configs/react.d.ts.map +0 -1
- package/dist/configs/storybook.d.ts.map +0 -1
- package/dist/configs/tailwind.d.ts.map +0 -1
- package/dist/configs/testing.d.ts.map +0 -1
- package/dist/configs/turbo.d.ts.map +0 -1
- package/dist/configs/typescriptTypeAware.d.ts.map +0 -1
- package/dist/configs/unicorn.d.ts.map +0 -1
- package/dist/exts.d.ts.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/lib.d.ts.map +0 -1
- package/dist/lintPrepare.d.ts.map +0 -1
- package/dist/lintStaged.d.ts.map +0 -1
- package/dist/prettier.d.ts.map +0 -1
- package/dist/restrictions.d.ts.map +0 -1
- package/dist/yarn.d.cts.map +0 -1
- package/skills/spec-steward/workflows/audit.js +0 -539
|
@@ -0,0 +1,742 @@
|
|
|
1
|
+
//#region ../../node_modules/jsonrepair/lib/esm/utils/JSONRepairError.js
|
|
2
|
+
var JSONRepairError = class extends Error {
|
|
3
|
+
constructor(message, position) {
|
|
4
|
+
super(`${message} at position ${position}`);
|
|
5
|
+
this.position = position;
|
|
6
|
+
}
|
|
7
|
+
};
|
|
8
|
+
//#endregion
|
|
9
|
+
//#region ../../node_modules/jsonrepair/lib/esm/utils/stringUtils.js
|
|
10
|
+
const codeSpace = 32;
|
|
11
|
+
const codeNewline = 10;
|
|
12
|
+
const codeTab = 9;
|
|
13
|
+
const codeReturn = 13;
|
|
14
|
+
const codeNonBreakingSpace = 160;
|
|
15
|
+
const codeMongolianVowelSeparator = 6158;
|
|
16
|
+
const codeEnQuad = 8192;
|
|
17
|
+
const codeZeroWidthSpace = 8203;
|
|
18
|
+
const codeNarrowNoBreakSpace = 8239;
|
|
19
|
+
const codeMediumMathematicalSpace = 8287;
|
|
20
|
+
const codeIdeographicSpace = 12288;
|
|
21
|
+
const codeZeroWidthNoBreakSpace = 65279;
|
|
22
|
+
function isHex(char) {
|
|
23
|
+
return /^[0-9A-Fa-f]$/.test(char);
|
|
24
|
+
}
|
|
25
|
+
function isDigit(char) {
|
|
26
|
+
return char >= "0" && char <= "9";
|
|
27
|
+
}
|
|
28
|
+
function isValidStringCharacter(char) {
|
|
29
|
+
return char >= " ";
|
|
30
|
+
}
|
|
31
|
+
function isDelimiter(char) {
|
|
32
|
+
return ",:[]/{}()\n+".includes(char);
|
|
33
|
+
}
|
|
34
|
+
function isFunctionNameCharStart(char) {
|
|
35
|
+
return char >= "a" && char <= "z" || char >= "A" && char <= "Z" || char === "_" || char === "$";
|
|
36
|
+
}
|
|
37
|
+
function isFunctionNameChar(char) {
|
|
38
|
+
return char >= "a" && char <= "z" || char >= "A" && char <= "Z" || char === "_" || char === "$" || char >= "0" && char <= "9";
|
|
39
|
+
}
|
|
40
|
+
const regexUrlStart = /^(http|https|ftp|mailto|file|data|irc):\/\/$/;
|
|
41
|
+
const regexUrlChar = /^[A-Za-z0-9-._~:/?#@!$&'()*+;=]$/;
|
|
42
|
+
function isUnquotedStringDelimiter(char) {
|
|
43
|
+
return ",[]/{}\n+".includes(char);
|
|
44
|
+
}
|
|
45
|
+
function isStartOfValue(char) {
|
|
46
|
+
return isQuote(char) || regexStartOfValue.test(char);
|
|
47
|
+
}
|
|
48
|
+
const regexStartOfValue = /^[[{\w-]$/;
|
|
49
|
+
function isControlCharacter(char) {
|
|
50
|
+
return char === "\n" || char === "\r" || char === " " || char === "\b" || char === "\f";
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Check if the given character is a whitespace character like space, tab, or
|
|
54
|
+
* newline
|
|
55
|
+
*/
|
|
56
|
+
function isWhitespace(text, index) {
|
|
57
|
+
const code = text.charCodeAt(index);
|
|
58
|
+
return code === codeSpace || code === codeNewline || code === codeTab || code === codeReturn;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Check if the given character is a whitespace character like space or tab,
|
|
62
|
+
* but NOT a newline
|
|
63
|
+
*/
|
|
64
|
+
function isWhitespaceExceptNewline(text, index) {
|
|
65
|
+
const code = text.charCodeAt(index);
|
|
66
|
+
return code === codeSpace || code === codeTab || code === codeReturn;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Check if the given character is a special whitespace character, some
|
|
70
|
+
* unicode variant
|
|
71
|
+
*/
|
|
72
|
+
function isSpecialWhitespace(text, index) {
|
|
73
|
+
const code = text.charCodeAt(index);
|
|
74
|
+
return code === codeNonBreakingSpace || code === codeMongolianVowelSeparator || code >= codeEnQuad && code <= codeZeroWidthSpace || code === codeNarrowNoBreakSpace || code === codeMediumMathematicalSpace || code === codeIdeographicSpace || code === codeZeroWidthNoBreakSpace;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Test whether the given character is a quote or double quote character.
|
|
78
|
+
* Also tests for special variants of quotes.
|
|
79
|
+
*/
|
|
80
|
+
function isQuote(char) {
|
|
81
|
+
return isDoubleQuoteLike(char) || isSingleQuoteLike(char);
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Test whether the given character is a double quote character.
|
|
85
|
+
* Also tests for special variants of double quotes.
|
|
86
|
+
*/
|
|
87
|
+
function isDoubleQuoteLike(char) {
|
|
88
|
+
return char === "\"" || char === "“" || char === "”";
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Test whether the given character is a double quote character.
|
|
92
|
+
* Does NOT test for special variants of double quotes.
|
|
93
|
+
*/
|
|
94
|
+
function isDoubleQuote(char) {
|
|
95
|
+
return char === "\"";
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Test whether the given character is a single quote character.
|
|
99
|
+
* Also tests for special variants of single quotes.
|
|
100
|
+
*/
|
|
101
|
+
function isSingleQuoteLike(char) {
|
|
102
|
+
return char === "'" || char === "‘" || char === "’" || char === "`" || char === "´";
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Test whether the given character is a single quote character.
|
|
106
|
+
* Does NOT test for special variants of single quotes.
|
|
107
|
+
*/
|
|
108
|
+
function isSingleQuote(char) {
|
|
109
|
+
return char === "'";
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Strip last occurrence of textToStrip from text
|
|
113
|
+
*/
|
|
114
|
+
function stripLastOccurrence(text, textToStrip) {
|
|
115
|
+
let stripRemainingText = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : false;
|
|
116
|
+
const index = text.lastIndexOf(textToStrip);
|
|
117
|
+
return index !== -1 ? text.substring(0, index) + (stripRemainingText ? "" : text.substring(index + 1)) : text;
|
|
118
|
+
}
|
|
119
|
+
function insertBeforeLastWhitespace(text, textToInsert) {
|
|
120
|
+
let index = text.length;
|
|
121
|
+
if (!isWhitespace(text, index - 1)) return text + textToInsert;
|
|
122
|
+
while (isWhitespace(text, index - 1)) index--;
|
|
123
|
+
return text.substring(0, index) + textToInsert + text.substring(index);
|
|
124
|
+
}
|
|
125
|
+
function removeAtIndex(text, start, count) {
|
|
126
|
+
return text.substring(0, start) + text.substring(start + count);
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Test whether a string ends with a newline or comma character and optional whitespace
|
|
130
|
+
*/
|
|
131
|
+
function endsWithCommaOrNewline(text) {
|
|
132
|
+
return /[,\n][ \t\r]*$/.test(text);
|
|
133
|
+
}
|
|
134
|
+
const namedHtmlEntities = {
|
|
135
|
+
""": "\"",
|
|
136
|
+
"&": "&",
|
|
137
|
+
"<": "<",
|
|
138
|
+
">": ">",
|
|
139
|
+
"'": "'"
|
|
140
|
+
};
|
|
141
|
+
/**
|
|
142
|
+
* Try to match an HTML entity at the start of the given fragment. The fragment
|
|
143
|
+
* is a small slice of text that begins exactly at the candidate '&'. Returns the
|
|
144
|
+
* decoded character and the number of characters consumed, or null when there
|
|
145
|
+
* is no complete, valid entity (for example a truncated """ without ';').
|
|
146
|
+
*/
|
|
147
|
+
function matchHtmlEntity(fragment) {
|
|
148
|
+
if (fragment.charAt(0) !== "&") return null;
|
|
149
|
+
const semicolon = fragment.indexOf(";");
|
|
150
|
+
if (semicolon === -1) return null;
|
|
151
|
+
const entity = fragment.substring(0, semicolon + 1);
|
|
152
|
+
const named = namedHtmlEntities[entity];
|
|
153
|
+
if (named !== void 0) return {
|
|
154
|
+
char: named,
|
|
155
|
+
length: entity.length
|
|
156
|
+
};
|
|
157
|
+
if (fragment.charAt(1) === "#") {
|
|
158
|
+
const body = fragment.substring(2, semicolon);
|
|
159
|
+
const hex = body.charAt(0) === "x" || body.charAt(0) === "X";
|
|
160
|
+
const digits = hex ? body.substring(1) : body;
|
|
161
|
+
if (digits.length > 0) {
|
|
162
|
+
const code = Number.parseInt(digits, hex ? 16 : 10);
|
|
163
|
+
if (!Number.isNaN(code) && code >= 0 && code <= 1114111) return {
|
|
164
|
+
char: String.fromCodePoint(code),
|
|
165
|
+
length: entity.length
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
return null;
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Test whether a matched HTML entity decodes to a double quote character
|
|
173
|
+
*/
|
|
174
|
+
function isDoubleQuoteEntity(match) {
|
|
175
|
+
return match !== null && match.char === "\"";
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Test whether a matched HTML entity decodes to a single quote character
|
|
179
|
+
*/
|
|
180
|
+
function isSingleQuoteEntity(match) {
|
|
181
|
+
return match !== null && match.char === "'";
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Count the number of occurrences of a single character in a string
|
|
185
|
+
*/
|
|
186
|
+
function countOccurrences(text, char) {
|
|
187
|
+
let count = 0;
|
|
188
|
+
for (let i = 0; i < text.length; i++) if (text.charAt(i) === char) count++;
|
|
189
|
+
return count;
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Test whether `closeChar` is a closing bracket and `text` still contains an
|
|
193
|
+
* unmatched opening bracket of the same kind. This indicates that the end of
|
|
194
|
+
* `text` is located inside the brackets, for example the quote in
|
|
195
|
+
* `"a (b") c"` is followed by `)` while `(` is still unclosed.
|
|
196
|
+
*
|
|
197
|
+
* Note that the (potentially expensive) counting is only performed when
|
|
198
|
+
* `closeChar` actually is a closing bracket.
|
|
199
|
+
*/
|
|
200
|
+
function isInsideUnclosedBracket(text, closeChar) {
|
|
201
|
+
switch (closeChar) {
|
|
202
|
+
case ")": return countOccurrences(text, "(") > countOccurrences(text, ")");
|
|
203
|
+
case "]": return countOccurrences(text, "[") > countOccurrences(text, "]");
|
|
204
|
+
case "}": return countOccurrences(text, "{") > countOccurrences(text, "}");
|
|
205
|
+
default: return false;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
//#endregion
|
|
209
|
+
//#region ../../node_modules/jsonrepair/lib/esm/regular/jsonrepair.js
|
|
210
|
+
const controlCharacters = {
|
|
211
|
+
"\b": "\\b",
|
|
212
|
+
"\f": "\\f",
|
|
213
|
+
"\n": "\\n",
|
|
214
|
+
"\r": "\\r",
|
|
215
|
+
" ": "\\t"
|
|
216
|
+
};
|
|
217
|
+
const escapeCharacters = {
|
|
218
|
+
"\"": "\"",
|
|
219
|
+
"\\": "\\",
|
|
220
|
+
"/": "/",
|
|
221
|
+
b: "\b",
|
|
222
|
+
f: "\f",
|
|
223
|
+
n: "\n",
|
|
224
|
+
r: "\r",
|
|
225
|
+
t: " "
|
|
226
|
+
};
|
|
227
|
+
/**
|
|
228
|
+
* Repair a string containing an invalid JSON document.
|
|
229
|
+
* For example changes JavaScript notation into JSON notation.
|
|
230
|
+
*
|
|
231
|
+
* Example:
|
|
232
|
+
*
|
|
233
|
+
* try {
|
|
234
|
+
* const json = "{name: 'John'}"
|
|
235
|
+
* const repaired = jsonrepair(json)
|
|
236
|
+
* console.log(repaired)
|
|
237
|
+
* // '{"name": "John"}'
|
|
238
|
+
* } catch (err) {
|
|
239
|
+
* console.error(err)
|
|
240
|
+
* }
|
|
241
|
+
*
|
|
242
|
+
*/
|
|
243
|
+
function jsonrepair(text) {
|
|
244
|
+
let i = 0;
|
|
245
|
+
let output = "";
|
|
246
|
+
parseMarkdownCodeBlock([
|
|
247
|
+
"```",
|
|
248
|
+
"[```",
|
|
249
|
+
"{```"
|
|
250
|
+
]);
|
|
251
|
+
if (!parseValue()) throwUnexpectedEnd();
|
|
252
|
+
parseMarkdownCodeBlock([
|
|
253
|
+
"```",
|
|
254
|
+
"```]",
|
|
255
|
+
"```}"
|
|
256
|
+
]);
|
|
257
|
+
const processedComma = parseCharacter(",");
|
|
258
|
+
if (processedComma) parseWhitespaceAndSkipComments();
|
|
259
|
+
if (isStartOfValue(text[i]) && endsWithCommaOrNewline(output)) {
|
|
260
|
+
if (!processedComma) output = insertBeforeLastWhitespace(output, ",");
|
|
261
|
+
parseNewlineDelimitedJSON();
|
|
262
|
+
} else if (processedComma) output = stripLastOccurrence(output, ",");
|
|
263
|
+
while (text[i] === "}" || text[i] === "]") {
|
|
264
|
+
i++;
|
|
265
|
+
parseWhitespaceAndSkipComments();
|
|
266
|
+
}
|
|
267
|
+
if (i >= text.length) return output;
|
|
268
|
+
throwUnexpectedCharacter();
|
|
269
|
+
function parseValue() {
|
|
270
|
+
parseWhitespaceAndSkipComments();
|
|
271
|
+
const processed = parseObject() || parseArray() || parseString() || parseNumber() || parseKeywords() || parseUnquotedString(false) || parseRegex();
|
|
272
|
+
parseWhitespaceAndSkipComments();
|
|
273
|
+
return processed;
|
|
274
|
+
}
|
|
275
|
+
function parseWhitespaceAndSkipComments() {
|
|
276
|
+
let skipNewline = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : true;
|
|
277
|
+
const start = i;
|
|
278
|
+
let changed = parseWhitespace(skipNewline);
|
|
279
|
+
do {
|
|
280
|
+
changed = parseComment();
|
|
281
|
+
if (changed) changed = parseWhitespace(skipNewline);
|
|
282
|
+
} while (changed);
|
|
283
|
+
return i > start;
|
|
284
|
+
}
|
|
285
|
+
function parseWhitespace(skipNewline) {
|
|
286
|
+
const _isWhiteSpace = skipNewline ? isWhitespace : isWhitespaceExceptNewline;
|
|
287
|
+
let whitespace = "";
|
|
288
|
+
while (true) if (_isWhiteSpace(text, i)) {
|
|
289
|
+
whitespace += text[i];
|
|
290
|
+
i++;
|
|
291
|
+
} else if (isSpecialWhitespace(text, i)) {
|
|
292
|
+
whitespace += " ";
|
|
293
|
+
i++;
|
|
294
|
+
} else break;
|
|
295
|
+
if (whitespace.length > 0) {
|
|
296
|
+
output += whitespace;
|
|
297
|
+
return true;
|
|
298
|
+
}
|
|
299
|
+
return false;
|
|
300
|
+
}
|
|
301
|
+
function parseComment() {
|
|
302
|
+
if (text[i] === "/" && text[i + 1] === "*") {
|
|
303
|
+
while (i < text.length && !atEndOfBlockComment(text, i)) i++;
|
|
304
|
+
i += 2;
|
|
305
|
+
return true;
|
|
306
|
+
}
|
|
307
|
+
if (text[i] === "/" && text[i + 1] === "/") {
|
|
308
|
+
while (i < text.length && text[i] !== "\n") i++;
|
|
309
|
+
return true;
|
|
310
|
+
}
|
|
311
|
+
return false;
|
|
312
|
+
}
|
|
313
|
+
function parseMarkdownCodeBlock(blocks) {
|
|
314
|
+
if (skipMarkdownCodeBlock(blocks)) {
|
|
315
|
+
if (isFunctionNameCharStart(text[i])) while (i < text.length && isFunctionNameChar(text[i])) i++;
|
|
316
|
+
parseWhitespaceAndSkipComments();
|
|
317
|
+
return true;
|
|
318
|
+
}
|
|
319
|
+
return false;
|
|
320
|
+
}
|
|
321
|
+
function skipMarkdownCodeBlock(blocks) {
|
|
322
|
+
parseWhitespace(true);
|
|
323
|
+
for (const block of blocks) {
|
|
324
|
+
const end = i + block.length;
|
|
325
|
+
if (text.slice(i, end) === block) {
|
|
326
|
+
i = end;
|
|
327
|
+
return true;
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
return false;
|
|
331
|
+
}
|
|
332
|
+
function parseCharacter(char) {
|
|
333
|
+
if (text[i] === char) {
|
|
334
|
+
output += text[i];
|
|
335
|
+
i++;
|
|
336
|
+
return true;
|
|
337
|
+
}
|
|
338
|
+
return false;
|
|
339
|
+
}
|
|
340
|
+
function skipCharacter(char) {
|
|
341
|
+
if (text[i] === char) {
|
|
342
|
+
i++;
|
|
343
|
+
return true;
|
|
344
|
+
}
|
|
345
|
+
return false;
|
|
346
|
+
}
|
|
347
|
+
function skipEscapeCharacter() {
|
|
348
|
+
return skipCharacter("\\");
|
|
349
|
+
}
|
|
350
|
+
/**
|
|
351
|
+
* Skip ellipsis like "[1,2,3,...]" or "[1,2,3,...,9]" or "[...,7,8,9]"
|
|
352
|
+
* or a similar construct in objects.
|
|
353
|
+
*/
|
|
354
|
+
function skipEllipsis() {
|
|
355
|
+
parseWhitespaceAndSkipComments();
|
|
356
|
+
if (text[i] === "." && text[i + 1] === "." && text[i + 2] === ".") {
|
|
357
|
+
i += 3;
|
|
358
|
+
parseWhitespaceAndSkipComments();
|
|
359
|
+
skipCharacter(",");
|
|
360
|
+
return true;
|
|
361
|
+
}
|
|
362
|
+
return false;
|
|
363
|
+
}
|
|
364
|
+
/**
|
|
365
|
+
* Parse an object like '{"key": "value"}'
|
|
366
|
+
*/
|
|
367
|
+
function parseObject() {
|
|
368
|
+
if (text[i] === "{") {
|
|
369
|
+
output += "{";
|
|
370
|
+
i++;
|
|
371
|
+
parseWhitespaceAndSkipComments();
|
|
372
|
+
if (skipCharacter(",")) parseWhitespaceAndSkipComments();
|
|
373
|
+
let initial = true;
|
|
374
|
+
while (i < text.length && text[i] !== "}") {
|
|
375
|
+
let processedComma;
|
|
376
|
+
if (!initial) {
|
|
377
|
+
processedComma = parseCharacter(",");
|
|
378
|
+
if (!processedComma) output = insertBeforeLastWhitespace(output, ",");
|
|
379
|
+
parseWhitespaceAndSkipComments();
|
|
380
|
+
} else processedComma = true;
|
|
381
|
+
skipEllipsis();
|
|
382
|
+
if (!(parseString() || parseUnquotedString(true))) {
|
|
383
|
+
if (text[i] === "}" || text[i] === "{" || text[i] === "]" || text[i] === "[" || text[i] === void 0) {
|
|
384
|
+
if (!initial) output = stripLastOccurrence(output, ",");
|
|
385
|
+
} else throwObjectKeyExpected();
|
|
386
|
+
break;
|
|
387
|
+
}
|
|
388
|
+
parseWhitespaceAndSkipComments();
|
|
389
|
+
const processedColon = parseCharacter(":");
|
|
390
|
+
const truncatedText = i >= text.length;
|
|
391
|
+
if (!processedColon) {
|
|
392
|
+
if (isStartOfValue(text[i]) || truncatedText) output = insertBeforeLastWhitespace(output, ":");
|
|
393
|
+
else throwColonExpected();
|
|
394
|
+
}
|
|
395
|
+
if (!parseValue()) {
|
|
396
|
+
if (processedColon || truncatedText) output += "null";
|
|
397
|
+
else throwColonExpected();
|
|
398
|
+
}
|
|
399
|
+
initial = false;
|
|
400
|
+
}
|
|
401
|
+
if (text[i] === "}") {
|
|
402
|
+
output += "}";
|
|
403
|
+
i++;
|
|
404
|
+
} else output = insertBeforeLastWhitespace(output, "}");
|
|
405
|
+
return true;
|
|
406
|
+
}
|
|
407
|
+
return false;
|
|
408
|
+
}
|
|
409
|
+
/**
|
|
410
|
+
* Parse an array like '["item1", "item2", ...]'
|
|
411
|
+
*/
|
|
412
|
+
function parseArray() {
|
|
413
|
+
if (text[i] === "[") {
|
|
414
|
+
output += "[";
|
|
415
|
+
i++;
|
|
416
|
+
parseWhitespaceAndSkipComments();
|
|
417
|
+
if (skipCharacter(",")) parseWhitespaceAndSkipComments();
|
|
418
|
+
let initial = true;
|
|
419
|
+
while (i < text.length && text[i] !== "]") {
|
|
420
|
+
if (!initial) {
|
|
421
|
+
if (!parseCharacter(",")) output = insertBeforeLastWhitespace(output, ",");
|
|
422
|
+
}
|
|
423
|
+
skipEllipsis();
|
|
424
|
+
if (!parseValue()) {
|
|
425
|
+
if (!initial) output = stripLastOccurrence(output, ",");
|
|
426
|
+
break;
|
|
427
|
+
}
|
|
428
|
+
initial = false;
|
|
429
|
+
}
|
|
430
|
+
if (text[i] === "]") {
|
|
431
|
+
output += "]";
|
|
432
|
+
i++;
|
|
433
|
+
} else output = insertBeforeLastWhitespace(output, "]");
|
|
434
|
+
return true;
|
|
435
|
+
}
|
|
436
|
+
return false;
|
|
437
|
+
}
|
|
438
|
+
/**
|
|
439
|
+
* Parse and repair Newline Delimited JSON (NDJSON):
|
|
440
|
+
* multiple JSON objects separated by a newline character
|
|
441
|
+
*/
|
|
442
|
+
function parseNewlineDelimitedJSON() {
|
|
443
|
+
let initial = true;
|
|
444
|
+
let processedValue = true;
|
|
445
|
+
while (processedValue) {
|
|
446
|
+
if (!initial) {
|
|
447
|
+
if (!parseCharacter(",")) output = insertBeforeLastWhitespace(output, ",");
|
|
448
|
+
} else initial = false;
|
|
449
|
+
processedValue = parseValue();
|
|
450
|
+
}
|
|
451
|
+
if (!processedValue) output = stripLastOccurrence(output, ",");
|
|
452
|
+
output = `[\n${output}\n]`;
|
|
453
|
+
}
|
|
454
|
+
/**
|
|
455
|
+
* Parse a string enclosed by double quotes "...". Can contain escaped quotes
|
|
456
|
+
* Repair strings enclosed in single quotes or special quotes
|
|
457
|
+
* Repair an escaped string
|
|
458
|
+
*
|
|
459
|
+
* The function can run in two stages:
|
|
460
|
+
* - First, it assumes the string has a valid end quote
|
|
461
|
+
* - If it turns out that the string does not have a valid end quote followed
|
|
462
|
+
* by a delimiter (which should be the case), the function runs again in a
|
|
463
|
+
* more conservative way, stopping the string at the first next delimiter
|
|
464
|
+
* and fixing the string by inserting a quote there, or stopping at a
|
|
465
|
+
* stop index detected in the first iteration.
|
|
466
|
+
*/
|
|
467
|
+
function parseString() {
|
|
468
|
+
let stopAtDelimiter = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : false;
|
|
469
|
+
let stopAtIndex = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : -1;
|
|
470
|
+
const skipEscapeChars = text[i] === "\\";
|
|
471
|
+
if (skipEscapeChars) {
|
|
472
|
+
i++;
|
|
473
|
+
if (!isQuote(text[i])) throwUnexpectedCharacter();
|
|
474
|
+
}
|
|
475
|
+
const openEntity = text[i] === "&" ? matchHtmlEntity(text.slice(i, i + 12)) : null;
|
|
476
|
+
const openedByEntity = isDoubleQuoteEntity(openEntity) || isSingleQuoteEntity(openEntity);
|
|
477
|
+
if (isQuote(text[i]) || openedByEntity) {
|
|
478
|
+
const isEndQuote = isDoubleQuote(text[i]) ? isDoubleQuote : isSingleQuote(text[i]) ? isSingleQuote : isSingleQuoteLike(text[i]) ? isSingleQuoteLike : isDoubleQuoteLike;
|
|
479
|
+
const iBefore = i;
|
|
480
|
+
const oBefore = output.length;
|
|
481
|
+
let str = "\"";
|
|
482
|
+
i += openedByEntity && openEntity ? openEntity.length : 1;
|
|
483
|
+
while (true) {
|
|
484
|
+
if (i >= text.length) {
|
|
485
|
+
const iPrev = prevNonWhitespaceIndex(i - 1);
|
|
486
|
+
if (!stopAtDelimiter && isDelimiter(text.charAt(iPrev))) {
|
|
487
|
+
i = iBefore;
|
|
488
|
+
output = output.substring(0, oBefore);
|
|
489
|
+
return parseString(true);
|
|
490
|
+
}
|
|
491
|
+
str = insertBeforeLastWhitespace(str, "\"");
|
|
492
|
+
output += str;
|
|
493
|
+
return true;
|
|
494
|
+
}
|
|
495
|
+
if (i === stopAtIndex) {
|
|
496
|
+
str = insertBeforeLastWhitespace(str, "\"");
|
|
497
|
+
output += str;
|
|
498
|
+
return true;
|
|
499
|
+
}
|
|
500
|
+
const entity = openedByEntity && text[i] === "&" ? matchHtmlEntity(text.slice(i, i + 12)) : null;
|
|
501
|
+
if (entity && openEntity ? entity.char === openEntity.char : isEndQuote(text[i])) {
|
|
502
|
+
const iQuote = i;
|
|
503
|
+
const oQuote = str.length;
|
|
504
|
+
str += "\"";
|
|
505
|
+
i += entity ? entity.length : 1;
|
|
506
|
+
output += str;
|
|
507
|
+
parseWhitespaceAndSkipComments(false);
|
|
508
|
+
if (stopAtDelimiter || i >= text.length || isDelimiter(text[i]) && !isInsideUnclosedBracket(str, text[i]) || isQuote(text[i]) && !nextQuoteIsEndQuote(i) || isDigit(text[i])) {
|
|
509
|
+
parseConcatenatedString();
|
|
510
|
+
return true;
|
|
511
|
+
}
|
|
512
|
+
if (text[i] === "\\") throwUnexpectedCharacter();
|
|
513
|
+
const iPrevChar = prevNonWhitespaceIndex(iQuote - 1);
|
|
514
|
+
const prevChar = text.charAt(iPrevChar);
|
|
515
|
+
if (prevChar === ",") {
|
|
516
|
+
i = iBefore;
|
|
517
|
+
output = output.substring(0, oBefore);
|
|
518
|
+
return parseString(false, iPrevChar);
|
|
519
|
+
}
|
|
520
|
+
if (isDelimiter(prevChar)) {
|
|
521
|
+
i = iBefore;
|
|
522
|
+
output = output.substring(0, oBefore);
|
|
523
|
+
return parseString(true);
|
|
524
|
+
}
|
|
525
|
+
output = output.substring(0, oBefore);
|
|
526
|
+
i = iQuote + (entity ? entity.length : 1);
|
|
527
|
+
str = `${str.substring(0, oQuote)}\\${str.substring(oQuote)}`;
|
|
528
|
+
} else if (stopAtDelimiter && isUnquotedStringDelimiter(text[i])) {
|
|
529
|
+
if (text[i - 1] === ":" && regexUrlStart.test(text.substring(iBefore + 1, i + 2))) while (i < text.length && regexUrlChar.test(text[i])) {
|
|
530
|
+
str += text[i];
|
|
531
|
+
i++;
|
|
532
|
+
}
|
|
533
|
+
str = insertBeforeLastWhitespace(str, "\"");
|
|
534
|
+
output += str;
|
|
535
|
+
parseConcatenatedString();
|
|
536
|
+
return true;
|
|
537
|
+
} else if (entity) {
|
|
538
|
+
const char = entity.char;
|
|
539
|
+
if (char === "\"") str += "\\\"";
|
|
540
|
+
else if (isControlCharacter(char)) str += controlCharacters[char];
|
|
541
|
+
else str += char;
|
|
542
|
+
i += entity.length;
|
|
543
|
+
} else if (text[i] === "\\") {
|
|
544
|
+
const char = text.charAt(i + 1);
|
|
545
|
+
if (escapeCharacters[char] !== void 0) {
|
|
546
|
+
str += text.slice(i, i + 2);
|
|
547
|
+
i += 2;
|
|
548
|
+
} else if (char === "u") {
|
|
549
|
+
let j = 2;
|
|
550
|
+
while (j < 6 && isHex(text[i + j])) j++;
|
|
551
|
+
if (j === 6) {
|
|
552
|
+
str += text.slice(i, i + 6);
|
|
553
|
+
i += 6;
|
|
554
|
+
} else if (i + j >= text.length) i = text.length;
|
|
555
|
+
else throwInvalidUnicodeCharacter();
|
|
556
|
+
} else if (char === "\n") {
|
|
557
|
+
str += "\\n";
|
|
558
|
+
i += 2;
|
|
559
|
+
} else {
|
|
560
|
+
str += char;
|
|
561
|
+
i += 2;
|
|
562
|
+
}
|
|
563
|
+
} else {
|
|
564
|
+
const char = text.charAt(i);
|
|
565
|
+
if (char === "\"" && text[i - 1] !== "\\") {
|
|
566
|
+
str += `\\${char}`;
|
|
567
|
+
i++;
|
|
568
|
+
} else if (isControlCharacter(char)) {
|
|
569
|
+
str += controlCharacters[char];
|
|
570
|
+
i++;
|
|
571
|
+
} else {
|
|
572
|
+
if (!isValidStringCharacter(char)) throwInvalidCharacter(char);
|
|
573
|
+
str += char;
|
|
574
|
+
i++;
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
if (skipEscapeChars) skipEscapeCharacter();
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
return false;
|
|
581
|
+
}
|
|
582
|
+
/**
|
|
583
|
+
* Repair concatenated strings like "hello" + "world", change this into "helloworld"
|
|
584
|
+
*/
|
|
585
|
+
function parseConcatenatedString() {
|
|
586
|
+
let processed = false;
|
|
587
|
+
parseWhitespaceAndSkipComments();
|
|
588
|
+
while (text[i] === "+") {
|
|
589
|
+
processed = true;
|
|
590
|
+
i++;
|
|
591
|
+
parseWhitespaceAndSkipComments();
|
|
592
|
+
output = stripLastOccurrence(output, "\"", true);
|
|
593
|
+
const start = output.length;
|
|
594
|
+
if (parseString()) output = removeAtIndex(output, start, 1);
|
|
595
|
+
else output = insertBeforeLastWhitespace(output, "\"");
|
|
596
|
+
}
|
|
597
|
+
return processed;
|
|
598
|
+
}
|
|
599
|
+
/**
|
|
600
|
+
* Parse a number like 2.4 or 2.4e6
|
|
601
|
+
*/
|
|
602
|
+
function parseNumber() {
|
|
603
|
+
const start = i;
|
|
604
|
+
let num = "";
|
|
605
|
+
let invalid = false;
|
|
606
|
+
if (text[i] === "-") {
|
|
607
|
+
num += text[i];
|
|
608
|
+
i++;
|
|
609
|
+
if (!isDigit(text[i]) && atEndOfNumber()) num += "0";
|
|
610
|
+
}
|
|
611
|
+
if (text[i] === "0" && isDigit(text[i + 1])) invalid = true;
|
|
612
|
+
while (isDigit(text[i])) {
|
|
613
|
+
num += text[i];
|
|
614
|
+
i++;
|
|
615
|
+
}
|
|
616
|
+
if (text[i] === ".") {
|
|
617
|
+
if (num === "" || num === "-") num += "0";
|
|
618
|
+
num += text[i];
|
|
619
|
+
i++;
|
|
620
|
+
if (!isDigit(text[i])) num += "0";
|
|
621
|
+
while (isDigit(text[i])) {
|
|
622
|
+
num += text[i];
|
|
623
|
+
i++;
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
if (i > start) {
|
|
627
|
+
if (text[i] === "e" || text[i] === "E") {
|
|
628
|
+
if (num === "-") invalid = true;
|
|
629
|
+
num += text[i];
|
|
630
|
+
i++;
|
|
631
|
+
if (text[i] === "-" || text[i] === "+") {
|
|
632
|
+
num += text[i];
|
|
633
|
+
i++;
|
|
634
|
+
}
|
|
635
|
+
if (!isDigit(text[i])) num += "0";
|
|
636
|
+
while (isDigit(text[i])) {
|
|
637
|
+
num += text[i];
|
|
638
|
+
i++;
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
if (!atEndOfNumber()) {
|
|
642
|
+
i = start;
|
|
643
|
+
return false;
|
|
644
|
+
}
|
|
645
|
+
output += invalid ? `"${text.substring(start, i)}"` : num;
|
|
646
|
+
return true;
|
|
647
|
+
}
|
|
648
|
+
return false;
|
|
649
|
+
}
|
|
650
|
+
/**
|
|
651
|
+
* Parse keywords true, false, null
|
|
652
|
+
* Repair Python keywords True, False, None
|
|
653
|
+
*/
|
|
654
|
+
function parseKeywords() {
|
|
655
|
+
return parseKeyword("true", "true") || parseKeyword("false", "false") || parseKeyword("null", "null") || parseKeyword("True", "true") || parseKeyword("False", "false") || parseKeyword("None", "null");
|
|
656
|
+
}
|
|
657
|
+
function parseKeyword(name, value) {
|
|
658
|
+
if (text.slice(i, i + name.length) === name && !isFunctionNameChar(text[i + name.length])) {
|
|
659
|
+
output += value;
|
|
660
|
+
i += name.length;
|
|
661
|
+
return true;
|
|
662
|
+
}
|
|
663
|
+
return false;
|
|
664
|
+
}
|
|
665
|
+
/**
|
|
666
|
+
* Repair an unquoted string by adding quotes around it
|
|
667
|
+
* Repair a MongoDB function call like NumberLong("2")
|
|
668
|
+
* Repair a JSONP function call like callback({...});
|
|
669
|
+
*/
|
|
670
|
+
function parseUnquotedString(isKey) {
|
|
671
|
+
const start = i;
|
|
672
|
+
if (isFunctionNameCharStart(text[i])) {
|
|
673
|
+
while (i < text.length && isFunctionNameChar(text[i])) i++;
|
|
674
|
+
let j = i;
|
|
675
|
+
while (isWhitespace(text, j)) j++;
|
|
676
|
+
if (text[j] === "(") {
|
|
677
|
+
i = j + 1;
|
|
678
|
+
parseValue();
|
|
679
|
+
if (text[i] === ")") {
|
|
680
|
+
i++;
|
|
681
|
+
if (text[i] === ";") i++;
|
|
682
|
+
}
|
|
683
|
+
return true;
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
while (i < text.length && !isUnquotedStringDelimiter(text[i]) && !isQuote(text[i]) && (!isKey || text[i] !== ":")) i++;
|
|
687
|
+
if (text[i - 1] === ":" && regexUrlStart.test(text.substring(start, i + 2))) while (i < text.length && regexUrlChar.test(text[i])) i++;
|
|
688
|
+
if (i > start) {
|
|
689
|
+
while (isWhitespace(text, i - 1) && i > 0) i--;
|
|
690
|
+
const symbol = text.slice(start, i);
|
|
691
|
+
output += symbol === "undefined" ? "null" : JSON.stringify(symbol);
|
|
692
|
+
if (text[i] === "\"") i++;
|
|
693
|
+
return true;
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
function parseRegex() {
|
|
697
|
+
if (text[i] === "/") {
|
|
698
|
+
const start = i;
|
|
699
|
+
i++;
|
|
700
|
+
while (i < text.length && (text[i] !== "/" || text[i - 1] === "\\")) i++;
|
|
701
|
+
i++;
|
|
702
|
+
output += JSON.stringify(text.substring(start, i));
|
|
703
|
+
return true;
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
function prevNonWhitespaceIndex(start) {
|
|
707
|
+
let prev = start;
|
|
708
|
+
while (prev > 0 && isWhitespace(text, prev)) prev--;
|
|
709
|
+
return prev;
|
|
710
|
+
}
|
|
711
|
+
function nextQuoteIsEndQuote(index) {
|
|
712
|
+
let next = index + 1;
|
|
713
|
+
while (next < text.length && isWhitespace(text, next)) next++;
|
|
714
|
+
return next >= text.length || isDelimiter(text[next]);
|
|
715
|
+
}
|
|
716
|
+
function atEndOfNumber() {
|
|
717
|
+
return i >= text.length || isDelimiter(text[i]) || isWhitespace(text, i);
|
|
718
|
+
}
|
|
719
|
+
function throwInvalidCharacter(char) {
|
|
720
|
+
throw new JSONRepairError(`Invalid character ${JSON.stringify(char)}`, i);
|
|
721
|
+
}
|
|
722
|
+
function throwUnexpectedCharacter() {
|
|
723
|
+
throw new JSONRepairError(`Unexpected character ${JSON.stringify(text[i])}`, i);
|
|
724
|
+
}
|
|
725
|
+
function throwUnexpectedEnd() {
|
|
726
|
+
throw new JSONRepairError("Unexpected end of json string", text.length);
|
|
727
|
+
}
|
|
728
|
+
function throwObjectKeyExpected() {
|
|
729
|
+
throw new JSONRepairError("Object key expected", i);
|
|
730
|
+
}
|
|
731
|
+
function throwColonExpected() {
|
|
732
|
+
throw new JSONRepairError("Colon expected", i);
|
|
733
|
+
}
|
|
734
|
+
function throwInvalidUnicodeCharacter() {
|
|
735
|
+
throw new JSONRepairError(`Invalid unicode character "${text.slice(i, i + 6)}"`, i);
|
|
736
|
+
}
|
|
737
|
+
}
|
|
738
|
+
function atEndOfBlockComment(text, i) {
|
|
739
|
+
return text[i] === "*" && text[i + 1] === "/";
|
|
740
|
+
}
|
|
741
|
+
//#endregion
|
|
742
|
+
export { jsonrepair as t };
|