UNPKG

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