UNPKG

js-ast-parser

Version:
76 lines 2.81 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Scope = void 0; const Context_1 = require("./Context"); const uuid_1 = require("uuid"); class Scope { constructor(parent) { this.parent = parent; this.definitions = []; this.definitionsMap = {}; this.context = new Context_1.Context(); this.functionScope = false; this.exports = []; this.unusedDefination = new Map(); this.UndeclaredVariables = new Set(); this.id = (0, uuid_1.v4)(); } addDefinition(definition) { this.definitions.push(this.definitionsMap[definition.identifier] = definition); } addDefinitions(definitions) { definitions.forEach(definition => this.addDefinition(definition)); } getDefinition(identifier) { return this.definitionsMap[identifier] || (this.parent && this.parent.getDefinition(identifier) || null); } deleteUnuse(definition) { if (this.unusedDefination.has(definition.astNode)) { this.unusedDefination.delete(definition.astNode); } else if (this.parent) { this.parent.deleteUnuse(definition); } } addUnuse(definition) { this.unusedDefination.set(definition.astNode, definition); } isUnuse(definition) { return !!this.unusedDefination.get(definition.astNode); } fullMatchDefinition(text) { return this.definitionsMap[text] || this.fullMatchParentDefinitions(text); } matchParentDefinitions(text, near = 0) { if (this.parent) return this.parent.matchDefinitions(text, near); return []; } fullMatchParentDefinitions(text) { if (this.parent) return this.parent.fullMatchDefinition(text); return []; } matchOwnDefinitions(text, near = 0) { return this.definitions.reduce((result, definition) => { let index; if (definition.identifier !== text && (index = definition.identifier.indexOf(text)) > -1) { let textLeft = definition.identifier.slice(0, index), textRight = definition.identifier.slice(index + text.length); result.push({ near: near, definition: definition, index: index, text: text, textLeft: textLeft, textRight: textRight }); } return result; }, []); } matchDefinitions(text, near = 0) { return this.matchOwnDefinitions(text, near).concat(this.matchParentDefinitions(text, near + 1)); } } exports.Scope = Scope; //# sourceMappingURL=Scope.js.map