source-code.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  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 speces 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. */
  266. getComments(node) {
  267. if (this._commentCache.has(node)) {
  268. return this._commentCache.get(node);
  269. }
  270. const comments = {
  271. leading: [],
  272. trailing: []
  273. };
  274. /*
  275. * Return all comments as leading comments of the Program node when
  276. * there is no executable code.
  277. */
  278. if (node.type === "Program") {
  279. if (node.body.length === 0) {
  280. comments.leading = node.comments;
  281. }
  282. } else {
  283. /*
  284. * Return comments as trailing comments of nodes that only contain
  285. * comments (to mimic the comment attachment behavior present in Espree).
  286. */
  287. if ((node.type === "BlockStatement" || node.type === "ClassBody") && node.body.length === 0 ||
  288. node.type === "ObjectExpression" && node.properties.length === 0 ||
  289. node.type === "ArrayExpression" && node.elements.length === 0 ||
  290. node.type === "SwitchStatement" && node.cases.length === 0
  291. ) {
  292. comments.trailing = this.getTokens(node, {
  293. includeComments: true,
  294. filter: isCommentToken
  295. });
  296. }
  297. /*
  298. * Iterate over tokens before and after node and collect comment tokens.
  299. * Do not include comments that exist outside of the parent node
  300. * to avoid duplication.
  301. */
  302. let currentToken = this.getTokenBefore(node, { includeComments: true });
  303. while (currentToken && isCommentToken(currentToken)) {
  304. if (node.parent && (currentToken.start < node.parent.start)) {
  305. break;
  306. }
  307. comments.leading.push(currentToken);
  308. currentToken = this.getTokenBefore(currentToken, { includeComments: true });
  309. }
  310. comments.leading.reverse();
  311. currentToken = this.getTokenAfter(node, { includeComments: true });
  312. while (currentToken && isCommentToken(currentToken)) {
  313. if (node.parent && (currentToken.end > node.parent.end)) {
  314. break;
  315. }
  316. comments.trailing.push(currentToken);
  317. currentToken = this.getTokenAfter(currentToken, { includeComments: true });
  318. }
  319. }
  320. this._commentCache.set(node, comments);
  321. return comments;
  322. }
  323. /**
  324. * Retrieves the JSDoc comment for a given node.
  325. * @param {ASTNode} node The AST node to get the comment for.
  326. * @returns {Token|null} The Block comment token containing the JSDoc comment
  327. * for the given node or null if not found.
  328. * @public
  329. * @deprecated
  330. */
  331. getJSDocComment(node) {
  332. /**
  333. * Checks for the presence of a JSDoc comment for the given node and returns it.
  334. * @param {ASTNode} astNode 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. * @private
  338. */
  339. const findJSDocComment = astNode => {
  340. const tokenBefore = this.getTokenBefore(astNode, { includeComments: true });
  341. if (
  342. tokenBefore &&
  343. isCommentToken(tokenBefore) &&
  344. tokenBefore.type === "Block" &&
  345. tokenBefore.value.charAt(0) === "*" &&
  346. astNode.loc.start.line - tokenBefore.loc.end.line <= 1
  347. ) {
  348. return tokenBefore;
  349. }
  350. return null;
  351. };
  352. let parent = node.parent;
  353. switch (node.type) {
  354. case "ClassDeclaration":
  355. case "FunctionDeclaration":
  356. return findJSDocComment(looksLikeExport(parent) ? parent : node);
  357. case "ClassExpression":
  358. return findJSDocComment(parent.parent);
  359. case "ArrowFunctionExpression":
  360. case "FunctionExpression":
  361. if (parent.type !== "CallExpression" && parent.type !== "NewExpression") {
  362. while (
  363. !this.getCommentsBefore(parent).length &&
  364. !/Function/u.test(parent.type) &&
  365. parent.type !== "MethodDefinition" &&
  366. parent.type !== "Property"
  367. ) {
  368. parent = parent.parent;
  369. if (!parent) {
  370. break;
  371. }
  372. }
  373. if (parent && parent.type !== "FunctionDeclaration" && parent.type !== "Program") {
  374. return findJSDocComment(parent);
  375. }
  376. }
  377. return findJSDocComment(node);
  378. // falls through
  379. default:
  380. return null;
  381. }
  382. }
  383. /**
  384. * Gets the deepest node containing a range index.
  385. * @param {int} index Range index of the desired node.
  386. * @returns {ASTNode} The node if found or null if not found.
  387. * @public
  388. */
  389. getNodeByRangeIndex(index) {
  390. let result = null;
  391. Traverser.traverse(this.ast, {
  392. visitorKeys: this.visitorKeys,
  393. enter(node) {
  394. if (node.range[0] <= index && index < node.range[1]) {
  395. result = node;
  396. } else {
  397. this.skip();
  398. }
  399. },
  400. leave(node) {
  401. if (node === result) {
  402. this.break();
  403. }
  404. }
  405. });
  406. return result;
  407. }
  408. /**
  409. * Determines if two nodes or tokens have at least one whitespace character
  410. * between them. Order does not matter. Returns false if the given nodes or
  411. * tokens overlap.
  412. * @param {ASTNode|Token} first The first node or token to check between.
  413. * @param {ASTNode|Token} second The second node or token to check between.
  414. * @returns {boolean} True if there is a whitespace character between
  415. * any of the tokens found between the two given nodes or tokens.
  416. * @public
  417. */
  418. isSpaceBetween(first, second) {
  419. return isSpaceBetween(this, first, second, false);
  420. }
  421. /**
  422. * Determines if two nodes or tokens have at least one whitespace character
  423. * between them. Order does not matter. Returns false if the given nodes or
  424. * tokens overlap.
  425. * For backward compatibility, this method returns true if there are
  426. * `JSXText` tokens that contain whitespaces between the two.
  427. * @param {ASTNode|Token} first The first node or token to check between.
  428. * @param {ASTNode|Token} second The second node or token to check between.
  429. * @returns {boolean} True if there is a whitespace character between
  430. * any of the tokens found between the two given nodes or tokens.
  431. * @deprecated in favor of isSpaceBetween().
  432. * @public
  433. */
  434. isSpaceBetweenTokens(first, second) {
  435. return isSpaceBetween(this, first, second, true);
  436. }
  437. /**
  438. * Converts a source text index into a (line, column) pair.
  439. * @param {number} index The index of a character in a file
  440. * @returns {Object} A {line, column} location object with a 0-indexed column
  441. * @public
  442. */
  443. getLocFromIndex(index) {
  444. if (typeof index !== "number") {
  445. throw new TypeError("Expected `index` to be a number.");
  446. }
  447. if (index < 0 || index > this.text.length) {
  448. throw new RangeError(`Index out of range (requested index ${index}, but source text has length ${this.text.length}).`);
  449. }
  450. /*
  451. * For an argument of this.text.length, return the location one "spot" past the last character
  452. * of the file. If the last character is a linebreak, the location will be column 0 of the next
  453. * line; otherwise, the location will be in the next column on the same line.
  454. *
  455. * See getIndexFromLoc for the motivation for this special case.
  456. */
  457. if (index === this.text.length) {
  458. return { line: this.lines.length, column: this.lines[this.lines.length - 1].length };
  459. }
  460. /*
  461. * To figure out which line rangeIndex is on, determine the last index at which rangeIndex could
  462. * be inserted into lineIndices to keep the list sorted.
  463. */
  464. const lineNumber = lodash.sortedLastIndex(this.lineStartIndices, index);
  465. return { line: lineNumber, column: index - this.lineStartIndices[lineNumber - 1] };
  466. }
  467. /**
  468. * Converts a (line, column) pair into a range index.
  469. * @param {Object} loc A line/column location
  470. * @param {number} loc.line The line number of the location (1-indexed)
  471. * @param {number} loc.column The column number of the location (0-indexed)
  472. * @returns {number} The range index of the location in the file.
  473. * @public
  474. */
  475. getIndexFromLoc(loc) {
  476. if (typeof loc !== "object" || typeof loc.line !== "number" || typeof loc.column !== "number") {
  477. throw new TypeError("Expected `loc` to be an object with numeric `line` and `column` properties.");
  478. }
  479. if (loc.line <= 0) {
  480. throw new RangeError(`Line number out of range (line ${loc.line} requested). Line numbers should be 1-based.`);
  481. }
  482. if (loc.line > this.lineStartIndices.length) {
  483. throw new RangeError(`Line number out of range (line ${loc.line} requested, but only ${this.lineStartIndices.length} lines present).`);
  484. }
  485. const lineStartIndex = this.lineStartIndices[loc.line - 1];
  486. const lineEndIndex = loc.line === this.lineStartIndices.length ? this.text.length : this.lineStartIndices[loc.line];
  487. const positionIndex = lineStartIndex + loc.column;
  488. /*
  489. * By design, getIndexFromLoc({ line: lineNum, column: 0 }) should return the start index of
  490. * the given line, provided that the line number is valid element of this.lines. Since the
  491. * last element of this.lines is an empty string for files with trailing newlines, add a
  492. * special case where getting the index for the first location after the end of the file
  493. * will return the length of the file, rather than throwing an error. This allows rules to
  494. * use getIndexFromLoc consistently without worrying about edge cases at the end of a file.
  495. */
  496. if (
  497. loc.line === this.lineStartIndices.length && positionIndex > lineEndIndex ||
  498. loc.line < this.lineStartIndices.length && positionIndex >= lineEndIndex
  499. ) {
  500. throw new RangeError(`Column number out of range (column ${loc.column} requested, but the length of line ${loc.line} is ${lineEndIndex - lineStartIndex}).`);
  501. }
  502. return positionIndex;
  503. }
  504. }
  505. module.exports = SourceCode;