--- /dev/null
+"use strict";\r
+Object.defineProperty(exports, "__esModule", { value: true });\r
+exports.collectVariableUsage = exports.getDeclarationDomain = exports.getUsageDomain = exports.UsageDomain = exports.DeclarationDomain = void 0;\r
+const util_1 = require("./util");\r
+const ts = require("typescript");\r
+var DeclarationDomain;\r
+(function (DeclarationDomain) {\r
+ DeclarationDomain[DeclarationDomain["Namespace"] = 1] = "Namespace";\r
+ DeclarationDomain[DeclarationDomain["Type"] = 2] = "Type";\r
+ DeclarationDomain[DeclarationDomain["Value"] = 4] = "Value";\r
+ DeclarationDomain[DeclarationDomain["Import"] = 8] = "Import";\r
+ DeclarationDomain[DeclarationDomain["Any"] = 7] = "Any";\r
+})(DeclarationDomain = exports.DeclarationDomain || (exports.DeclarationDomain = {}));\r
+var UsageDomain;\r
+(function (UsageDomain) {\r
+ UsageDomain[UsageDomain["Namespace"] = 1] = "Namespace";\r
+ UsageDomain[UsageDomain["Type"] = 2] = "Type";\r
+ UsageDomain[UsageDomain["Value"] = 4] = "Value";\r
+ UsageDomain[UsageDomain["ValueOrNamespace"] = 5] = "ValueOrNamespace";\r
+ UsageDomain[UsageDomain["Any"] = 7] = "Any";\r
+ UsageDomain[UsageDomain["TypeQuery"] = 8] = "TypeQuery";\r
+})(UsageDomain = exports.UsageDomain || (exports.UsageDomain = {}));\r
+// TODO handle cases where values are used only for their types, e.g. `declare [propSymbol]: number`\r
+function getUsageDomain(node) {\r
+ const parent = node.parent;\r
+ switch (parent.kind) {\r
+ case ts.SyntaxKind.TypeReference:\r
+ return node.originalKeywordKind !== ts.SyntaxKind.ConstKeyword ? 2 /* Type */ : undefined;\r
+ case ts.SyntaxKind.ExpressionWithTypeArguments:\r
+ return parent.parent.token === ts.SyntaxKind.ImplementsKeyword ||\r
+ parent.parent.parent.kind === ts.SyntaxKind.InterfaceDeclaration\r
+ ? 2 /* Type */\r
+ : 4 /* Value */;\r
+ case ts.SyntaxKind.TypeQuery:\r
+ return 5 /* ValueOrNamespace */ | 8 /* TypeQuery */;\r
+ case ts.SyntaxKind.QualifiedName:\r
+ if (parent.left === node) {\r
+ if (getEntityNameParent(parent).kind === ts.SyntaxKind.TypeQuery)\r
+ return 1 /* Namespace */ | 8 /* TypeQuery */;\r
+ return 1 /* Namespace */;\r
+ }\r
+ break;\r
+ case ts.SyntaxKind.ExportSpecifier:\r
+ // either {name} or {propertyName as name}\r
+ if (parent.propertyName === undefined ||\r
+ parent.propertyName === node)\r
+ return 7 /* Any */; // TODO handle type-only exports\r
+ break;\r
+ case ts.SyntaxKind.ExportAssignment:\r
+ return 7 /* Any */;\r
+ // Value\r
+ case ts.SyntaxKind.BindingElement:\r
+ if (parent.initializer === node)\r
+ return 5 /* ValueOrNamespace */;\r
+ break;\r
+ case ts.SyntaxKind.Parameter:\r
+ case ts.SyntaxKind.EnumMember:\r
+ case ts.SyntaxKind.PropertyDeclaration:\r
+ case ts.SyntaxKind.VariableDeclaration:\r
+ case ts.SyntaxKind.PropertyAssignment:\r
+ case ts.SyntaxKind.PropertyAccessExpression:\r
+ case ts.SyntaxKind.ImportEqualsDeclaration:\r
+ if (parent.name !== node)\r
+ return 5 /* ValueOrNamespace */; // TODO handle type-only imports\r
+ break;\r
+ case ts.SyntaxKind.JsxAttribute:\r
+ case ts.SyntaxKind.FunctionDeclaration:\r
+ case ts.SyntaxKind.FunctionExpression:\r
+ case ts.SyntaxKind.NamespaceImport:\r
+ case ts.SyntaxKind.ClassDeclaration:\r
+ case ts.SyntaxKind.ClassExpression:\r
+ case ts.SyntaxKind.ModuleDeclaration:\r
+ case ts.SyntaxKind.MethodDeclaration:\r
+ case ts.SyntaxKind.EnumDeclaration:\r
+ case ts.SyntaxKind.GetAccessor:\r
+ case ts.SyntaxKind.SetAccessor:\r
+ case ts.SyntaxKind.LabeledStatement:\r
+ case ts.SyntaxKind.BreakStatement:\r
+ case ts.SyntaxKind.ContinueStatement:\r
+ case ts.SyntaxKind.ImportClause:\r
+ case ts.SyntaxKind.ImportSpecifier:\r
+ case ts.SyntaxKind.TypePredicate: // TODO this actually references a parameter\r
+ case ts.SyntaxKind.MethodSignature:\r
+ case ts.SyntaxKind.PropertySignature:\r
+ case ts.SyntaxKind.NamespaceExportDeclaration:\r
+ case ts.SyntaxKind.NamespaceExport:\r
+ case ts.SyntaxKind.InterfaceDeclaration:\r
+ case ts.SyntaxKind.TypeAliasDeclaration:\r
+ case ts.SyntaxKind.TypeParameter:\r
+ case ts.SyntaxKind.NamedTupleMember:\r
+ break;\r
+ default:\r
+ return 5 /* ValueOrNamespace */;\r
+ }\r
+}\r
+exports.getUsageDomain = getUsageDomain;\r
+function getDeclarationDomain(node) {\r
+ switch (node.parent.kind) {\r
+ case ts.SyntaxKind.TypeParameter:\r
+ case ts.SyntaxKind.InterfaceDeclaration:\r
+ case ts.SyntaxKind.TypeAliasDeclaration:\r
+ return 2 /* Type */;\r
+ case ts.SyntaxKind.ClassDeclaration:\r
+ case ts.SyntaxKind.ClassExpression:\r
+ return 2 /* Type */ | 4 /* Value */;\r
+ case ts.SyntaxKind.EnumDeclaration:\r
+ return 7 /* Any */;\r
+ case ts.SyntaxKind.NamespaceImport:\r
+ case ts.SyntaxKind.ImportClause:\r
+ return 7 /* Any */ | 8 /* Import */; // TODO handle type-only imports\r
+ case ts.SyntaxKind.ImportEqualsDeclaration:\r
+ case ts.SyntaxKind.ImportSpecifier:\r
+ return node.parent.name === node\r
+ ? 7 /* Any */ | 8 /* Import */ // TODO handle type-only imports\r
+ : undefined;\r
+ case ts.SyntaxKind.ModuleDeclaration:\r
+ return 1 /* Namespace */;\r
+ case ts.SyntaxKind.Parameter:\r
+ if (node.parent.parent.kind === ts.SyntaxKind.IndexSignature || node.originalKeywordKind === ts.SyntaxKind.ThisKeyword)\r
+ return;\r
+ // falls through\r
+ case ts.SyntaxKind.BindingElement:\r
+ case ts.SyntaxKind.VariableDeclaration:\r
+ return node.parent.name === node ? 4 /* Value */ : undefined;\r
+ case ts.SyntaxKind.FunctionDeclaration:\r
+ case ts.SyntaxKind.FunctionExpression:\r
+ return 4 /* Value */;\r
+ }\r
+}\r
+exports.getDeclarationDomain = getDeclarationDomain;\r
+function collectVariableUsage(sourceFile) {\r
+ return new UsageWalker().getUsage(sourceFile);\r
+}\r
+exports.collectVariableUsage = collectVariableUsage;\r
+class AbstractScope {\r
+ constructor(_global) {\r
+ this._global = _global;\r
+ this._variables = new Map();\r
+ this._uses = [];\r
+ this._namespaceScopes = undefined;\r
+ this._enumScopes = undefined;\r
+ }\r
+ addVariable(identifier, name, selector, exported, domain) {\r
+ const variables = this.getDestinationScope(selector).getVariables();\r
+ const declaration = {\r
+ domain,\r
+ exported,\r
+ declaration: name,\r
+ };\r
+ const variable = variables.get(identifier);\r
+ if (variable === undefined) {\r
+ variables.set(identifier, {\r
+ domain,\r
+ declarations: [declaration],\r
+ uses: [],\r
+ });\r
+ }\r
+ else {\r
+ variable.domain |= domain;\r
+ variable.declarations.push(declaration);\r
+ }\r
+ }\r
+ addUse(use) {\r
+ this._uses.push(use);\r
+ }\r
+ getVariables() {\r
+ return this._variables;\r
+ }\r
+ getFunctionScope() {\r
+ return this;\r
+ }\r
+ end(cb) {\r
+ if (this._namespaceScopes !== undefined)\r
+ this._namespaceScopes.forEach((value) => value.finish(cb));\r
+ this._namespaceScopes = this._enumScopes = undefined;\r
+ this._applyUses();\r
+ this._variables.forEach((variable) => {\r
+ for (const declaration of variable.declarations) {\r
+ const result = {\r
+ declarations: [],\r
+ domain: declaration.domain,\r
+ exported: declaration.exported,\r
+ inGlobalScope: this._global,\r
+ uses: [],\r
+ };\r
+ for (const other of variable.declarations)\r
+ if (other.domain & declaration.domain)\r
+ result.declarations.push(other.declaration);\r
+ for (const use of variable.uses)\r
+ if (use.domain & declaration.domain)\r
+ result.uses.push(use);\r
+ cb(result, declaration.declaration, this);\r
+ }\r
+ });\r
+ }\r
+ // tslint:disable-next-line:prefer-function-over-method\r
+ markExported(_name) { } // only relevant for the root scope\r
+ createOrReuseNamespaceScope(name, _exported, ambient, hasExportStatement) {\r
+ let scope;\r
+ if (this._namespaceScopes === undefined) {\r
+ this._namespaceScopes = new Map();\r
+ }\r
+ else {\r
+ scope = this._namespaceScopes.get(name);\r
+ }\r
+ if (scope === undefined) {\r
+ scope = new NamespaceScope(ambient, hasExportStatement, this);\r
+ this._namespaceScopes.set(name, scope);\r
+ }\r
+ else {\r
+ scope.refresh(ambient, hasExportStatement);\r
+ }\r
+ return scope;\r
+ }\r
+ createOrReuseEnumScope(name, _exported) {\r
+ let scope;\r
+ if (this._enumScopes === undefined) {\r
+ this._enumScopes = new Map();\r
+ }\r
+ else {\r
+ scope = this._enumScopes.get(name);\r
+ }\r
+ if (scope === undefined) {\r
+ scope = new EnumScope(this);\r
+ this._enumScopes.set(name, scope);\r
+ }\r
+ return scope;\r
+ }\r
+ _applyUses() {\r
+ for (const use of this._uses)\r
+ if (!this._applyUse(use))\r
+ this._addUseToParent(use);\r
+ this._uses = [];\r
+ }\r
+ _applyUse(use, variables = this._variables) {\r
+ const variable = variables.get(use.location.text);\r
+ if (variable === undefined || (variable.domain & use.domain) === 0)\r
+ return false;\r
+ variable.uses.push(use);\r
+ return true;\r
+ }\r
+ _addUseToParent(_use) { } // tslint:disable-line:prefer-function-over-method\r
+}\r
+class RootScope extends AbstractScope {\r
+ constructor(_exportAll, global) {\r
+ super(global);\r
+ this._exportAll = _exportAll;\r
+ this._exports = undefined;\r
+ this._innerScope = new NonRootScope(this, 1 /* Function */);\r
+ }\r
+ addVariable(identifier, name, selector, exported, domain) {\r
+ if (domain & 8 /* Import */)\r
+ return super.addVariable(identifier, name, selector, exported, domain);\r
+ return this._innerScope.addVariable(identifier, name, selector, exported, domain);\r
+ }\r
+ addUse(use, origin) {\r
+ if (origin === this._innerScope)\r
+ return super.addUse(use);\r
+ return this._innerScope.addUse(use);\r
+ }\r
+ markExported(id) {\r
+ if (this._exports === undefined) {\r
+ this._exports = [id.text];\r
+ }\r
+ else {\r
+ this._exports.push(id.text);\r
+ }\r
+ }\r
+ end(cb) {\r
+ this._innerScope.end((value, key) => {\r
+ value.exported = value.exported || this._exportAll\r
+ || this._exports !== undefined && this._exports.includes(key.text);\r
+ value.inGlobalScope = this._global;\r
+ return cb(value, key, this);\r
+ });\r
+ return super.end((value, key, scope) => {\r
+ value.exported = value.exported || scope === this\r
+ && this._exports !== undefined && this._exports.includes(key.text);\r
+ return cb(value, key, scope);\r
+ });\r
+ }\r
+ getDestinationScope() {\r
+ return this;\r
+ }\r
+}\r
+class NonRootScope extends AbstractScope {\r
+ constructor(_parent, _boundary) {\r
+ super(false);\r
+ this._parent = _parent;\r
+ this._boundary = _boundary;\r
+ }\r
+ _addUseToParent(use) {\r
+ return this._parent.addUse(use, this);\r
+ }\r
+ getDestinationScope(selector) {\r
+ return this._boundary & selector\r
+ ? this\r
+ : this._parent.getDestinationScope(selector);\r
+ }\r
+}\r
+class EnumScope extends NonRootScope {\r
+ constructor(parent) {\r
+ super(parent, 1 /* Function */);\r
+ }\r
+ end() {\r
+ this._applyUses();\r
+ }\r
+}\r
+class ConditionalTypeScope extends NonRootScope {\r
+ constructor(parent) {\r
+ super(parent, 8 /* ConditionalType */);\r
+ this._state = 0 /* Initial */;\r
+ }\r
+ updateState(newState) {\r
+ this._state = newState;\r
+ }\r
+ addUse(use) {\r
+ if (this._state === 2 /* TrueType */)\r
+ return void this._uses.push(use);\r
+ return this._parent.addUse(use, this);\r
+ }\r
+}\r
+class FunctionScope extends NonRootScope {\r
+ constructor(parent) {\r
+ super(parent, 1 /* Function */);\r
+ }\r
+ beginBody() {\r
+ this._applyUses();\r
+ }\r
+}\r
+class AbstractNamedExpressionScope extends NonRootScope {\r
+ constructor(_name, _domain, parent) {\r
+ super(parent, 1 /* Function */);\r
+ this._name = _name;\r
+ this._domain = _domain;\r
+ }\r
+ end(cb) {\r
+ this._innerScope.end(cb);\r
+ return cb({\r
+ declarations: [this._name],\r
+ domain: this._domain,\r
+ exported: false,\r
+ uses: this._uses,\r
+ inGlobalScope: false,\r
+ }, this._name, this);\r
+ }\r
+ addUse(use, source) {\r
+ if (source !== this._innerScope)\r
+ return this._innerScope.addUse(use);\r
+ if (use.domain & this._domain && use.location.text === this._name.text) {\r
+ this._uses.push(use);\r
+ }\r
+ else {\r
+ return this._parent.addUse(use, this);\r
+ }\r
+ }\r
+ getFunctionScope() {\r
+ return this._innerScope;\r
+ }\r
+ getDestinationScope() {\r
+ return this._innerScope;\r
+ }\r
+}\r
+class FunctionExpressionScope extends AbstractNamedExpressionScope {\r
+ constructor(name, parent) {\r
+ super(name, 4 /* Value */, parent);\r
+ this._innerScope = new FunctionScope(this);\r
+ }\r
+ beginBody() {\r
+ return this._innerScope.beginBody();\r
+ }\r
+}\r
+class ClassExpressionScope extends AbstractNamedExpressionScope {\r
+ constructor(name, parent) {\r
+ super(name, 4 /* Value */ | 2 /* Type */, parent);\r
+ this._innerScope = new NonRootScope(this, 1 /* Function */);\r
+ }\r
+}\r
+class BlockScope extends NonRootScope {\r
+ constructor(_functionScope, parent) {\r
+ super(parent, 2 /* Block */);\r
+ this._functionScope = _functionScope;\r
+ }\r
+ getFunctionScope() {\r
+ return this._functionScope;\r
+ }\r
+}\r
+function mapDeclaration(declaration) {\r
+ return {\r
+ declaration,\r
+ exported: true,\r
+ domain: getDeclarationDomain(declaration),\r
+ };\r
+}\r
+class NamespaceScope extends NonRootScope {\r
+ constructor(_ambient, _hasExport, parent) {\r
+ super(parent, 1 /* Function */);\r
+ this._ambient = _ambient;\r
+ this._hasExport = _hasExport;\r
+ this._innerScope = new NonRootScope(this, 1 /* Function */);\r
+ this._exports = undefined;\r
+ }\r
+ finish(cb) {\r
+ return super.end(cb);\r
+ }\r
+ end(cb) {\r
+ this._innerScope.end((variable, key, scope) => {\r
+ if (scope !== this._innerScope ||\r
+ !variable.exported && (!this._ambient || this._exports !== undefined && !this._exports.has(key.text)))\r
+ return cb(variable, key, scope);\r
+ const namespaceVar = this._variables.get(key.text);\r
+ if (namespaceVar === undefined) {\r
+ this._variables.set(key.text, {\r
+ declarations: variable.declarations.map(mapDeclaration),\r
+ domain: variable.domain,\r
+ uses: [...variable.uses],\r
+ });\r
+ }\r
+ else {\r
+ outer: for (const declaration of variable.declarations) {\r
+ for (const existing of namespaceVar.declarations)\r
+ if (existing.declaration === declaration)\r
+ continue outer;\r
+ namespaceVar.declarations.push(mapDeclaration(declaration));\r
+ }\r
+ namespaceVar.domain |= variable.domain;\r
+ for (const use of variable.uses) {\r
+ if (namespaceVar.uses.includes(use))\r
+ continue;\r
+ namespaceVar.uses.push(use);\r
+ }\r
+ }\r
+ });\r
+ this._applyUses();\r
+ this._innerScope = new NonRootScope(this, 1 /* Function */);\r
+ }\r
+ createOrReuseNamespaceScope(name, exported, ambient, hasExportStatement) {\r
+ if (!exported && (!this._ambient || this._hasExport))\r
+ return this._innerScope.createOrReuseNamespaceScope(name, exported, ambient || this._ambient, hasExportStatement);\r
+ return super.createOrReuseNamespaceScope(name, exported, ambient || this._ambient, hasExportStatement);\r
+ }\r
+ createOrReuseEnumScope(name, exported) {\r
+ if (!exported && (!this._ambient || this._hasExport))\r
+ return this._innerScope.createOrReuseEnumScope(name, exported);\r
+ return super.createOrReuseEnumScope(name, exported);\r
+ }\r
+ addUse(use, source) {\r
+ if (source !== this._innerScope)\r
+ return this._innerScope.addUse(use);\r
+ this._uses.push(use);\r
+ }\r
+ refresh(ambient, hasExport) {\r
+ this._ambient = ambient;\r
+ this._hasExport = hasExport;\r
+ }\r
+ markExported(name, _as) {\r
+ if (this._exports === undefined)\r
+ this._exports = new Set();\r
+ this._exports.add(name.text);\r
+ }\r
+ getDestinationScope() {\r
+ return this._innerScope;\r
+ }\r
+}\r
+function getEntityNameParent(name) {\r
+ let parent = name.parent;\r
+ while (parent.kind === ts.SyntaxKind.QualifiedName)\r
+ parent = parent.parent;\r
+ return parent;\r
+}\r
+// TODO class decorators resolve outside of class, element and parameter decorator resolve inside/at the class\r
+// TODO computed property name resolves inside/at the cass\r
+// TODO this and super in all of them are resolved outside of the class\r
+class UsageWalker {\r
+ constructor() {\r
+ this._result = new Map();\r
+ }\r
+ getUsage(sourceFile) {\r
+ const variableCallback = (variable, key) => {\r
+ this._result.set(key, variable);\r
+ };\r
+ const isModule = ts.isExternalModule(sourceFile);\r
+ this._scope = new RootScope(sourceFile.isDeclarationFile && isModule && !containsExportStatement(sourceFile), !isModule);\r
+ const cb = (node) => {\r
+ if (util_1.isBlockScopeBoundary(node))\r
+ return continueWithScope(node, new BlockScope(this._scope.getFunctionScope(), this._scope), handleBlockScope);\r
+ switch (node.kind) {\r
+ case ts.SyntaxKind.ClassExpression:\r
+ return continueWithScope(node, node.name !== undefined\r
+ ? new ClassExpressionScope(node.name, this._scope)\r
+ : new NonRootScope(this._scope, 1 /* Function */));\r
+ case ts.SyntaxKind.ClassDeclaration:\r
+ this._handleDeclaration(node, true, 4 /* Value */ | 2 /* Type */);\r
+ return continueWithScope(node, new NonRootScope(this._scope, 1 /* Function */));\r
+ case ts.SyntaxKind.InterfaceDeclaration:\r
+ case ts.SyntaxKind.TypeAliasDeclaration:\r
+ this._handleDeclaration(node, true, 2 /* Type */);\r
+ return continueWithScope(node, new NonRootScope(this._scope, 4 /* Type */));\r
+ case ts.SyntaxKind.EnumDeclaration:\r
+ this._handleDeclaration(node, true, 7 /* Any */);\r
+ return continueWithScope(node, this._scope.createOrReuseEnumScope(node.name.text, util_1.hasModifier(node.modifiers, ts.SyntaxKind.ExportKeyword)));\r
+ case ts.SyntaxKind.ModuleDeclaration:\r
+ return this._handleModule(node, continueWithScope);\r
+ case ts.SyntaxKind.MappedType:\r
+ return continueWithScope(node, new NonRootScope(this._scope, 4 /* Type */));\r
+ case ts.SyntaxKind.FunctionExpression:\r
+ case ts.SyntaxKind.ArrowFunction:\r
+ case ts.SyntaxKind.Constructor:\r
+ case ts.SyntaxKind.MethodDeclaration:\r
+ case ts.SyntaxKind.FunctionDeclaration:\r
+ case ts.SyntaxKind.GetAccessor:\r
+ case ts.SyntaxKind.SetAccessor:\r
+ case ts.SyntaxKind.MethodSignature:\r
+ case ts.SyntaxKind.CallSignature:\r
+ case ts.SyntaxKind.ConstructSignature:\r
+ case ts.SyntaxKind.ConstructorType:\r
+ case ts.SyntaxKind.FunctionType:\r
+ return this._handleFunctionLikeDeclaration(node, cb, variableCallback);\r
+ case ts.SyntaxKind.ConditionalType:\r
+ return this._handleConditionalType(node, cb, variableCallback);\r
+ // End of Scope specific handling\r
+ case ts.SyntaxKind.VariableDeclarationList:\r
+ this._handleVariableDeclaration(node);\r
+ break;\r
+ case ts.SyntaxKind.Parameter:\r
+ if (node.parent.kind !== ts.SyntaxKind.IndexSignature &&\r
+ (node.name.kind !== ts.SyntaxKind.Identifier ||\r
+ node.name.originalKeywordKind !== ts.SyntaxKind.ThisKeyword))\r
+ this._handleBindingName(node.name, false, false);\r
+ break;\r
+ case ts.SyntaxKind.EnumMember:\r
+ this._scope.addVariable(util_1.getPropertyName(node.name), node.name, 1 /* Function */, true, 4 /* Value */);\r
+ break;\r
+ case ts.SyntaxKind.ImportClause:\r
+ case ts.SyntaxKind.ImportSpecifier:\r
+ case ts.SyntaxKind.NamespaceImport:\r
+ case ts.SyntaxKind.ImportEqualsDeclaration:\r
+ this._handleDeclaration(node, false, 7 /* Any */ | 8 /* Import */);\r
+ break;\r
+ case ts.SyntaxKind.TypeParameter:\r
+ this._scope.addVariable(node.name.text, node.name, node.parent.kind === ts.SyntaxKind.InferType ? 8 /* InferType */ : 7 /* Type */, false, 2 /* Type */);\r
+ break;\r
+ case ts.SyntaxKind.ExportSpecifier:\r
+ if (node.propertyName !== undefined)\r
+ return this._scope.markExported(node.propertyName, node.name);\r
+ return this._scope.markExported(node.name);\r
+ case ts.SyntaxKind.ExportAssignment:\r
+ if (node.expression.kind === ts.SyntaxKind.Identifier)\r
+ return this._scope.markExported(node.expression);\r
+ break;\r
+ case ts.SyntaxKind.Identifier:\r
+ const domain = getUsageDomain(node);\r
+ if (domain !== undefined)\r
+ this._scope.addUse({ domain, location: node });\r
+ return;\r
+ }\r
+ return ts.forEachChild(node, cb);\r
+ };\r
+ const continueWithScope = (node, scope, next = forEachChild) => {\r
+ const savedScope = this._scope;\r
+ this._scope = scope;\r
+ next(node);\r
+ this._scope.end(variableCallback);\r
+ this._scope = savedScope;\r
+ };\r
+ const handleBlockScope = (node) => {\r
+ if (node.kind === ts.SyntaxKind.CatchClause && node.variableDeclaration !== undefined)\r
+ this._handleBindingName(node.variableDeclaration.name, true, false);\r
+ return ts.forEachChild(node, cb);\r
+ };\r
+ ts.forEachChild(sourceFile, cb);\r
+ this._scope.end(variableCallback);\r
+ return this._result;\r
+ function forEachChild(node) {\r
+ return ts.forEachChild(node, cb);\r
+ }\r
+ }\r
+ _handleConditionalType(node, cb, varCb) {\r
+ const savedScope = this._scope;\r
+ const scope = this._scope = new ConditionalTypeScope(savedScope);\r
+ cb(node.checkType);\r
+ scope.updateState(1 /* Extends */);\r
+ cb(node.extendsType);\r
+ scope.updateState(2 /* TrueType */);\r
+ cb(node.trueType);\r
+ scope.updateState(3 /* FalseType */);\r
+ cb(node.falseType);\r
+ scope.end(varCb);\r
+ this._scope = savedScope;\r
+ }\r
+ _handleFunctionLikeDeclaration(node, cb, varCb) {\r
+ if (node.decorators !== undefined)\r
+ node.decorators.forEach(cb);\r
+ const savedScope = this._scope;\r
+ if (node.kind === ts.SyntaxKind.FunctionDeclaration)\r
+ this._handleDeclaration(node, false, 4 /* Value */);\r
+ const scope = this._scope = node.kind === ts.SyntaxKind.FunctionExpression && node.name !== undefined\r
+ ? new FunctionExpressionScope(node.name, savedScope)\r
+ : new FunctionScope(savedScope);\r
+ if (node.name !== undefined)\r
+ cb(node.name);\r
+ if (node.typeParameters !== undefined)\r
+ node.typeParameters.forEach(cb);\r
+ node.parameters.forEach(cb);\r
+ if (node.type !== undefined)\r
+ cb(node.type);\r
+ if (node.body !== undefined) {\r
+ scope.beginBody();\r
+ cb(node.body);\r
+ }\r
+ scope.end(varCb);\r
+ this._scope = savedScope;\r
+ }\r
+ _handleModule(node, next) {\r
+ if (node.flags & ts.NodeFlags.GlobalAugmentation)\r
+ return next(node, this._scope.createOrReuseNamespaceScope('-global', false, true, false));\r
+ if (node.name.kind === ts.SyntaxKind.Identifier) {\r
+ const exported = isNamespaceExported(node);\r
+ this._scope.addVariable(node.name.text, node.name, 1 /* Function */, exported, 1 /* Namespace */ | 4 /* Value */);\r
+ const ambient = util_1.hasModifier(node.modifiers, ts.SyntaxKind.DeclareKeyword);\r
+ return next(node, this._scope.createOrReuseNamespaceScope(node.name.text, exported, ambient, ambient && namespaceHasExportStatement(node)));\r
+ }\r
+ return next(node, this._scope.createOrReuseNamespaceScope(`"${node.name.text}"`, false, true, namespaceHasExportStatement(node)));\r
+ }\r
+ _handleDeclaration(node, blockScoped, domain) {\r
+ if (node.name !== undefined)\r
+ this._scope.addVariable(node.name.text, node.name, blockScoped ? 3 /* Block */ : 1 /* Function */, util_1.hasModifier(node.modifiers, ts.SyntaxKind.ExportKeyword), domain);\r
+ }\r
+ _handleBindingName(name, blockScoped, exported) {\r
+ if (name.kind === ts.SyntaxKind.Identifier)\r
+ return this._scope.addVariable(name.text, name, blockScoped ? 3 /* Block */ : 1 /* Function */, exported, 4 /* Value */);\r
+ util_1.forEachDestructuringIdentifier(name, (declaration) => {\r
+ this._scope.addVariable(declaration.name.text, declaration.name, blockScoped ? 3 /* Block */ : 1 /* Function */, exported, 4 /* Value */);\r
+ });\r
+ }\r
+ _handleVariableDeclaration(declarationList) {\r
+ const blockScoped = util_1.isBlockScopedVariableDeclarationList(declarationList);\r
+ const exported = declarationList.parent.kind === ts.SyntaxKind.VariableStatement &&\r
+ util_1.hasModifier(declarationList.parent.modifiers, ts.SyntaxKind.ExportKeyword);\r
+ for (const declaration of declarationList.declarations)\r
+ this._handleBindingName(declaration.name, blockScoped, exported);\r
+ }\r
+}\r
+function isNamespaceExported(node) {\r
+ return node.parent.kind === ts.SyntaxKind.ModuleDeclaration || util_1.hasModifier(node.modifiers, ts.SyntaxKind.ExportKeyword);\r
+}\r
+function namespaceHasExportStatement(ns) {\r
+ if (ns.body === undefined || ns.body.kind !== ts.SyntaxKind.ModuleBlock)\r
+ return false;\r
+ return containsExportStatement(ns.body);\r
+}\r
+function containsExportStatement(block) {\r
+ for (const statement of block.statements)\r
+ if (statement.kind === ts.SyntaxKind.ExportDeclaration || statement.kind === ts.SyntaxKind.ExportAssignment)\r
+ return true;\r
+ return false;\r
+}\r
+//# sourceMappingURL=usage.js.map
\ No newline at end of file