estree-toolkit
Version:
Traverser, scope tracker, and more tools for working with ESTree AST
46 lines (45 loc) • 1.31 kB
JavaScript
class BaseBinding {
constructor() {
this.references = [];
this.constantViolations = [];
}
addReference(path) {
this.references.push(path);
}
// Splice is performance intensive, I guess it wouldn't matter for small
// arrays, anyway someone's hardly gonna use this remove* methods
removeReference(path) {
const idx = this.references.findIndex((x) => x === path);
if (idx > -1)
this.references.splice(idx, 1);
}
addConstantViolation(path) {
this.constantViolations.push(path);
}
removeConstantViolation(path) {
const idx = this.constantViolations.findIndex((x) => x === path);
if (idx > -1)
this.constantViolations.splice(idx, 1);
}
}
export class Binding extends BaseBinding {
constructor(data) {
super();
this.kind = data.kind;
this.name = data.name;
this.scope = data.scope;
this.identifierPath = data.identifierPath;
this.path = data.path;
}
get constant() {
return this.constantViolations.length === 0;
}
}
export class GlobalBinding extends BaseBinding {
constructor(data) {
super();
this.kind = 'global';
this.constant = false;
this.name = data.name;
}
}