My dotfiles
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

290 wiersze
10 KiB

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