static-injector 1.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/import/commonjs/index.js +1248 -0
- package/import/es2015/decorator/injectable.js +14 -0
- package/import/es2015/decorator/interface/provider.js +8 -0
- package/import/es2015/di/forward_ref.js +53 -0
- package/import/es2015/di/inject_switch.js +31 -0
- package/import/es2015/di/injection_token.js +71 -0
- package/import/es2015/di/injector.js +63 -0
- package/import/es2015/di/injector_compatibility.js +172 -0
- package/import/es2015/di/injector_marker.js +8 -0
- package/import/es2015/di/injector_token.js +21 -0
- package/import/es2015/di/interface/defs.js +72 -0
- package/import/es2015/di/interface/injector.js +25 -0
- package/import/es2015/di/interface/provider.js +8 -0
- package/import/es2015/di/metadata.js +49 -0
- package/import/es2015/di/null_injector.js +19 -0
- package/import/es2015/di/provider_token.js +8 -0
- package/import/es2015/di/r3_injector.js +382 -0
- package/import/es2015/di/scope.js +14 -0
- package/import/es2015/index.js +11 -0
- package/import/es2015/interface/lifecycle_hooks.js +8 -0
- package/import/es2015/interface/type.js +8 -0
- package/import/es2015/render3/definition_factory.js +15 -0
- package/import/es2015/render3/error_code.js +29 -0
- package/import/es2015/render3/errors_di.js +14 -0
- package/import/es2015/render3/fields.js +11 -0
- package/import/es2015/render3/instructions/di.js +23 -0
- package/import/es2015/render3/util/stringify_utils.js +37 -0
- package/import/es2015/util/array_utils.js +17 -0
- package/import/es2015/util/closure.js +19 -0
- package/import/es2015/util/decorators.js +30 -0
- package/import/es2015/util/empty.js +14 -0
- package/import/es2015/util/property.js +15 -0
- package/import/es2015/util/stringify.js +30 -0
- package/import/fesm2015/index.js +1211 -0
- package/import/typings/decorator/injectable.d.ts +82 -0
- package/import/typings/decorator/interface/provider.d.ts +312 -0
- package/import/typings/di/forward_ref.d.ts +49 -0
- package/import/typings/di/inject_switch.d.ts +17 -0
- package/import/typings/di/injection_token.d.ts +57 -0
- package/import/typings/di/injector.d.ts +76 -0
- package/import/typings/di/injector_compatibility.d.ts +77 -0
- package/import/typings/di/injector_marker.d.ts +23 -0
- package/import/typings/di/injector_token.d.ts +18 -0
- package/import/typings/di/interface/defs.d.ts +155 -0
- package/import/typings/di/interface/injector.d.ts +48 -0
- package/import/typings/di/interface/provider.d.ts +312 -0
- package/import/typings/di/metadata.d.ts +176 -0
- package/import/typings/di/null_injector.d.ts +11 -0
- package/import/typings/di/provider_token.d.ts +17 -0
- package/import/typings/di/r3_injector.d.ts +92 -0
- package/import/typings/di/scope.d.ts +14 -0
- package/import/typings/index.d.ts +11 -0
- package/import/typings/interface/lifecycle_hooks.d.ts +28 -0
- package/import/typings/interface/type.d.ts +21 -0
- package/import/typings/render3/definition_factory.d.ts +24 -0
- package/import/typings/render3/error_code.d.ts +24 -0
- package/import/typings/render3/errors_di.d.ts +2 -0
- package/import/typings/render3/fields.d.ts +8 -0
- package/import/typings/render3/instructions/di.d.ts +20 -0
- package/import/typings/render3/util/stringify_utils.d.ts +20 -0
- package/import/typings/util/array_utils.d.ts +10 -0
- package/import/typings/util/closure.d.ts +17 -0
- package/import/typings/util/decorators.d.ts +27 -0
- package/import/typings/util/empty.d.ts +14 -0
- package/import/typings/util/property.d.ts +8 -0
- package/import/typings/util/stringify.d.ts +8 -0
- package/package.json +34 -0
- package/readme.md +27 -0
- package/transform/compiler/index.d.ts +5 -0
- package/transform/compiler/index.js +17 -0
- package/transform/compiler/src/core.d.ts +19 -0
- package/transform/compiler/src/core.js +9 -0
- package/transform/compiler/src/injectable_compiler_2.d.ts +53 -0
- package/transform/compiler/src/injectable_compiler_2.js +159 -0
- package/transform/compiler/src/output/output_ast.d.ts +534 -0
- package/transform/compiler/src/output/output_ast.js +986 -0
- package/transform/compiler/src/parse_util.d.ts +50 -0
- package/transform/compiler/src/parse_util.js +61 -0
- package/transform/compiler/src/render3/partial/api.d.ts +166 -0
- package/transform/compiler/src/render3/partial/api.js +11 -0
- package/transform/compiler/src/render3/partial/util.d.ts +16 -0
- package/transform/compiler/src/render3/partial/util.js +44 -0
- package/transform/compiler/src/render3/r3_factory.d.ts +97 -0
- package/transform/compiler/src/render3/r3_factory.js +239 -0
- package/transform/compiler/src/render3/r3_identifiers.d.ts +20 -0
- package/transform/compiler/src/render3/r3_identifiers.js +51 -0
- package/transform/compiler/src/render3/util.d.ts +21 -0
- package/transform/compiler/src/render3/util.js +41 -0
- package/transform/compiler/src/render3/view/util.d.ts +30 -0
- package/transform/compiler/src/render3/view/util.js +57 -0
- package/transform/compiler-cli/src/ngtsc/annotations/src/factory.d.ts +11 -0
- package/transform/compiler-cli/src/ngtsc/annotations/src/factory.js +21 -0
- package/transform/compiler-cli/src/ngtsc/annotations/src/injectable.d.ts +42 -0
- package/transform/compiler-cli/src/ngtsc/annotations/src/injectable.js +278 -0
- package/transform/compiler-cli/src/ngtsc/annotations/src/util.d.ts +91 -0
- package/transform/compiler-cli/src/ngtsc/annotations/src/util.js +370 -0
- package/transform/compiler-cli/src/ngtsc/diagnostics/error.d.ts +24 -0
- package/transform/compiler-cli/src/ngtsc/diagnostics/error.js +76 -0
- package/transform/compiler-cli/src/ngtsc/diagnostics/error_code.d.ts +188 -0
- package/transform/compiler-cli/src/ngtsc/diagnostics/error_code.js +206 -0
- package/transform/compiler-cli/src/ngtsc/diagnostics/index.d.ts +2 -0
- package/transform/compiler-cli/src/ngtsc/diagnostics/index.js +14 -0
- package/transform/compiler-cli/src/ngtsc/imports/index.d.ts +1 -0
- package/transform/compiler-cli/src/ngtsc/imports/index.js +13 -0
- package/transform/compiler-cli/src/ngtsc/imports/src/core.d.ts +36 -0
- package/transform/compiler-cli/src/ngtsc/imports/src/core.js +25 -0
- package/transform/compiler-cli/src/ngtsc/reflection/index.d.ts +3 -0
- package/transform/compiler-cli/src/ngtsc/reflection/index.js +15 -0
- package/transform/compiler-cli/src/ngtsc/reflection/src/host.d.ts +667 -0
- package/transform/compiler-cli/src/ngtsc/reflection/src/host.js +98 -0
- package/transform/compiler-cli/src/ngtsc/reflection/src/type_to_value.d.ts +24 -0
- package/transform/compiler-cli/src/ngtsc/reflection/src/type_to_value.js +271 -0
- package/transform/compiler-cli/src/ngtsc/reflection/src/typescript.d.ts +50 -0
- package/transform/compiler-cli/src/ngtsc/reflection/src/typescript.js +396 -0
- package/transform/compiler-cli/src/ngtsc/reflection/src/util.d.ts +12 -0
- package/transform/compiler-cli/src/ngtsc/reflection/src/util.js +45 -0
- package/transform/compiler-cli/src/ngtsc/transform/index.d.ts +2 -0
- package/transform/compiler-cli/src/ngtsc/transform/index.js +14 -0
- package/transform/compiler-cli/src/ngtsc/transform/src/api.d.ts +115 -0
- package/transform/compiler-cli/src/ngtsc/transform/src/api.js +30 -0
- package/transform/compiler-cli/src/ngtsc/transform/src/utils.d.ts +15 -0
- package/transform/compiler-cli/src/ngtsc/transform/src/utils.js +84 -0
- package/transform/compiler-cli/src/ngtsc/translator/index.d.ts +5 -0
- package/transform/compiler-cli/src/ngtsc/translator/index.js +17 -0
- package/transform/compiler-cli/src/ngtsc/translator/src/api/ast_factory.d.ts +274 -0
- package/transform/compiler-cli/src/ngtsc/translator/src/api/ast_factory.js +9 -0
- package/transform/compiler-cli/src/ngtsc/translator/src/api/import_generator.d.ts +27 -0
- package/transform/compiler-cli/src/ngtsc/translator/src/api/import_generator.js +9 -0
- package/transform/compiler-cli/src/ngtsc/translator/src/context.d.ts +18 -0
- package/transform/compiler-cli/src/ngtsc/translator/src/context.js +27 -0
- package/transform/compiler-cli/src/ngtsc/translator/src/import_manager.d.ts +29 -0
- package/transform/compiler-cli/src/ngtsc/translator/src/import_manager.js +70 -0
- package/transform/compiler-cli/src/ngtsc/translator/src/translator.d.ts +58 -0
- package/transform/compiler-cli/src/ngtsc/translator/src/translator.js +270 -0
- package/transform/compiler-cli/src/ngtsc/translator/src/typescript_ast_factory.d.ts +54 -0
- package/transform/compiler-cli/src/ngtsc/translator/src/typescript_ast_factory.js +245 -0
- package/transform/compiler-cli/src/ngtsc/translator/src/typescript_translator.d.ts +13 -0
- package/transform/compiler-cli/src/ngtsc/translator/src/typescript_translator.js +21 -0
- package/transform/compiler-cli/src/ngtsc/util/src/typescript.d.ts +11 -0
- package/transform/compiler-cli/src/ngtsc/util/src/typescript.js +39 -0
- package/transform/index.d.ts +1 -0
- package/transform/index.js +13 -0
- package/transform/injectable-transform.d.ts +41 -0
- package/transform/injectable-transform.js +195 -0
- package/transform/node-Iteration.d.ts +2 -0
- package/transform/node-Iteration.js +12 -0
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @license
|
|
4
|
+
* Copyright Google LLC All Rights Reserved.
|
|
5
|
+
*
|
|
6
|
+
* Use of this source code is governed by an MIT-style license that can be
|
|
7
|
+
* found in the LICENSE file at https://angular.io/license
|
|
8
|
+
*/
|
|
9
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
12
|
+
}) : (function(o, m, k, k2) {
|
|
13
|
+
if (k2 === undefined) k2 = k;
|
|
14
|
+
o[k2] = m[k];
|
|
15
|
+
}));
|
|
16
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
17
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
18
|
+
}) : function(o, v) {
|
|
19
|
+
o["default"] = v;
|
|
20
|
+
});
|
|
21
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
22
|
+
if (mod && mod.__esModule) return mod;
|
|
23
|
+
var result = {};
|
|
24
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
25
|
+
__setModuleDefault(result, mod);
|
|
26
|
+
return result;
|
|
27
|
+
};
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
exports.isConcreteDeclaration = exports.KnownDeclaration = exports.ClassMemberKind = exports.isDecoratorIdentifier = exports.Decorator = void 0;
|
|
30
|
+
const ts = __importStar(require("typescript"));
|
|
31
|
+
exports.Decorator = {
|
|
32
|
+
nodeForError: (decorator) => {
|
|
33
|
+
if (decorator.node !== null) {
|
|
34
|
+
return decorator.node;
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
// TODO(alxhub): we can't rely on narrowing until TS 3.6 is in g3.
|
|
38
|
+
return decorator.synthesizedFor;
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
/** 装饰器可能是 Injectable或者xx.Injectable */
|
|
43
|
+
function isDecoratorIdentifier(exp) {
|
|
44
|
+
return (ts.isIdentifier(exp) ||
|
|
45
|
+
(ts.isPropertyAccessExpression(exp) &&
|
|
46
|
+
ts.isIdentifier(exp.expression) &&
|
|
47
|
+
ts.isIdentifier(exp.name)));
|
|
48
|
+
}
|
|
49
|
+
exports.isDecoratorIdentifier = isDecoratorIdentifier;
|
|
50
|
+
/**
|
|
51
|
+
* An enumeration of possible kinds of class members.
|
|
52
|
+
*/
|
|
53
|
+
var ClassMemberKind;
|
|
54
|
+
(function (ClassMemberKind) {
|
|
55
|
+
ClassMemberKind[ClassMemberKind["Constructor"] = 0] = "Constructor";
|
|
56
|
+
ClassMemberKind[ClassMemberKind["Getter"] = 1] = "Getter";
|
|
57
|
+
ClassMemberKind[ClassMemberKind["Setter"] = 2] = "Setter";
|
|
58
|
+
ClassMemberKind[ClassMemberKind["Property"] = 3] = "Property";
|
|
59
|
+
ClassMemberKind[ClassMemberKind["Method"] = 4] = "Method";
|
|
60
|
+
})(ClassMemberKind = exports.ClassMemberKind || (exports.ClassMemberKind = {}));
|
|
61
|
+
/**
|
|
62
|
+
* Possible declarations of known values, such as built-in objects/functions or TypeScript helpers.
|
|
63
|
+
*/
|
|
64
|
+
var KnownDeclaration;
|
|
65
|
+
(function (KnownDeclaration) {
|
|
66
|
+
/**
|
|
67
|
+
* Indicates the JavaScript global `Object` class.
|
|
68
|
+
*/
|
|
69
|
+
KnownDeclaration[KnownDeclaration["JsGlobalObject"] = 0] = "JsGlobalObject";
|
|
70
|
+
/**
|
|
71
|
+
* Indicates the `__assign` TypeScript helper function.
|
|
72
|
+
*/
|
|
73
|
+
KnownDeclaration[KnownDeclaration["TsHelperAssign"] = 1] = "TsHelperAssign";
|
|
74
|
+
/**
|
|
75
|
+
* Indicates the `__spread` TypeScript helper function.
|
|
76
|
+
*/
|
|
77
|
+
KnownDeclaration[KnownDeclaration["TsHelperSpread"] = 2] = "TsHelperSpread";
|
|
78
|
+
/**
|
|
79
|
+
* Indicates the `__spreadArrays` TypeScript helper function.
|
|
80
|
+
*/
|
|
81
|
+
KnownDeclaration[KnownDeclaration["TsHelperSpreadArrays"] = 3] = "TsHelperSpreadArrays";
|
|
82
|
+
/**
|
|
83
|
+
* Indicates the `__spreadArray` TypeScript helper function.
|
|
84
|
+
*/
|
|
85
|
+
KnownDeclaration[KnownDeclaration["TsHelperSpreadArray"] = 4] = "TsHelperSpreadArray";
|
|
86
|
+
/**
|
|
87
|
+
* Indicates the `__read` TypeScript helper function.
|
|
88
|
+
*/
|
|
89
|
+
KnownDeclaration[KnownDeclaration["TsHelperRead"] = 5] = "TsHelperRead";
|
|
90
|
+
})(KnownDeclaration = exports.KnownDeclaration || (exports.KnownDeclaration = {}));
|
|
91
|
+
/**
|
|
92
|
+
* Returns true if the `decl` is a `ConcreteDeclaration` (ie. that its `node` property is a
|
|
93
|
+
* `ts.Declaration`).
|
|
94
|
+
*/
|
|
95
|
+
function isConcreteDeclaration(decl) {
|
|
96
|
+
return decl.kind === 0 /* Concrete */;
|
|
97
|
+
}
|
|
98
|
+
exports.isConcreteDeclaration = isConcreteDeclaration;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright Google LLC All Rights Reserved.
|
|
4
|
+
*
|
|
5
|
+
* Use of this source code is governed by an MIT-style license that can be
|
|
6
|
+
* found in the LICENSE file at https://angular.io/license
|
|
7
|
+
*/
|
|
8
|
+
import * as ts from "typescript";
|
|
9
|
+
import { TypeValueReference } from "./host";
|
|
10
|
+
/**
|
|
11
|
+
* Potentially convert a `ts.TypeNode` to a `TypeValueReference`, which indicates how to use the
|
|
12
|
+
* type given in the `ts.TypeNode` in a value position.
|
|
13
|
+
*
|
|
14
|
+
* This can return `null` if the `typeNode` is `null`, if it does not refer to a symbol with a value
|
|
15
|
+
* declaration, or if it is not possible to statically understand.
|
|
16
|
+
*/
|
|
17
|
+
export declare function typeToValue(typeNode: ts.TypeNode | null, checker: ts.TypeChecker): TypeValueReference;
|
|
18
|
+
/**
|
|
19
|
+
* Attempt to extract a `ts.Expression` that's equivalent to a `ts.TypeNode`, as the two have
|
|
20
|
+
* different AST shapes but can reference the same symbols.
|
|
21
|
+
*
|
|
22
|
+
* This will return `null` if an equivalent expression cannot be constructed.
|
|
23
|
+
*/
|
|
24
|
+
export declare function typeNodeToValueExpr(node: ts.TypeNode): ts.Expression | null;
|
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @license
|
|
4
|
+
* Copyright Google LLC All Rights Reserved.
|
|
5
|
+
*
|
|
6
|
+
* Use of this source code is governed by an MIT-style license that can be
|
|
7
|
+
* found in the LICENSE file at https://angular.io/license
|
|
8
|
+
*/
|
|
9
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
12
|
+
}) : (function(o, m, k, k2) {
|
|
13
|
+
if (k2 === undefined) k2 = k;
|
|
14
|
+
o[k2] = m[k];
|
|
15
|
+
}));
|
|
16
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
17
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
18
|
+
}) : function(o, v) {
|
|
19
|
+
o["default"] = v;
|
|
20
|
+
});
|
|
21
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
22
|
+
if (mod && mod.__esModule) return mod;
|
|
23
|
+
var result = {};
|
|
24
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
25
|
+
__setModuleDefault(result, mod);
|
|
26
|
+
return result;
|
|
27
|
+
};
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
exports.typeNodeToValueExpr = exports.typeToValue = void 0;
|
|
30
|
+
const ts = __importStar(require("typescript"));
|
|
31
|
+
/**
|
|
32
|
+
* Potentially convert a `ts.TypeNode` to a `TypeValueReference`, which indicates how to use the
|
|
33
|
+
* type given in the `ts.TypeNode` in a value position.
|
|
34
|
+
*
|
|
35
|
+
* This can return `null` if the `typeNode` is `null`, if it does not refer to a symbol with a value
|
|
36
|
+
* declaration, or if it is not possible to statically understand.
|
|
37
|
+
*/
|
|
38
|
+
function typeToValue(typeNode, checker) {
|
|
39
|
+
// It's not possible to get a value expression if the parameter doesn't even have a type.
|
|
40
|
+
if (typeNode === null) {
|
|
41
|
+
return missingType();
|
|
42
|
+
}
|
|
43
|
+
if (!ts.isTypeReferenceNode(typeNode)) {
|
|
44
|
+
return unsupportedType(typeNode);
|
|
45
|
+
}
|
|
46
|
+
const symbols = resolveTypeSymbols(typeNode, checker);
|
|
47
|
+
if (symbols === null) {
|
|
48
|
+
return unknownReference(typeNode);
|
|
49
|
+
}
|
|
50
|
+
const { local, decl } = symbols;
|
|
51
|
+
// It's only valid to convert a type reference to a value reference if the type actually
|
|
52
|
+
// has a value declaration associated with it. Note that const enums are an exception,
|
|
53
|
+
// because while they do have a value declaration, they don't exist at runtime.
|
|
54
|
+
if (decl.valueDeclaration === undefined ||
|
|
55
|
+
decl.flags & ts.SymbolFlags.ConstEnum) {
|
|
56
|
+
let typeOnlyDecl = null;
|
|
57
|
+
if (decl.declarations !== undefined && decl.declarations.length > 0) {
|
|
58
|
+
typeOnlyDecl = decl.declarations[0];
|
|
59
|
+
}
|
|
60
|
+
return noValueDeclaration(typeNode, typeOnlyDecl);
|
|
61
|
+
}
|
|
62
|
+
// The type points to a valid value declaration. Rewrite the TypeReference into an
|
|
63
|
+
// Expression which references the value pointed to by the TypeReference, if possible.
|
|
64
|
+
// Look at the local `ts.Symbol`'s declarations and see if it comes from an import
|
|
65
|
+
// statement. If so, extract the module specifier and the name of the imported type.
|
|
66
|
+
const firstDecl = local.declarations && local.declarations[0];
|
|
67
|
+
if (firstDecl !== undefined) {
|
|
68
|
+
if (ts.isImportClause(firstDecl) && firstDecl.name !== undefined) {
|
|
69
|
+
// This is a default import.
|
|
70
|
+
// import Foo from 'foo';
|
|
71
|
+
if (firstDecl.isTypeOnly) {
|
|
72
|
+
// Type-only imports cannot be represented as value.
|
|
73
|
+
return typeOnlyImport(typeNode, firstDecl);
|
|
74
|
+
}
|
|
75
|
+
return {
|
|
76
|
+
kind: 0 /* LOCAL */,
|
|
77
|
+
expression: firstDecl.name,
|
|
78
|
+
defaultImportStatement: firstDecl.parent,
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
else if (ts.isImportSpecifier(firstDecl)) {
|
|
82
|
+
// The symbol was imported by name
|
|
83
|
+
// import {Foo} from 'foo';
|
|
84
|
+
// or
|
|
85
|
+
// import {Foo as Bar} from 'foo';
|
|
86
|
+
if (firstDecl.parent.parent.isTypeOnly) {
|
|
87
|
+
// Type-only imports cannot be represented as value.
|
|
88
|
+
return typeOnlyImport(typeNode, firstDecl.parent.parent);
|
|
89
|
+
}
|
|
90
|
+
// Determine the name to import (`Foo`) from the import specifier, as the symbol names of
|
|
91
|
+
// the imported type could refer to a local alias (like `Bar` in the example above).
|
|
92
|
+
const importedName = (firstDecl.propertyName || firstDecl.name).text;
|
|
93
|
+
// The first symbol name refers to the local name, which is replaced by `importedName` above.
|
|
94
|
+
// Any remaining symbol names make up the complete path to the value.
|
|
95
|
+
const [_localName, ...nestedPath] = symbols.symbolNames;
|
|
96
|
+
const moduleName = extractModuleName(firstDecl.parent.parent.parent);
|
|
97
|
+
return {
|
|
98
|
+
kind: 1 /* IMPORTED */,
|
|
99
|
+
valueDeclaration: decl.valueDeclaration,
|
|
100
|
+
moduleName,
|
|
101
|
+
importedName,
|
|
102
|
+
nestedPath,
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
else if (ts.isNamespaceImport(firstDecl)) {
|
|
106
|
+
// The import is a namespace import
|
|
107
|
+
// import * as Foo from 'foo';
|
|
108
|
+
if (firstDecl.parent.isTypeOnly) {
|
|
109
|
+
// Type-only imports cannot be represented as value.
|
|
110
|
+
return typeOnlyImport(typeNode, firstDecl.parent);
|
|
111
|
+
}
|
|
112
|
+
if (symbols.symbolNames.length === 1) {
|
|
113
|
+
// The type refers to the namespace itself, which cannot be represented as a value.
|
|
114
|
+
return namespaceImport(typeNode, firstDecl.parent);
|
|
115
|
+
}
|
|
116
|
+
// The first symbol name refers to the local name of the namespace, which is is discarded
|
|
117
|
+
// as a new namespace import will be generated. This is followed by the symbol name that needs
|
|
118
|
+
// to be imported and any remaining names that constitute the complete path to the value.
|
|
119
|
+
const [_ns, importedName, ...nestedPath] = symbols.symbolNames;
|
|
120
|
+
const moduleName = extractModuleName(firstDecl.parent.parent);
|
|
121
|
+
return {
|
|
122
|
+
kind: 1 /* IMPORTED */,
|
|
123
|
+
valueDeclaration: decl.valueDeclaration,
|
|
124
|
+
moduleName,
|
|
125
|
+
importedName,
|
|
126
|
+
nestedPath,
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
// If the type is not imported, the type reference can be converted into an expression as is.
|
|
131
|
+
const expression = typeNodeToValueExpr(typeNode);
|
|
132
|
+
if (expression !== null) {
|
|
133
|
+
return {
|
|
134
|
+
kind: 0 /* LOCAL */,
|
|
135
|
+
expression,
|
|
136
|
+
defaultImportStatement: null,
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
else {
|
|
140
|
+
return unsupportedType(typeNode);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
exports.typeToValue = typeToValue;
|
|
144
|
+
function unsupportedType(typeNode) {
|
|
145
|
+
return {
|
|
146
|
+
kind: 2 /* UNAVAILABLE */,
|
|
147
|
+
reason: { kind: 5 /* UNSUPPORTED */, typeNode },
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
function noValueDeclaration(typeNode, decl) {
|
|
151
|
+
return {
|
|
152
|
+
kind: 2 /* UNAVAILABLE */,
|
|
153
|
+
reason: { kind: 1 /* NO_VALUE_DECLARATION */, typeNode, decl },
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
function typeOnlyImport(typeNode, importClause) {
|
|
157
|
+
return {
|
|
158
|
+
kind: 2 /* UNAVAILABLE */,
|
|
159
|
+
reason: {
|
|
160
|
+
kind: 2 /* TYPE_ONLY_IMPORT */,
|
|
161
|
+
typeNode,
|
|
162
|
+
importClause,
|
|
163
|
+
},
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
function unknownReference(typeNode) {
|
|
167
|
+
return {
|
|
168
|
+
kind: 2 /* UNAVAILABLE */,
|
|
169
|
+
reason: { kind: 3 /* UNKNOWN_REFERENCE */, typeNode },
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
function namespaceImport(typeNode, importClause) {
|
|
173
|
+
return {
|
|
174
|
+
kind: 2 /* UNAVAILABLE */,
|
|
175
|
+
reason: { kind: 4 /* NAMESPACE */, typeNode, importClause },
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
function missingType() {
|
|
179
|
+
return {
|
|
180
|
+
kind: 2 /* UNAVAILABLE */,
|
|
181
|
+
reason: { kind: 0 /* MISSING_TYPE */ },
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Attempt to extract a `ts.Expression` that's equivalent to a `ts.TypeNode`, as the two have
|
|
186
|
+
* different AST shapes but can reference the same symbols.
|
|
187
|
+
*
|
|
188
|
+
* This will return `null` if an equivalent expression cannot be constructed.
|
|
189
|
+
*/
|
|
190
|
+
function typeNodeToValueExpr(node) {
|
|
191
|
+
if (ts.isTypeReferenceNode(node)) {
|
|
192
|
+
return entityNameToValue(node.typeName);
|
|
193
|
+
}
|
|
194
|
+
else {
|
|
195
|
+
return null;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
exports.typeNodeToValueExpr = typeNodeToValueExpr;
|
|
199
|
+
/**
|
|
200
|
+
* Resolve a `TypeReference` node to the `ts.Symbol`s for both its declaration and its local source.
|
|
201
|
+
*
|
|
202
|
+
* In the event that the `TypeReference` refers to a locally declared symbol, these will be the
|
|
203
|
+
* same. If the `TypeReference` refers to an imported symbol, then `decl` will be the fully resolved
|
|
204
|
+
* `ts.Symbol` of the referenced symbol. `local` will be the `ts.Symbol` of the `ts.Identifier`
|
|
205
|
+
* which points to the import statement by which the symbol was imported.
|
|
206
|
+
*
|
|
207
|
+
* All symbol names that make up the type reference are returned left-to-right into the
|
|
208
|
+
* `symbolNames` array, which is guaranteed to include at least one entry.
|
|
209
|
+
*/
|
|
210
|
+
function resolveTypeSymbols(typeRef, checker) {
|
|
211
|
+
const typeName = typeRef.typeName;
|
|
212
|
+
// typeRefSymbol is the ts.Symbol of the entire type reference.
|
|
213
|
+
const typeRefSymbol = checker.getSymbolAtLocation(typeName);
|
|
214
|
+
if (typeRefSymbol === undefined) {
|
|
215
|
+
return null;
|
|
216
|
+
}
|
|
217
|
+
// `local` is the `ts.Symbol` for the local `ts.Identifier` for the type.
|
|
218
|
+
// If the type is actually locally declared or is imported by name, for example:
|
|
219
|
+
// import {Foo} from './foo';
|
|
220
|
+
// then it'll be the same as `typeRefSymbol`.
|
|
221
|
+
//
|
|
222
|
+
// If the type is imported via a namespace import, for example:
|
|
223
|
+
// import * as foo from './foo';
|
|
224
|
+
// and then referenced as:
|
|
225
|
+
// constructor(f: foo.Foo)
|
|
226
|
+
// then `local` will be the `ts.Symbol` of `foo`, whereas `typeRefSymbol` will be the `ts.Symbol`
|
|
227
|
+
// of `foo.Foo`. This allows tracking of the import behind whatever type reference exists.
|
|
228
|
+
let local = typeRefSymbol;
|
|
229
|
+
// Destructure a name like `foo.X.Y.Z` as follows:
|
|
230
|
+
// - in `leftMost`, the `ts.Identifier` of the left-most name (`foo`) in the qualified name.
|
|
231
|
+
// This identifier is used to resolve the `ts.Symbol` for `local`.
|
|
232
|
+
// - in `symbolNames`, all names involved in the qualified path, or a single symbol name if the
|
|
233
|
+
// type is not qualified.
|
|
234
|
+
let leftMost = typeName;
|
|
235
|
+
const symbolNames = [];
|
|
236
|
+
while (ts.isQualifiedName(leftMost)) {
|
|
237
|
+
symbolNames.unshift(leftMost.right.text);
|
|
238
|
+
leftMost = leftMost.left;
|
|
239
|
+
}
|
|
240
|
+
symbolNames.unshift(leftMost.text);
|
|
241
|
+
if (leftMost !== typeName) {
|
|
242
|
+
const localTmp = checker.getSymbolAtLocation(leftMost);
|
|
243
|
+
if (localTmp !== undefined) {
|
|
244
|
+
local = localTmp;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
// De-alias the top-level type reference symbol to get the symbol of the actual declaration.
|
|
248
|
+
let decl = typeRefSymbol;
|
|
249
|
+
if (typeRefSymbol.flags & ts.SymbolFlags.Alias) {
|
|
250
|
+
decl = checker.getAliasedSymbol(typeRefSymbol);
|
|
251
|
+
}
|
|
252
|
+
return { local, decl, symbolNames };
|
|
253
|
+
}
|
|
254
|
+
function entityNameToValue(node) {
|
|
255
|
+
if (ts.isQualifiedName(node)) {
|
|
256
|
+
const left = entityNameToValue(node.left);
|
|
257
|
+
return left !== null ? ts.createPropertyAccess(left, node.right) : null;
|
|
258
|
+
}
|
|
259
|
+
else if (ts.isIdentifier(node)) {
|
|
260
|
+
return ts.getMutableClone(node);
|
|
261
|
+
}
|
|
262
|
+
else {
|
|
263
|
+
return null;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
function extractModuleName(node) {
|
|
267
|
+
if (!ts.isStringLiteral(node.moduleSpecifier)) {
|
|
268
|
+
throw new Error("not a module specifier");
|
|
269
|
+
}
|
|
270
|
+
return node.moduleSpecifier.text;
|
|
271
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright Google LLC All Rights Reserved.
|
|
4
|
+
*
|
|
5
|
+
* Use of this source code is governed by an MIT-style license that can be
|
|
6
|
+
* found in the LICENSE file at https://angular.io/license
|
|
7
|
+
*/
|
|
8
|
+
import * as ts from 'typescript';
|
|
9
|
+
import { ClassDeclaration, ClassMember, CtorParameter, DeclarationNode, Decorator, Import, ReflectionHost } from './host';
|
|
10
|
+
/**
|
|
11
|
+
* reflector.ts implements static reflection of declarations using the TypeScript `ts.TypeChecker`.
|
|
12
|
+
*/
|
|
13
|
+
export declare class TypeScriptReflectionHost implements ReflectionHost {
|
|
14
|
+
protected checker: ts.TypeChecker;
|
|
15
|
+
constructor(checker: ts.TypeChecker);
|
|
16
|
+
getDecoratorsOfDeclaration(declaration: DeclarationNode): Decorator[] | null;
|
|
17
|
+
getMembersOfClass(clazz: ClassDeclaration): ClassMember[];
|
|
18
|
+
getConstructorParameters(clazz: ClassDeclaration): CtorParameter[] | null;
|
|
19
|
+
getImportOfIdentifier(id: ts.Identifier): Import | null;
|
|
20
|
+
isClass(node: ts.Node): node is ClassDeclaration;
|
|
21
|
+
hasBaseClass(clazz: ClassDeclaration): boolean;
|
|
22
|
+
getBaseClassExpression(clazz: ClassDeclaration): ts.Expression | null;
|
|
23
|
+
getGenericArityOfClass(clazz: ClassDeclaration): number | null;
|
|
24
|
+
getDtsDeclaration(_: ClassDeclaration): ts.Declaration | null;
|
|
25
|
+
getInternalNameOfClass(clazz: ClassDeclaration): ts.Identifier;
|
|
26
|
+
getAdjacentNameOfClass(clazz: ClassDeclaration): ts.Identifier;
|
|
27
|
+
protected getDirectImportOfIdentifier(id: ts.Identifier): Import | null;
|
|
28
|
+
/**
|
|
29
|
+
* Try to get the import info for this identifier as though it is a namespaced import.
|
|
30
|
+
*
|
|
31
|
+
* For example, if the identifier is the `Directive` part of a qualified type chain like:
|
|
32
|
+
*
|
|
33
|
+
* ```
|
|
34
|
+
* core.Directive
|
|
35
|
+
* ```
|
|
36
|
+
*
|
|
37
|
+
* then it might be that `core` is a namespace import such as:
|
|
38
|
+
*
|
|
39
|
+
* ```
|
|
40
|
+
* import * as core from 'tslib';
|
|
41
|
+
* ```
|
|
42
|
+
*
|
|
43
|
+
* @param id the TypeScript identifier to find the import info for.
|
|
44
|
+
* @returns The import info if this is a namespaced import or `null`.
|
|
45
|
+
*/
|
|
46
|
+
protected getImportOfNamespacedIdentifier(id: ts.Identifier, namespaceIdentifier: ts.Identifier | null): Import | null;
|
|
47
|
+
private _reflectDecorator;
|
|
48
|
+
private _reflectMember;
|
|
49
|
+
}
|
|
50
|
+
export declare function reflectObjectLiteral(node: ts.ObjectLiteralExpression): Map<string, ts.Expression>;
|
|
@@ -0,0 +1,396 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @license
|
|
4
|
+
* Copyright Google LLC All Rights Reserved.
|
|
5
|
+
*
|
|
6
|
+
* Use of this source code is governed by an MIT-style license that can be
|
|
7
|
+
* found in the LICENSE file at https://angular.io/license
|
|
8
|
+
*/
|
|
9
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
12
|
+
}) : (function(o, m, k, k2) {
|
|
13
|
+
if (k2 === undefined) k2 = k;
|
|
14
|
+
o[k2] = m[k];
|
|
15
|
+
}));
|
|
16
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
17
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
18
|
+
}) : function(o, v) {
|
|
19
|
+
o["default"] = v;
|
|
20
|
+
});
|
|
21
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
22
|
+
if (mod && mod.__esModule) return mod;
|
|
23
|
+
var result = {};
|
|
24
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
25
|
+
__setModuleDefault(result, mod);
|
|
26
|
+
return result;
|
|
27
|
+
};
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
exports.reflectObjectLiteral = exports.TypeScriptReflectionHost = void 0;
|
|
30
|
+
const ts = __importStar(require("typescript"));
|
|
31
|
+
const host_1 = require("./host");
|
|
32
|
+
const type_to_value_1 = require("./type_to_value");
|
|
33
|
+
const util_1 = require("./util");
|
|
34
|
+
/**
|
|
35
|
+
* reflector.ts implements static reflection of declarations using the TypeScript `ts.TypeChecker`.
|
|
36
|
+
*/
|
|
37
|
+
class TypeScriptReflectionHost {
|
|
38
|
+
constructor(checker) {
|
|
39
|
+
this.checker = checker;
|
|
40
|
+
}
|
|
41
|
+
getDecoratorsOfDeclaration(declaration) {
|
|
42
|
+
if (declaration.decorators === undefined ||
|
|
43
|
+
declaration.decorators.length === 0) {
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
return declaration.decorators
|
|
47
|
+
.map((decorator) => this._reflectDecorator(decorator))
|
|
48
|
+
.filter((dec) => dec !== null);
|
|
49
|
+
}
|
|
50
|
+
getMembersOfClass(clazz) {
|
|
51
|
+
const tsClazz = castDeclarationToClassOrDie(clazz);
|
|
52
|
+
return tsClazz.members
|
|
53
|
+
.map((member) => this._reflectMember(member))
|
|
54
|
+
.filter((member) => member !== null);
|
|
55
|
+
}
|
|
56
|
+
getConstructorParameters(clazz) {
|
|
57
|
+
const tsClazz = castDeclarationToClassOrDie(clazz);
|
|
58
|
+
const isDeclaration = tsClazz.getSourceFile().isDeclarationFile;
|
|
59
|
+
// For non-declaration files, we want to find the constructor with a `body`. The constructors
|
|
60
|
+
// without a `body` are overloads whereas we want the implementation since it's the one that'll
|
|
61
|
+
// be executed and which can have decorators. For declaration files, we take the first one that
|
|
62
|
+
// we get.
|
|
63
|
+
const ctor = tsClazz.members.find((member) => ts.isConstructorDeclaration(member) &&
|
|
64
|
+
(isDeclaration || member.body !== undefined));
|
|
65
|
+
if (ctor === undefined) {
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
return ctor.parameters.map((node) => {
|
|
69
|
+
// The name of the parameter is easy.
|
|
70
|
+
const name = parameterName(node.name);
|
|
71
|
+
const decorators = this.getDecoratorsOfDeclaration(node);
|
|
72
|
+
// It may or may not be possible to write an expression that refers to the value side of the
|
|
73
|
+
// type named for the parameter.
|
|
74
|
+
let originalTypeNode = node.type || null;
|
|
75
|
+
let typeNode = originalTypeNode;
|
|
76
|
+
// Check if we are dealing with a simple nullable union type e.g. `foo: Foo|null`
|
|
77
|
+
// and extract the type. More complex union types e.g. `foo: Foo|Bar` are not supported.
|
|
78
|
+
// We also don't need to support `foo: Foo|undefined` because Angular's DI injects `null` for
|
|
79
|
+
// optional tokes that don't have providers.
|
|
80
|
+
if (typeNode && ts.isUnionTypeNode(typeNode)) {
|
|
81
|
+
let childTypeNodes = typeNode.types.filter((childTypeNode) => !(ts.isLiteralTypeNode(childTypeNode) &&
|
|
82
|
+
childTypeNode.literal.kind === ts.SyntaxKind.NullKeyword));
|
|
83
|
+
if (childTypeNodes.length === 1) {
|
|
84
|
+
typeNode = childTypeNodes[0];
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
const typeValueReference = type_to_value_1.typeToValue(typeNode, this.checker);
|
|
88
|
+
return {
|
|
89
|
+
name,
|
|
90
|
+
nameNode: node.name,
|
|
91
|
+
typeValueReference,
|
|
92
|
+
typeNode: originalTypeNode,
|
|
93
|
+
decorators,
|
|
94
|
+
};
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
getImportOfIdentifier(id) {
|
|
98
|
+
const directImport = this.getDirectImportOfIdentifier(id);
|
|
99
|
+
if (directImport !== null) {
|
|
100
|
+
return directImport;
|
|
101
|
+
}
|
|
102
|
+
else if (ts.isQualifiedName(id.parent) && id.parent.right === id) {
|
|
103
|
+
return this.getImportOfNamespacedIdentifier(id, getQualifiedNameRoot(id.parent));
|
|
104
|
+
}
|
|
105
|
+
else if (ts.isPropertyAccessExpression(id.parent) &&
|
|
106
|
+
id.parent.name === id) {
|
|
107
|
+
return this.getImportOfNamespacedIdentifier(id, getFarLeftIdentifier(id.parent));
|
|
108
|
+
}
|
|
109
|
+
else {
|
|
110
|
+
return null;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
isClass(node) {
|
|
114
|
+
// For our purposes, classes are "named" ts.ClassDeclarations;
|
|
115
|
+
// (`node.name` can be undefined in unnamed default exports: `default export class { ... }`).
|
|
116
|
+
return util_1.isNamedClassDeclaration(node);
|
|
117
|
+
}
|
|
118
|
+
hasBaseClass(clazz) {
|
|
119
|
+
return this.getBaseClassExpression(clazz) !== null;
|
|
120
|
+
}
|
|
121
|
+
getBaseClassExpression(clazz) {
|
|
122
|
+
if (!(ts.isClassDeclaration(clazz) || ts.isClassExpression(clazz)) ||
|
|
123
|
+
clazz.heritageClauses === undefined) {
|
|
124
|
+
return null;
|
|
125
|
+
}
|
|
126
|
+
const extendsClause = clazz.heritageClauses.find((clause) => clause.token === ts.SyntaxKind.ExtendsKeyword);
|
|
127
|
+
if (extendsClause === undefined) {
|
|
128
|
+
return null;
|
|
129
|
+
}
|
|
130
|
+
const extendsType = extendsClause.types[0];
|
|
131
|
+
if (extendsType === undefined) {
|
|
132
|
+
return null;
|
|
133
|
+
}
|
|
134
|
+
return extendsType.expression;
|
|
135
|
+
}
|
|
136
|
+
getGenericArityOfClass(clazz) {
|
|
137
|
+
if (!ts.isClassDeclaration(clazz)) {
|
|
138
|
+
return null;
|
|
139
|
+
}
|
|
140
|
+
return clazz.typeParameters !== undefined ? clazz.typeParameters.length : 0;
|
|
141
|
+
}
|
|
142
|
+
getDtsDeclaration(_) {
|
|
143
|
+
return null;
|
|
144
|
+
}
|
|
145
|
+
getInternalNameOfClass(clazz) {
|
|
146
|
+
return clazz.name;
|
|
147
|
+
}
|
|
148
|
+
getAdjacentNameOfClass(clazz) {
|
|
149
|
+
return clazz.name;
|
|
150
|
+
}
|
|
151
|
+
getDirectImportOfIdentifier(id) {
|
|
152
|
+
const symbol = this.checker.getSymbolAtLocation(id);
|
|
153
|
+
if (symbol === undefined ||
|
|
154
|
+
symbol.declarations === undefined ||
|
|
155
|
+
symbol.declarations.length !== 1) {
|
|
156
|
+
return null;
|
|
157
|
+
}
|
|
158
|
+
const decl = symbol.declarations[0];
|
|
159
|
+
const importDecl = getContainingImportDeclaration(decl);
|
|
160
|
+
// Ignore declarations that are defined locally (not imported).
|
|
161
|
+
if (importDecl === null) {
|
|
162
|
+
return null;
|
|
163
|
+
}
|
|
164
|
+
// The module specifier is guaranteed to be a string literal, so this should always pass.
|
|
165
|
+
if (!ts.isStringLiteral(importDecl.moduleSpecifier)) {
|
|
166
|
+
// Not allowed to happen in TypeScript ASTs.
|
|
167
|
+
return null;
|
|
168
|
+
}
|
|
169
|
+
return {
|
|
170
|
+
from: importDecl.moduleSpecifier.text,
|
|
171
|
+
name: getExportedName(decl, id),
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Try to get the import info for this identifier as though it is a namespaced import.
|
|
176
|
+
*
|
|
177
|
+
* For example, if the identifier is the `Directive` part of a qualified type chain like:
|
|
178
|
+
*
|
|
179
|
+
* ```
|
|
180
|
+
* core.Directive
|
|
181
|
+
* ```
|
|
182
|
+
*
|
|
183
|
+
* then it might be that `core` is a namespace import such as:
|
|
184
|
+
*
|
|
185
|
+
* ```
|
|
186
|
+
* import * as core from 'tslib';
|
|
187
|
+
* ```
|
|
188
|
+
*
|
|
189
|
+
* @param id the TypeScript identifier to find the import info for.
|
|
190
|
+
* @returns The import info if this is a namespaced import or `null`.
|
|
191
|
+
*/
|
|
192
|
+
getImportOfNamespacedIdentifier(id, namespaceIdentifier) {
|
|
193
|
+
if (namespaceIdentifier === null) {
|
|
194
|
+
return null;
|
|
195
|
+
}
|
|
196
|
+
const namespaceSymbol = this.checker.getSymbolAtLocation(namespaceIdentifier);
|
|
197
|
+
if (!namespaceSymbol || namespaceSymbol.declarations === undefined) {
|
|
198
|
+
return null;
|
|
199
|
+
}
|
|
200
|
+
const declaration = namespaceSymbol.declarations.length === 1
|
|
201
|
+
? namespaceSymbol.declarations[0]
|
|
202
|
+
: null;
|
|
203
|
+
if (!declaration) {
|
|
204
|
+
return null;
|
|
205
|
+
}
|
|
206
|
+
const namespaceDeclaration = ts.isNamespaceImport(declaration)
|
|
207
|
+
? declaration
|
|
208
|
+
: null;
|
|
209
|
+
if (!namespaceDeclaration) {
|
|
210
|
+
return null;
|
|
211
|
+
}
|
|
212
|
+
const importDeclaration = namespaceDeclaration.parent.parent;
|
|
213
|
+
if (!ts.isStringLiteral(importDeclaration.moduleSpecifier)) {
|
|
214
|
+
// Should not happen as this would be invalid TypesScript
|
|
215
|
+
return null;
|
|
216
|
+
}
|
|
217
|
+
return {
|
|
218
|
+
from: importDeclaration.moduleSpecifier.text,
|
|
219
|
+
name: id.text,
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
_reflectDecorator(node) {
|
|
223
|
+
// Attempt to resolve the decorator expression into a reference to a concrete Identifier. The
|
|
224
|
+
// expression may contain a call to a function which returns the decorator function, in which
|
|
225
|
+
// case we want to return the arguments.
|
|
226
|
+
let decoratorExpr = node.expression;
|
|
227
|
+
let args = null;
|
|
228
|
+
// Check for call expressions.
|
|
229
|
+
if (ts.isCallExpression(decoratorExpr)) {
|
|
230
|
+
args = Array.from(decoratorExpr.arguments);
|
|
231
|
+
decoratorExpr = decoratorExpr.expression;
|
|
232
|
+
}
|
|
233
|
+
// The final resolved decorator should be a `ts.Identifier` - if it's not, then something is
|
|
234
|
+
// wrong and the decorator can't be resolved statically.
|
|
235
|
+
if (!host_1.isDecoratorIdentifier(decoratorExpr)) {
|
|
236
|
+
return null;
|
|
237
|
+
}
|
|
238
|
+
const decoratorIdentifier = ts.isIdentifier(decoratorExpr)
|
|
239
|
+
? decoratorExpr
|
|
240
|
+
: decoratorExpr.name;
|
|
241
|
+
const importDecl = this.getImportOfIdentifier(decoratorIdentifier);
|
|
242
|
+
return {
|
|
243
|
+
name: decoratorIdentifier.text,
|
|
244
|
+
identifier: decoratorExpr,
|
|
245
|
+
import: importDecl,
|
|
246
|
+
node,
|
|
247
|
+
args,
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
_reflectMember(node) {
|
|
251
|
+
let kind = null;
|
|
252
|
+
let value = null;
|
|
253
|
+
let name = null;
|
|
254
|
+
let nameNode = null;
|
|
255
|
+
if (ts.isPropertyDeclaration(node)) {
|
|
256
|
+
kind = host_1.ClassMemberKind.Property;
|
|
257
|
+
value = node.initializer || null;
|
|
258
|
+
}
|
|
259
|
+
else if (ts.isGetAccessorDeclaration(node)) {
|
|
260
|
+
kind = host_1.ClassMemberKind.Getter;
|
|
261
|
+
}
|
|
262
|
+
else if (ts.isSetAccessorDeclaration(node)) {
|
|
263
|
+
kind = host_1.ClassMemberKind.Setter;
|
|
264
|
+
}
|
|
265
|
+
else if (ts.isMethodDeclaration(node)) {
|
|
266
|
+
kind = host_1.ClassMemberKind.Method;
|
|
267
|
+
}
|
|
268
|
+
else if (ts.isConstructorDeclaration(node)) {
|
|
269
|
+
kind = host_1.ClassMemberKind.Constructor;
|
|
270
|
+
}
|
|
271
|
+
else {
|
|
272
|
+
return null;
|
|
273
|
+
}
|
|
274
|
+
if (ts.isConstructorDeclaration(node)) {
|
|
275
|
+
name = 'constructor';
|
|
276
|
+
}
|
|
277
|
+
else if (ts.isIdentifier(node.name)) {
|
|
278
|
+
name = node.name.text;
|
|
279
|
+
nameNode = node.name;
|
|
280
|
+
}
|
|
281
|
+
else if (ts.isStringLiteral(node.name)) {
|
|
282
|
+
name = node.name.text;
|
|
283
|
+
nameNode = node.name;
|
|
284
|
+
}
|
|
285
|
+
else {
|
|
286
|
+
return null;
|
|
287
|
+
}
|
|
288
|
+
const decorators = this.getDecoratorsOfDeclaration(node);
|
|
289
|
+
const isStatic = node.modifiers !== undefined &&
|
|
290
|
+
node.modifiers.some((mod) => mod.kind === ts.SyntaxKind.StaticKeyword);
|
|
291
|
+
return {
|
|
292
|
+
node,
|
|
293
|
+
implementation: node,
|
|
294
|
+
kind,
|
|
295
|
+
type: node.type || null,
|
|
296
|
+
name,
|
|
297
|
+
nameNode,
|
|
298
|
+
decorators,
|
|
299
|
+
value,
|
|
300
|
+
isStatic,
|
|
301
|
+
};
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
exports.TypeScriptReflectionHost = TypeScriptReflectionHost;
|
|
305
|
+
function reflectObjectLiteral(node) {
|
|
306
|
+
const map = new Map();
|
|
307
|
+
node.properties.forEach((prop) => {
|
|
308
|
+
if (ts.isPropertyAssignment(prop)) {
|
|
309
|
+
const name = propertyNameToString(prop.name);
|
|
310
|
+
if (name === null) {
|
|
311
|
+
return;
|
|
312
|
+
}
|
|
313
|
+
map.set(name, prop.initializer);
|
|
314
|
+
}
|
|
315
|
+
else if (ts.isShorthandPropertyAssignment(prop)) {
|
|
316
|
+
map.set(prop.name.text, prop.name);
|
|
317
|
+
}
|
|
318
|
+
else {
|
|
319
|
+
return;
|
|
320
|
+
}
|
|
321
|
+
});
|
|
322
|
+
return map;
|
|
323
|
+
}
|
|
324
|
+
exports.reflectObjectLiteral = reflectObjectLiteral;
|
|
325
|
+
function castDeclarationToClassOrDie(declaration) {
|
|
326
|
+
if (!ts.isClassDeclaration(declaration)) {
|
|
327
|
+
throw new Error(`Reflecting on a ${ts.SyntaxKind[declaration.kind]} instead of a ClassDeclaration.`);
|
|
328
|
+
}
|
|
329
|
+
return declaration;
|
|
330
|
+
}
|
|
331
|
+
function parameterName(name) {
|
|
332
|
+
if (ts.isIdentifier(name)) {
|
|
333
|
+
return name.text;
|
|
334
|
+
}
|
|
335
|
+
else {
|
|
336
|
+
return null;
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
function propertyNameToString(node) {
|
|
340
|
+
if (ts.isIdentifier(node) ||
|
|
341
|
+
ts.isStringLiteral(node) ||
|
|
342
|
+
ts.isNumericLiteral(node)) {
|
|
343
|
+
return node.text;
|
|
344
|
+
}
|
|
345
|
+
else {
|
|
346
|
+
return null;
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
/**
|
|
350
|
+
* Compute the left most identifier in a qualified type chain. E.g. the `a` of `a.b.c.SomeType`.
|
|
351
|
+
* @param qualifiedName The starting property access expression from which we want to compute
|
|
352
|
+
* the left most identifier.
|
|
353
|
+
* @returns the left most identifier in the chain or `null` if it is not an identifier.
|
|
354
|
+
*/
|
|
355
|
+
function getQualifiedNameRoot(qualifiedName) {
|
|
356
|
+
while (ts.isQualifiedName(qualifiedName.left)) {
|
|
357
|
+
qualifiedName = qualifiedName.left;
|
|
358
|
+
}
|
|
359
|
+
return ts.isIdentifier(qualifiedName.left) ? qualifiedName.left : null;
|
|
360
|
+
}
|
|
361
|
+
/**
|
|
362
|
+
* Compute the left most identifier in a property access chain. E.g. the `a` of `a.b.c.d`.
|
|
363
|
+
* @param propertyAccess The starting property access expression from which we want to compute
|
|
364
|
+
* the left most identifier.
|
|
365
|
+
* @returns the left most identifier in the chain or `null` if it is not an identifier.
|
|
366
|
+
*/
|
|
367
|
+
function getFarLeftIdentifier(propertyAccess) {
|
|
368
|
+
while (ts.isPropertyAccessExpression(propertyAccess.expression)) {
|
|
369
|
+
propertyAccess = propertyAccess.expression;
|
|
370
|
+
}
|
|
371
|
+
return ts.isIdentifier(propertyAccess.expression)
|
|
372
|
+
? propertyAccess.expression
|
|
373
|
+
: null;
|
|
374
|
+
}
|
|
375
|
+
/**
|
|
376
|
+
* Return the ImportDeclaration for the given `node` if it is either an `ImportSpecifier` or a
|
|
377
|
+
* `NamespaceImport`. If not return `null`.
|
|
378
|
+
*/
|
|
379
|
+
function getContainingImportDeclaration(node) {
|
|
380
|
+
return ts.isImportSpecifier(node)
|
|
381
|
+
? node.parent.parent.parent
|
|
382
|
+
: ts.isNamespaceImport(node)
|
|
383
|
+
? node.parent.parent
|
|
384
|
+
: null;
|
|
385
|
+
}
|
|
386
|
+
/**
|
|
387
|
+
* Compute the name by which the `decl` was exported, not imported.
|
|
388
|
+
* If no such declaration can be found (e.g. it is a namespace import)
|
|
389
|
+
* then fallback to the `originalId`.
|
|
390
|
+
*/
|
|
391
|
+
function getExportedName(decl, originalId) {
|
|
392
|
+
return ts.isImportSpecifier(decl)
|
|
393
|
+
? (decl.propertyName !== undefined ? decl.propertyName : decl.name).text
|
|
394
|
+
: originalId.text;
|
|
395
|
+
}
|
|
396
|
+
const LocalExportedClasses = Symbol('LocalExportedClasses');
|