UNPKG

11.1 kBJavaScriptView Raw
1(function (factory) {
2 if (typeof module === "object" && typeof module.exports === "object") {
3 var v = factory(require, exports);
4 if (v !== undefined) module.exports = v;
5 }
6 else if (typeof define === "function" && define.amd) {
7 define(["require", "exports", "./impl/format", "./impl/edit", "./impl/scanner", "./impl/parser"], factory);
8 }
9})(function (require, exports) {
10 /*---------------------------------------------------------------------------------------------
11 * Copyright (c) Microsoft Corporation. All rights reserved.
12 * Licensed under the MIT License. See License.txt in the project root for license information.
13 *--------------------------------------------------------------------------------------------*/
14 'use strict';
15 Object.defineProperty(exports, "__esModule", { value: true });
16 exports.applyEdits = exports.modify = exports.format = exports.printParseErrorCode = exports.ParseErrorCode = exports.stripComments = exports.visit = exports.getNodeValue = exports.getNodePath = exports.findNodeAtOffset = exports.findNodeAtLocation = exports.parseTree = exports.parse = exports.getLocation = exports.SyntaxKind = exports.ScanError = exports.createScanner = void 0;
17 const formatter = require("./impl/format");
18 const edit = require("./impl/edit");
19 const scanner = require("./impl/scanner");
20 const parser = require("./impl/parser");
21 /**
22 * Creates a JSON scanner on the given text.
23 * If ignoreTrivia is set, whitespaces or comments are ignored.
24 */
25 exports.createScanner = scanner.createScanner;
26 var ScanError;
27 (function (ScanError) {
28 ScanError[ScanError["None"] = 0] = "None";
29 ScanError[ScanError["UnexpectedEndOfComment"] = 1] = "UnexpectedEndOfComment";
30 ScanError[ScanError["UnexpectedEndOfString"] = 2] = "UnexpectedEndOfString";
31 ScanError[ScanError["UnexpectedEndOfNumber"] = 3] = "UnexpectedEndOfNumber";
32 ScanError[ScanError["InvalidUnicode"] = 4] = "InvalidUnicode";
33 ScanError[ScanError["InvalidEscapeCharacter"] = 5] = "InvalidEscapeCharacter";
34 ScanError[ScanError["InvalidCharacter"] = 6] = "InvalidCharacter";
35 })(ScanError || (exports.ScanError = ScanError = {}));
36 var SyntaxKind;
37 (function (SyntaxKind) {
38 SyntaxKind[SyntaxKind["OpenBraceToken"] = 1] = "OpenBraceToken";
39 SyntaxKind[SyntaxKind["CloseBraceToken"] = 2] = "CloseBraceToken";
40 SyntaxKind[SyntaxKind["OpenBracketToken"] = 3] = "OpenBracketToken";
41 SyntaxKind[SyntaxKind["CloseBracketToken"] = 4] = "CloseBracketToken";
42 SyntaxKind[SyntaxKind["CommaToken"] = 5] = "CommaToken";
43 SyntaxKind[SyntaxKind["ColonToken"] = 6] = "ColonToken";
44 SyntaxKind[SyntaxKind["NullKeyword"] = 7] = "NullKeyword";
45 SyntaxKind[SyntaxKind["TrueKeyword"] = 8] = "TrueKeyword";
46 SyntaxKind[SyntaxKind["FalseKeyword"] = 9] = "FalseKeyword";
47 SyntaxKind[SyntaxKind["StringLiteral"] = 10] = "StringLiteral";
48 SyntaxKind[SyntaxKind["NumericLiteral"] = 11] = "NumericLiteral";
49 SyntaxKind[SyntaxKind["LineCommentTrivia"] = 12] = "LineCommentTrivia";
50 SyntaxKind[SyntaxKind["BlockCommentTrivia"] = 13] = "BlockCommentTrivia";
51 SyntaxKind[SyntaxKind["LineBreakTrivia"] = 14] = "LineBreakTrivia";
52 SyntaxKind[SyntaxKind["Trivia"] = 15] = "Trivia";
53 SyntaxKind[SyntaxKind["Unknown"] = 16] = "Unknown";
54 SyntaxKind[SyntaxKind["EOF"] = 17] = "EOF";
55 })(SyntaxKind || (exports.SyntaxKind = SyntaxKind = {}));
56 /**
57 * 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.
58 */
59 exports.getLocation = parser.getLocation;
60 /**
61 * 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.
62 * Therefore, always check the errors list to find out if the input was valid.
63 */
64 exports.parse = parser.parse;
65 /**
66 * 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.
67 */
68 exports.parseTree = parser.parseTree;
69 /**
70 * Finds the node at the given path in a JSON DOM.
71 */
72 exports.findNodeAtLocation = parser.findNodeAtLocation;
73 /**
74 * Finds the innermost node at the given offset. If includeRightBound is set, also finds nodes that end at the given offset.
75 */
76 exports.findNodeAtOffset = parser.findNodeAtOffset;
77 /**
78 * Gets the JSON path of the given JSON DOM node
79 */
80 exports.getNodePath = parser.getNodePath;
81 /**
82 * Evaluates the JavaScript object of the given JSON DOM node
83 */
84 exports.getNodeValue = parser.getNodeValue;
85 /**
86 * Parses the given text and invokes the visitor functions for each object, array and literal reached.
87 */
88 exports.visit = parser.visit;
89 /**
90 * Takes JSON with JavaScript-style comments and remove
91 * them. Optionally replaces every none-newline character
92 * of comments with a replaceCharacter
93 */
94 exports.stripComments = parser.stripComments;
95 var ParseErrorCode;
96 (function (ParseErrorCode) {
97 ParseErrorCode[ParseErrorCode["InvalidSymbol"] = 1] = "InvalidSymbol";
98 ParseErrorCode[ParseErrorCode["InvalidNumberFormat"] = 2] = "InvalidNumberFormat";
99 ParseErrorCode[ParseErrorCode["PropertyNameExpected"] = 3] = "PropertyNameExpected";
100 ParseErrorCode[ParseErrorCode["ValueExpected"] = 4] = "ValueExpected";
101 ParseErrorCode[ParseErrorCode["ColonExpected"] = 5] = "ColonExpected";
102 ParseErrorCode[ParseErrorCode["CommaExpected"] = 6] = "CommaExpected";
103 ParseErrorCode[ParseErrorCode["CloseBraceExpected"] = 7] = "CloseBraceExpected";
104 ParseErrorCode[ParseErrorCode["CloseBracketExpected"] = 8] = "CloseBracketExpected";
105 ParseErrorCode[ParseErrorCode["EndOfFileExpected"] = 9] = "EndOfFileExpected";
106 ParseErrorCode[ParseErrorCode["InvalidCommentToken"] = 10] = "InvalidCommentToken";
107 ParseErrorCode[ParseErrorCode["UnexpectedEndOfComment"] = 11] = "UnexpectedEndOfComment";
108 ParseErrorCode[ParseErrorCode["UnexpectedEndOfString"] = 12] = "UnexpectedEndOfString";
109 ParseErrorCode[ParseErrorCode["UnexpectedEndOfNumber"] = 13] = "UnexpectedEndOfNumber";
110 ParseErrorCode[ParseErrorCode["InvalidUnicode"] = 14] = "InvalidUnicode";
111 ParseErrorCode[ParseErrorCode["InvalidEscapeCharacter"] = 15] = "InvalidEscapeCharacter";
112 ParseErrorCode[ParseErrorCode["InvalidCharacter"] = 16] = "InvalidCharacter";
113 })(ParseErrorCode || (exports.ParseErrorCode = ParseErrorCode = {}));
114 function printParseErrorCode(code) {
115 switch (code) {
116 case 1 /* ParseErrorCode.InvalidSymbol */: return 'InvalidSymbol';
117 case 2 /* ParseErrorCode.InvalidNumberFormat */: return 'InvalidNumberFormat';
118 case 3 /* ParseErrorCode.PropertyNameExpected */: return 'PropertyNameExpected';
119 case 4 /* ParseErrorCode.ValueExpected */: return 'ValueExpected';
120 case 5 /* ParseErrorCode.ColonExpected */: return 'ColonExpected';
121 case 6 /* ParseErrorCode.CommaExpected */: return 'CommaExpected';
122 case 7 /* ParseErrorCode.CloseBraceExpected */: return 'CloseBraceExpected';
123 case 8 /* ParseErrorCode.CloseBracketExpected */: return 'CloseBracketExpected';
124 case 9 /* ParseErrorCode.EndOfFileExpected */: return 'EndOfFileExpected';
125 case 10 /* ParseErrorCode.InvalidCommentToken */: return 'InvalidCommentToken';
126 case 11 /* ParseErrorCode.UnexpectedEndOfComment */: return 'UnexpectedEndOfComment';
127 case 12 /* ParseErrorCode.UnexpectedEndOfString */: return 'UnexpectedEndOfString';
128 case 13 /* ParseErrorCode.UnexpectedEndOfNumber */: return 'UnexpectedEndOfNumber';
129 case 14 /* ParseErrorCode.InvalidUnicode */: return 'InvalidUnicode';
130 case 15 /* ParseErrorCode.InvalidEscapeCharacter */: return 'InvalidEscapeCharacter';
131 case 16 /* ParseErrorCode.InvalidCharacter */: return 'InvalidCharacter';
132 }
133 return '<unknown ParseErrorCode>';
134 }
135 exports.printParseErrorCode = printParseErrorCode;
136 /**
137 * Computes the edit operations needed to format a JSON document.
138 *
139 * @param documentText The input text
140 * @param range The range to format or `undefined` to format the full content
141 * @param options The formatting options
142 * @returns The edit operations describing the formatting changes to the original document following the format described in {@linkcode EditResult}.
143 * To apply the edit operations to the input, use {@linkcode applyEdits}.
144 */
145 function format(documentText, range, options) {
146 return formatter.format(documentText, range, options);
147 }
148 exports.format = format;
149 /**
150 * Computes the edit operations needed to modify a value in the JSON document.
151 *
152 * @param documentText The input text
153 * @param path The path of the value to change. The path represents either to the document root, a property or an array item.
154 * If the path points to an non-existing property or item, it will be created.
155 * @param value The new value for the specified property or item. If the value is undefined,
156 * the property or item will be removed.
157 * @param options Options
158 * @returns The edit operations describing the changes to the original document, following the format described in {@linkcode EditResult}.
159 * To apply the edit operations to the input, use {@linkcode applyEdits}.
160 */
161 function modify(text, path, value, options) {
162 return edit.setProperty(text, path, value, options);
163 }
164 exports.modify = modify;
165 /**
166 * Applies edits to an input string.
167 * @param text The input text
168 * @param edits Edit operations following the format described in {@linkcode EditResult}.
169 * @returns The text with the applied edits.
170 * @throws An error if the edit operations are not well-formed as described in {@linkcode EditResult}.
171 */
172 function applyEdits(text, edits) {
173 let sortedEdits = edits.slice(0).sort((a, b) => {
174 const diff = a.offset - b.offset;
175 if (diff === 0) {
176 return a.length - b.length;
177 }
178 return diff;
179 });
180 let lastModifiedOffset = text.length;
181 for (let i = sortedEdits.length - 1; i >= 0; i--) {
182 let e = sortedEdits[i];
183 if (e.offset + e.length <= lastModifiedOffset) {
184 text = edit.applyEdit(text, e);
185 }
186 else {
187 throw new Error('Overlapping edit');
188 }
189 lastModifiedOffset = e.offset;
190 }
191 return text;
192 }
193 exports.applyEdits = applyEdits;
194});