eslint-plugin-enact
Version:
Enact specific linting rules for ESLint
59 lines (51 loc) • 1.62 kB
JavaScript
/**
* @fileoverview Utility functions for React components detection
* @author Yannick Croissant
*/
;
/**
* Search a particular variable in a list
* @param {Array} variables The variables list.
* @param {Array} name The name of the variable to search.
* @returns {Boolean} True if the variable was found, false if not.
*/
function findVariable(variables, name) {
var i;
var len;
for (i = 0, len = variables.length; i < len; i++) {
if (variables[i].name === name) {
return true;
}
}
return false;
}
/**
* List all variable in a given scope
*
* Contain a patch for babel-eslint to avoid https://github.com/babel/babel-eslint/issues/21
*
* @param {Object} sourceCode The current rule sourceCode.
* @param {Object} context The current rule context.
* @param {ASTNode} node The AST node being checked (must be a MemberExpression).
* @returns {Boolean} True if the variable was found, false if not.
*/
function variablesInScope(sourceCode, context, node) {
let scope = sourceCode.getScope ? sourceCode.getScope(node) : context.getScope();
var variables = scope.variables;
while (scope.type !== 'global') {
scope = scope.upper;
variables = scope.variables.concat(variables);
}
if (scope.childScopes.length) {
variables = scope.childScopes[0].variables.concat(variables);
if (scope.childScopes[0].childScopes.length) {
variables = scope.childScopes[0].childScopes[0].variables.concat(variables);
}
}
variables.reverse();
return variables;
}
module.exports = {
findVariable: findVariable,
variablesInScope: variablesInScope
};