UNPKG

@angular/core

Version:

Angular - the core framework

1,218 lines (1,210 loc) • 79.1 kB
'use strict'; /** * @license Angular v22.0.0 * (c) 2010-2026 Google LLC. https://angular.dev/ * License: MIT */ 'use strict'; var require$$0$1 = require('node:os'); function getAugmentedNamespace(n) { if (Object.prototype.hasOwnProperty.call(n, '__esModule')) return n; var f = n.default; if (typeof f == "function") { var a = function a () { var isInstance = false; try { isInstance = this instanceof a; } catch {} if (isInstance) { return Reflect.construct(f, arguments, this.constructor); } return f.apply(this, arguments); }; a.prototype = f.prototype; } else a = {}; Object.defineProperty(a, '__esModule', {value: true}); Object.keys(n).forEach(function (k) { var d = Object.getOwnPropertyDescriptor(n, k); Object.defineProperty(a, k, d.get ? d : { enumerable: true, get: function () { return n[k]; } }); }); return a; } var jsonFile = {}; /*--------------------------------------------------------------------------------------------- * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ /** * Creates a JSON scanner on the given text. * If ignoreTrivia is set, whitespaces or comments are ignored. */ function createScanner$1(text, ignoreTrivia = false) { const len = text.length; let pos = 0, value = '', tokenOffset = 0, token = 16 /* SyntaxKind.Unknown */, lineNumber = 0, lineStartOffset = 0, tokenLineStartOffset = 0, prevTokenLineStartOffset = 0, scanError = 0 /* ScanError.None */; function scanHexDigits(count, exact) { let digits = 0; let value = 0; while (digits < count || false) { let ch = text.charCodeAt(pos); if (ch >= 48 /* CharacterCodes._0 */ && ch <= 57 /* CharacterCodes._9 */) { value = value * 16 + ch - 48 /* CharacterCodes._0 */; } else if (ch >= 65 /* CharacterCodes.A */ && ch <= 70 /* CharacterCodes.F */) { value = value * 16 + ch - 65 /* CharacterCodes.A */ + 10; } else if (ch >= 97 /* CharacterCodes.a */ && ch <= 102 /* CharacterCodes.f */) { value = value * 16 + ch - 97 /* CharacterCodes.a */ + 10; } else { break; } pos++; digits++; } if (digits < count) { value = -1; } return value; } function setPosition(newPosition) { pos = newPosition; value = ''; tokenOffset = 0; token = 16 /* SyntaxKind.Unknown */; scanError = 0 /* ScanError.None */; } function scanNumber() { let start = pos; if (text.charCodeAt(pos) === 48 /* CharacterCodes._0 */) { pos++; } else { pos++; while (pos < text.length && isDigit(text.charCodeAt(pos))) { pos++; } } if (pos < text.length && text.charCodeAt(pos) === 46 /* CharacterCodes.dot */) { pos++; if (pos < text.length && isDigit(text.charCodeAt(pos))) { pos++; while (pos < text.length && isDigit(text.charCodeAt(pos))) { pos++; } } else { scanError = 3 /* ScanError.UnexpectedEndOfNumber */; return text.substring(start, pos); } } let end = pos; if (pos < text.length && (text.charCodeAt(pos) === 69 /* CharacterCodes.E */ || text.charCodeAt(pos) === 101 /* CharacterCodes.e */)) { pos++; if (pos < text.length && text.charCodeAt(pos) === 43 /* CharacterCodes.plus */ || text.charCodeAt(pos) === 45 /* CharacterCodes.minus */) { pos++; } if (pos < text.length && isDigit(text.charCodeAt(pos))) { pos++; while (pos < text.length && isDigit(text.charCodeAt(pos))) { pos++; } end = pos; } else { scanError = 3 /* ScanError.UnexpectedEndOfNumber */; } } return text.substring(start, end); } function scanString() { let result = '', start = pos; while (true) { if (pos >= len) { result += text.substring(start, pos); scanError = 2 /* ScanError.UnexpectedEndOfString */; break; } const ch = text.charCodeAt(pos); if (ch === 34 /* CharacterCodes.doubleQuote */) { result += text.substring(start, pos); pos++; break; } if (ch === 92 /* CharacterCodes.backslash */) { result += text.substring(start, pos); pos++; if (pos >= len) { scanError = 2 /* ScanError.UnexpectedEndOfString */; break; } const ch2 = text.charCodeAt(pos++); switch (ch2) { case 34 /* CharacterCodes.doubleQuote */: result += '\"'; break; case 92 /* CharacterCodes.backslash */: result += '\\'; break; case 47 /* CharacterCodes.slash */: result += '/'; break; case 98 /* CharacterCodes.b */: result += '\b'; break; case 102 /* CharacterCodes.f */: result += '\f'; break; case 110 /* CharacterCodes.n */: result += '\n'; break; case 114 /* CharacterCodes.r */: result += '\r'; break; case 116 /* CharacterCodes.t */: result += '\t'; break; case 117 /* CharacterCodes.u */: const ch3 = scanHexDigits(4); if (ch3 >= 0) { result += String.fromCharCode(ch3); } else { scanError = 4 /* ScanError.InvalidUnicode */; } break; default: scanError = 5 /* ScanError.InvalidEscapeCharacter */; } start = pos; continue; } if (ch >= 0 && ch <= 0x1f) { if (isLineBreak(ch)) { result += text.substring(start, pos); scanError = 2 /* ScanError.UnexpectedEndOfString */; break; } else { scanError = 6 /* ScanError.InvalidCharacter */; // mark as error but continue with string } } pos++; } return result; } function scanNext() { value = ''; scanError = 0 /* ScanError.None */; tokenOffset = pos; lineStartOffset = lineNumber; prevTokenLineStartOffset = tokenLineStartOffset; if (pos >= len) { // at the end tokenOffset = len; return token = 17 /* SyntaxKind.EOF */; } let code = text.charCodeAt(pos); // trivia: whitespace if (isWhiteSpace(code)) { do { pos++; value += String.fromCharCode(code); code = text.charCodeAt(pos); } while (isWhiteSpace(code)); return token = 15 /* SyntaxKind.Trivia */; } // trivia: newlines if (isLineBreak(code)) { pos++; value += String.fromCharCode(code); if (code === 13 /* CharacterCodes.carriageReturn */ && text.charCodeAt(pos) === 10 /* CharacterCodes.lineFeed */) { pos++; value += '\n'; } lineNumber++; tokenLineStartOffset = pos; return token = 14 /* SyntaxKind.LineBreakTrivia */; } switch (code) { // tokens: []{}:, case 123 /* CharacterCodes.openBrace */: pos++; return token = 1 /* SyntaxKind.OpenBraceToken */; case 125 /* CharacterCodes.closeBrace */: pos++; return token = 2 /* SyntaxKind.CloseBraceToken */; case 91 /* CharacterCodes.openBracket */: pos++; return token = 3 /* SyntaxKind.OpenBracketToken */; case 93 /* CharacterCodes.closeBracket */: pos++; return token = 4 /* SyntaxKind.CloseBracketToken */; case 58 /* CharacterCodes.colon */: pos++; return token = 6 /* SyntaxKind.ColonToken */; case 44 /* CharacterCodes.comma */: pos++; return token = 5 /* SyntaxKind.CommaToken */; // strings case 34 /* CharacterCodes.doubleQuote */: pos++; value = scanString(); return token = 10 /* SyntaxKind.StringLiteral */; // comments case 47 /* CharacterCodes.slash */: const start = pos - 1; // Single-line comment if (text.charCodeAt(pos + 1) === 47 /* CharacterCodes.slash */) { pos += 2; while (pos < len) { if (isLineBreak(text.charCodeAt(pos))) { break; } pos++; } value = text.substring(start, pos); return token = 12 /* SyntaxKind.LineCommentTrivia */; } // Multi-line comment if (text.charCodeAt(pos + 1) === 42 /* CharacterCodes.asterisk */) { pos += 2; const safeLength = len - 1; // For lookahead. let commentClosed = false; while (pos < safeLength) { const ch = text.charCodeAt(pos); if (ch === 42 /* CharacterCodes.asterisk */ && text.charCodeAt(pos + 1) === 47 /* CharacterCodes.slash */) { pos += 2; commentClosed = true; break; } pos++; if (isLineBreak(ch)) { if (ch === 13 /* CharacterCodes.carriageReturn */ && text.charCodeAt(pos) === 10 /* CharacterCodes.lineFeed */) { pos++; } lineNumber++; tokenLineStartOffset = pos; } } if (!commentClosed) { pos++; scanError = 1 /* ScanError.UnexpectedEndOfComment */; } value = text.substring(start, pos); return token = 13 /* SyntaxKind.BlockCommentTrivia */; } // just a single slash value += String.fromCharCode(code); pos++; return token = 16 /* SyntaxKind.Unknown */; // numbers case 45 /* CharacterCodes.minus */: value += String.fromCharCode(code); pos++; if (pos === len || !isDigit(text.charCodeAt(pos))) { return token = 16 /* SyntaxKind.Unknown */; } // found a minus, followed by a number so // we fall through to proceed with scanning // numbers case 48 /* CharacterCodes._0 */: case 49 /* CharacterCodes._1 */: case 50 /* CharacterCodes._2 */: case 51 /* CharacterCodes._3 */: case 52 /* CharacterCodes._4 */: case 53 /* CharacterCodes._5 */: case 54 /* CharacterCodes._6 */: case 55 /* CharacterCodes._7 */: case 56 /* CharacterCodes._8 */: case 57 /* CharacterCodes._9 */: value += scanNumber(); return token = 11 /* SyntaxKind.NumericLiteral */; // literals and unknown symbols default: // is a literal? Read the full word. while (pos < len && isUnknownContentCharacter(code)) { pos++; code = text.charCodeAt(pos); } if (tokenOffset !== pos) { value = text.substring(tokenOffset, pos); // keywords: true, false, null switch (value) { case 'true': return token = 8 /* SyntaxKind.TrueKeyword */; case 'false': return token = 9 /* SyntaxKind.FalseKeyword */; case 'null': return token = 7 /* SyntaxKind.NullKeyword */; } return token = 16 /* SyntaxKind.Unknown */; } // some value += String.fromCharCode(code); pos++; return token = 16 /* SyntaxKind.Unknown */; } } function isUnknownContentCharacter(code) { if (isWhiteSpace(code) || isLineBreak(code)) { return false; } switch (code) { case 125 /* CharacterCodes.closeBrace */: case 93 /* CharacterCodes.closeBracket */: case 123 /* CharacterCodes.openBrace */: case 91 /* CharacterCodes.openBracket */: case 34 /* CharacterCodes.doubleQuote */: case 58 /* CharacterCodes.colon */: case 44 /* CharacterCodes.comma */: case 47 /* CharacterCodes.slash */: return false; } return true; } function scanNextNonTrivia() { let result; do { result = scanNext(); } while (result >= 12 /* SyntaxKind.LineCommentTrivia */ && result <= 15 /* SyntaxKind.Trivia */); return result; } return { setPosition: setPosition, getPosition: () => pos, scan: ignoreTrivia ? scanNextNonTrivia : scanNext, getToken: () => token, getTokenValue: () => value, getTokenOffset: () => tokenOffset, getTokenLength: () => pos - tokenOffset, getTokenStartLine: () => lineStartOffset, getTokenStartCharacter: () => tokenOffset - prevTokenLineStartOffset, getTokenError: () => scanError, }; } function isWhiteSpace(ch) { return ch === 32 /* CharacterCodes.space */ || ch === 9 /* CharacterCodes.tab */; } function isLineBreak(ch) { return ch === 10 /* CharacterCodes.lineFeed */ || ch === 13 /* CharacterCodes.carriageReturn */; } function isDigit(ch) { return ch >= 48 /* CharacterCodes._0 */ && ch <= 57 /* CharacterCodes._9 */; } var CharacterCodes; (function (CharacterCodes) { CharacterCodes[CharacterCodes["lineFeed"] = 10] = "lineFeed"; CharacterCodes[CharacterCodes["carriageReturn"] = 13] = "carriageReturn"; CharacterCodes[CharacterCodes["space"] = 32] = "space"; CharacterCodes[CharacterCodes["_0"] = 48] = "_0"; CharacterCodes[CharacterCodes["_1"] = 49] = "_1"; CharacterCodes[CharacterCodes["_2"] = 50] = "_2"; CharacterCodes[CharacterCodes["_3"] = 51] = "_3"; CharacterCodes[CharacterCodes["_4"] = 52] = "_4"; CharacterCodes[CharacterCodes["_5"] = 53] = "_5"; CharacterCodes[CharacterCodes["_6"] = 54] = "_6"; CharacterCodes[CharacterCodes["_7"] = 55] = "_7"; CharacterCodes[CharacterCodes["_8"] = 56] = "_8"; CharacterCodes[CharacterCodes["_9"] = 57] = "_9"; CharacterCodes[CharacterCodes["a"] = 97] = "a"; CharacterCodes[CharacterCodes["b"] = 98] = "b"; CharacterCodes[CharacterCodes["c"] = 99] = "c"; CharacterCodes[CharacterCodes["d"] = 100] = "d"; CharacterCodes[CharacterCodes["e"] = 101] = "e"; CharacterCodes[CharacterCodes["f"] = 102] = "f"; CharacterCodes[CharacterCodes["g"] = 103] = "g"; CharacterCodes[CharacterCodes["h"] = 104] = "h"; CharacterCodes[CharacterCodes["i"] = 105] = "i"; CharacterCodes[CharacterCodes["j"] = 106] = "j"; CharacterCodes[CharacterCodes["k"] = 107] = "k"; CharacterCodes[CharacterCodes["l"] = 108] = "l"; CharacterCodes[CharacterCodes["m"] = 109] = "m"; CharacterCodes[CharacterCodes["n"] = 110] = "n"; CharacterCodes[CharacterCodes["o"] = 111] = "o"; CharacterCodes[CharacterCodes["p"] = 112] = "p"; CharacterCodes[CharacterCodes["q"] = 113] = "q"; CharacterCodes[CharacterCodes["r"] = 114] = "r"; CharacterCodes[CharacterCodes["s"] = 115] = "s"; CharacterCodes[CharacterCodes["t"] = 116] = "t"; CharacterCodes[CharacterCodes["u"] = 117] = "u"; CharacterCodes[CharacterCodes["v"] = 118] = "v"; CharacterCodes[CharacterCodes["w"] = 119] = "w"; CharacterCodes[CharacterCodes["x"] = 120] = "x"; CharacterCodes[CharacterCodes["y"] = 121] = "y"; CharacterCodes[CharacterCodes["z"] = 122] = "z"; CharacterCodes[CharacterCodes["A"] = 65] = "A"; CharacterCodes[CharacterCodes["B"] = 66] = "B"; CharacterCodes[CharacterCodes["C"] = 67] = "C"; CharacterCodes[CharacterCodes["D"] = 68] = "D"; CharacterCodes[CharacterCodes["E"] = 69] = "E"; CharacterCodes[CharacterCodes["F"] = 70] = "F"; CharacterCodes[CharacterCodes["G"] = 71] = "G"; CharacterCodes[CharacterCodes["H"] = 72] = "H"; CharacterCodes[CharacterCodes["I"] = 73] = "I"; CharacterCodes[CharacterCodes["J"] = 74] = "J"; CharacterCodes[CharacterCodes["K"] = 75] = "K"; CharacterCodes[CharacterCodes["L"] = 76] = "L"; CharacterCodes[CharacterCodes["M"] = 77] = "M"; CharacterCodes[CharacterCodes["N"] = 78] = "N"; CharacterCodes[CharacterCodes["O"] = 79] = "O"; CharacterCodes[CharacterCodes["P"] = 80] = "P"; CharacterCodes[CharacterCodes["Q"] = 81] = "Q"; CharacterCodes[CharacterCodes["R"] = 82] = "R"; CharacterCodes[CharacterCodes["S"] = 83] = "S"; CharacterCodes[CharacterCodes["T"] = 84] = "T"; CharacterCodes[CharacterCodes["U"] = 85] = "U"; CharacterCodes[CharacterCodes["V"] = 86] = "V"; CharacterCodes[CharacterCodes["W"] = 87] = "W"; CharacterCodes[CharacterCodes["X"] = 88] = "X"; CharacterCodes[CharacterCodes["Y"] = 89] = "Y"; CharacterCodes[CharacterCodes["Z"] = 90] = "Z"; CharacterCodes[CharacterCodes["asterisk"] = 42] = "asterisk"; CharacterCodes[CharacterCodes["backslash"] = 92] = "backslash"; CharacterCodes[CharacterCodes["closeBrace"] = 125] = "closeBrace"; CharacterCodes[CharacterCodes["closeBracket"] = 93] = "closeBracket"; CharacterCodes[CharacterCodes["colon"] = 58] = "colon"; CharacterCodes[CharacterCodes["comma"] = 44] = "comma"; CharacterCodes[CharacterCodes["dot"] = 46] = "dot"; CharacterCodes[CharacterCodes["doubleQuote"] = 34] = "doubleQuote"; CharacterCodes[CharacterCodes["minus"] = 45] = "minus"; CharacterCodes[CharacterCodes["openBrace"] = 123] = "openBrace"; CharacterCodes[CharacterCodes["openBracket"] = 91] = "openBracket"; CharacterCodes[CharacterCodes["plus"] = 43] = "plus"; CharacterCodes[CharacterCodes["slash"] = 47] = "slash"; CharacterCodes[CharacterCodes["formFeed"] = 12] = "formFeed"; CharacterCodes[CharacterCodes["tab"] = 9] = "tab"; })(CharacterCodes || (CharacterCodes = {})); const cachedSpaces = new Array(20).fill(0).map((_, index) => { return ' '.repeat(index); }); const maxCachedValues = 200; const cachedBreakLinesWithSpaces = { ' ': { '\n': new Array(maxCachedValues).fill(0).map((_, index) => { return '\n' + ' '.repeat(index); }), '\r': new Array(maxCachedValues).fill(0).map((_, index) => { return '\r' + ' '.repeat(index); }), '\r\n': new Array(maxCachedValues).fill(0).map((_, index) => { return '\r\n' + ' '.repeat(index); }), }, '\t': { '\n': new Array(maxCachedValues).fill(0).map((_, index) => { return '\n' + '\t'.repeat(index); }), '\r': new Array(maxCachedValues).fill(0).map((_, index) => { return '\r' + '\t'.repeat(index); }), '\r\n': new Array(maxCachedValues).fill(0).map((_, index) => { return '\r\n' + '\t'.repeat(index); }), } }; const supportedEols = ['\n', '\r', '\r\n']; /*--------------------------------------------------------------------------------------------- * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ function format$1(documentText, range, options) { let initialIndentLevel; let formatText; let formatTextStart; let rangeStart; let rangeEnd; if (range) { rangeStart = range.offset; rangeEnd = rangeStart + range.length; formatTextStart = rangeStart; while (formatTextStart > 0 && !isEOL(documentText, formatTextStart - 1)) { formatTextStart--; } let endOffset = rangeEnd; while (endOffset < documentText.length && !isEOL(documentText, endOffset)) { endOffset++; } formatText = documentText.substring(formatTextStart, endOffset); initialIndentLevel = computeIndentLevel(formatText, options); } else { formatText = documentText; initialIndentLevel = 0; formatTextStart = 0; rangeStart = 0; rangeEnd = documentText.length; } const eol = getEOL(options, documentText); const eolFastPathSupported = supportedEols.includes(eol); let numberLineBreaks = 0; let indentLevel = 0; let indentValue; if (options.insertSpaces) { indentValue = cachedSpaces[options.tabSize || 4] ?? repeat(cachedSpaces[1], options.tabSize || 4); } else { indentValue = '\t'; } const indentType = indentValue === '\t' ? '\t' : ' '; let scanner = createScanner$1(formatText, false); let hasError = false; function newLinesAndIndent() { if (numberLineBreaks > 1) { return repeat(eol, numberLineBreaks) + repeat(indentValue, initialIndentLevel + indentLevel); } const amountOfSpaces = indentValue.length * (initialIndentLevel + indentLevel); if (!eolFastPathSupported || amountOfSpaces > cachedBreakLinesWithSpaces[indentType][eol].length) { return eol + repeat(indentValue, initialIndentLevel + indentLevel); } if (amountOfSpaces <= 0) { return eol; } return cachedBreakLinesWithSpaces[indentType][eol][amountOfSpaces]; } function scanNext() { let token = scanner.scan(); numberLineBreaks = 0; while (token === 15 /* SyntaxKind.Trivia */ || token === 14 /* SyntaxKind.LineBreakTrivia */) { if (token === 14 /* SyntaxKind.LineBreakTrivia */ && options.keepLines) { numberLineBreaks += 1; } else if (token === 14 /* SyntaxKind.LineBreakTrivia */) { numberLineBreaks = 1; } token = scanner.scan(); } hasError = token === 16 /* SyntaxKind.Unknown */ || scanner.getTokenError() !== 0 /* ScanError.None */; return token; } const editOperations = []; function addEdit(text, startOffset, endOffset) { if (!hasError && (!range || (startOffset < rangeEnd && endOffset > rangeStart)) && documentText.substring(startOffset, endOffset) !== text) { editOperations.push({ offset: startOffset, length: endOffset - startOffset, content: text }); } } let firstToken = scanNext(); if (options.keepLines && numberLineBreaks > 0) { addEdit(repeat(eol, numberLineBreaks), 0, 0); } if (firstToken !== 17 /* SyntaxKind.EOF */) { let firstTokenStart = scanner.getTokenOffset() + formatTextStart; let initialIndent = (indentValue.length * initialIndentLevel < 20) && options.insertSpaces ? cachedSpaces[indentValue.length * initialIndentLevel] : repeat(indentValue, initialIndentLevel); addEdit(initialIndent, formatTextStart, firstTokenStart); } while (firstToken !== 17 /* SyntaxKind.EOF */) { let firstTokenEnd = scanner.getTokenOffset() + scanner.getTokenLength() + formatTextStart; let secondToken = scanNext(); let replaceContent = ''; let needsLineBreak = false; while (numberLineBreaks === 0 && (secondToken === 12 /* SyntaxKind.LineCommentTrivia */ || secondToken === 13 /* SyntaxKind.BlockCommentTrivia */)) { let commentTokenStart = scanner.getTokenOffset() + formatTextStart; addEdit(cachedSpaces[1], firstTokenEnd, commentTokenStart); firstTokenEnd = scanner.getTokenOffset() + scanner.getTokenLength() + formatTextStart; needsLineBreak = secondToken === 12 /* SyntaxKind.LineCommentTrivia */; replaceContent = needsLineBreak ? newLinesAndIndent() : ''; secondToken = scanNext(); } if (secondToken === 2 /* SyntaxKind.CloseBraceToken */) { if (firstToken !== 1 /* SyntaxKind.OpenBraceToken */) { indentLevel--; } if (options.keepLines && numberLineBreaks > 0 || !options.keepLines && firstToken !== 1 /* SyntaxKind.OpenBraceToken */) { replaceContent = newLinesAndIndent(); } else if (options.keepLines) { replaceContent = cachedSpaces[1]; } } else if (secondToken === 4 /* SyntaxKind.CloseBracketToken */) { if (firstToken !== 3 /* SyntaxKind.OpenBracketToken */) { indentLevel--; } if (options.keepLines && numberLineBreaks > 0 || !options.keepLines && firstToken !== 3 /* SyntaxKind.OpenBracketToken */) { replaceContent = newLinesAndIndent(); } else if (options.keepLines) { replaceContent = cachedSpaces[1]; } } else { switch (firstToken) { case 3 /* SyntaxKind.OpenBracketToken */: case 1 /* SyntaxKind.OpenBraceToken */: indentLevel++; if (options.keepLines && numberLineBreaks > 0 || !options.keepLines) { replaceContent = newLinesAndIndent(); } else { replaceContent = cachedSpaces[1]; } break; case 5 /* SyntaxKind.CommaToken */: if (options.keepLines && numberLineBreaks > 0 || !options.keepLines) { replaceContent = newLinesAndIndent(); } else { replaceContent = cachedSpaces[1]; } break; case 12 /* SyntaxKind.LineCommentTrivia */: replaceContent = newLinesAndIndent(); break; case 13 /* SyntaxKind.BlockCommentTrivia */: if (numberLineBreaks > 0) { replaceContent = newLinesAndIndent(); } else if (!needsLineBreak) { replaceContent = cachedSpaces[1]; } break; case 6 /* SyntaxKind.ColonToken */: if (options.keepLines && numberLineBreaks > 0) { replaceContent = newLinesAndIndent(); } else if (!needsLineBreak) { replaceContent = cachedSpaces[1]; } break; case 10 /* SyntaxKind.StringLiteral */: if (options.keepLines && numberLineBreaks > 0) { replaceContent = newLinesAndIndent(); } else if (secondToken === 6 /* SyntaxKind.ColonToken */ && !needsLineBreak) { replaceContent = ''; } break; case 7 /* SyntaxKind.NullKeyword */: case 8 /* SyntaxKind.TrueKeyword */: case 9 /* SyntaxKind.FalseKeyword */: case 11 /* SyntaxKind.NumericLiteral */: case 2 /* SyntaxKind.CloseBraceToken */: case 4 /* SyntaxKind.CloseBracketToken */: if (options.keepLines && numberLineBreaks > 0) { replaceContent = newLinesAndIndent(); } else { if ((secondToken === 12 /* SyntaxKind.LineCommentTrivia */ || secondToken === 13 /* SyntaxKind.BlockCommentTrivia */) && !needsLineBreak) { replaceContent = cachedSpaces[1]; } else if (secondToken !== 5 /* SyntaxKind.CommaToken */ && secondToken !== 17 /* SyntaxKind.EOF */) { hasError = true; } } break; case 16 /* SyntaxKind.Unknown */: hasError = true; break; } if (numberLineBreaks > 0 && (secondToken === 12 /* SyntaxKind.LineCommentTrivia */ || secondToken === 13 /* SyntaxKind.BlockCommentTrivia */)) { replaceContent = newLinesAndIndent(); } } if (secondToken === 17 /* SyntaxKind.EOF */) { if (options.keepLines && numberLineBreaks > 0) { replaceContent = newLinesAndIndent(); } else { replaceContent = options.insertFinalNewline ? eol : ''; } } const secondTokenStart = scanner.getTokenOffset() + formatTextStart; addEdit(replaceContent, firstTokenEnd, secondTokenStart); firstToken = secondToken; } return editOperations; } function repeat(s, count) { let result = ''; for (let i = 0; i < count; i++) { result += s; } return result; } function computeIndentLevel(content, options) { let i = 0; let nChars = 0; const tabSize = options.tabSize || 4; while (i < content.length) { let ch = content.charAt(i); if (ch === cachedSpaces[1]) { nChars++; } else if (ch === '\t') { nChars += tabSize; } else { break; } i++; } return Math.floor(nChars / tabSize); } function getEOL(options, text) { for (let i = 0; i < text.length; i++) { const ch = text.charAt(i); if (ch === '\r') { if (i + 1 < text.length && text.charAt(i + 1) === '\n') { return '\r\n'; } return '\r'; } else if (ch === '\n') { return '\n'; } } return (options && options.eol) || '\n'; } function isEOL(text, offset) { return '\r\n'.indexOf(text.charAt(offset)) !== -1; } /*--------------------------------------------------------------------------------------------- * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ var ParseOptions; (function (ParseOptions) { ParseOptions.DEFAULT = { allowTrailingComma: false }; })(ParseOptions || (ParseOptions = {})); /** * For a given offset, evaluate the location in the JSON document. Each segment in the location path is either a property name or an array index. */ function getLocation$1(text, position) { const segments = []; // strings or numbers const earlyReturnException = new Object(); let previousNode = undefined; const previousNodeInst = { value: {}, offset: 0, length: 0, type: 'object', parent: undefined }; let isAtPropertyKey = false; function setPreviousNode(value, offset, length, type) { previousNodeInst.value = value; previousNodeInst.offset = offset; previousNodeInst.length = length; previousNodeInst.type = type; previousNodeInst.colonOffset = undefined; previousNode = previousNodeInst; } try { visit$1(text, { onObjectBegin: (offset, length) => { if (position <= offset) { throw earlyReturnException; } previousNode = undefined; isAtPropertyKey = position > offset; segments.push(''); // push a placeholder (will be replaced) }, onObjectProperty: (name, offset, length) => { if (position < offset) { throw earlyReturnException; } setPreviousNode(name, offset, length, 'property'); segments[segments.length - 1] = name; if (position <= offset + length) { throw earlyReturnException; } }, onObjectEnd: (offset, length) => { if (position <= offset) { throw earlyReturnException; } previousNode = undefined; segments.pop(); }, onArrayBegin: (offset, length) => { if (position <= offset) { throw earlyReturnException; } previousNode = undefined; segments.push(0); }, onArrayEnd: (offset, length) => { if (position <= offset) { throw earlyReturnException; } previousNode = undefined; segments.pop(); }, onLiteralValue: (value, offset, length) => { if (position < offset) { throw earlyReturnException; } setPreviousNode(value, offset, length, getNodeType(value)); if (position <= offset + length) { throw earlyReturnException; } }, onSeparator: (sep, offset, length) => { if (position <= offset) { throw earlyReturnException; } if (sep === ':' && previousNode && previousNode.type === 'property') { previousNode.colonOffset = offset; isAtPropertyKey = false; previousNode = undefined; } else if (sep === ',') { const last = segments[segments.length - 1]; if (typeof last === 'number') { segments[segments.length - 1] = last + 1; } else { isAtPropertyKey = true; segments[segments.length - 1] = ''; } previousNode = undefined; } } }); } catch (e) { if (e !== earlyReturnException) { throw e; } } return { path: segments, previousNode, isAtPropertyKey, matches: (pattern) => { let k = 0; for (let i = 0; k < pattern.length && i < segments.length; i++) { if (pattern[k] === segments[i] || pattern[k] === '*') { k++; } else if (pattern[k] !== '**') { return false; } } return k === pattern.length; } }; } /** * Parses the given text and returns the object the JSON content represents. On invalid input, the parser tries to be as fault tolerant as possible, but still return a result. * Therefore always check the errors list to find out if the input was valid. */ function parse$1(text, errors = [], options = ParseOptions.DEFAULT) { let currentProperty = null; let currentParent = []; const previousParents = []; function onValue(value) { if (Array.isArray(currentParent)) { currentParent.push(value); } else if (currentProperty !== null) { currentParent[currentProperty] = value; } } const visitor = { onObjectBegin: () => { const object = {}; onValue(object); previousParents.push(currentParent); currentParent = object; currentProperty = null; }, onObjectProperty: (name) => { currentProperty = name; }, onObjectEnd: () => { currentParent = previousParents.pop(); }, onArrayBegin: () => { const array = []; onValue(array); previousParents.push(currentParent); currentParent = array; currentProperty = null; }, onArrayEnd: () => { currentParent = previousParents.pop(); }, onLiteralValue: onValue, onError: (error, offset, length) => { errors.push({ error, offset, length }); } }; visit$1(text, visitor, options); return currentParent[0]; } /** * Parses the given text and returns a tree representation the JSON content. On invalid input, the parser tries to be as fault tolerant as possible, but still return a result. */ function parseTree$1(text, errors = [], options = ParseOptions.DEFAULT) { let currentParent = { type: 'array', offset: -1, length: -1, children: [], parent: undefined }; // artificial root function ensurePropertyComplete(endOffset) { if (currentParent.type === 'property') { currentParent.length = endOffset - currentParent.offset; currentParent = currentParent.parent; } } function onValue(valueNode) { currentParent.children.push(valueNode); return valueNode; } const visitor = { onObjectBegin: (offset) => { currentParent = onValue({ type: 'object', offset, length: -1, parent: currentParent, children: [] }); }, onObjectProperty: (name, offset, length) => { currentParent = onValue({ type: 'property', offset, length: -1, parent: currentParent, children: [] }); currentParent.children.push({ type: 'string', value: name, offset, length, parent: currentParent }); }, onObjectEnd: (offset, length) => { ensurePropertyComplete(offset + length); // in case of a missing value for a property: make sure property is complete currentParent.length = offset + length - currentParent.offset; currentParent = currentParent.parent; ensurePropertyComplete(offset + length); }, onArrayBegin: (offset, length) => { currentParent = onValue({ type: 'array', offset, length: -1, parent: currentParent, children: [] }); }, onArrayEnd: (offset, length) => { currentParent.length = offset + length - currentParent.offset; currentParent = currentParent.parent; ensurePropertyComplete(offset + length); }, onLiteralValue: (value, offset, length) => { onValue({ type: getNodeType(value), offset, length, parent: currentParent, value }); ensurePropertyComplete(offset + length); }, onSeparator: (sep, offset, length) => { if (currentParent.type === 'property') { if (sep === ':') { currentParent.colonOffset = offset; } else if (sep === ',') { ensurePropertyComplete(offset); } } }, onError: (error, offset, length) => { errors.push({ error, offset, length }); } }; visit$1(text, visitor, options); const result = currentParent.children[0]; if (result) { delete result.parent; } return result; } /** * Finds the node at the given path in a JSON DOM. */ function findNodeAtLocation$1(root, path) { if (!root) { return undefined; } let node = root; for (let segment of path) { if (typeof segment === 'string') { if (node.type !== 'object' || !Array.isArray(node.children)) { return undefined; } let found = false; for (const propertyNode of node.children) { if (Array.isArray(propertyNode.children) && propertyNode.children[0].value === segment && propertyNode.children.length === 2) { node = propertyNode.children[1]; found = true; break; } } if (!found) { return undefined; } } else { const index = segment; if (node.type !== 'array' || index < 0 || !Array.isArray(node.children) || index >= node.children.length) { return undefined; } node = node.children[index]; } } return node; } /** * Gets the JSON path of the given JSON DOM node */ function getNodePath$1(node) { if (!node.parent || !node.parent.children) { return []; } const path = getNodePath$1(node.parent); if (node.parent.type === 'property') { const key = node.parent.children[0].value; path.push(key); } else if (node.parent.type === 'array') { const index = node.parent.children.indexOf(node); if (index !== -1) { path.push(index); } } return path; } /** * Evaluates the JavaScript object of the given JSON DOM node */ function getNodeValue$1(node) { switch (node.type) { case 'array': return node.children.map(getNodeValue$1); case 'object': const obj = Object.create(null); for (let prop of node.children) { const valueNode = prop.children[1]; if (valueNode) { obj[prop.children[0].value] = getNodeValue$1(valueNode); } } return obj; case 'null': case 'string': case 'number': case 'boolean': return node.value; default: return undefined; } } function contains(node, offset, includeRightBound = false) { return (offset >= node.offset && offset < (node.offset + node.length)) || includeRightBound && (offset === (node.offset + node.length)); } /** * Finds the most inner node at the given offset. If includeRightBound is set, also finds nodes that end at the given offset. */ function findNodeAtOffset$1(node, offset, includeRightBound = false) { if (contains(node, offset, includeRightBound)) { const children = node.children; if (Array.isArray(children)) { for (let i = 0; i < children.length && children[i].offset <= offset; i++) { const item = findNodeAtOffset$1(children[i], offset, includeRightBound); if (item) { return item; } } } return node; } return undefined; } /** * Parses the given text and invokes the visitor functions for each object, array and literal reached. */ function visit$1(text, visitor, options = ParseOptions.DEFAULT) { const _scanner = createScanner$1(text, false); // Important: Only pass copies of this to visitor functions to prevent accidental modification, and // to not affect visitor functions which stored a reference to a previous JSONPath const _jsonPath = []; // Depth of onXXXBegin() callbacks suppressed. onXXXEnd() decrements this if it isn't 0 already. // Callbacks are only called when this value is 0. let suppressedCallbacks = 0; function toNoArgVisit(visitFunction) { return visitFunction ? () => suppressedCallbacks === 0 && visitFunction(_scanner.getTokenOffset(), _scanner.getTokenLength(), _scanner.getTokenStartLine(), _scanner.getTokenStartCharacter()) : () => true; } function toOneArgVisit(visitFunction) { return visitFunction ? (arg) => suppressedCallbacks === 0 && visitFunction(arg, _scanner.getTokenOffset(), _scanner.getTokenLength(), _scanner.getTokenStartLine(), _scanner.getTokenStartCharacter()) : () => true; } function toOneArgVisitWithPath(visitFunction) { return visitFunction ? (arg) => suppressedCallbacks === 0 && visitFunction(arg, _scanner.getTokenOffset(), _scanner.getTokenLength(), _scanner.getTokenStartLine(), _scanner.getTokenStartCharacter(), () => _jsonPath.slice()) : () => true; } function toBeginVisit(visitFunction) { return visitFunction ? () => { if (suppressedCallbacks > 0) { suppressedCallbacks++; } else { let cbReturn = visitFunction(_scanner.getTokenOffset(), _scanner.getTokenLength(), _scanner.getTokenStartLine(), _scanner.getTokenStartCharacter(), () => _jsonPath.slice()); if (cbReturn === false) { suppressedCallbacks = 1; } } } : () => true; } function toEndVisit(visitFunction) { return visitFunction ? () => { if (suppressedCallbacks > 0) { suppressedCallbacks--; } if (suppressedCallbacks === 0) { visitFunction(_scanner.getTokenOffset(), _scanner.getTokenLength(), _scanner.getTokenStartLine(), _scanner.getTokenStartCharacter()); } } : () => true; } const onObjectBegin = toBeginVisit(visitor.onObjectBegin), onObjectProperty = toOneArgVisitWithPath(visitor.onObjectProperty), onObjectEnd = toEndVisit(visitor.onObjectEnd), onArrayBegin = toBeginVisit(visitor.onArrayBegin), onArrayEnd = toEndVisit(visitor.onArrayEnd), onLiteralValue = toOneArgVisitWithPath(visitor.onLiteralValue), onSeparator = toOneArgVisit(visitor.onSeparator), onComment = toNoArgVisit(visitor.onComment), onError = toOneArgVisit(visitor.onError); const disallowComments = options && options.disallowComments; const allowTrailingComma = options && options.allowTrailingComma; function scanNext() { while (true) { const token = _scanner.scan(); switch (_scanner.getTokenError()) { case 4 /* ScanError.InvalidUnicode */: handleError(14 /* ParseErrorCode.InvalidUnicode */); break; case 5 /* ScanError.InvalidEscapeCharacter */: handleError(15 /* ParseErrorCode.InvalidEscapeCharacter */); break; case 3 /* ScanError.UnexpectedEndOfNumber */: handleError(13 /* ParseErrorCode.UnexpectedEndOfNumber */); break; case 1 /* ScanError.UnexpectedEndOfComment */: if (!disallowComments) { handleError(11 /* ParseErrorCode.UnexpectedEndOfComment */); } break; case 2 /* ScanError.UnexpectedEndOfString */: handleError(12 /* ParseErrorCode.UnexpectedEndOfString */); break; case 6 /* ScanError.InvalidCharacter */: handleError(16 /* ParseErrorCode.InvalidCharacter */); break; } switch (token) { case 12 /* SyntaxKind.LineCommentTrivia */: case 13 /* SyntaxKind.BlockCommentTrivia */: if (disallowComments) { handleError(10 /* ParseErrorCode.InvalidCommentToken */); } else { onComment(); } break; case 16 /* SyntaxKind.Unknown */: handleError(1 /* ParseErrorCode.InvalidSymbol */); break; case 15 /* SyntaxKind.Trivia */: case 14 /* SyntaxKind.LineBreakTrivia */: break; default: return token; } } } function handleError(error, skipUntilAfter = [], skipUntil = []) { onError(error); if (skipUntilAfter.length + skipUntil.length > 0) { let token = _scanner.getToken(); while (token !== 17 /* SyntaxKind.EOF */) { if (skipUntilAfter.indexOf(token) !== -1) { scanNext(); break; } else if (skipUntil.indexOf(token) !== -1) {