eslint-codemod-utils
Version:
A collection of AST helper functions for more complex ESLint rule fixes.
25 lines (24 loc) • 983 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getIdentifierInParentScope = void 0;
/**
* A useful function for finding a variable / variables value. This function
* traverses the scopes upwards until it arrives at the global scope. It will
* return when it exhausts the scopes or finds the variable.
*
* @param scope The current scope the variable exists in:
* @param identifierName The identifier / variable we're trying to look up
* @returns
*/
function getIdentifierInParentScope(scope, identifierName) {
let traversingScope = scope;
while (traversingScope && traversingScope.type !== 'global') {
const matchedVariable = traversingScope.variables.find((variable) => variable.name === identifierName);
if (matchedVariable) {
return matchedVariable;
}
traversingScope = traversingScope.upper;
}
return null;
}
exports.getIdentifierInParentScope = getIdentifierInParentScope;