source-code.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  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. //------------------------------------------------------------------------------
  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. /**
  126. * Represents parsed source code.
  127. */
  128. class SourceCode extends TokenStore {
  129. /**
  130. * @param {string|Object} textOrConfig The source code text or config object.
  131. * @param {string} textOrConfig.text The source code text.
  132. * @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.
  133. * @param {Object|null} textOrConfig.parserServices The parser services.
  134. * @param {ScopeManager|null} textOrConfig.scopeManager The scope of this source code.
  135. * @param {Object|null} textOrConfig.visitorKeys The visitor keys to traverse AST.
  136. * @param {ASTNode} [astIfNoConfig] The Program node of the AST representing the code. This AST should be created from the text that BOM was stripped.
  137. */
  138. constructor(textOrConfig, astIfNoConfig) {
  139. let text, ast, parserServices, scopeManager, visitorKeys;
  140. // Process overloading.
  141. if (typeof textOrConfig === "string") {
  142. text = textOrConfig;
  143. ast = astIfNoConfig;
  144. } else if (typeof textOrConfig === "object" && textOrConfig !== null) {
  145. text = textOrConfig.text;
  146. ast = textOrConfig.ast;
  147. parserServices = textOrConfig.parserServices;
  148. scopeManager = textOrConfig.scopeManager;
  149. visitorKeys = textOrConfig.visitorKeys;
  150. }
  151. validate(ast);
  152. super(ast.tokens, ast.comments);
  153. /**
  154. * The flag to indicate that the source code has Unicode BOM.
  155. * @type {boolean}
  156. */
  157. this.hasBOM = (text.charCodeAt(0) === 0xFEFF);
  158. /**
  159. * The original text source code.
  160. * BOM was stripped from this text.
  161. * @type {string}
  162. */
  163. this.text = (this.hasBOM ? text.slice(1) : text);
  164. /**
  165. * The parsed AST for the source code.
  166. * @type {ASTNode}
  167. */
  168. this.ast = ast;
  169. /**
  170. * The parser services of this source code.
  171. * @type {Object}
  172. */
  173. this.parserServices = parserServices || {};
  174. /**
  175. * The scope of this source code.
  176. * @type {ScopeManager|null}
  177. */
  178. this.scopeManager = scopeManager || null;
  179. /**
  180. * The visitor keys to traverse AST.
  181. * @type {Object}
  182. */
  183. this.visitorKeys = visitorKeys || Traverser.DEFAULT_VISITOR_KEYS;
  184. // Check the source text for the presence of a shebang since it is parsed as a standard line comment.
  185. const shebangMatched = this.text.match(astUtils.shebangPattern);
  186. const hasShebang = shebangMatched && ast.comments.length && ast.comments[0].value === shebangMatched[1];
  187. if (hasShebang) {
  188. ast.comments[0].type = "Shebang";
  189. }
  190. this.tokensAndComments = sortedMerge(ast.tokens, ast.comments);
  191. /**
  192. * The source code split into lines according to ECMA-262 specification.
  193. * This is done to avoid each rule needing to do so separately.
  194. * @type {string[]}
  195. */
  196. this.lines = [];
  197. this.lineStartIndices = [0];
  198. const lineEndingPattern = astUtils.createGlobalLinebreakMatcher();
  199. let match;
  200. /*
  201. * Previously, this was implemented using a regex that
  202. * matched a sequence of non-linebreak characters followed by a
  203. * linebreak, then adding the lengths of the matches. However,
  204. * this caused a catastrophic backtracking issue when the end
  205. * of a file contained a large number of non-newline characters.
  206. * To avoid this, the current implementation just matches newlines
  207. * and uses match.index to get the correct line start indices.
  208. */
  209. while ((match = lineEndingPattern.exec(this.text))) {
  210. this.lines.push(this.text.slice(this.lineStartIndices[this.lineStartIndices.length - 1], match.index));
  211. this.lineStartIndices.push(match.index + match[0].length);
  212. }
  213. this.lines.push(this.text.slice(this.lineStartIndices[this.lineStartIndices.length - 1]));
  214. // Cache for comments found using getComments().
  215. this._commentCache = new WeakMap();
  216. // don't allow modification of this object
  217. Object.freeze(this);
  218. Object.freeze(this.lines);
  219. }
  220. /**
  221. * Split the source code into multiple lines based on the line delimiters.
  222. * @param {string} text Source code as a string.
  223. * @returns {string[]} Array of source code lines.
  224. * @public
  225. */
  226. static splitLines(text) {
  227. return text.split(astUtils.createGlobalLinebreakMatcher());
  228. }
  229. /**
  230. * Gets the source code for the given node.
  231. * @param {ASTNode} [node] The AST node to get the text for.
  232. * @param {int} [beforeCount] The number of characters before the node to retrieve.
  233. * @param {int} [afterCount] The number of characters after the node to retrieve.
  234. * @returns {string} The text representing the AST node.
  235. * @public
  236. */
  237. getText(node, beforeCount, afterCount) {
  238. if (node) {
  239. return this.text.slice(Math.max(node.range[0] - (beforeCount || 0), 0),
  240. node.range[1] + (afterCount || 0));
  241. }
  242. return this.text;
  243. }
  244. /**
  245. * Gets the entire source text split into an array of lines.
  246. * @returns {Array} The source text as an array of lines.
  247. * @public
  248. */
  249. getLines() {
  250. return this.lines;
  251. }
  252. /**
  253. * Retrieves an array containing all comments in the source code.
  254. * @returns {ASTNode[]} An array of comment nodes.
  255. * @public
  256. */
  257. getAllComments() {
  258. return this.ast.comments;
  259. }
  260. /**
  261. * Gets all comments for the given node.
  262. * @param {ASTNode} node The AST node to get the comments for.
  263. * @returns {Object} An object containing a leading and trailing array
  264. * of comments indexed by their position.
  265. * @public
  266. * @deprecated replaced by getCommentsBefore(), getCommentsAfter(), and getCommentsInside().
  267. */
  268. getComments(node) {
  269. if (this._commentCache.has(node)) {
  270. return this._commentCache.get(node);
  271. }
  272. const comments = {
  273. leading: [],
  274. trailing: []
  275. };
  276. /*
  277. * Return all comments as leading comments of the Program node when
  278. * there is no executable code.
  279. */
  280. if (node.type === "Program") {
  281. if (node.body.length === 0) {
  282. comments.leading = node.comments;
  283. }
  284. } else {
  285. /*
  286. * Return comments as trailing comments of nodes that only contain
  287. * comments (to mimic the comment attachment behavior present in Espree).
  288. */
  289. if ((node.type === "BlockStatement" || node.type === "ClassBody") && node.body.length === 0 ||
  290. node.type === "ObjectExpression" && node.properties.length === 0 ||
  291. node.type === "ArrayExpression" && node.elements.length === 0 ||
  292. node.type === "SwitchStatement" && node.cases.length === 0
  293. ) {
  294. comments.trailing = this.getTokens(node, {
  295. includeComments: true,
  296. filter: isCommentToken
  297. });
  298. }
  299. /*
  300. * Iterate over tokens before and after node and collect comment tokens.
  301. * Do not include comments that exist outside of the parent node
  302. * to avoid duplication.
  303. */
  304. let currentToken = this.getTokenBefore(node, { includeComments: true });
  305. while (currentToken && isCommentToken(currentToken)) {
  306. if (node.parent && node.parent.type !== "Program" && (currentToken.start < node.parent.start)) {
  307. break;
  308. }
  309. comments.leading.push(currentToken);
  310. currentToken = this.getTokenBefore(currentToken, { includeComments: true });
  311. }
  312. comments.leading.reverse();
  313. currentToken = this.getTokenAfter(node, { includeComments: true });
  314. while (currentToken && isCommentToken(currentToken)) {
  315. if (node.parent && node.parent.type !== "Program" && (currentToken.end > node.parent.end)) {
  316. break;
  317. }
  318. comments.trailing.push(currentToken);
  319. currentToken = this.getTokenAfter(currentToken, { includeComments: true });
  320. }
  321. }
  322. this._commentCache.set(node, comments);
  323. return comments;
  324. }
  325. /**
  326. * Retrieves the JSDoc comment for a given node.
  327. * @param {ASTNode} node The AST node to get the comment for.
  328. * @returns {Token|null} The Block comment token containing the JSDoc comment
  329. * for the given node or null if not found.
  330. * @public
  331. * @deprecated
  332. */
  333. getJSDocComment(node) {
  334. /**
  335. * Checks for the presence of a JSDoc comment for the given node and returns it.
  336. * @param {ASTNode} astNode The AST node to get the comment for.
  337. * @returns {Token|null} The Block comment token containing the JSDoc comment
  338. * for the given node or null if not found.
  339. * @private
  340. */
  341. const findJSDocComment = astNode => {
  342. const tokenBefore = this.getTokenBefore(astNode, { includeComments: true });
  343. if (
  344. tokenBefore &&
  345. isCommentToken(tokenBefore) &&
  346. tokenBefore.type === "Block" &&
  347. tokenBefore.value.charAt(0) === "*" &&
  348. astNode.loc.start.line - tokenBefore.loc.end.line <= 1
  349. ) {
  350. return tokenBefore;
  351. }
  352. return null;
  353. };
  354. let parent = node.parent;
  355. switch (node.type) {
  356. case "ClassDeclaration":
  357. case "FunctionDeclaration":
  358. return findJSDocComment(looksLikeExport(parent) ? parent : node);
  359. case "ClassExpression":
  360. return findJSDocComment(parent.parent);
  361. case "ArrowFunctionExpression":
  362. case "FunctionExpression":
  363. if (parent.type !== "CallExpression" && parent.type !== "NewExpression") {
  364. while (
  365. !this.getCommentsBefore(parent).length &&
  366. !/Function/u.test(parent.type) &&
  367. parent.type !== "MethodDefinition" &&
  368. parent.type !== "Property"
  369. ) {
  370. parent = parent.parent;
  371. if (!parent) {
  372. break;
  373. }
  374. }
  375. if (parent && parent.type !== "FunctionDeclaration" && parent.type !== "Program") {
  376. return findJSDocComment(parent);
  377. }
  378. }
  379. return findJSDocComment(node);
  380. // falls through
  381. default:
  382. return null;
  383. }
  384. }
  385. /**
  386. * Gets the deepest node containing a range index.
  387. * @param {int} index Range index of the desired node.
  388. * @returns {ASTNode} The node if found or null if not found.
  389. * @public
  390. */
  391. getNodeByRangeIndex(index) {
  392. let result = null;
  393. Traverser.traverse(this.ast, {
  394. visitorKeys: this.visitorKeys,
  395. enter(node) {
  396. if (node.range[0] <= index && index < node.range[1]) {
  397. result = node;
  398. } else {
  399. this.skip();
  400. }
  401. },
  402. leave(node) {
  403. if (node === result) {
  404. this.break();
  405. }
  406. }
  407. });
  408. return result;
  409. }
  410. /**
  411. * Determines if two nodes or tokens have at least one whitespace character
  412. * between them. Order does not matter. Returns false if the given nodes or
  413. * tokens overlap.
  414. * @param {ASTNode|Token} first The first node or token to check between.
  415. * @param {ASTNode|Token} second The second node or token to check between.
  416. * @returns {boolean} True if there is a whitespace character between
  417. * any of the tokens found between the two given nodes or tokens.
  418. * @public
  419. */
  420. isSpaceBetween(first, second) {
  421. return isSpaceBetween(this, first, second, false);
  422. }
  423. /**
  424. * Determines if two nodes or tokens have at least one whitespace character
  425. * between them. Order does not matter. Returns false if the given nodes or
  426. * tokens overlap.
  427. * For backward compatibility, this method returns true if there are
  428. * `JSXText` tokens that contain whitespaces between the two.
  429. * @param {ASTNode|Token} first The first node or token to check between.
  430. * @param {ASTNode|Token} second The second node or token to check between.
  431. * @returns {boolean} True if there is a whitespace character between
  432. * any of the tokens found between the two given nodes or tokens.
  433. * @deprecated in favor of isSpaceBetween().
  434. * @public
  435. */
  436. isSpaceBetweenTokens(first, second) {
  437. return isSpaceBetween(this, first, second, true);
  438. }
  439. /**
  440. * Converts a source text index into a (line, column) pair.
  441. * @param {number} index The index of a character in a file
  442. * @throws {TypeError} If non-numeric index or index out of range.
  443. * @returns {Object} A {line, column} location object with a 0-indexed column
  444. * @public
  445. */
  446. getLocFromIndex(index) {
  447. if (typeof index !== "number") {
  448. throw new TypeError("Expected `index` to be a number.");
  449. }
  450. if (index < 0 || index > this.text.length) {
  451. throw new RangeError(`Index out of range (requested index ${index}, but source text has length ${this.text.length}).`);
  452. }
  453. /*
  454. * For an argument of this.text.length, return the location one "spot" past the last character
  455. * of the file. If the last character is a linebreak, the location will be column 0 of the next
  456. * line; otherwise, the location will be in the next column on the same line.
  457. *
  458. * See getIndexFromLoc for the motivation for this special case.
  459. */
  460. if (index === this.text.length) {
  461. return { line: this.lines.length, column: this.lines[this.lines.length - 1].length };
  462. }
  463. /*
  464. * To figure out which line index is on, determine the last place at which index could
  465. * be inserted into lineStartIndices to keep the list sorted.
  466. */
  467. const lineNumber = index >= this.lineStartIndices[this.lineStartIndices.length - 1]
  468. ? this.lineStartIndices.length
  469. : this.lineStartIndices.findIndex(el => index < el);
  470. return { line: lineNumber, column: index - this.lineStartIndices[lineNumber - 1] };
  471. }
  472. /**
  473. * Converts a (line, column) pair into a range index.
  474. * @param {Object} loc A line/column location
  475. * @param {number} loc.line The line number of the location (1-indexed)
  476. * @param {number} loc.column The column number of the location (0-indexed)
  477. * @throws {TypeError|RangeError} If `loc` is not an object with a numeric
  478. * `line` and `column`, if the `line` is less than or equal to zero or
  479. * the line or column is out of the expected range.
  480. * @returns {number} The range index of the location in the file.
  481. * @public
  482. */
  483. getIndexFromLoc(loc) {
  484. if (typeof loc !== "object" || typeof loc.line !== "number" || typeof loc.column !== "number") {
  485. throw new TypeError("Expected `loc` to be an object with numeric `line` and `column` properties.");
  486. }
  487. if (loc.line <= 0) {
  488. throw new RangeError(`Line number out of range (line ${loc.line} requested). Line numbers should be 1-based.`);
  489. }
  490. if (loc.line > this.lineStartIndices.length) {
  491. throw new RangeError(`Line number out of range (line ${loc.line} requested, but only ${this.lineStartIndices.length} lines present).`);
  492. }
  493. const lineStartIndex = this.lineStartIndices[loc.line - 1];
  494. const lineEndIndex = loc.line === this.lineStartIndices.length ? this.text.length : this.lineStartIndices[loc.line];
  495. const positionIndex = lineStartIndex + loc.column;
  496. /*
  497. * By design, getIndexFromLoc({ line: lineNum, column: 0 }) should return the start index of
  498. * the given line, provided that the line number is valid element of this.lines. Since the
  499. * last element of this.lines is an empty string for files with trailing newlines, add a
  500. * special case where getting the index for the first location after the end of the file
  501. * will return the length of the file, rather than throwing an error. This allows rules to
  502. * use getIndexFromLoc consistently without worrying about edge cases at the end of a file.
  503. */
  504. if (
  505. loc.line === this.lineStartIndices.length && positionIndex > lineEndIndex ||
  506. loc.line < this.lineStartIndices.length && positionIndex >= lineEndIndex
  507. ) {
  508. throw new RangeError(`Column number out of range (column ${loc.column} requested, but the length of line ${loc.line} is ${lineEndIndex - lineStartIndex}).`);
  509. }
  510. return positionIndex;
  511. }
  512. }
  513. module.exports = SourceCode;