main.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*---------------------------------------------------------------------------------------------
  2. * Copyright (c) Microsoft Corporation. All rights reserved.
  3. * Licensed under the MIT License. See License.txt in the project root for license information.
  4. *--------------------------------------------------------------------------------------------*/
  5. 'use strict';
  6. import * as formatter from './impl/format';
  7. import * as edit from './impl/edit';
  8. import * as scanner from './impl/scanner';
  9. import * as parser from './impl/parser';
  10. /**
  11. * Creates a JSON scanner on the given text.
  12. * If ignoreTrivia is set, whitespaces or comments are ignored.
  13. */
  14. export var createScanner = scanner.createScanner;
  15. /**
  16. * 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.
  17. */
  18. export var getLocation = parser.getLocation;
  19. /**
  20. * 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.
  21. * Therefore, always check the errors list to find out if the input was valid.
  22. */
  23. export var parse = parser.parse;
  24. /**
  25. * 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.
  26. */
  27. export var parseTree = parser.parseTree;
  28. /**
  29. * Finds the node at the given path in a JSON DOM.
  30. */
  31. export var findNodeAtLocation = parser.findNodeAtLocation;
  32. /**
  33. * Finds the innermost node at the given offset. If includeRightBound is set, also finds nodes that end at the given offset.
  34. */
  35. export var findNodeAtOffset = parser.findNodeAtOffset;
  36. /**
  37. * Gets the JSON path of the given JSON DOM node
  38. */
  39. export var getNodePath = parser.getNodePath;
  40. /**
  41. * Evaluates the JavaScript object of the given JSON DOM node
  42. */
  43. export var getNodeValue = parser.getNodeValue;
  44. /**
  45. * Parses the given text and invokes the visitor functions for each object, array and literal reached.
  46. */
  47. export var visit = parser.visit;
  48. /**
  49. * Takes JSON with JavaScript-style comments and remove
  50. * them. Optionally replaces every none-newline character
  51. * of comments with a replaceCharacter
  52. */
  53. export var stripComments = parser.stripComments;
  54. export function printParseErrorCode(code) {
  55. switch (code) {
  56. case 1 /* InvalidSymbol */: return 'InvalidSymbol';
  57. case 2 /* InvalidNumberFormat */: return 'InvalidNumberFormat';
  58. case 3 /* PropertyNameExpected */: return 'PropertyNameExpected';
  59. case 4 /* ValueExpected */: return 'ValueExpected';
  60. case 5 /* ColonExpected */: return 'ColonExpected';
  61. case 6 /* CommaExpected */: return 'CommaExpected';
  62. case 7 /* CloseBraceExpected */: return 'CloseBraceExpected';
  63. case 8 /* CloseBracketExpected */: return 'CloseBracketExpected';
  64. case 9 /* EndOfFileExpected */: return 'EndOfFileExpected';
  65. case 10 /* InvalidCommentToken */: return 'InvalidCommentToken';
  66. case 11 /* UnexpectedEndOfComment */: return 'UnexpectedEndOfComment';
  67. case 12 /* UnexpectedEndOfString */: return 'UnexpectedEndOfString';
  68. case 13 /* UnexpectedEndOfNumber */: return 'UnexpectedEndOfNumber';
  69. case 14 /* InvalidUnicode */: return 'InvalidUnicode';
  70. case 15 /* InvalidEscapeCharacter */: return 'InvalidEscapeCharacter';
  71. case 16 /* InvalidCharacter */: return 'InvalidCharacter';
  72. }
  73. return '<unknown ParseErrorCode>';
  74. }
  75. /**
  76. * Computes the edits needed to format a JSON document.
  77. *
  78. * @param documentText The input text
  79. * @param range The range to format or `undefined` to format the full content
  80. * @param options The formatting options
  81. * @returns A list of edit operations describing the formatting changes to the original document. Edits can be either inserts, replacements or
  82. * 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
  83. * text in the original document. However, multiple edits can have
  84. * 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.
  85. * To apply edits to an input, you can use `applyEdits`.
  86. */
  87. export function format(documentText, range, options) {
  88. return formatter.format(documentText, range, options);
  89. }
  90. /**
  91. * Computes the edits needed to modify a value in the JSON document.
  92. *
  93. * @param documentText The input text
  94. * @param path The path of the value to change. The path represents either to the document root, a property or an array item.
  95. * If the path points to an non-existing property or item, it will be created.
  96. * @param value The new value for the specified property or item. If the value is undefined,
  97. * the property or item will be removed.
  98. * @param options Options
  99. * @returns A list of edit operations describing the formatting changes to the original document. Edits can be either inserts, replacements or
  100. * 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
  101. * text in the original document. However, multiple edits can have
  102. * 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.
  103. * To apply edits to an input, you can use `applyEdits`.
  104. */
  105. export function modify(text, path, value, options) {
  106. return edit.setProperty(text, path, value, options);
  107. }
  108. /**
  109. * Applies edits to a input string.
  110. */
  111. export function applyEdits(text, edits) {
  112. for (var i = edits.length - 1; i >= 0; i--) {
  113. text = edit.applyEdit(text, edits[i]);
  114. }
  115. return text;
  116. }