source-code.js 24 KB

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