UNPKG

9.57 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 createTokenStore = require("../token-store.js"),
12 Traverser = require("./traverser");
13
14//------------------------------------------------------------------------------
15// Private
16//------------------------------------------------------------------------------
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 */
25function validate(ast) {
26
27 if (!ast.tokens) {
28 throw new Error("AST is missing the tokens array.");
29 }
30
31 if (!ast.comments) {
32 throw new Error("AST is missing the comments array.");
33 }
34
35 if (!ast.loc) {
36 throw new Error("AST is missing location information.");
37 }
38
39 if (!ast.range) {
40 throw new Error("AST is missing range information");
41 }
42}
43
44/**
45 * Finds a JSDoc comment node in an array of comment nodes.
46 * @param {ASTNode[]} comments The array of comment nodes to search.
47 * @param {int} line Line number to look around
48 * @returns {ASTNode} The node if found, null if not.
49 * @private
50 */
51function findJSDocComment(comments, line) {
52
53 if (comments) {
54 for (let i = comments.length - 1; i >= 0; i--) {
55 if (comments[i].type === "Block" && comments[i].value.charAt(0) === "*") {
56
57 if (line - comments[i].loc.end.line <= 1) {
58 return comments[i];
59 } else {
60 break;
61 }
62 }
63 }
64 }
65
66 return null;
67}
68
69/**
70 * Check to see if its a ES6 export declaration
71 * @param {ASTNode} astNode - any node
72 * @returns {boolean} whether the given node represents a export declaration
73 * @private
74 */
75function looksLikeExport(astNode) {
76 return astNode.type === "ExportDefaultDeclaration" || astNode.type === "ExportNamedDeclaration" ||
77 astNode.type === "ExportAllDeclaration" || astNode.type === "ExportSpecifier";
78}
79
80
81//------------------------------------------------------------------------------
82// Public Interface
83//------------------------------------------------------------------------------
84
85/**
86 * Represents parsed source code.
87 * @param {string} text - The source code text.
88 * @param {ASTNode} ast - The Program node of the AST representing the code. This AST should be created from the text that BOM was stripped.
89 * @constructor
90 */
91function SourceCode(text, ast) {
92 validate(ast);
93
94 /**
95 * The flag to indicate that the source code has Unicode BOM.
96 * @type boolean
97 */
98 this.hasBOM = (text.charCodeAt(0) === 0xFEFF);
99
100 /**
101 * The original text source code.
102 * BOM was stripped from this text.
103 * @type string
104 */
105 this.text = (this.hasBOM ? text.slice(1) : text);
106
107 /**
108 * The parsed AST for the source code.
109 * @type ASTNode
110 */
111 this.ast = ast;
112
113 /**
114 * The source code split into lines according to ECMA-262 specification.
115 * This is done to avoid each rule needing to do so separately.
116 * @type string[]
117 */
118 this.lines = SourceCode.splitLines(this.text);
119
120 this.tokensAndComments = ast.tokens.concat(ast.comments).sort(function(left, right) {
121 return left.range[0] - right.range[0];
122 });
123
124 // create token store methods
125 const tokenStore = createTokenStore(ast.tokens);
126
127 Object.keys(tokenStore).forEach(function(methodName) {
128 this[methodName] = tokenStore[methodName];
129 }, this);
130
131 const tokensAndCommentsStore = createTokenStore(this.tokensAndComments);
132
133 this.getTokenOrCommentBefore = tokensAndCommentsStore.getTokenBefore;
134 this.getTokenOrCommentAfter = tokensAndCommentsStore.getTokenAfter;
135
136 // don't allow modification of this object
137 Object.freeze(this);
138 Object.freeze(this.lines);
139}
140
141/**
142 * Split the source code into multiple lines based on the line delimiters
143 * @param {string} text Source code as a string
144 * @returns {string[]} Array of source code lines
145 * @public
146 */
147SourceCode.splitLines = function(text) {
148 return text.split(/\r\n|\r|\n|\u2028|\u2029/g);
149};
150
151SourceCode.prototype = {
152 constructor: SourceCode,
153
154 /**
155 * Gets the source code for the given node.
156 * @param {ASTNode=} node The AST node to get the text for.
157 * @param {int=} beforeCount The number of characters before the node to retrieve.
158 * @param {int=} afterCount The number of characters after the node to retrieve.
159 * @returns {string} The text representing the AST node.
160 */
161 getText(node, beforeCount, afterCount) {
162 if (node) {
163 return this.text.slice(Math.max(node.range[0] - (beforeCount || 0), 0),
164 node.range[1] + (afterCount || 0));
165 } else {
166 return this.text;
167 }
168
169 },
170
171 /**
172 * Gets the entire source text split into an array of lines.
173 * @returns {Array} The source text as an array of lines.
174 */
175 getLines() {
176 return this.lines;
177 },
178
179 /**
180 * Retrieves an array containing all comments in the source code.
181 * @returns {ASTNode[]} An array of comment nodes.
182 */
183 getAllComments() {
184 return this.ast.comments;
185 },
186
187 /**
188 * Gets all comments for the given node.
189 * @param {ASTNode} node The AST node to get the comments for.
190 * @returns {Object} The list of comments indexed by their position.
191 * @public
192 */
193 getComments(node) {
194
195 let leadingComments = node.leadingComments || [];
196 const trailingComments = node.trailingComments || [];
197
198 /*
199 * espree adds a "comments" array on Program nodes rather than
200 * leadingComments/trailingComments. Comments are only left in the
201 * Program node comments array if there is no executable code.
202 */
203 if (node.type === "Program") {
204 if (node.body.length === 0) {
205 leadingComments = node.comments;
206 }
207 }
208
209 return {
210 leading: leadingComments,
211 trailing: trailingComments
212 };
213 },
214
215 /**
216 * Retrieves the JSDoc comment for a given node.
217 * @param {ASTNode} node The AST node to get the comment for.
218 * @returns {ASTNode} The BlockComment node containing the JSDoc for the
219 * given node or null if not found.
220 * @public
221 */
222 getJSDocComment(node) {
223
224 let parent = node.parent;
225
226 switch (node.type) {
227 case "ClassDeclaration":
228 case "FunctionDeclaration":
229 if (looksLikeExport(parent)) {
230 return findJSDocComment(parent.leadingComments, parent.loc.start.line);
231 }
232 return findJSDocComment(node.leadingComments, node.loc.start.line);
233
234 case "ClassExpression":
235 return findJSDocComment(parent.parent.leadingComments, parent.parent.loc.start.line);
236
237 case "ArrowFunctionExpression":
238 case "FunctionExpression":
239
240 if (parent.type !== "CallExpression" && parent.type !== "NewExpression") {
241 while (parent && !parent.leadingComments && !/Function/.test(parent.type) && parent.type !== "MethodDefinition" && parent.type !== "Property") {
242 parent = parent.parent;
243 }
244
245 return parent && (parent.type !== "FunctionDeclaration") ? findJSDocComment(parent.leadingComments, parent.loc.start.line) : null;
246 } else if (node.leadingComments) {
247 return findJSDocComment(node.leadingComments, node.loc.start.line);
248 }
249
250 // falls through
251
252 default:
253 return null;
254 }
255 },
256
257 /**
258 * Gets the deepest node containing a range index.
259 * @param {int} index Range index of the desired node.
260 * @returns {ASTNode} The node if found or null if not found.
261 */
262 getNodeByRangeIndex(index) {
263 let result = null,
264 resultParent = null;
265 const traverser = new Traverser();
266
267 traverser.traverse(this.ast, {
268 enter(node, parent) {
269 if (node.range[0] <= index && index < node.range[1]) {
270 result = node;
271 resultParent = parent;
272 } else {
273 this.skip();
274 }
275 },
276 leave(node) {
277 if (node === result) {
278 this.break();
279 }
280 }
281 });
282
283 return result ? Object.assign({parent: resultParent}, result) : null;
284 },
285
286 /**
287 * Determines if two tokens have at least one whitespace character
288 * between them. This completely disregards comments in making the
289 * determination, so comments count as zero-length substrings.
290 * @param {Token} first The token to check after.
291 * @param {Token} second The token to check before.
292 * @returns {boolean} True if there is only space between tokens, false
293 * if there is anything other than whitespace between tokens.
294 */
295 isSpaceBetweenTokens(first, second) {
296 const text = this.text.slice(first.range[1], second.range[0]);
297
298 return /\s/.test(text.replace(/\/\*.*?\*\//g, ""));
299 }
300};
301
302
303module.exports = SourceCode;