UNPKG

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