1 | /**
|
2 | * @fileoverview Common utils for AST.
|
3 | * @author Gyandeep Singh
|
4 | * @copyright 2015 Gyandeep Singh. All rights reserved.
|
5 | * See LICENSE file in root directory for full license.
|
6 | */
|
7 |
|
8 | ;
|
9 |
|
10 | //------------------------------------------------------------------------------
|
11 | // Helpers
|
12 | //------------------------------------------------------------------------------
|
13 |
|
14 | /**
|
15 | * Checks reference if is non initializer and writable.
|
16 | * @param {Reference} reference - A reference to check.
|
17 | * @param {int} index - The index of the reference in the references.
|
18 | * @param {Reference[]} references - The array that the reference belongs to.
|
19 | * @returns {boolean} Success/Failure
|
20 | * @private
|
21 | */
|
22 | function isModifyingReference(reference, index, references) {
|
23 | var identifier = reference.identifier;
|
24 |
|
25 | return (identifier &&
|
26 | reference.init === false &&
|
27 | reference.isWrite() &&
|
28 | // Destructuring assignments can have multiple default value,
|
29 | // so possibly there are multiple writeable references for the same identifier.
|
30 | (index === 0 || references[index - 1].identifier !== identifier)
|
31 | );
|
32 | }
|
33 |
|
34 | //------------------------------------------------------------------------------
|
35 | // Public Interface
|
36 | //------------------------------------------------------------------------------
|
37 |
|
38 | module.exports = {
|
39 |
|
40 | /**
|
41 | * Determines whether two adjacent tokens are have whitespace between them.
|
42 | * @param {Object} left - The left token object.
|
43 | * @param {Object} right - The right token object.
|
44 | * @returns {boolean} Whether or not there is space between the tokens.
|
45 | * @public
|
46 | */
|
47 | isTokenSpaced: function(left, right) {
|
48 | return left.range[1] < right.range[0];
|
49 | },
|
50 |
|
51 | /**
|
52 | * Determines whether two adjacent tokens are on the same line.
|
53 | * @param {Object} left - The left token object.
|
54 | * @param {Object} right - The right token object.
|
55 | * @returns {boolean} Whether or not the tokens are on the same line.
|
56 | * @public
|
57 | */
|
58 | isTokenOnSameLine: function(left, right) {
|
59 | return left.loc.end.line === right.loc.start.line;
|
60 | },
|
61 |
|
62 | /**
|
63 | * Checks whether or not a node is `null` or `undefined`.
|
64 | * @param {ASTNode} node - A node to check.
|
65 | * @returns {boolean} Whether or not the node is a `null` or `undefined`.
|
66 | * @public
|
67 | */
|
68 | isNullOrUndefined: function(node) {
|
69 | return (
|
70 | (node.type === "Literal" && node.value === null) ||
|
71 | (node.type === "Identifier" && node.name === "undefined") ||
|
72 | (node.type === "UnaryExpression" && node.operator === "void")
|
73 | );
|
74 | },
|
75 |
|
76 | /**
|
77 | * Checks whether or not a given node is a string literal.
|
78 | * @param {ASTNode} node - A node to check.
|
79 | * @returns {boolean} `true` if the node is a string literal.
|
80 | */
|
81 | isStringLiteral: function(node) {
|
82 | return (
|
83 | (node.type === "Literal" && typeof node.value === "string") ||
|
84 | node.type === "TemplateLiteral"
|
85 | );
|
86 | },
|
87 |
|
88 | /**
|
89 | * Gets references which are non initializer and writable.
|
90 | * @param {Reference[]} references - An array of references.
|
91 | * @returns {Reference[]} An array of only references which are non initializer and writable.
|
92 | * @public
|
93 | */
|
94 | getModifyingReferences: function(references) {
|
95 | return references.filter(isModifyingReference);
|
96 | },
|
97 |
|
98 | /**
|
99 | * Validate that a string passed in is surrounded by the specified character
|
100 | * @param {string} val The text to check.
|
101 | * @param {string} character The character to see if it's surrounded by.
|
102 | * @returns {boolean} True if the text is surrounded by the character, false if not.
|
103 | * @private
|
104 | */
|
105 | isSurroundedBy: function(val, character) {
|
106 | return val[0] === character && val[val.length - 1] === character;
|
107 | }
|
108 | };
|