UNPKG

12.8 kBTypeScriptView Raw
1/**
2 * Creates a JSON scanner on the given text.
3 * If ignoreTrivia is set, whitespaces or comments are ignored.
4 */
5export declare const createScanner: (text: string, ignoreTrivia?: boolean) => JSONScanner;
6export declare const enum ScanError {
7 None = 0,
8 UnexpectedEndOfComment = 1,
9 UnexpectedEndOfString = 2,
10 UnexpectedEndOfNumber = 3,
11 InvalidUnicode = 4,
12 InvalidEscapeCharacter = 5,
13 InvalidCharacter = 6
14}
15export declare const enum SyntaxKind {
16 OpenBraceToken = 1,
17 CloseBraceToken = 2,
18 OpenBracketToken = 3,
19 CloseBracketToken = 4,
20 CommaToken = 5,
21 ColonToken = 6,
22 NullKeyword = 7,
23 TrueKeyword = 8,
24 FalseKeyword = 9,
25 StringLiteral = 10,
26 NumericLiteral = 11,
27 LineCommentTrivia = 12,
28 BlockCommentTrivia = 13,
29 LineBreakTrivia = 14,
30 Trivia = 15,
31 Unknown = 16,
32 EOF = 17
33}
34/**
35 * The scanner object, representing a JSON scanner at a position in the input string.
36 */
37export interface JSONScanner {
38 /**
39 * Sets the scan position to a new offset. A call to 'scan' is needed to get the first token.
40 */
41 setPosition(pos: number): void;
42 /**
43 * Read the next token. Returns the token code.
44 */
45 scan(): SyntaxKind;
46 /**
47 * Returns the current scan position, which is after the last read token.
48 */
49 getPosition(): number;
50 /**
51 * Returns the last read token.
52 */
53 getToken(): SyntaxKind;
54 /**
55 * Returns the last read token value. The value for strings is the decoded string content. For numbers it's of type number, for boolean it's true or false.
56 */
57 getTokenValue(): string;
58 /**
59 * The start offset of the last read token.
60 */
61 getTokenOffset(): number;
62 /**
63 * The length of the last read token.
64 */
65 getTokenLength(): number;
66 /**
67 * The zero-based start line number of the last read token.
68 */
69 getTokenStartLine(): number;
70 /**
71 * The zero-based start character (column) of the last read token.
72 */
73 getTokenStartCharacter(): number;
74 /**
75 * An error code of the last scan.
76 */
77 getTokenError(): ScanError;
78}
79/**
80 * 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.
81 */
82export declare const getLocation: (text: string, position: number) => Location;
83/**
84 * 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.
85 * Therefore, always check the errors list to find out if the input was valid.
86 */
87export declare const parse: (text: string, errors?: ParseError[], options?: ParseOptions) => any;
88/**
89 * 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.
90 */
91export declare const parseTree: (text: string, errors?: ParseError[], options?: ParseOptions) => Node | undefined;
92/**
93 * Finds the node at the given path in a JSON DOM.
94 */
95export declare const findNodeAtLocation: (root: Node, path: JSONPath) => Node | undefined;
96/**
97 * Finds the innermost node at the given offset. If includeRightBound is set, also finds nodes that end at the given offset.
98 */
99export declare const findNodeAtOffset: (root: Node, offset: number, includeRightBound?: boolean) => Node | undefined;
100/**
101 * Gets the JSON path of the given JSON DOM node
102 */
103export declare const getNodePath: (node: Node) => JSONPath;
104/**
105 * Evaluates the JavaScript object of the given JSON DOM node
106 */
107export declare const getNodeValue: (node: Node) => any;
108/**
109 * Parses the given text and invokes the visitor functions for each object, array and literal reached.
110 */
111export declare const visit: (text: string, visitor: JSONVisitor, options?: ParseOptions) => any;
112/**
113 * Takes JSON with JavaScript-style comments and remove
114 * them. Optionally replaces every none-newline character
115 * of comments with a replaceCharacter
116 */
117export declare const stripComments: (text: string, replaceCh?: string) => string;
118export interface ParseError {
119 error: ParseErrorCode;
120 offset: number;
121 length: number;
122}
123export declare const enum ParseErrorCode {
124 InvalidSymbol = 1,
125 InvalidNumberFormat = 2,
126 PropertyNameExpected = 3,
127 ValueExpected = 4,
128 ColonExpected = 5,
129 CommaExpected = 6,
130 CloseBraceExpected = 7,
131 CloseBracketExpected = 8,
132 EndOfFileExpected = 9,
133 InvalidCommentToken = 10,
134 UnexpectedEndOfComment = 11,
135 UnexpectedEndOfString = 12,
136 UnexpectedEndOfNumber = 13,
137 InvalidUnicode = 14,
138 InvalidEscapeCharacter = 15,
139 InvalidCharacter = 16
140}
141export declare function printParseErrorCode(code: ParseErrorCode): "InvalidSymbol" | "InvalidNumberFormat" | "PropertyNameExpected" | "ValueExpected" | "ColonExpected" | "CommaExpected" | "CloseBraceExpected" | "CloseBracketExpected" | "EndOfFileExpected" | "InvalidCommentToken" | "UnexpectedEndOfComment" | "UnexpectedEndOfString" | "UnexpectedEndOfNumber" | "InvalidUnicode" | "InvalidEscapeCharacter" | "InvalidCharacter" | "<unknown ParseErrorCode>";
142export declare type NodeType = 'object' | 'array' | 'property' | 'string' | 'number' | 'boolean' | 'null';
143export interface Node {
144 readonly type: NodeType;
145 readonly value?: any;
146 readonly offset: number;
147 readonly length: number;
148 readonly colonOffset?: number;
149 readonly parent?: Node;
150 readonly children?: Node[];
151}
152export declare type Segment = string | number;
153export declare type JSONPath = Segment[];
154export interface Location {
155 /**
156 * The previous property key or literal value (string, number, boolean or null) or undefined.
157 */
158 previousNode?: Node;
159 /**
160 * The path describing the location in the JSON document. The path consists of a sequence of strings
161 * representing an object property or numbers for array indices.
162 */
163 path: JSONPath;
164 /**
165 * Matches the locations path against a pattern consisting of strings (for properties) and numbers (for array indices).
166 * '*' will match a single segment of any property name or index.
167 * '**' will match a sequence of segments of any property name or index, or no segment.
168 */
169 matches: (patterns: JSONPath) => boolean;
170 /**
171 * If set, the location's offset is at a property key.
172 */
173 isAtPropertyKey: boolean;
174}
175export interface ParseOptions {
176 disallowComments?: boolean;
177 allowTrailingComma?: boolean;
178 allowEmptyContent?: boolean;
179}
180export interface JSONVisitor {
181 /**
182 * Invoked when an open brace is encountered and an object is started. The offset and length represent the location of the open brace.
183 */
184 onObjectBegin?: (offset: number, length: number, startLine: number, startCharacter: number) => void;
185 /**
186 * Invoked when a property is encountered. The offset and length represent the location of the property name.
187 */
188 onObjectProperty?: (property: string, offset: number, length: number, startLine: number, startCharacter: number) => void;
189 /**
190 * Invoked when a closing brace is encountered and an object is completed. The offset and length represent the location of the closing brace.
191 */
192 onObjectEnd?: (offset: number, length: number, startLine: number, startCharacter: number) => void;
193 /**
194 * Invoked when an open bracket is encountered. The offset and length represent the location of the open bracket.
195 */
196 onArrayBegin?: (offset: number, length: number, startLine: number, startCharacter: number) => void;
197 /**
198 * Invoked when a closing bracket is encountered. The offset and length represent the location of the closing bracket.
199 */
200 onArrayEnd?: (offset: number, length: number, startLine: number, startCharacter: number) => void;
201 /**
202 * Invoked when a literal value is encountered. The offset and length represent the location of the literal value.
203 */
204 onLiteralValue?: (value: any, offset: number, length: number, startLine: number, startCharacter: number) => void;
205 /**
206 * Invoked when a comma or colon separator is encountered. The offset and length represent the location of the separator.
207 */
208 onSeparator?: (character: string, offset: number, length: number, startLine: number, startCharacter: number) => void;
209 /**
210 * When comments are allowed, invoked when a line or block comment is encountered. The offset and length represent the location of the comment.
211 */
212 onComment?: (offset: number, length: number, startLine: number, startCharacter: number) => void;
213 /**
214 * Invoked on an error.
215 */
216 onError?: (error: ParseErrorCode, offset: number, length: number, startLine: number, startCharacter: number) => void;
217}
218/**
219 * Represents a text modification
220 */
221export interface Edit {
222 /**
223 * The start offset of the modification.
224 */
225 offset: number;
226 /**
227 * The length of the modification. Must not be negative. Empty length represents an *insert*.
228 */
229 length: number;
230 /**
231 * The new content. Empty content represents a *remove*.
232 */
233 content: string;
234}
235/**
236 * A text range in the document
237*/
238export interface Range {
239 /**
240 * The start offset of the range.
241 */
242 offset: number;
243 /**
244 * The length of the range. Must not be negative.
245 */
246 length: number;
247}
248export interface FormattingOptions {
249 /**
250 * If indentation is based on spaces (`insertSpaces` = true), the number of spaces that make an indent.
251 */
252 tabSize?: number;
253 /**
254 * Is indentation based on spaces?
255 */
256 insertSpaces?: boolean;
257 /**
258 * The default 'end of line' character. If not set, '\n' is used as default.
259 */
260 eol?: string;
261 /**
262 * If set, will add a new line at the end of the document.
263 */
264 insertFinalNewline?: boolean;
265}
266/**
267 * Computes the edits needed to format a JSON document.
268 *
269 * @param documentText The input text
270 * @param range The range to format or `undefined` to format the full content
271 * @param options The formatting options
272 * @returns A list of edit operations describing the formatting changes to the original document. Edits can be either inserts, replacements or
273 * removals of text segments. All offsets refer to the original state of the document. No two edits must change or remove the same range of
274 * text in the original document. However, multiple edits can have
275 * the same offset, for example multiple inserts, or an insert followed by a remove or replace. The order in the array defines which edit is applied first.
276 * To apply edits to an input, you can use `applyEdits`.
277 */
278export declare function format(documentText: string, range: Range | undefined, options: FormattingOptions): Edit[];
279/**
280 * Options used when computing the modification edits
281 */
282export interface ModificationOptions {
283 /**
284 * Formatting options. If undefined, the newly inserted code will be inserted unformatted.
285 */
286 formattingOptions?: FormattingOptions;
287 /**
288 * Default false. If `JSONPath` refers to an index of an array and {@property isArrayInsertion} is `true`, then
289 * {@function modify} will insert a new item at that location instead of overwriting its contents.
290 */
291 isArrayInsertion?: boolean;
292 /**
293 * Optional function to define the insertion index given an existing list of properties.
294 */
295 getInsertionIndex?: (properties: string[]) => number;
296}
297/**
298 * Computes the edits needed to modify a value in the JSON document.
299 *
300 * @param documentText The input text
301 * @param path The path of the value to change. The path represents either to the document root, a property or an array item.
302 * If the path points to an non-existing property or item, it will be created.
303 * @param value The new value for the specified property or item. If the value is undefined,
304 * the property or item will be removed.
305 * @param options Options
306 * @returns A list of edit operations describing the formatting changes to the original document. Edits can be either inserts, replacements or
307 * removals of text segments. All offsets refer to the original state of the document. No two edits must change or remove the same range of
308 * text in the original document. However, multiple edits can have
309 * the same offset, for example multiple inserts, or an insert followed by a remove or replace. The order in the array defines which edit is applied first.
310 * To apply edits to an input, you can use `applyEdits`.
311 */
312export declare function modify(text: string, path: JSONPath, value: any, options: ModificationOptions): Edit[];
313/**
314 * Applies edits to a input string.
315 */
316export declare function applyEdits(text: string, edits: Edit[]): string;