Blame view
node_modules/eslint/lib/ast-utils.js
5.15 KB
c39994410
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
/** * @fileoverview Common utils for AST. * @author Gyandeep Singh * @copyright 2015 Gyandeep Singh. All rights reserved. * See LICENSE file in root directory for full license. */ "use strict"; //------------------------------------------------------------------------------ // Requirements //------------------------------------------------------------------------------ var esutils = require("esutils"); //------------------------------------------------------------------------------ // Helpers //------------------------------------------------------------------------------ /** * Checks reference if is non initializer and writable. * @param {Reference} reference - A reference to check. * @param {int} index - The index of the reference in the references. * @param {Reference[]} references - The array that the reference belongs to. * @returns {boolean} Success/Failure * @private */ function isModifyingReference(reference, index, references) { var identifier = reference.identifier; return (identifier && reference.init === false && reference.isWrite() && // Destructuring assignments can have multiple default value, // so possibly there are multiple writeable references for the same identifier. (index === 0 || references[index - 1].identifier !== identifier) ); } //------------------------------------------------------------------------------ // Public Interface //------------------------------------------------------------------------------ module.exports = { /** * Determines whether two adjacent tokens are on the same line. * @param {Object} left - The left token object. * @param {Object} right - The right token object. * @returns {boolean} Whether or not the tokens are on the same line. * @public */ isTokenOnSameLine: function(left, right) { return left.loc.end.line === right.loc.start.line; }, /** * Checks whether or not a node is `null` or `undefined`. * @param {ASTNode} node - A node to check. * @returns {boolean} Whether or not the node is a `null` or `undefined`. * @public */ isNullOrUndefined: function(node) { return ( (node.type === "Literal" && node.value === null) || (node.type === "Identifier" && node.name === "undefined") || (node.type === "UnaryExpression" && node.operator === "void") ); }, /** * Checks whether or not a given node is a string literal. * @param {ASTNode} node - A node to check. * @returns {boolean} `true` if the node is a string literal. */ isStringLiteral: function(node) { return ( (node.type === "Literal" && typeof node.value === "string") || node.type === "TemplateLiteral" ); }, /** * Gets references which are non initializer and writable. * @param {Reference[]} references - An array of references. * @returns {Reference[]} An array of only references which are non initializer and writable. * @public */ getModifyingReferences: function(references) { return references.filter(isModifyingReference); }, /** * Validate that a string passed in is surrounded by the specified character * @param {string} val The text to check. * @param {string} character The character to see if it's surrounded by. * @returns {boolean} True if the text is surrounded by the character, false if not. * @private */ isSurroundedBy: function(val, character) { return val[0] === character && val[val.length - 1] === character; }, /** * Returns whether the provided node is an ESLint directive comment or not * @param {LineComment|BlockComment} node The node to be checked * @returns {boolean} `true` if the node is an ESLint directive comment */ isDirectiveComment: function(node) { var comment = node.value.trim(); return ( node.type === "Line" && comment.indexOf("eslint-") === 0 || node.type === "Block" && ( comment.indexOf("global ") === 0 || comment.indexOf("eslint ") === 0 || comment.indexOf("eslint-") === 0 ) ); }, /** * Gets the trailing statement of a given node. * * if (code) * consequent; * * When taking this `IfStatement`, returns `consequent;` statement. * * @param {ASTNode} A node to get. * @returns {ASTNode|null} The trailing statement's node. */ getTrailingStatement: esutils.ast.trailingStatement, /** * Finds the variable by a given name in a given scope and its upper scopes. * * @param {escope.Scope} initScope - A scope to start find. * @param {string} name - A variable name to find. * @returns {escope.Variable|null} A found variable or `null`. */ getVariableByName: function(initScope, name) { var scope = initScope; while (scope) { var variable = scope.set.get(name); if (variable) { return variable; } scope = scope.upper; } return null; } }; |