Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
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.

source-code.js 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  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. class SourceCode extends TokenStore {
  126. /**
  127. * Represents parsed source code.
  128. * @param {string|Object} textOrConfig The source code text or config object.
  129. * @param {string} textOrConfig.text The source code text.
  130. * @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.
  131. * @param {Object|null} textOrConfig.parserServices The parser services.
  132. * @param {ScopeManager|null} textOrConfig.scopeManager The scope of this source code.
  133. * @param {Object|null} textOrConfig.visitorKeys The visitor keys to traverse AST.
  134. * @param {ASTNode} [astIfNoConfig] The Program node of the AST representing the code. This AST should be created from the text that BOM was stripped.
  135. */
  136. constructor(textOrConfig, astIfNoConfig) {
  137. let text, ast, parserServices, scopeManager, visitorKeys;
  138. // Process overloading.
  139. if (typeof textOrConfig === "string") {
  140. text = textOrConfig;
  141. ast = astIfNoConfig;
  142. } else if (typeof textOrConfig === "object" && textOrConfig !== null) {
  143. text = textOrConfig.text;
  144. ast = textOrConfig.ast;
  145. parserServices = textOrConfig.parserServices;
  146. scopeManager = textOrConfig.scopeManager;
  147. visitorKeys = textOrConfig.visitorKeys;
  148. }
  149. validate(ast);
  150. super(ast.tokens, ast.comments);
  151. /**
  152. * The flag to indicate that the source code has Unicode BOM.
  153. * @type boolean
  154. */
  155. this.hasBOM = (text.charCodeAt(0) === 0xFEFF);
  156. /**
  157. * The original text source code.
  158. * BOM was stripped from this text.
  159. * @type string
  160. */
  161. this.text = (this.hasBOM ? text.slice(1) : text);
  162. /**
  163. * The parsed AST for the source code.
  164. * @type ASTNode
  165. */
  166. this.ast = ast;
  167. /**
  168. * The parser services of this source code.
  169. * @type {Object}
  170. */
  171. this.parserServices = parserServices || {};
  172. /**
  173. * The scope of this source code.
  174. * @type {ScopeManager|null}
  175. */
  176. this.scopeManager = scopeManager || null;
  177. /**
  178. * The visitor keys to traverse AST.
  179. * @type {Object}
  180. */
  181. this.visitorKeys = visitorKeys || Traverser.DEFAULT_VISITOR_KEYS;
  182. // Check the source text for the presence of a shebang since it is parsed as a standard line comment.
  183. const shebangMatched = this.text.match(astUtils.shebangPattern);
  184. const hasShebang = shebangMatched && ast.comments.length && ast.comments[0].value === shebangMatched[1];
  185. if (hasShebang) {
  186. ast.comments[0].type = "Shebang";
  187. }
  188. this.tokensAndComments = sortedMerge(ast.tokens, ast.comments);
  189. /**
  190. * The source code split into lines according to ECMA-262 specification.
  191. * This is done to avoid each rule needing to do so separately.
  192. * @type string[]
  193. */
  194. this.lines = [];
  195. this.lineStartIndices = [0];
  196. const lineEndingPattern = astUtils.createGlobalLinebreakMatcher();
  197. let match;
  198. /*
  199. * Previously, this was implemented using a regex that
  200. * matched a sequence of non-linebreak characters followed by a
  201. * linebreak, then adding the lengths of the matches. However,
  202. * this caused a catastrophic backtracking issue when the end
  203. * of a file contained a large number of non-newline characters.
  204. * To avoid this, the current implementation just matches newlines
  205. * and uses match.index to get the correct line start indices.
  206. */
  207. while ((match = lineEndingPattern.exec(this.text))) {
  208. this.lines.push(this.text.slice(this.lineStartIndices[this.lineStartIndices.length - 1], match.index));
  209. this.lineStartIndices.push(match.index + match[0].length);
  210. }
  211. this.lines.push(this.text.slice(this.lineStartIndices[this.lineStartIndices.length - 1]));
  212. // Cache for comments found using getComments().
  213. this._commentCache = new WeakMap();
  214. // don't allow modification of this object
  215. Object.freeze(this);
  216. Object.freeze(this.lines);
  217. }
  218. /**
  219. * Split the source code into multiple lines based on the line delimiters.
  220. * @param {string} text Source code as a string.
  221. * @returns {string[]} Array of source code lines.
  222. * @public
  223. */
  224. static splitLines(text) {
  225. return text.split(astUtils.createGlobalLinebreakMatcher());
  226. }
  227. /**
  228. * Gets the source code for the given node.
  229. * @param {ASTNode} [node] The AST node to get the text for.
  230. * @param {int} [beforeCount] The number of characters before the node to retrieve.
  231. * @param {int} [afterCount] The number of characters after the node to retrieve.
  232. * @returns {string} The text representing the AST node.
  233. * @public
  234. */
  235. getText(node, beforeCount, afterCount) {
  236. if (node) {
  237. return this.text.slice(Math.max(node.range[0] - (beforeCount || 0), 0),
  238. node.range[1] + (afterCount || 0));
  239. }
  240. return this.text;
  241. }
  242. /**
  243. * Gets the entire source text split into an array of lines.
  244. * @returns {Array} The source text as an array of lines.
  245. * @public
  246. */
  247. getLines() {
  248. return this.lines;
  249. }
  250. /**
  251. * Retrieves an array containing all comments in the source code.
  252. * @returns {ASTNode[]} An array of comment nodes.
  253. * @public
  254. */
  255. getAllComments() {
  256. return this.ast.comments;
  257. }
  258. /**
  259. * Gets all comments for the given node.
  260. * @param {ASTNode} node The AST node to get the comments for.
  261. * @returns {Object} An object containing a leading and trailing array
  262. * of comments indexed by their position.
  263. * @public
  264. * @deprecated replaced by getCommentsBefore(), getCommentsAfter(), and getCommentsInside().
  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 && node.parent.type !== "Program" && (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 && node.parent.type !== "Program" && (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 index is on, determine the last place at which index could
  462. * be inserted into lineStartIndices to keep the list sorted.
  463. */
  464. const lineNumber = index >= this.lineStartIndices[this.lineStartIndices.length - 1]
  465. ? this.lineStartIndices.length
  466. : this.lineStartIndices.findIndex(el => index < el);
  467. return { line: lineNumber, column: index - this.lineStartIndices[lineNumber - 1] };
  468. }
  469. /**
  470. * Converts a (line, column) pair into a range index.
  471. * @param {Object} loc A line/column location
  472. * @param {number} loc.line The line number of the location (1-indexed)
  473. * @param {number} loc.column The column number of the location (0-indexed)
  474. * @returns {number} The range index of the location in the file.
  475. * @public
  476. */
  477. getIndexFromLoc(loc) {
  478. if (typeof loc !== "object" || typeof loc.line !== "number" || typeof loc.column !== "number") {
  479. throw new TypeError("Expected `loc` to be an object with numeric `line` and `column` properties.");
  480. }
  481. if (loc.line <= 0) {
  482. throw new RangeError(`Line number out of range (line ${loc.line} requested). Line numbers should be 1-based.`);
  483. }
  484. if (loc.line > this.lineStartIndices.length) {
  485. throw new RangeError(`Line number out of range (line ${loc.line} requested, but only ${this.lineStartIndices.length} lines present).`);
  486. }
  487. const lineStartIndex = this.lineStartIndices[loc.line - 1];
  488. const lineEndIndex = loc.line === this.lineStartIndices.length ? this.text.length : this.lineStartIndices[loc.line];
  489. const positionIndex = lineStartIndex + loc.column;
  490. /*
  491. * By design, getIndexFromLoc({ line: lineNum, column: 0 }) should return the start index of
  492. * the given line, provided that the line number is valid element of this.lines. Since the
  493. * last element of this.lines is an empty string for files with trailing newlines, add a
  494. * special case where getting the index for the first location after the end of the file
  495. * will return the length of the file, rather than throwing an error. This allows rules to
  496. * use getIndexFromLoc consistently without worrying about edge cases at the end of a file.
  497. */
  498. if (
  499. loc.line === this.lineStartIndices.length && positionIndex > lineEndIndex ||
  500. loc.line < this.lineStartIndices.length && positionIndex >= lineEndIndex
  501. ) {
  502. throw new RangeError(`Column number out of range (column ${loc.column} requested, but the length of line ${loc.line} is ${lineEndIndex - lineStartIndex}).`);
  503. }
  504. return positionIndex;
  505. }
  506. }
  507. module.exports = SourceCode;