2020-01-28 13:08:03 +08:00
|
|
|
"use strict";
|
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
2020-03-07 11:45:40 +08:00
|
|
|
const util_1 = require("./util");
|
|
|
|
const ts = require("typescript");
|
2020-01-28 13:08:03 +08:00
|
|
|
var DeclarationDomain;
|
|
|
|
(function (DeclarationDomain) {
|
|
|
|
DeclarationDomain[DeclarationDomain["Namespace"] = 1] = "Namespace";
|
|
|
|
DeclarationDomain[DeclarationDomain["Type"] = 2] = "Type";
|
|
|
|
DeclarationDomain[DeclarationDomain["Value"] = 4] = "Value";
|
|
|
|
DeclarationDomain[DeclarationDomain["Import"] = 8] = "Import";
|
|
|
|
DeclarationDomain[DeclarationDomain["Any"] = 7] = "Any";
|
|
|
|
})(DeclarationDomain = exports.DeclarationDomain || (exports.DeclarationDomain = {}));
|
|
|
|
var UsageDomain;
|
|
|
|
(function (UsageDomain) {
|
|
|
|
UsageDomain[UsageDomain["Namespace"] = 1] = "Namespace";
|
|
|
|
UsageDomain[UsageDomain["Type"] = 2] = "Type";
|
|
|
|
UsageDomain[UsageDomain["Value"] = 4] = "Value";
|
|
|
|
UsageDomain[UsageDomain["ValueOrNamespace"] = 5] = "ValueOrNamespace";
|
|
|
|
UsageDomain[UsageDomain["Any"] = 7] = "Any";
|
|
|
|
UsageDomain[UsageDomain["TypeQuery"] = 8] = "TypeQuery";
|
|
|
|
})(UsageDomain = exports.UsageDomain || (exports.UsageDomain = {}));
|
|
|
|
function getUsageDomain(node) {
|
2020-03-07 11:45:40 +08:00
|
|
|
const parent = node.parent;
|
2020-01-28 13:08:03 +08:00
|
|
|
switch (parent.kind) {
|
|
|
|
case ts.SyntaxKind.TypeReference:
|
2020-03-07 11:45:40 +08:00
|
|
|
return node.originalKeywordKind !== ts.SyntaxKind.ConstKeyword ? 2 : undefined;
|
2020-01-28 13:08:03 +08:00
|
|
|
case ts.SyntaxKind.ExpressionWithTypeArguments:
|
|
|
|
return parent.parent.token === ts.SyntaxKind.ImplementsKeyword ||
|
|
|
|
parent.parent.parent.kind === ts.SyntaxKind.InterfaceDeclaration
|
|
|
|
? 2
|
|
|
|
: 4;
|
|
|
|
case ts.SyntaxKind.TypeQuery:
|
|
|
|
return 5 | 8;
|
|
|
|
case ts.SyntaxKind.QualifiedName:
|
|
|
|
if (parent.left === node) {
|
|
|
|
if (getEntityNameParent(parent).kind === ts.SyntaxKind.TypeQuery)
|
|
|
|
return 1 | 8;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ts.SyntaxKind.ExportSpecifier:
|
|
|
|
if (parent.propertyName === undefined ||
|
|
|
|
parent.propertyName === node)
|
|
|
|
return 7;
|
|
|
|
break;
|
|
|
|
case ts.SyntaxKind.ExportAssignment:
|
|
|
|
return 7;
|
|
|
|
case ts.SyntaxKind.BindingElement:
|
|
|
|
if (parent.initializer === node)
|
|
|
|
return 5;
|
|
|
|
break;
|
|
|
|
case ts.SyntaxKind.Parameter:
|
|
|
|
case ts.SyntaxKind.EnumMember:
|
|
|
|
case ts.SyntaxKind.PropertyDeclaration:
|
|
|
|
case ts.SyntaxKind.VariableDeclaration:
|
|
|
|
case ts.SyntaxKind.PropertyAssignment:
|
|
|
|
case ts.SyntaxKind.PropertyAccessExpression:
|
|
|
|
case ts.SyntaxKind.ImportEqualsDeclaration:
|
|
|
|
if (parent.name !== node)
|
|
|
|
return 5;
|
|
|
|
break;
|
|
|
|
case ts.SyntaxKind.JsxAttribute:
|
|
|
|
case ts.SyntaxKind.FunctionDeclaration:
|
|
|
|
case ts.SyntaxKind.FunctionExpression:
|
|
|
|
case ts.SyntaxKind.NamespaceImport:
|
|
|
|
case ts.SyntaxKind.ClassDeclaration:
|
|
|
|
case ts.SyntaxKind.ClassExpression:
|
|
|
|
case ts.SyntaxKind.ModuleDeclaration:
|
|
|
|
case ts.SyntaxKind.MethodDeclaration:
|
|
|
|
case ts.SyntaxKind.EnumDeclaration:
|
|
|
|
case ts.SyntaxKind.GetAccessor:
|
|
|
|
case ts.SyntaxKind.SetAccessor:
|
|
|
|
case ts.SyntaxKind.LabeledStatement:
|
|
|
|
case ts.SyntaxKind.BreakStatement:
|
|
|
|
case ts.SyntaxKind.ContinueStatement:
|
|
|
|
case ts.SyntaxKind.ImportClause:
|
|
|
|
case ts.SyntaxKind.ImportSpecifier:
|
|
|
|
case ts.SyntaxKind.TypePredicate:
|
|
|
|
case ts.SyntaxKind.MethodSignature:
|
|
|
|
case ts.SyntaxKind.PropertySignature:
|
|
|
|
case ts.SyntaxKind.NamespaceExportDeclaration:
|
|
|
|
case ts.SyntaxKind.InterfaceDeclaration:
|
|
|
|
case ts.SyntaxKind.TypeAliasDeclaration:
|
|
|
|
case ts.SyntaxKind.TypeParameter:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return 5;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
exports.getUsageDomain = getUsageDomain;
|
|
|
|
function getDeclarationDomain(node) {
|
|
|
|
switch (node.parent.kind) {
|
|
|
|
case ts.SyntaxKind.TypeParameter:
|
|
|
|
case ts.SyntaxKind.InterfaceDeclaration:
|
|
|
|
case ts.SyntaxKind.TypeAliasDeclaration:
|
|
|
|
return 2;
|
|
|
|
case ts.SyntaxKind.ClassDeclaration:
|
|
|
|
case ts.SyntaxKind.ClassExpression:
|
|
|
|
return 2 | 4;
|
|
|
|
case ts.SyntaxKind.EnumDeclaration:
|
|
|
|
return 7;
|
|
|
|
case ts.SyntaxKind.NamespaceImport:
|
|
|
|
case ts.SyntaxKind.ImportClause:
|
|
|
|
return 7 | 8;
|
|
|
|
case ts.SyntaxKind.ImportEqualsDeclaration:
|
|
|
|
case ts.SyntaxKind.ImportSpecifier:
|
|
|
|
return node.parent.name === node
|
|
|
|
? 7 | 8
|
|
|
|
: undefined;
|
|
|
|
case ts.SyntaxKind.ModuleDeclaration:
|
|
|
|
return 1;
|
|
|
|
case ts.SyntaxKind.Parameter:
|
2020-03-07 11:45:40 +08:00
|
|
|
if (node.parent.parent.kind === ts.SyntaxKind.IndexSignature || node.originalKeywordKind === ts.SyntaxKind.ThisKeyword)
|
2020-01-28 13:08:03 +08:00
|
|
|
return;
|
|
|
|
case ts.SyntaxKind.BindingElement:
|
|
|
|
case ts.SyntaxKind.VariableDeclaration:
|
|
|
|
return node.parent.name === node ? 4 : undefined;
|
|
|
|
case ts.SyntaxKind.FunctionDeclaration:
|
|
|
|
case ts.SyntaxKind.FunctionExpression:
|
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
exports.getDeclarationDomain = getDeclarationDomain;
|
|
|
|
function collectVariableUsage(sourceFile) {
|
|
|
|
return new UsageWalker().getUsage(sourceFile);
|
|
|
|
}
|
|
|
|
exports.collectVariableUsage = collectVariableUsage;
|
2020-03-07 11:45:40 +08:00
|
|
|
class AbstractScope {
|
|
|
|
constructor(_global) {
|
2020-01-28 13:08:03 +08:00
|
|
|
this._global = _global;
|
|
|
|
this._variables = new Map();
|
|
|
|
this._uses = [];
|
|
|
|
this._namespaceScopes = undefined;
|
|
|
|
this._enumScopes = undefined;
|
|
|
|
}
|
2020-03-07 11:45:40 +08:00
|
|
|
addVariable(identifier, name, selector, exported, domain) {
|
|
|
|
const variables = this.getDestinationScope(selector).getVariables();
|
|
|
|
const declaration = {
|
|
|
|
domain,
|
|
|
|
exported,
|
2020-01-28 13:08:03 +08:00
|
|
|
declaration: name,
|
|
|
|
};
|
2020-03-07 11:45:40 +08:00
|
|
|
const variable = variables.get(identifier);
|
2020-01-28 13:08:03 +08:00
|
|
|
if (variable === undefined) {
|
|
|
|
variables.set(identifier, {
|
2020-03-07 11:45:40 +08:00
|
|
|
domain,
|
2020-01-28 13:08:03 +08:00
|
|
|
declarations: [declaration],
|
|
|
|
uses: [],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
variable.domain |= domain;
|
|
|
|
variable.declarations.push(declaration);
|
|
|
|
}
|
2020-03-07 11:45:40 +08:00
|
|
|
}
|
|
|
|
addUse(use) {
|
2020-01-28 13:08:03 +08:00
|
|
|
this._uses.push(use);
|
2020-03-07 11:45:40 +08:00
|
|
|
}
|
|
|
|
getVariables() {
|
2020-01-28 13:08:03 +08:00
|
|
|
return this._variables;
|
2020-03-07 11:45:40 +08:00
|
|
|
}
|
|
|
|
getFunctionScope() {
|
2020-01-28 13:08:03 +08:00
|
|
|
return this;
|
2020-03-07 11:45:40 +08:00
|
|
|
}
|
|
|
|
end(cb) {
|
2020-01-28 13:08:03 +08:00
|
|
|
if (this._namespaceScopes !== undefined)
|
2020-03-07 11:45:40 +08:00
|
|
|
this._namespaceScopes.forEach((value) => value.finish(cb));
|
2020-01-28 13:08:03 +08:00
|
|
|
this._namespaceScopes = this._enumScopes = undefined;
|
|
|
|
this._applyUses();
|
2020-03-07 11:45:40 +08:00
|
|
|
this._variables.forEach((variable) => {
|
|
|
|
for (const declaration of variable.declarations) {
|
|
|
|
const result = {
|
2020-01-28 13:08:03 +08:00
|
|
|
declarations: [],
|
|
|
|
domain: declaration.domain,
|
|
|
|
exported: declaration.exported,
|
2020-03-07 11:45:40 +08:00
|
|
|
inGlobalScope: this._global,
|
2020-01-28 13:08:03 +08:00
|
|
|
uses: [],
|
|
|
|
};
|
2020-03-07 11:45:40 +08:00
|
|
|
for (const other of variable.declarations)
|
2020-01-28 13:08:03 +08:00
|
|
|
if (other.domain & declaration.domain)
|
|
|
|
result.declarations.push(other.declaration);
|
2020-03-07 11:45:40 +08:00
|
|
|
for (const use of variable.uses)
|
2020-01-28 13:08:03 +08:00
|
|
|
if (use.domain & declaration.domain)
|
|
|
|
result.uses.push(use);
|
2020-03-07 11:45:40 +08:00
|
|
|
cb(result, declaration.declaration, this);
|
2020-01-28 13:08:03 +08:00
|
|
|
}
|
|
|
|
});
|
2020-03-07 11:45:40 +08:00
|
|
|
}
|
|
|
|
markExported(_name) { }
|
|
|
|
createOrReuseNamespaceScope(name, _exported, ambient, hasExportStatement) {
|
|
|
|
let scope;
|
2020-01-28 13:08:03 +08:00
|
|
|
if (this._namespaceScopes === undefined) {
|
|
|
|
this._namespaceScopes = new Map();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
scope = this._namespaceScopes.get(name);
|
|
|
|
}
|
|
|
|
if (scope === undefined) {
|
|
|
|
scope = new NamespaceScope(ambient, hasExportStatement, this);
|
|
|
|
this._namespaceScopes.set(name, scope);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
scope.refresh(ambient, hasExportStatement);
|
|
|
|
}
|
|
|
|
return scope;
|
2020-03-07 11:45:40 +08:00
|
|
|
}
|
|
|
|
createOrReuseEnumScope(name, _exported) {
|
|
|
|
let scope;
|
2020-01-28 13:08:03 +08:00
|
|
|
if (this._enumScopes === undefined) {
|
|
|
|
this._enumScopes = new Map();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
scope = this._enumScopes.get(name);
|
|
|
|
}
|
|
|
|
if (scope === undefined) {
|
|
|
|
scope = new EnumScope(this);
|
|
|
|
this._enumScopes.set(name, scope);
|
|
|
|
}
|
|
|
|
return scope;
|
2020-03-07 11:45:40 +08:00
|
|
|
}
|
|
|
|
_applyUses() {
|
|
|
|
for (const use of this._uses)
|
2020-01-28 13:08:03 +08:00
|
|
|
if (!this._applyUse(use))
|
|
|
|
this._addUseToParent(use);
|
|
|
|
this._uses = [];
|
2020-03-07 11:45:40 +08:00
|
|
|
}
|
|
|
|
_applyUse(use, variables = this._variables) {
|
|
|
|
const variable = variables.get(use.location.text);
|
2020-01-28 13:08:03 +08:00
|
|
|
if (variable === undefined || (variable.domain & use.domain) === 0)
|
|
|
|
return false;
|
|
|
|
variable.uses.push(use);
|
|
|
|
return true;
|
2020-03-07 11:45:40 +08:00
|
|
|
}
|
|
|
|
_addUseToParent(_use) { }
|
|
|
|
}
|
|
|
|
class RootScope extends AbstractScope {
|
|
|
|
constructor(_exportAll, global) {
|
|
|
|
super(global);
|
|
|
|
this._exportAll = _exportAll;
|
|
|
|
this._exports = undefined;
|
|
|
|
this._innerScope = new NonRootScope(this, 1);
|
|
|
|
}
|
|
|
|
addVariable(identifier, name, selector, exported, domain) {
|
2020-01-28 13:08:03 +08:00
|
|
|
if (domain & 8)
|
2020-03-07 11:45:40 +08:00
|
|
|
return super.addVariable(identifier, name, selector, exported, domain);
|
|
|
|
return this._innerScope.addVariable(identifier, name, selector, exported, domain);
|
|
|
|
}
|
|
|
|
addUse(use, origin) {
|
2020-01-28 13:08:03 +08:00
|
|
|
if (origin === this._innerScope)
|
2020-03-07 11:45:40 +08:00
|
|
|
return super.addUse(use);
|
2020-01-28 13:08:03 +08:00
|
|
|
return this._innerScope.addUse(use);
|
2020-03-07 11:45:40 +08:00
|
|
|
}
|
|
|
|
markExported(id) {
|
2020-01-28 13:08:03 +08:00
|
|
|
if (this._exports === undefined) {
|
2020-03-07 11:45:40 +08:00
|
|
|
this._exports = [id.text];
|
2020-01-28 13:08:03 +08:00
|
|
|
}
|
|
|
|
else {
|
2020-03-07 11:45:40 +08:00
|
|
|
this._exports.push(id.text);
|
2020-01-28 13:08:03 +08:00
|
|
|
}
|
2020-03-07 11:45:40 +08:00
|
|
|
}
|
|
|
|
end(cb) {
|
|
|
|
this._innerScope.end((value, key) => {
|
|
|
|
value.exported = value.exported || this._exportAll
|
|
|
|
|| this._exports !== undefined && this._exports.includes(key.text);
|
|
|
|
value.inGlobalScope = this._global;
|
|
|
|
return cb(value, key, this);
|
2020-01-28 13:08:03 +08:00
|
|
|
});
|
2020-03-07 11:45:40 +08:00
|
|
|
return super.end((value, key, scope) => {
|
|
|
|
value.exported = value.exported || scope === this
|
|
|
|
&& this._exports !== undefined && this._exports.includes(key.text);
|
2020-01-28 13:08:03 +08:00
|
|
|
return cb(value, key, scope);
|
|
|
|
});
|
2020-03-07 11:45:40 +08:00
|
|
|
}
|
|
|
|
getDestinationScope() {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
class NonRootScope extends AbstractScope {
|
|
|
|
constructor(_parent, _boundary) {
|
|
|
|
super(false);
|
|
|
|
this._parent = _parent;
|
|
|
|
this._boundary = _boundary;
|
|
|
|
}
|
|
|
|
_addUseToParent(use) {
|
2020-01-28 13:08:03 +08:00
|
|
|
return this._parent.addUse(use, this);
|
2020-03-07 11:45:40 +08:00
|
|
|
}
|
|
|
|
getDestinationScope(selector) {
|
|
|
|
return this._boundary & selector
|
|
|
|
? this
|
|
|
|
: this._parent.getDestinationScope(selector);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
class EnumScope extends NonRootScope {
|
|
|
|
constructor(parent) {
|
|
|
|
super(parent, 1);
|
|
|
|
}
|
|
|
|
end() {
|
2020-01-28 13:08:03 +08:00
|
|
|
this._applyUses();
|
2020-03-07 11:45:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
class ConditionalTypeScope extends NonRootScope {
|
|
|
|
constructor(parent) {
|
|
|
|
super(parent, 8);
|
|
|
|
this._state = 0;
|
|
|
|
}
|
|
|
|
updateState(newState) {
|
2020-01-28 13:08:03 +08:00
|
|
|
this._state = newState;
|
2020-03-07 11:45:40 +08:00
|
|
|
}
|
|
|
|
addUse(use) {
|
2020-01-28 13:08:03 +08:00
|
|
|
if (this._state === 2)
|
|
|
|
return void this._uses.push(use);
|
|
|
|
return this._parent.addUse(use, this);
|
2020-03-07 11:45:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
class FunctionScope extends NonRootScope {
|
|
|
|
constructor(parent) {
|
|
|
|
super(parent, 1);
|
|
|
|
}
|
|
|
|
beginBody() {
|
2020-01-28 13:08:03 +08:00
|
|
|
this._applyUses();
|
2020-03-07 11:45:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
class AbstractNamedExpressionScope extends NonRootScope {
|
|
|
|
constructor(_name, _domain, parent) {
|
|
|
|
super(parent, 1);
|
|
|
|
this._name = _name;
|
|
|
|
this._domain = _domain;
|
|
|
|
}
|
|
|
|
end(cb) {
|
2020-01-28 13:08:03 +08:00
|
|
|
this._innerScope.end(cb);
|
|
|
|
return cb({
|
|
|
|
declarations: [this._name],
|
|
|
|
domain: this._domain,
|
|
|
|
exported: false,
|
|
|
|
uses: this._uses,
|
|
|
|
inGlobalScope: false,
|
|
|
|
}, this._name, this);
|
2020-03-07 11:45:40 +08:00
|
|
|
}
|
|
|
|
addUse(use, source) {
|
2020-01-28 13:08:03 +08:00
|
|
|
if (source !== this._innerScope)
|
|
|
|
return this._innerScope.addUse(use);
|
2020-03-07 11:45:40 +08:00
|
|
|
if (use.domain & this._domain && use.location.text === this._name.text) {
|
2020-01-28 13:08:03 +08:00
|
|
|
this._uses.push(use);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return this._parent.addUse(use, this);
|
|
|
|
}
|
2020-03-07 11:45:40 +08:00
|
|
|
}
|
|
|
|
getFunctionScope() {
|
2020-01-28 13:08:03 +08:00
|
|
|
return this._innerScope;
|
2020-03-07 11:45:40 +08:00
|
|
|
}
|
|
|
|
getDestinationScope() {
|
2020-01-28 13:08:03 +08:00
|
|
|
return this._innerScope;
|
2020-03-07 11:45:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
class FunctionExpressionScope extends AbstractNamedExpressionScope {
|
|
|
|
constructor(name, parent) {
|
|
|
|
super(name, 4, parent);
|
|
|
|
this._innerScope = new FunctionScope(this);
|
|
|
|
}
|
|
|
|
beginBody() {
|
2020-01-28 13:08:03 +08:00
|
|
|
return this._innerScope.beginBody();
|
2020-03-07 11:45:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
class ClassExpressionScope extends AbstractNamedExpressionScope {
|
|
|
|
constructor(name, parent) {
|
|
|
|
super(name, 4 | 2, parent);
|
|
|
|
this._innerScope = new NonRootScope(this, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
class BlockScope extends NonRootScope {
|
|
|
|
constructor(_functionScope, parent) {
|
|
|
|
super(parent, 2);
|
|
|
|
this._functionScope = _functionScope;
|
|
|
|
}
|
|
|
|
getFunctionScope() {
|
2020-01-28 13:08:03 +08:00
|
|
|
return this._functionScope;
|
2020-03-07 11:45:40 +08:00
|
|
|
}
|
|
|
|
}
|
2020-01-28 13:08:03 +08:00
|
|
|
function mapDeclaration(declaration) {
|
|
|
|
return {
|
2020-03-07 11:45:40 +08:00
|
|
|
declaration,
|
2020-01-28 13:08:03 +08:00
|
|
|
exported: true,
|
|
|
|
domain: getDeclarationDomain(declaration),
|
|
|
|
};
|
|
|
|
}
|
2020-03-07 11:45:40 +08:00
|
|
|
class NamespaceScope extends NonRootScope {
|
|
|
|
constructor(_ambient, _hasExport, parent) {
|
|
|
|
super(parent, 1);
|
|
|
|
this._ambient = _ambient;
|
|
|
|
this._hasExport = _hasExport;
|
|
|
|
this._innerScope = new NonRootScope(this, 1);
|
|
|
|
this._exports = undefined;
|
|
|
|
}
|
|
|
|
finish(cb) {
|
|
|
|
return super.end(cb);
|
|
|
|
}
|
|
|
|
end(cb) {
|
|
|
|
this._innerScope.end((variable, key, scope) => {
|
|
|
|
if (scope !== this._innerScope ||
|
|
|
|
!variable.exported && (!this._ambient || this._exports !== undefined && !this._exports.has(key.text)))
|
2020-01-28 13:08:03 +08:00
|
|
|
return cb(variable, key, scope);
|
2020-03-07 11:45:40 +08:00
|
|
|
const namespaceVar = this._variables.get(key.text);
|
2020-01-28 13:08:03 +08:00
|
|
|
if (namespaceVar === undefined) {
|
2020-03-07 11:45:40 +08:00
|
|
|
this._variables.set(key.text, {
|
2020-01-28 13:08:03 +08:00
|
|
|
declarations: variable.declarations.map(mapDeclaration),
|
|
|
|
domain: variable.domain,
|
2020-03-07 11:45:40 +08:00
|
|
|
uses: [...variable.uses],
|
2020-01-28 13:08:03 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
2020-03-07 11:45:40 +08:00
|
|
|
outer: for (const declaration of variable.declarations) {
|
|
|
|
for (const existing of namespaceVar.declarations)
|
2020-01-28 13:08:03 +08:00
|
|
|
if (existing.declaration === declaration)
|
|
|
|
continue outer;
|
|
|
|
namespaceVar.declarations.push(mapDeclaration(declaration));
|
|
|
|
}
|
|
|
|
namespaceVar.domain |= variable.domain;
|
2020-03-07 11:45:40 +08:00
|
|
|
for (const use of variable.uses) {
|
|
|
|
if (namespaceVar.uses.includes(use))
|
2020-01-28 13:08:03 +08:00
|
|
|
continue;
|
|
|
|
namespaceVar.uses.push(use);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
this._applyUses();
|
2020-03-07 11:45:40 +08:00
|
|
|
this._innerScope = new NonRootScope(this, 1);
|
|
|
|
}
|
|
|
|
createOrReuseNamespaceScope(name, exported, ambient, hasExportStatement) {
|
2020-01-28 13:08:03 +08:00
|
|
|
if (!exported && (!this._ambient || this._hasExport))
|
|
|
|
return this._innerScope.createOrReuseNamespaceScope(name, exported, ambient || this._ambient, hasExportStatement);
|
2020-03-07 11:45:40 +08:00
|
|
|
return super.createOrReuseNamespaceScope(name, exported, ambient || this._ambient, hasExportStatement);
|
|
|
|
}
|
|
|
|
createOrReuseEnumScope(name, exported) {
|
2020-01-28 13:08:03 +08:00
|
|
|
if (!exported && (!this._ambient || this._hasExport))
|
|
|
|
return this._innerScope.createOrReuseEnumScope(name, exported);
|
2020-03-07 11:45:40 +08:00
|
|
|
return super.createOrReuseEnumScope(name, exported);
|
|
|
|
}
|
|
|
|
addUse(use, source) {
|
2020-01-28 13:08:03 +08:00
|
|
|
if (source !== this._innerScope)
|
|
|
|
return this._innerScope.addUse(use);
|
|
|
|
this._uses.push(use);
|
2020-03-07 11:45:40 +08:00
|
|
|
}
|
|
|
|
refresh(ambient, hasExport) {
|
2020-01-28 13:08:03 +08:00
|
|
|
this._ambient = ambient;
|
|
|
|
this._hasExport = hasExport;
|
2020-03-07 11:45:40 +08:00
|
|
|
}
|
|
|
|
markExported(name, _as) {
|
2020-01-28 13:08:03 +08:00
|
|
|
if (this._exports === undefined)
|
|
|
|
this._exports = new Set();
|
2020-03-07 11:45:40 +08:00
|
|
|
this._exports.add(name.text);
|
|
|
|
}
|
|
|
|
getDestinationScope() {
|
2020-01-28 13:08:03 +08:00
|
|
|
return this._innerScope;
|
2020-03-07 11:45:40 +08:00
|
|
|
}
|
|
|
|
}
|
2020-01-28 13:08:03 +08:00
|
|
|
function getEntityNameParent(name) {
|
2020-03-07 11:45:40 +08:00
|
|
|
let parent = name.parent;
|
2020-01-28 13:08:03 +08:00
|
|
|
while (parent.kind === ts.SyntaxKind.QualifiedName)
|
|
|
|
parent = parent.parent;
|
|
|
|
return parent;
|
|
|
|
}
|
2020-03-07 11:45:40 +08:00
|
|
|
class UsageWalker {
|
|
|
|
constructor() {
|
2020-01-28 13:08:03 +08:00
|
|
|
this._result = new Map();
|
|
|
|
}
|
2020-03-07 11:45:40 +08:00
|
|
|
getUsage(sourceFile) {
|
|
|
|
const variableCallback = (variable, key) => {
|
|
|
|
this._result.set(key, variable);
|
2020-01-28 13:08:03 +08:00
|
|
|
};
|
2020-03-07 11:45:40 +08:00
|
|
|
const isModule = ts.isExternalModule(sourceFile);
|
2020-01-28 13:08:03 +08:00
|
|
|
this._scope = new RootScope(sourceFile.isDeclarationFile && isModule && !containsExportStatement(sourceFile), !isModule);
|
2020-03-07 11:45:40 +08:00
|
|
|
const cb = (node) => {
|
2020-01-28 13:08:03 +08:00
|
|
|
if (util_1.isBlockScopeBoundary(node))
|
2020-03-07 11:45:40 +08:00
|
|
|
return continueWithScope(node, new BlockScope(this._scope.getFunctionScope(), this._scope), handleBlockScope);
|
2020-01-28 13:08:03 +08:00
|
|
|
switch (node.kind) {
|
|
|
|
case ts.SyntaxKind.ClassExpression:
|
|
|
|
return continueWithScope(node, node.name !== undefined
|
2020-03-07 11:45:40 +08:00
|
|
|
? new ClassExpressionScope(node.name, this._scope)
|
|
|
|
: new NonRootScope(this._scope, 1));
|
2020-01-28 13:08:03 +08:00
|
|
|
case ts.SyntaxKind.ClassDeclaration:
|
2020-03-07 11:45:40 +08:00
|
|
|
this._handleDeclaration(node, true, 4 | 2);
|
|
|
|
return continueWithScope(node, new NonRootScope(this._scope, 1));
|
2020-01-28 13:08:03 +08:00
|
|
|
case ts.SyntaxKind.InterfaceDeclaration:
|
|
|
|
case ts.SyntaxKind.TypeAliasDeclaration:
|
2020-03-07 11:45:40 +08:00
|
|
|
this._handleDeclaration(node, true, 2);
|
|
|
|
return continueWithScope(node, new NonRootScope(this._scope, 4));
|
2020-01-28 13:08:03 +08:00
|
|
|
case ts.SyntaxKind.EnumDeclaration:
|
2020-03-07 11:45:40 +08:00
|
|
|
this._handleDeclaration(node, true, 7);
|
|
|
|
return continueWithScope(node, this._scope.createOrReuseEnumScope(node.name.text, util_1.hasModifier(node.modifiers, ts.SyntaxKind.ExportKeyword)));
|
2020-01-28 13:08:03 +08:00
|
|
|
case ts.SyntaxKind.ModuleDeclaration:
|
2020-03-07 11:45:40 +08:00
|
|
|
return this._handleModule(node, continueWithScope);
|
2020-01-28 13:08:03 +08:00
|
|
|
case ts.SyntaxKind.MappedType:
|
2020-03-07 11:45:40 +08:00
|
|
|
return continueWithScope(node, new NonRootScope(this._scope, 4));
|
2020-01-28 13:08:03 +08:00
|
|
|
case ts.SyntaxKind.FunctionExpression:
|
|
|
|
case ts.SyntaxKind.ArrowFunction:
|
|
|
|
case ts.SyntaxKind.Constructor:
|
|
|
|
case ts.SyntaxKind.MethodDeclaration:
|
|
|
|
case ts.SyntaxKind.FunctionDeclaration:
|
|
|
|
case ts.SyntaxKind.GetAccessor:
|
|
|
|
case ts.SyntaxKind.SetAccessor:
|
|
|
|
case ts.SyntaxKind.MethodSignature:
|
|
|
|
case ts.SyntaxKind.CallSignature:
|
|
|
|
case ts.SyntaxKind.ConstructSignature:
|
|
|
|
case ts.SyntaxKind.ConstructorType:
|
|
|
|
case ts.SyntaxKind.FunctionType:
|
2020-03-07 11:45:40 +08:00
|
|
|
return this._handleFunctionLikeDeclaration(node, cb, variableCallback);
|
2020-01-28 13:08:03 +08:00
|
|
|
case ts.SyntaxKind.ConditionalType:
|
2020-03-07 11:45:40 +08:00
|
|
|
return this._handleConditionalType(node, cb, variableCallback);
|
2020-01-28 13:08:03 +08:00
|
|
|
case ts.SyntaxKind.VariableDeclarationList:
|
2020-03-07 11:45:40 +08:00
|
|
|
this._handleVariableDeclaration(node);
|
2020-01-28 13:08:03 +08:00
|
|
|
break;
|
|
|
|
case ts.SyntaxKind.Parameter:
|
|
|
|
if (node.parent.kind !== ts.SyntaxKind.IndexSignature &&
|
|
|
|
(node.name.kind !== ts.SyntaxKind.Identifier ||
|
|
|
|
node.name.originalKeywordKind !== ts.SyntaxKind.ThisKeyword))
|
2020-03-07 11:45:40 +08:00
|
|
|
this._handleBindingName(node.name, false, false);
|
2020-01-28 13:08:03 +08:00
|
|
|
break;
|
|
|
|
case ts.SyntaxKind.EnumMember:
|
2020-03-07 11:45:40 +08:00
|
|
|
this._scope.addVariable(util_1.getPropertyName(node.name), node.name, 1, true, 4);
|
2020-01-28 13:08:03 +08:00
|
|
|
break;
|
|
|
|
case ts.SyntaxKind.ImportClause:
|
|
|
|
case ts.SyntaxKind.ImportSpecifier:
|
|
|
|
case ts.SyntaxKind.NamespaceImport:
|
|
|
|
case ts.SyntaxKind.ImportEqualsDeclaration:
|
2020-03-07 11:45:40 +08:00
|
|
|
this._handleDeclaration(node, false, 7 | 8);
|
2020-01-28 13:08:03 +08:00
|
|
|
break;
|
|
|
|
case ts.SyntaxKind.TypeParameter:
|
2020-03-07 11:45:40 +08:00
|
|
|
this._scope.addVariable(node.name.text, node.name, node.parent.kind === ts.SyntaxKind.InferType ? 8 : 7, false, 2);
|
2020-01-28 13:08:03 +08:00
|
|
|
break;
|
|
|
|
case ts.SyntaxKind.ExportSpecifier:
|
|
|
|
if (node.propertyName !== undefined)
|
2020-03-07 11:45:40 +08:00
|
|
|
return this._scope.markExported(node.propertyName, node.name);
|
|
|
|
return this._scope.markExported(node.name);
|
2020-01-28 13:08:03 +08:00
|
|
|
case ts.SyntaxKind.ExportAssignment:
|
|
|
|
if (node.expression.kind === ts.SyntaxKind.Identifier)
|
2020-03-07 11:45:40 +08:00
|
|
|
return this._scope.markExported(node.expression);
|
2020-01-28 13:08:03 +08:00
|
|
|
break;
|
|
|
|
case ts.SyntaxKind.Identifier:
|
2020-03-07 11:45:40 +08:00
|
|
|
const domain = getUsageDomain(node);
|
2020-01-28 13:08:03 +08:00
|
|
|
if (domain !== undefined)
|
2020-03-07 11:45:40 +08:00
|
|
|
this._scope.addUse({ domain, location: node });
|
2020-01-28 13:08:03 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
return ts.forEachChild(node, cb);
|
|
|
|
};
|
2020-03-07 11:45:40 +08:00
|
|
|
const continueWithScope = (node, scope, next = forEachChild) => {
|
|
|
|
const savedScope = this._scope;
|
|
|
|
this._scope = scope;
|
2020-01-28 13:08:03 +08:00
|
|
|
next(node);
|
2020-03-07 11:45:40 +08:00
|
|
|
this._scope.end(variableCallback);
|
|
|
|
this._scope = savedScope;
|
2020-01-28 13:08:03 +08:00
|
|
|
};
|
2020-03-07 11:45:40 +08:00
|
|
|
const handleBlockScope = (node) => {
|
2020-01-28 13:08:03 +08:00
|
|
|
if (node.kind === ts.SyntaxKind.CatchClause && node.variableDeclaration !== undefined)
|
2020-03-07 11:45:40 +08:00
|
|
|
this._handleBindingName(node.variableDeclaration.name, true, false);
|
2020-01-28 13:08:03 +08:00
|
|
|
return ts.forEachChild(node, cb);
|
|
|
|
};
|
|
|
|
ts.forEachChild(sourceFile, cb);
|
|
|
|
this._scope.end(variableCallback);
|
|
|
|
return this._result;
|
|
|
|
function forEachChild(node) {
|
|
|
|
return ts.forEachChild(node, cb);
|
|
|
|
}
|
2020-03-07 11:45:40 +08:00
|
|
|
}
|
|
|
|
_handleConditionalType(node, cb, varCb) {
|
|
|
|
const savedScope = this._scope;
|
|
|
|
const scope = this._scope = new ConditionalTypeScope(savedScope);
|
2020-01-28 13:08:03 +08:00
|
|
|
cb(node.checkType);
|
|
|
|
scope.updateState(1);
|
|
|
|
cb(node.extendsType);
|
|
|
|
scope.updateState(2);
|
|
|
|
cb(node.trueType);
|
|
|
|
scope.updateState(3);
|
|
|
|
cb(node.falseType);
|
|
|
|
scope.end(varCb);
|
|
|
|
this._scope = savedScope;
|
2020-03-07 11:45:40 +08:00
|
|
|
}
|
|
|
|
_handleFunctionLikeDeclaration(node, cb, varCb) {
|
2020-01-28 13:08:03 +08:00
|
|
|
if (node.decorators !== undefined)
|
|
|
|
node.decorators.forEach(cb);
|
2020-03-07 11:45:40 +08:00
|
|
|
const savedScope = this._scope;
|
2020-01-28 13:08:03 +08:00
|
|
|
if (node.kind === ts.SyntaxKind.FunctionDeclaration)
|
|
|
|
this._handleDeclaration(node, false, 4);
|
2020-03-07 11:45:40 +08:00
|
|
|
const scope = this._scope = node.kind === ts.SyntaxKind.FunctionExpression && node.name !== undefined
|
2020-01-28 13:08:03 +08:00
|
|
|
? new FunctionExpressionScope(node.name, savedScope)
|
|
|
|
: new FunctionScope(savedScope);
|
|
|
|
if (node.name !== undefined)
|
|
|
|
cb(node.name);
|
|
|
|
if (node.typeParameters !== undefined)
|
|
|
|
node.typeParameters.forEach(cb);
|
|
|
|
node.parameters.forEach(cb);
|
|
|
|
if (node.type !== undefined)
|
|
|
|
cb(node.type);
|
|
|
|
if (node.body !== undefined) {
|
|
|
|
scope.beginBody();
|
|
|
|
cb(node.body);
|
|
|
|
}
|
|
|
|
scope.end(varCb);
|
|
|
|
this._scope = savedScope;
|
2020-03-07 11:45:40 +08:00
|
|
|
}
|
|
|
|
_handleModule(node, next) {
|
2020-01-28 13:08:03 +08:00
|
|
|
if (node.flags & ts.NodeFlags.GlobalAugmentation)
|
|
|
|
return next(node, this._scope.createOrReuseNamespaceScope('-global', false, true, false));
|
|
|
|
if (node.name.kind === ts.SyntaxKind.Identifier) {
|
2020-03-07 11:45:40 +08:00
|
|
|
const exported = isNamespaceExported(node);
|
|
|
|
this._scope.addVariable(node.name.text, node.name, 1, exported, 1 | 4);
|
|
|
|
const ambient = util_1.hasModifier(node.modifiers, ts.SyntaxKind.DeclareKeyword);
|
|
|
|
return next(node, this._scope.createOrReuseNamespaceScope(node.name.text, exported, ambient, ambient && namespaceHasExportStatement(node)));
|
2020-01-28 13:08:03 +08:00
|
|
|
}
|
2020-03-07 11:45:40 +08:00
|
|
|
return next(node, this._scope.createOrReuseNamespaceScope(`"${node.name.text}"`, false, true, namespaceHasExportStatement(node)));
|
|
|
|
}
|
|
|
|
_handleDeclaration(node, blockScoped, domain) {
|
2020-01-28 13:08:03 +08:00
|
|
|
if (node.name !== undefined)
|
2020-03-07 11:45:40 +08:00
|
|
|
this._scope.addVariable(node.name.text, node.name, blockScoped ? 3 : 1, util_1.hasModifier(node.modifiers, ts.SyntaxKind.ExportKeyword), domain);
|
|
|
|
}
|
|
|
|
_handleBindingName(name, blockScoped, exported) {
|
2020-01-28 13:08:03 +08:00
|
|
|
if (name.kind === ts.SyntaxKind.Identifier)
|
2020-03-07 11:45:40 +08:00
|
|
|
return this._scope.addVariable(name.text, name, blockScoped ? 3 : 1, exported, 4);
|
|
|
|
util_1.forEachDestructuringIdentifier(name, (declaration) => {
|
|
|
|
this._scope.addVariable(declaration.name.text, declaration.name, blockScoped ? 3 : 1, exported, 4);
|
2020-01-28 13:08:03 +08:00
|
|
|
});
|
2020-03-07 11:45:40 +08:00
|
|
|
}
|
|
|
|
_handleVariableDeclaration(declarationList) {
|
|
|
|
const blockScoped = util_1.isBlockScopedVariableDeclarationList(declarationList);
|
|
|
|
const exported = declarationList.parent.kind === ts.SyntaxKind.VariableStatement &&
|
2020-01-28 13:08:03 +08:00
|
|
|
util_1.hasModifier(declarationList.parent.modifiers, ts.SyntaxKind.ExportKeyword);
|
2020-03-07 11:45:40 +08:00
|
|
|
for (const declaration of declarationList.declarations)
|
2020-01-28 13:08:03 +08:00
|
|
|
this._handleBindingName(declaration.name, blockScoped, exported);
|
2020-03-07 11:45:40 +08:00
|
|
|
}
|
|
|
|
}
|
2020-01-28 13:08:03 +08:00
|
|
|
function isNamespaceExported(node) {
|
|
|
|
return node.parent.kind === ts.SyntaxKind.ModuleDeclaration || util_1.hasModifier(node.modifiers, ts.SyntaxKind.ExportKeyword);
|
|
|
|
}
|
|
|
|
function namespaceHasExportStatement(ns) {
|
|
|
|
if (ns.body === undefined || ns.body.kind !== ts.SyntaxKind.ModuleBlock)
|
|
|
|
return false;
|
|
|
|
return containsExportStatement(ns.body);
|
|
|
|
}
|
|
|
|
function containsExportStatement(block) {
|
2020-03-07 11:45:40 +08:00
|
|
|
for (const statement of block.statements)
|
2020-01-28 13:08:03 +08:00
|
|
|
if (statement.kind === ts.SyntaxKind.ExportDeclaration || statement.kind === ts.SyntaxKind.ExportAssignment)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|