main.js 7.0 KB

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