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.

586 lines
22 KiB

4 years ago
  1. /**
  2. * @fileoverview Abstraction of JavaScript source code.
  3. * @author Nicholas C. Zakas
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const
  10. { isCommentToken } = require("eslint-utils"),
  11. TokenStore = require("./token-store"),
  12. astUtils = require("../shared/ast-utils"),
  13. Traverser = require("../shared/traverser"),
  14. lodash = require("lodash");
  15. //------------------------------------------------------------------------------
  16. // Private
  17. //------------------------------------------------------------------------------
  18. /**
  19. * Validates that the given AST has the required information.
  20. * @param {ASTNode} ast The Program node of the AST to check.
  21. * @throws {Error} If the AST doesn't contain the correct information.
  22. * @returns {void}
  23. * @private
  24. */
  25. function validate(ast) {
  26. if (!ast.tokens) {
  27. throw new Error("AST is missing the tokens array.");
  28. }
  29. if (!ast.comments) {
  30. throw new Error("AST is missing the comments array.");
  31. }
  32. if (!ast.loc) {
  33. throw new Error("AST is missing location information.");
  34. }
  35. if (!ast.range) {
  36. throw new Error("AST is missing range information");
  37. }
  38. }
  39. /**
  40. * Check to see if its a ES6 export declaration.
  41. * @param {ASTNode} astNode An AST node.
  42. * @returns {boolean} whether the given node represents an export declaration.
  43. * @private
  44. */
  45. function looksLikeExport(astNode) {
  46. return astNode.type === "ExportDefaultDeclaration" || astNode.type === "ExportNamedDeclaration" ||
  47. astNode.type === "ExportAllDeclaration" || astNode.type === "ExportSpecifier";
  48. }
  49. /**
  50. * Merges two sorted lists into a larger sorted list in O(n) time.
  51. * @param {Token[]} tokens The list of tokens.
  52. * @param {Token[]} comments The list of comments.
  53. * @returns {Token[]} A sorted list of tokens and comments.
  54. * @private
  55. */
  56. function sortedMerge(tokens, comments) {
  57. const result = [];
  58. let tokenIndex = 0;
  59. let commentIndex = 0;
  60. while (tokenIndex < tokens.length || commentIndex < comments.length) {
  61. if (commentIndex >= comments.length || tokenIndex < tokens.length && tokens[tokenIndex].range[0] < comments[commentIndex].range[0]) {
  62. result.push(tokens[tokenIndex++]);
  63. } else {
  64. result.push(comments[commentIndex++]);
  65. }
  66. }
  67. return result;
  68. }
  69. /**
  70. * Determines if two nodes or tokens overlap.
  71. * @param {ASTNode|Token} first The first node or token to check.
  72. * @param {ASTNode|Token} second The second node or token to check.
  73. * @returns {boolean} True if the two nodes or tokens overlap.
  74. * @private
  75. */
  76. function nodesOrTokensOverlap(first, second) {
  77. return (first.range[0] <= second.range[0] && first.range[1] >= second.range[0]) ||
  78. (second.range[0] <= first.range[0] && second.range[1] >= first.range[0]);
  79. }
  80. /**
  81. * Determines if two nodes or tokens have at least one whitespace character
  82. * between them. Order does not matter. Returns false if the given nodes or
  83. * tokens overlap.
  84. * @param {SourceCode} sourceCode The source code object.
  85. * @param {ASTNode|Token} first The first node or token to check between.
  86. * @param {ASTNode|Token} second The second node or token to check between.
  87. * @param {boolean} checkInsideOfJSXText If `true` is present, check inside of JSXText tokens for backward compatibility.
  88. * @returns {boolean} True if there is a whitespace character between
  89. * any of the tokens found between the two given nodes or tokens.
  90. * @public
  91. */
  92. function isSpaceBetween(sourceCode, first, second, checkInsideOfJSXText) {
  93. if (nodesOrTokensOverlap(first, second)) {
  94. return false;
  95. }
  96. const [startingNodeOrToken, endingNodeOrToken] = first.range[1] <= second.range[0]
  97. ? [first, second]
  98. : [second, first];
  99. const firstToken = sourceCode.getLastToken(startingNodeOrToken) || startingNodeOrToken;
  100. const finalToken = sourceCode.getFirstToken(endingNodeOrToken) || endingNodeOrToken;
  101. let currentToken = firstToken;
  102. while (currentToken !== finalToken) {
  103. const nextToken = sourceCode.getTokenAfter(currentToken, { includeComments: true });
  104. if (
  105. currentToken.range[1] !== nextToken.range[0] ||
  106. /*
  107. * For backward compatibility, check spaces in JSXText.
  108. * https://github.com/eslint/eslint/issues/12614
  109. */
  110. (
  111. checkInsideOfJSXText &&
  112. nextToken !== finalToken &&
  113. nextToken.type === "JSXText" &&
  114. /\s/u.test(nextToken.value)
  115. )
  116. ) {
  117. return true;
  118. }
  119. currentToken = nextToken;
  120. }
  121. return false;
  122. }
  123. //------------------------------------------------------------------------------
  124. // Public Interface
  125. //------------------------------------------------------------------------------
  126. class SourceCode extends TokenStore {
  127. /**
  128. * Represents parsed source code.
  129. * @param {string|Object} textOrConfig The source code text or config object.
  130. * @param {string} textOrConfig.text The source code text.
  131. * @param {ASTNode} textOrConfig.ast The Program node of the AST representing the code. This AST should be created from the text that BOM was stripped.
  132. * @param {Object|null} textOrConfig.parserServices The parser services.
  133. * @param {ScopeManager|null} textOrConfig.scopeManager The scope of this source code.
  134. * @param {Object|null} textOrConfig.visitorKeys The visitor keys to traverse AST.
  135. * @param {ASTNode} [astIfNoConfig] The Program node of the AST representing the code. This AST should be created from the text that BOM was stripped.
  136. */
  137. constructor(textOrConfig, astIfNoConfig) {
  138. let text, ast, parserServices, scopeManager, visitorKeys;
  139. // Process overloading.
  140. if (typeof textOrConfig === "string") {
  141. text = textOrConfig;
  142. ast = astIfNoConfig;
  143. } else if (typeof textOrConfig === "object" && textOrConfig !== null) {
  144. text = textOrConfig.text;
  145. ast = textOrConfig.ast;
  146. parserServices = textOrConfig.parserServices;
  147. scopeManager = textOrConfig.scopeManager;
  148. visitorKeys = textOrConfig.visitorKeys;
  149. }
  150. validate(ast);
  151. super(ast.tokens, ast.comments);
  152. /**
  153. * The flag to indicate that the source code has Unicode BOM.
  154. * @type boolean
  155. */
  156. this.hasBOM = (text.charCodeAt(0) === 0xFEFF);
  157. /**
  158. * The original text source code.
  159. * BOM was stripped from this text.
  160. * @type string
  161. */
  162. this.text = (this.hasBOM ? text.slice(1) : text);
  163. /**
  164. * The parsed AST for the source code.
  165. * @type ASTNode
  166. */
  167. this.ast = ast;
  168. /**
  169. * The parser services of this source code.
  170. * @type {Object}
  171. */
  172. this.parserServices = parserServices || {};
  173. /**
  174. * The scope of this source code.
  175. * @type {ScopeManager|null}
  176. */
  177. this.scopeManager = scopeManager || null;
  178. /**
  179. * The visitor keys to traverse AST.
  180. * @type {Object}
  181. */
  182. this.visitorKeys = visitorKeys || Traverser.DEFAULT_VISITOR_KEYS;
  183. // Check the source text for the presence of a shebang since it is parsed as a standard line comment.
  184. const shebangMatched = this.text.match(astUtils.shebangPattern);
  185. const hasShebang = shebangMatched && ast.comments.length && ast.comments[0].value === shebangMatched[1];
  186. if (hasShebang) {
  187. ast.comments[0].type = "Shebang";
  188. }
  189. this.tokensAndComments = sortedMerge(ast.tokens, ast.comments);
  190. /**
  191. * The source code split into lines according to ECMA-262 specification.
  192. * This is done to avoid each rule needing to do so separately.
  193. * @type string[]
  194. */
  195. this.lines = [];
  196. this.lineStartIndices = [0];
  197. const lineEndingPattern = astUtils.createGlobalLinebreakMatcher();
  198. let match;
  199. /*
  200. * Previously, this was implemented using a regex that
  201. * matched a sequence of non-linebreak characters followed by a
  202. * linebreak, then adding the lengths of the matches. However,
  203. * this caused a catastrophic backtracking issue when the end
  204. * of a file contained a large number of non-newline characters.
  205. * To avoid this, the current implementation just matches newlines
  206. * and uses match.index to get the correct line start indices.
  207. */
  208. while ((match = lineEndingPattern.exec(this.text))) {
  209. this.lines.push(this.text.slice(this.lineStartIndices[this.lineStartIndices.length - 1], match.index));
  210. this.lineStartIndices.push(match.index + match[0].length);
  211. }
  212. this.lines.push(this.text.slice(this.lineStartIndices[this.lineStartIndices.length - 1]));
  213. // Cache for comments found using getComments().
  214. this._commentCache = new WeakMap();
  215. // don't allow modification of this object
  216. Object.freeze(this);
  217. Object.freeze(this.lines);
  218. }
  219. /**
  220. * Split the source code into multiple lines based on the line delimiters.
  221. * @param {string} text Source code as a string.
  222. * @returns {string[]} Array of source code lines.
  223. * @public
  224. */
  225. static splitLines(text) {
  226. return text.split(astUtils.createGlobalLinebreakMatcher());
  227. }
  228. /**
  229. * Gets the source code for the given node.
  230. * @param {ASTNode} [node] The AST node to get the text for.
  231. * @param {int} [beforeCount] The number of characters before the node to retrieve.
  232. * @param {int} [afterCount] The number of characters after the node to retrieve.
  233. * @returns {string} The text representing the AST node.
  234. * @public
  235. */
  236. getText(node, beforeCount, afterCount) {
  237. if (node) {
  238. return this.text.slice(Math.max(node.range[0] - (beforeCount || 0), 0),
  239. node.range[1] + (afterCount || 0));
  240. }
  241. return this.text;
  242. }
  243. /**
  244. * Gets the entire source text split into an array of lines.
  245. * @returns {Array} The source text as an array of lines.
  246. * @public
  247. */
  248. getLines() {
  249. return this.lines;
  250. }
  251. /**
  252. * Retrieves an array containing all comments in the source code.
  253. * @returns {ASTNode[]} An array of comment nodes.
  254. * @public
  255. */
  256. getAllComments() {
  257. return this.ast.comments;
  258. }
  259. /**
  260. * Gets all comments for the given node.
  261. * @param {ASTNode} node The AST node to get the comments for.
  262. * @returns {Object} An object containing a leading and trailing array
  263. * of comments indexed by their position.
  264. * @public
  265. * @deprecated replaced by getCommentsBefore(), getCommentsAfter(), and getCommentsInside().
  266. */
  267. getComments(node) {
  268. if (this._commentCache.has(node)) {
  269. return this._commentCache.get(node);
  270. }
  271. const comments = {
  272. leading: [],
  273. trailing: []
  274. };
  275. /*
  276. * Return all comments as leading comments of the Program node when
  277. * there is no executable code.
  278. */
  279. if (node.type === "Program") {
  280. if (node.body.length === 0) {
  281. comments.leading = node.comments;
  282. }
  283. } else {
  284. /*
  285. * Return comments as trailing comments of nodes that only contain
  286. * comments (to mimic the comment attachment behavior present in Espree).
  287. */
  288. if ((node.type === "BlockStatement" || node.type === "ClassBody") && node.body.length === 0 ||
  289. node.type === "ObjectExpression" && node.properties.length === 0 ||
  290. node.type === "ArrayExpression" && node.elements.length === 0 ||
  291. node.type === "SwitchStatement" && node.cases.length === 0
  292. ) {
  293. comments.trailing = this.getTokens(node, {
  294. includeComments: true,
  295. filter: isCommentToken
  296. });
  297. }
  298. /*
  299. * Iterate over tokens before and after node and collect comment tokens.
  300. * Do not include comments that exist outside of the parent node
  301. * to avoid duplication.
  302. */
  303. let currentToken = this.getTokenBefore(node, { includeComments: true });
  304. while (currentToken && isCommentToken(currentToken)) {
  305. if (node.parent && (currentToken.start < node.parent.start)) {
  306. break;
  307. }
  308. comments.leading.push(currentToken);
  309. currentToken = this.getTokenBefore(currentToken, { includeComments: true });
  310. }
  311. comments.leading.reverse();
  312. currentToken = this.getTokenAfter(node, { includeComments: true });
  313. while (currentToken && isCommentToken(currentToken)) {
  314. if (node.parent && (currentToken.end > node.parent.end)) {
  315. break;
  316. }
  317. comments.trailing.push(currentToken);
  318. currentToken = this.getTokenAfter(currentToken, { includeComments: true });
  319. }
  320. }
  321. this._commentCache.set(node, comments);
  322. return comments;
  323. }
  324. /**
  325. * Retrieves the JSDoc comment for a given node.
  326. * @param {ASTNode} node The AST node to get the comment for.
  327. * @returns {Token|null} The Block comment token containing the JSDoc comment
  328. * for the given node or null if not found.
  329. * @public
  330. * @deprecated
  331. */
  332. getJSDocComment(node) {
  333. /**
  334. * Checks for the presence of a JSDoc comment for the given node and returns it.
  335. * @param {ASTNode} astNode The AST node to get the comment for.
  336. * @returns {Token|null} The Block comment token containing the JSDoc comment
  337. * for the given node or null if not found.
  338. * @private
  339. */
  340. const findJSDocComment = astNode => {
  341. const tokenBefore = this.getTokenBefore(astNode, { includeComments: true });
  342. if (
  343. tokenBefore &&
  344. isCommentToken(tokenBefore) &&
  345. tokenBefore.type === "Block" &&
  346. tokenBefore.value.charAt(0) === "*" &&
  347. astNode.loc.start.line - tokenBefore.loc.end.line <= 1
  348. ) {
  349. return tokenBefore;
  350. }
  351. return null;
  352. };
  353. let parent = node.parent;
  354. switch (node.type) {
  355. case "ClassDeclaration":
  356. case "FunctionDeclaration":
  357. return findJSDocComment(looksLikeExport(parent) ? parent : node);
  358. case "ClassExpression":
  359. return findJSDocComment(parent.parent);
  360. case "ArrowFunctionExpression":
  361. case "FunctionExpression":
  362. if (parent.type !== "CallExpression" && parent.type !== "NewExpression") {
  363. while (
  364. !this.getCommentsBefore(parent).length &&
  365. !/Function/u.test(parent.type) &&
  366. parent.type !== "MethodDefinition" &&
  367. parent.type !== "Property"
  368. ) {
  369. parent = parent.parent;
  370. if (!parent) {
  371. break;
  372. }
  373. }
  374. if (parent && parent.type !== "FunctionDeclaration" && parent.type !== "Program") {
  375. return findJSDocComment(parent);
  376. }
  377. }
  378. return findJSDocComment(node);
  379. // falls through
  380. default:
  381. return null;
  382. }
  383. }
  384. /**
  385. * Gets the deepest node containing a range index.
  386. * @param {int} index Range index of the desired node.
  387. * @returns {ASTNode} The node if found or null if not found.
  388. * @public
  389. */
  390. getNodeByRangeIndex(index) {
  391. let result = null;
  392. Traverser.traverse(this.ast, {
  393. visitorKeys: this.visitorKeys,
  394. enter(node) {
  395. if (node.range[0] <= index && index < node.range[1]) {
  396. result = node;
  397. } else {
  398. this.skip();
  399. }
  400. },
  401. leave(node) {
  402. if (node === result) {
  403. this.break();
  404. }
  405. }
  406. });
  407. return result;
  408. }
  409. /**
  410. * Determines if two nodes or tokens have at least one whitespace character
  411. * between them. Order does not matter. Returns false if the given nodes or
  412. * tokens overlap.
  413. * @param {ASTNode|Token} first The first node or token to check between.
  414. * @param {ASTNode|Token} second The second node or token to check between.
  415. * @returns {boolean} True if there is a whitespace character between
  416. * any of the tokens found between the two given nodes or tokens.
  417. * @public
  418. */
  419. isSpaceBetween(first, second) {
  420. return isSpaceBetween(this, first, second, false);
  421. }
  422. /**
  423. * Determines if two nodes or tokens have at least one whitespace character
  424. * between them. Order does not matter. Returns false if the given nodes or
  425. * tokens overlap.
  426. * For backward compatibility, this method returns true if there are
  427. * `JSXText` tokens that contain whitespaces between the two.
  428. * @param {ASTNode|Token} first The first node or token to check between.
  429. * @param {ASTNode|Token} second The second node or token to check between.
  430. * @returns {boolean} True if there is a whitespace character between
  431. * any of the tokens found between the two given nodes or tokens.
  432. * @deprecated in favor of isSpaceBetween().
  433. * @public
  434. */
  435. isSpaceBetweenTokens(first, second) {
  436. return isSpaceBetween(this, first, second, true);
  437. }
  438. /**
  439. * Converts a source text index into a (line, column) pair.
  440. * @param {number} index The index of a character in a file
  441. * @returns {Object} A {line, column} location object with a 0-indexed column
  442. * @public
  443. */
  444. getLocFromIndex(index) {
  445. if (typeof index !== "number") {
  446. throw new TypeError("Expected `index` to be a number.");
  447. }
  448. if (index < 0 || index > this.text.length) {
  449. throw new RangeError(`Index out of range (requested index ${index}, but source text has length ${this.text.length}).`);
  450. }
  451. /*
  452. * For an argument of this.text.length, return the location one "spot" past the last character
  453. * of the file. If the last character is a linebreak, the location will be column 0 of the next
  454. * line; otherwise, the location will be in the next column on the same line.
  455. *
  456. * See getIndexFromLoc for the motivation for this special case.
  457. */
  458. if (index === this.text.length) {
  459. return { line: this.lines.length, column: this.lines[this.lines.length - 1].length };
  460. }
  461. /*
  462. * To figure out which line rangeIndex is on, determine the last index at which rangeIndex could
  463. * be inserted into lineIndices to keep the list sorted.
  464. */
  465. const lineNumber = lodash.sortedLastIndex(this.lineStartIndices, index);
  466. return { line: lineNumber, column: index - this.lineStartIndices[lineNumber - 1] };
  467. }
  468. /**
  469. * Converts a (line, column) pair into a range index.
  470. * @param {Object} loc A line/column location
  471. * @param {number} loc.line The line number of the location (1-indexed)
  472. * @param {number} loc.column The column number of the location (0-indexed)
  473. * @returns {number} The range index of the location in the file.
  474. * @public
  475. */
  476. getIndexFromLoc(loc) {
  477. if (typeof loc !== "object" || typeof loc.line !== "number" || typeof loc.column !== "number") {
  478. throw new TypeError("Expected `loc` to be an object with numeric `line` and `column` properties.");
  479. }
  480. if (loc.line <= 0) {
  481. throw new RangeError(`Line number out of range (line ${loc.line} requested). Line numbers should be 1-based.`);
  482. }
  483. if (loc.line > this.lineStartIndices.length) {
  484. throw new RangeError(`Line number out of range (line ${loc.line} requested, but only ${this.lineStartIndices.length} lines present).`);
  485. }
  486. const lineStartIndex = this.lineStartIndices[loc.line - 1];
  487. const lineEndIndex = loc.line === this.lineStartIndices.length ? this.text.length : this.lineStartIndices[loc.line];
  488. const positionIndex = lineStartIndex + loc.column;
  489. /*
  490. * By design, getIndexFromLoc({ line: lineNum, column: 0 }) should return the start index of
  491. * the given line, provided that the line number is valid element of this.lines. Since the
  492. * last element of this.lines is an empty string for files with trailing newlines, add a
  493. * special case where getting the index for the first location after the end of the file
  494. * will return the length of the file, rather than throwing an error. This allows rules to
  495. * use getIndexFromLoc consistently without worrying about edge cases at the end of a file.
  496. */
  497. if (
  498. loc.line === this.lineStartIndices.length && positionIndex > lineEndIndex ||
  499. loc.line < this.lineStartIndices.length && positionIndex >= lineEndIndex
  500. ) {
  501. throw new RangeError(`Column number out of range (column ${loc.column} requested, but the length of line ${loc.line} is ${lineEndIndex - lineStartIndex}).`);
  502. }
  503. return positionIndex;
  504. }
  505. }
  506. module.exports = SourceCode;