eslint-plugin-sonarjs
Version:
27 lines (26 loc) • 1.23 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.areEquivalent = areEquivalent;
/**
* Equivalence is implemented by comparing node types and their tokens.
* Classic implementation would recursively compare children,
* but "estree" doesn't provide access to children when node type is unknown
*/
function areEquivalent(first, second, sourceCode, tokenComparator = sameTokenValue) {
if (Array.isArray(first) && Array.isArray(second)) {
return (first.length === second.length &&
first.every((firstNode, index) => areEquivalent(firstNode, second[index], sourceCode, tokenComparator)));
}
else if (!Array.isArray(first) && !Array.isArray(second)) {
return (first.type === second.type &&
compareTokens(sourceCode.getTokens(first), sourceCode.getTokens(second), tokenComparator));
}
return false;
}
function sameTokenValue(firstToken, secondToken) {
return firstToken.value === secondToken.value;
}
function compareTokens(firstTokens, secondTokens, tokenComparator) {
return (firstTokens.length === secondTokens.length &&
firstTokens.every((firstToken, index) => tokenComparator(firstToken, secondTokens[index])));
}