main.d.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /**
  2. * Creates a JSON scanner on the given text.
  3. * If ignoreTrivia is set, whitespaces or comments are ignored.
  4. */
  5. export declare const createScanner: (text: string, ignoreTrivia?: boolean) => JSONScanner;
  6. export 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. }
  15. export 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. */
  37. export 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. */
  82. export 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. */
  87. export 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. */
  91. export 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. */
  95. export 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. */
  99. export declare const findNodeAtOffset: (root: Node, offset: number, includeRightBound?: boolean) => Node | undefined;
  100. /**
  101. * Gets the JSON path of the given JSON DOM node
  102. */
  103. export declare const getNodePath: (node: Node) => JSONPath;
  104. /**
  105. * Evaluates the JavaScript object of the given JSON DOM node
  106. */
  107. export 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. */
  111. export 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. */
  117. export declare const stripComments: (text: string, replaceCh?: string) => string;
  118. export interface ParseError {
  119. error: ParseErrorCode;
  120. offset: number;
  121. length: number;
  122. }
  123. export 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. }
  141. export declare function printParseErrorCode(code: ParseErrorCode): "InvalidSymbol" | "InvalidNumberFormat" | "PropertyNameExpected" | "ValueExpected" | "ColonExpected" | "CommaExpected" | "CloseBraceExpected" | "CloseBracketExpected" | "EndOfFileExpected" | "InvalidCommentToken" | "UnexpectedEndOfComment" | "UnexpectedEndOfString" | "UnexpectedEndOfNumber" | "InvalidUnicode" | "InvalidEscapeCharacter" | "InvalidCharacter" | "<unknown ParseErrorCode>";
  142. export declare type NodeType = 'object' | 'array' | 'property' | 'string' | 'number' | 'boolean' | 'null';
  143. export 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. }
  152. export declare type Segment = string | number;
  153. export declare type JSONPath = Segment[];
  154. export 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. }
  175. export interface ParseOptions {
  176. disallowComments?: boolean;
  177. allowTrailingComma?: boolean;
  178. allowEmptyContent?: boolean;
  179. }
  180. export 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. */
  221. export 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. */
  238. export 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. }
  248. export 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. */
  278. export declare function format(documentText: string, range: Range | undefined, options: FormattingOptions): Edit[];
  279. /**
  280. * Options used when computing the modification edits
  281. */
  282. export 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. */
  312. export declare function modify(text: string, path: JSONPath, value: any, options: ModificationOptions): Edit[];
  313. /**
  314. * Applies edits to a input string.
  315. */
  316. export declare function applyEdits(text: string, edits: Edit[]): string;