版博士V2.0程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

182 lines
6.0 KiB

  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.parseForESLint = void 0;
  4. const espree_1 = require("./modules/espree");
  5. const visitor_keys_1 = require("./visitor-keys");
  6. const convert_1 = require("./convert");
  7. const token_store_1 = require("./token-store");
  8. const semver_1 = require("semver");
  9. const extend_parser_1 = require("./extend-parser");
  10. const DEFAULT_ECMA_VERSION = 2019;
  11. function parseForESLint(code, options) {
  12. const parserOptions = Object.assign({ filePath: "<input>", ecmaVersion: DEFAULT_ECMA_VERSION }, options || {}, {
  13. loc: true,
  14. range: true,
  15. raw: true,
  16. tokens: true,
  17. comment: true,
  18. eslintVisitorKeys: true,
  19. eslintScopeManager: true,
  20. });
  21. parserOptions.ecmaVersion = normalizeEcmaVersion(parserOptions.ecmaVersion);
  22. const ctx = getJSONSyntaxContext(parserOptions.jsonSyntax);
  23. const tokens = [];
  24. const comments = [];
  25. const tokenStore = new token_store_1.TokenStore(tokens);
  26. const nodes = [];
  27. parserOptions.ctx = ctx;
  28. parserOptions.tokenStore = tokenStore;
  29. parserOptions.comments = comments;
  30. parserOptions.nodes = nodes;
  31. const baseAst = (0, extend_parser_1.getParser)().parseExpressionAt(code, 0, parserOptions);
  32. for (const node of nodes) {
  33. ;
  34. node.type = `JSON${node.type}`;
  35. }
  36. const ast = (0, convert_1.convertProgramNode)(baseAst, tokenStore, ctx, code);
  37. ast.tokens = tokens;
  38. ast.comments = comments;
  39. return {
  40. ast,
  41. visitorKeys: (0, visitor_keys_1.getVisitorKeys)(),
  42. services: {
  43. isJSON: true,
  44. },
  45. };
  46. }
  47. exports.parseForESLint = parseForESLint;
  48. function getJSONSyntaxContext(str) {
  49. const upperCase = str === null || str === void 0 ? void 0 : str.toUpperCase();
  50. if (upperCase === "JSON") {
  51. return {
  52. trailingCommas: false,
  53. comments: false,
  54. plusSigns: false,
  55. spacedSigns: false,
  56. leadingOrTrailingDecimalPoints: false,
  57. infinities: false,
  58. nans: false,
  59. numericSeparators: false,
  60. binaryNumericLiterals: false,
  61. octalNumericLiterals: false,
  62. legacyOctalNumericLiterals: false,
  63. invalidJsonNumbers: false,
  64. multilineStrings: false,
  65. unquoteProperties: false,
  66. singleQuotes: false,
  67. numberProperties: false,
  68. undefinedKeywords: false,
  69. sparseArrays: false,
  70. regExpLiterals: false,
  71. templateLiterals: false,
  72. bigintLiterals: false,
  73. unicodeCodepointEscapes: false,
  74. escapeSequenceInIdentifier: false,
  75. };
  76. }
  77. if (upperCase === "JSONC") {
  78. return {
  79. trailingCommas: true,
  80. comments: true,
  81. plusSigns: false,
  82. spacedSigns: false,
  83. leadingOrTrailingDecimalPoints: false,
  84. infinities: false,
  85. nans: false,
  86. numericSeparators: false,
  87. binaryNumericLiterals: false,
  88. octalNumericLiterals: false,
  89. legacyOctalNumericLiterals: false,
  90. invalidJsonNumbers: false,
  91. multilineStrings: false,
  92. unquoteProperties: false,
  93. singleQuotes: false,
  94. numberProperties: false,
  95. undefinedKeywords: false,
  96. sparseArrays: false,
  97. regExpLiterals: false,
  98. templateLiterals: false,
  99. bigintLiterals: false,
  100. unicodeCodepointEscapes: false,
  101. escapeSequenceInIdentifier: false,
  102. };
  103. }
  104. if (upperCase === "JSON5") {
  105. return {
  106. trailingCommas: true,
  107. comments: true,
  108. plusSigns: true,
  109. spacedSigns: true,
  110. leadingOrTrailingDecimalPoints: true,
  111. infinities: true,
  112. nans: true,
  113. numericSeparators: false,
  114. binaryNumericLiterals: false,
  115. octalNumericLiterals: false,
  116. legacyOctalNumericLiterals: false,
  117. invalidJsonNumbers: true,
  118. multilineStrings: true,
  119. unquoteProperties: true,
  120. singleQuotes: true,
  121. numberProperties: false,
  122. undefinedKeywords: false,
  123. sparseArrays: false,
  124. regExpLiterals: false,
  125. templateLiterals: false,
  126. bigintLiterals: false,
  127. unicodeCodepointEscapes: false,
  128. escapeSequenceInIdentifier: false,
  129. };
  130. }
  131. return {
  132. trailingCommas: true,
  133. comments: true,
  134. plusSigns: true,
  135. spacedSigns: true,
  136. leadingOrTrailingDecimalPoints: true,
  137. infinities: true,
  138. nans: true,
  139. numericSeparators: true,
  140. binaryNumericLiterals: true,
  141. octalNumericLiterals: true,
  142. legacyOctalNumericLiterals: true,
  143. invalidJsonNumbers: true,
  144. multilineStrings: true,
  145. unquoteProperties: true,
  146. singleQuotes: true,
  147. numberProperties: true,
  148. undefinedKeywords: true,
  149. sparseArrays: true,
  150. regExpLiterals: true,
  151. templateLiterals: true,
  152. bigintLiterals: true,
  153. unicodeCodepointEscapes: true,
  154. escapeSequenceInIdentifier: true,
  155. };
  156. }
  157. function normalizeEcmaVersion(version) {
  158. const espree = (0, espree_1.getEspree)();
  159. const latestEcmaVersion = getLatestEcmaVersion(espree);
  160. if (version == null || version === "latest") {
  161. return latestEcmaVersion;
  162. }
  163. return Math.min(getEcmaVersionYear(version), latestEcmaVersion);
  164. }
  165. function getLatestEcmaVersion(espree) {
  166. if (espree.latestEcmaVersion == null) {
  167. for (const { v, latest } of [
  168. { v: "6.1.0", latest: 2020 },
  169. { v: "4.0.0", latest: 2019 },
  170. ]) {
  171. if ((0, semver_1.lte)(v, espree.version)) {
  172. return latest;
  173. }
  174. }
  175. return 2018;
  176. }
  177. return getEcmaVersionYear(espree.latestEcmaVersion);
  178. }
  179. function getEcmaVersionYear(version) {
  180. return version > 5 && version < 2015 ? version + 2009 : version;
  181. }