static-injector 1.0.10 → 2.1.0
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 +337 -124
- package/import/es2015/di/create_injector.js +30 -0
- package/import/es2015/di/initializer_token.js +15 -0
- package/import/es2015/di/inject_switch.js +27 -6
- package/import/es2015/di/injection_token.js +6 -0
- package/import/es2015/di/injector.js +6 -8
- package/import/es2015/di/injector_compatibility.js +90 -34
- package/import/es2015/di/injector_token.js +1 -1
- package/import/es2015/di/interface/defs.js +39 -0
- package/import/es2015/di/interface/injector.js +1 -0
- package/import/es2015/di/internal_tokens.js +9 -0
- package/import/es2015/di/metadata.js +4 -4
- package/import/es2015/di/provider_collection.js +49 -0
- package/import/es2015/di/r3_injector.js +102 -72
- package/import/es2015/di/scope.js +2 -2
- package/import/es2015/index.js +1 -1
- package/import/es2015/render3/errors_di.js +1 -1
- package/import/fesm2015/index.js +328 -114
- package/import/typings/di/create_injector.d.ts +23 -0
- package/import/typings/di/initializer_token.d.ts +15 -0
- package/import/typings/di/inject_switch.d.ts +7 -2
- package/import/typings/{decorator → di}/injectable.d.ts +2 -2
- package/import/typings/di/injection_token.d.ts +4 -0
- package/import/typings/di/injector.d.ts +20 -7
- package/import/typings/di/injector_compatibility.d.ts +42 -21
- package/import/typings/di/interface/defs.d.ts +10 -0
- package/import/typings/di/interface/injector.d.ts +26 -0
- package/import/typings/di/interface/provider.d.ts +3 -8
- package/import/typings/di/internal_tokens.d.ts +10 -0
- package/import/typings/di/provider_collection.d.ts +30 -0
- package/import/typings/di/provider_token.d.ts +1 -1
- package/import/typings/di/r3_injector.d.ts +68 -37
- package/import/typings/di/scope.d.ts +3 -2
- package/import/typings/index.d.ts +1 -1
- package/import/typings/render3/definition_factory.d.ts +1 -1
- package/package.json +5 -3
- package/{import/es2015/decorator/interface/provider.js → transform/compiler/compiler.d.ts} +1 -1
- package/transform/compiler/compiler.js +28 -0
- package/transform/compiler/index.d.ts +8 -5
- package/transform/compiler/index.js +17 -6
- package/transform/compiler/public_api.d.ts +13 -0
- package/transform/compiler/public_api.js +30 -0
- package/transform/compiler/src/compiler.d.ts +27 -0
- package/transform/compiler/src/compiler.js +47 -0
- package/transform/compiler/src/injectable_compiler_2.d.ts +7 -36
- package/transform/compiler/src/injectable_compiler_2.js +20 -23
- package/transform/compiler/src/output/output_ast.d.ts +30 -109
- package/transform/compiler/src/output/output_ast.js +66 -188
- package/transform/compiler/src/render3/partial/api.d.ts +1 -1
- package/transform/compiler/src/render3/partial/api.js +7 -0
- package/transform/compiler/src/render3/r3_factory.d.ts +8 -15
- package/transform/compiler/src/render3/r3_factory.js +46 -61
- package/transform/compiler/src/render3/util.d.ts +64 -0
- package/transform/compiler/src/render3/util.js +52 -9
- package/transform/compiler/src/render3/view/util.d.ts +0 -8
- package/transform/compiler/src/render3/view/util.js +5 -9
- package/transform/compiler-cli/src/ngtsc/annotations/common/index.d.ts +10 -0
- package/transform/compiler-cli/src/ngtsc/annotations/common/index.js +26 -0
- package/transform/compiler-cli/src/ngtsc/annotations/common/src/di.d.ts +37 -0
- package/transform/compiler-cli/src/ngtsc/annotations/common/src/di.js +197 -0
- package/transform/compiler-cli/src/ngtsc/annotations/{src → common/src}/factory.d.ts +3 -3
- package/transform/compiler-cli/src/ngtsc/annotations/{src → common/src}/factory.js +3 -3
- package/transform/compiler-cli/src/ngtsc/annotations/{src → common/src}/util.d.ts +4 -34
- package/transform/compiler-cli/src/ngtsc/annotations/common/src/util.js +181 -0
- package/transform/compiler-cli/src/ngtsc/annotations/src/injectable.d.ts +3 -4
- package/transform/compiler-cli/src/ngtsc/annotations/src/injectable.js +46 -53
- package/transform/compiler-cli/src/ngtsc/diagnostics/error.js +6 -2
- package/transform/compiler-cli/src/ngtsc/diagnostics/index.d.ts +9 -2
- package/transform/compiler-cli/src/ngtsc/diagnostics/index.js +12 -1
- package/transform/compiler-cli/src/ngtsc/imports/index.d.ts +7 -0
- package/transform/compiler-cli/src/ngtsc/imports/index.js +12 -1
- package/transform/compiler-cli/src/ngtsc/imports/src/default.d.ts +19 -0
- package/transform/compiler-cli/src/ngtsc/imports/src/default.js +28 -0
- package/transform/compiler-cli/src/ngtsc/reflection/index.d.ts +10 -3
- package/transform/compiler-cli/src/ngtsc/reflection/index.js +12 -1
- package/transform/compiler-cli/src/ngtsc/reflection/src/host.d.ts +13 -14
- package/transform/compiler-cli/src/ngtsc/reflection/src/host.js +8 -25
- package/transform/compiler-cli/src/ngtsc/reflection/src/type_to_value.d.ts +2 -2
- package/transform/compiler-cli/src/ngtsc/reflection/src/type_to_value.js +44 -55
- package/transform/compiler-cli/src/ngtsc/reflection/src/typescript.d.ts +1 -1
- package/transform/compiler-cli/src/ngtsc/reflection/src/typescript.js +55 -69
- package/transform/compiler-cli/src/ngtsc/reflection/src/util.d.ts +2 -2
- package/transform/compiler-cli/src/ngtsc/reflection/src/util.js +7 -23
- package/transform/compiler-cli/src/ngtsc/transform/index.d.ts +9 -2
- package/transform/compiler-cli/src/ngtsc/transform/index.js +12 -1
- package/transform/compiler-cli/src/ngtsc/transform/src/api.d.ts +4 -4
- package/transform/compiler-cli/src/ngtsc/transform/src/utils.d.ts +2 -2
- package/transform/compiler-cli/src/ngtsc/transform/src/utils.js +16 -31
- package/transform/compiler-cli/src/ngtsc/translator/index.d.ts +12 -5
- package/transform/compiler-cli/src/ngtsc/translator/index.js +12 -1
- package/transform/compiler-cli/src/ngtsc/translator/src/api/ast_factory.d.ts +5 -5
- package/transform/compiler-cli/src/ngtsc/translator/src/import_manager.d.ts +4 -4
- package/transform/compiler-cli/src/ngtsc/translator/src/import_manager.js +5 -21
- package/transform/compiler-cli/src/ngtsc/translator/src/translator.d.ts +2 -6
- package/transform/compiler-cli/src/ngtsc/translator/src/translator.js +6 -18
- package/transform/compiler-cli/src/ngtsc/translator/src/typescript_ast_factory.d.ts +3 -6
- package/transform/compiler-cli/src/ngtsc/translator/src/typescript_ast_factory.js +83 -90
- package/transform/compiler-cli/src/ngtsc/translator/src/typescript_translator.d.ts +4 -4
- package/transform/compiler-cli/src/ngtsc/ts_compatibility/index.d.ts +8 -0
- package/transform/compiler-cli/src/ngtsc/ts_compatibility/index.js +24 -0
- package/transform/compiler-cli/src/ngtsc/ts_compatibility/src/ts_cross_version_utils.d.ts +45 -0
- package/transform/compiler-cli/src/ngtsc/ts_compatibility/src/ts_cross_version_utils.js +86 -0
- package/transform/compiler-cli/src/ngtsc/util/src/typescript.d.ts +1 -8
- package/transform/compiler-cli/src/ngtsc/util/src/typescript.js +4 -27
- package/transform/index.js +5 -1
- package/transform/injectable-transform.js +59 -38
- package/import/typings/decorator/interface/provider.d.ts +0 -312
- package/transform/compiler/src/render3/partial/util.d.ts +0 -16
- package/transform/compiler/src/render3/partial/util.js +0 -44
- package/transform/compiler-cli/src/ngtsc/annotations/src/util.js +0 -370
- /package/import/es2015/{decorator → di}/injectable.js +0 -0
|
@@ -0,0 +1,197 @@
|
|
|
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 __importDefault = (this && this.__importDefault) || function (mod) {
|
|
10
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
11
|
+
};
|
|
12
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
+
exports.validateConstructorDependencies = exports.getValidConstructorDependencies = exports.unwrapConstructorDependencies = exports.getConstructorDependencies = void 0;
|
|
14
|
+
const compiler_1 = require("static-injector/transform/compiler");
|
|
15
|
+
const typescript_1 = __importDefault(require("typescript"));
|
|
16
|
+
const diagnostics_1 = require("../../../diagnostics");
|
|
17
|
+
const reflection_1 = require("../../../reflection");
|
|
18
|
+
const util_1 = require("./util");
|
|
19
|
+
function getConstructorDependencies(clazz, reflector, isCore) {
|
|
20
|
+
const deps = [];
|
|
21
|
+
const errors = [];
|
|
22
|
+
let ctorParams = reflector.getConstructorParameters(clazz);
|
|
23
|
+
if (ctorParams === null) {
|
|
24
|
+
if (reflector.hasBaseClass(clazz)) {
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
ctorParams = [];
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
ctorParams.forEach((param, idx) => {
|
|
32
|
+
let token = (0, util_1.valueReferenceToExpression)(param.typeValueReference);
|
|
33
|
+
let attributeNameType = null;
|
|
34
|
+
let optional = false, self = false, skipSelf = false, host = false;
|
|
35
|
+
(param.decorators || [])
|
|
36
|
+
.filter((dec) => isCore || (0, util_1.isAngularCore)(dec))
|
|
37
|
+
.forEach((dec) => {
|
|
38
|
+
const name = isCore || dec.import === null ? dec.name : dec.import.name;
|
|
39
|
+
if (name === 'Inject') {
|
|
40
|
+
if (dec.args === null || dec.args.length !== 1) {
|
|
41
|
+
throw new diagnostics_1.FatalDiagnosticError(diagnostics_1.ErrorCode.DECORATOR_ARITY_WRONG, reflection_1.Decorator.nodeForError(dec), `Unexpected number of arguments to @Inject().`);
|
|
42
|
+
}
|
|
43
|
+
token = new compiler_1.WrappedNodeExpr(dec.args[0]);
|
|
44
|
+
}
|
|
45
|
+
else if (name === 'Optional') {
|
|
46
|
+
optional = true;
|
|
47
|
+
}
|
|
48
|
+
else if (name === 'SkipSelf') {
|
|
49
|
+
skipSelf = true;
|
|
50
|
+
}
|
|
51
|
+
else if (name === 'Self') {
|
|
52
|
+
self = true;
|
|
53
|
+
}
|
|
54
|
+
else if (name === 'Host') {
|
|
55
|
+
}
|
|
56
|
+
else if (name === 'Attribute') {
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
throw new diagnostics_1.FatalDiagnosticError(diagnostics_1.ErrorCode.DECORATOR_UNEXPECTED, reflection_1.Decorator.nodeForError(dec), `Unexpected decorator ${name} on parameter.`);
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
if (token === null) {
|
|
63
|
+
if (param.typeValueReference.kind !== 2 /* TypeValueReferenceKind.UNAVAILABLE */) {
|
|
64
|
+
throw new Error('Illegal state: expected value reference to be unavailable if no token is present');
|
|
65
|
+
}
|
|
66
|
+
errors.push({
|
|
67
|
+
index: idx,
|
|
68
|
+
param,
|
|
69
|
+
reason: param.typeValueReference.reason,
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
deps.push({ token, attributeNameType, optional, self, skipSelf });
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
if (errors.length === 0) {
|
|
77
|
+
return { deps };
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
return { deps: null, errors };
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
exports.getConstructorDependencies = getConstructorDependencies;
|
|
84
|
+
/**
|
|
85
|
+
* Convert `ConstructorDeps` into the `R3DependencyMetadata` array for those deps if they're valid,
|
|
86
|
+
* or into an `'invalid'` signal if they're not.
|
|
87
|
+
*
|
|
88
|
+
* This is a companion function to `validateConstructorDependencies` which accepts invalid deps.
|
|
89
|
+
*/
|
|
90
|
+
function unwrapConstructorDependencies(deps) {
|
|
91
|
+
if (deps === null) {
|
|
92
|
+
return null;
|
|
93
|
+
}
|
|
94
|
+
else if (deps.deps !== null) {
|
|
95
|
+
// These constructor dependencies are valid.
|
|
96
|
+
return deps.deps;
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
// These deps are invalid.
|
|
100
|
+
return 'invalid';
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
exports.unwrapConstructorDependencies = unwrapConstructorDependencies;
|
|
104
|
+
function getValidConstructorDependencies(clazz, reflector, isCore) {
|
|
105
|
+
return validateConstructorDependencies(clazz, getConstructorDependencies(clazz, reflector, isCore));
|
|
106
|
+
}
|
|
107
|
+
exports.getValidConstructorDependencies = getValidConstructorDependencies;
|
|
108
|
+
/**
|
|
109
|
+
* Validate that `ConstructorDeps` does not have any invalid dependencies and convert them into the
|
|
110
|
+
* `R3DependencyMetadata` array if so, or raise a diagnostic if some deps are invalid.
|
|
111
|
+
*
|
|
112
|
+
* This is a companion function to `unwrapConstructorDependencies` which does not accept invalid
|
|
113
|
+
* deps.
|
|
114
|
+
*/
|
|
115
|
+
function validateConstructorDependencies(clazz, deps) {
|
|
116
|
+
if (deps === null) {
|
|
117
|
+
return null;
|
|
118
|
+
}
|
|
119
|
+
else if (deps.deps !== null) {
|
|
120
|
+
return deps.deps;
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
// TODO(alxhub): this cast is necessary because the g3 typescript version doesn't narrow here.
|
|
124
|
+
// There is at least one error.
|
|
125
|
+
const error = deps.errors[0];
|
|
126
|
+
throw createUnsuitableInjectionTokenError(clazz, error);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
exports.validateConstructorDependencies = validateConstructorDependencies;
|
|
130
|
+
/**
|
|
131
|
+
* Creates a fatal error with diagnostic for an invalid injection token.
|
|
132
|
+
* @param clazz The class for which the injection token was unavailable.
|
|
133
|
+
* @param error The reason why no valid injection token is available.
|
|
134
|
+
*/
|
|
135
|
+
function createUnsuitableInjectionTokenError(clazz, error) {
|
|
136
|
+
const { param, index, reason } = error;
|
|
137
|
+
let chainMessage = undefined;
|
|
138
|
+
let hints = undefined;
|
|
139
|
+
switch (reason.kind) {
|
|
140
|
+
case 5 /* ValueUnavailableKind.UNSUPPORTED */:
|
|
141
|
+
chainMessage =
|
|
142
|
+
'Consider using the @Inject decorator to specify an injection token.';
|
|
143
|
+
hints = [
|
|
144
|
+
(0, diagnostics_1.makeRelatedInformation)(reason.typeNode, 'This type is not supported as injection token.'),
|
|
145
|
+
];
|
|
146
|
+
break;
|
|
147
|
+
case 1 /* ValueUnavailableKind.NO_VALUE_DECLARATION */:
|
|
148
|
+
chainMessage =
|
|
149
|
+
'Consider using the @Inject decorator to specify an injection token.';
|
|
150
|
+
hints = [
|
|
151
|
+
(0, diagnostics_1.makeRelatedInformation)(reason.typeNode, 'This type does not have a value, so it cannot be used as injection token.'),
|
|
152
|
+
];
|
|
153
|
+
if (reason.decl !== null) {
|
|
154
|
+
hints.push((0, diagnostics_1.makeRelatedInformation)(reason.decl, 'The type is declared here.'));
|
|
155
|
+
}
|
|
156
|
+
break;
|
|
157
|
+
case 2 /* ValueUnavailableKind.TYPE_ONLY_IMPORT */:
|
|
158
|
+
chainMessage =
|
|
159
|
+
'Consider changing the type-only import to a regular import, or use the @Inject decorator to specify an injection token.';
|
|
160
|
+
hints = [
|
|
161
|
+
(0, diagnostics_1.makeRelatedInformation)(reason.typeNode, 'This type is imported using a type-only import, which prevents it from being usable as an injection token.'),
|
|
162
|
+
(0, diagnostics_1.makeRelatedInformation)(reason.node, 'The type-only import occurs here.'),
|
|
163
|
+
];
|
|
164
|
+
break;
|
|
165
|
+
case 4 /* ValueUnavailableKind.NAMESPACE */:
|
|
166
|
+
chainMessage =
|
|
167
|
+
'Consider using the @Inject decorator to specify an injection token.';
|
|
168
|
+
hints = [
|
|
169
|
+
(0, diagnostics_1.makeRelatedInformation)(reason.typeNode, 'This type corresponds with a namespace, which cannot be used as injection token.'),
|
|
170
|
+
(0, diagnostics_1.makeRelatedInformation)(reason.importClause, 'The namespace import occurs here.'),
|
|
171
|
+
];
|
|
172
|
+
break;
|
|
173
|
+
case 3 /* ValueUnavailableKind.UNKNOWN_REFERENCE */:
|
|
174
|
+
chainMessage = 'The type should reference a known declaration.';
|
|
175
|
+
hints = [
|
|
176
|
+
(0, diagnostics_1.makeRelatedInformation)(reason.typeNode, 'This type could not be resolved.'),
|
|
177
|
+
];
|
|
178
|
+
break;
|
|
179
|
+
case 0 /* ValueUnavailableKind.MISSING_TYPE */:
|
|
180
|
+
chainMessage =
|
|
181
|
+
'Consider adding a type to the parameter or use the @Inject decorator to specify an injection token.';
|
|
182
|
+
break;
|
|
183
|
+
}
|
|
184
|
+
const chain = {
|
|
185
|
+
messageText: `No suitable injection token for parameter '${param.name || index}' of class '${clazz.name.text}'.`,
|
|
186
|
+
category: typescript_1.default.DiagnosticCategory.Error,
|
|
187
|
+
code: 0,
|
|
188
|
+
next: [
|
|
189
|
+
{
|
|
190
|
+
messageText: chainMessage,
|
|
191
|
+
category: typescript_1.default.DiagnosticCategory.Message,
|
|
192
|
+
code: 0,
|
|
193
|
+
},
|
|
194
|
+
],
|
|
195
|
+
};
|
|
196
|
+
return new diagnostics_1.FatalDiagnosticError(diagnostics_1.ErrorCode.PARAM_MISSING_TOKEN, param.nameNode, chain, hints);
|
|
197
|
+
}
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* Use of this source code is governed by an MIT-style license that can be
|
|
6
6
|
* found in the LICENSE file at https://angular.io/license
|
|
7
7
|
*/
|
|
8
|
-
import { R3FactoryMetadata } from
|
|
9
|
-
import { CompileResult } from
|
|
10
|
-
export
|
|
8
|
+
import { R3FactoryMetadata } from 'static-injector/transform/compiler';
|
|
9
|
+
import { CompileResult } from '../../../transform';
|
|
10
|
+
export type CompileFactoryFn = (metadata: R3FactoryMetadata) => CompileResult;
|
|
11
11
|
export declare function compileNgFactoryDefField(metadata: R3FactoryMetadata): CompileResult;
|
|
@@ -8,11 +8,11 @@
|
|
|
8
8
|
*/
|
|
9
9
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
10
|
exports.compileNgFactoryDefField = void 0;
|
|
11
|
-
const compiler_1 = require("
|
|
11
|
+
const compiler_1 = require("static-injector/transform/compiler");
|
|
12
12
|
function compileNgFactoryDefField(metadata) {
|
|
13
|
-
const res = compiler_1.compileFactoryFunction(metadata);
|
|
13
|
+
const res = (0, compiler_1.compileFactoryFunction)(metadata);
|
|
14
14
|
return {
|
|
15
|
-
name:
|
|
15
|
+
name: 'ɵfac',
|
|
16
16
|
initializer: res.expression,
|
|
17
17
|
statements: res.statements,
|
|
18
18
|
type: res.type,
|
|
@@ -5,23 +5,9 @@
|
|
|
5
5
|
* Use of this source code is governed by an MIT-style license that can be
|
|
6
6
|
* found in the LICENSE file at https://angular.io/license
|
|
7
7
|
*/
|
|
8
|
-
import { Expression,
|
|
9
|
-
import
|
|
10
|
-
import {
|
|
11
|
-
import * as ts from 'typescript';
|
|
12
|
-
import { ClassDeclaration, CtorParameter, Decorator, Import, ImportedTypeValueReference, LocalTypeValueReference, ReflectionHost, TypeValueReference, UnavailableValue } from '../../reflection';
|
|
13
|
-
export declare type ConstructorDeps = {
|
|
14
|
-
deps: R3DependencyMetadata[];
|
|
15
|
-
} | {
|
|
16
|
-
deps: null;
|
|
17
|
-
errors: ConstructorDepError[];
|
|
18
|
-
};
|
|
19
|
-
export interface ConstructorDepError {
|
|
20
|
-
index: number;
|
|
21
|
-
param: CtorParameter;
|
|
22
|
-
reason: UnavailableValue;
|
|
23
|
-
}
|
|
24
|
-
export declare function getConstructorDependencies(clazz: ClassDeclaration, reflector: ReflectionHost, isCore: boolean): ConstructorDeps | null;
|
|
8
|
+
import { Expression, FactoryTarget, R3FactoryMetadata, R3Reference } from 'static-injector/transform/compiler';
|
|
9
|
+
import ts from 'typescript';
|
|
10
|
+
import { ClassDeclaration, Decorator, Import, ImportedTypeValueReference, LocalTypeValueReference, ReflectionHost, TypeValueReference } from '../../../reflection';
|
|
25
11
|
/**
|
|
26
12
|
* Convert a `TypeValueReference` to an `Expression` which refers to the type as a value.
|
|
27
13
|
*
|
|
@@ -31,23 +17,6 @@ export declare function getConstructorDependencies(clazz: ClassDeclaration, refl
|
|
|
31
17
|
*/
|
|
32
18
|
export declare function valueReferenceToExpression(valueRef: LocalTypeValueReference | ImportedTypeValueReference): Expression;
|
|
33
19
|
export declare function valueReferenceToExpression(valueRef: TypeValueReference): Expression | null;
|
|
34
|
-
/**
|
|
35
|
-
* Convert `ConstructorDeps` into the `R3DependencyMetadata` array for those deps if they're valid,
|
|
36
|
-
* or into an `'invalid'` signal if they're not.
|
|
37
|
-
*
|
|
38
|
-
* This is a companion function to `validateConstructorDependencies` which accepts invalid deps.
|
|
39
|
-
*/
|
|
40
|
-
export declare function unwrapConstructorDependencies(deps: ConstructorDeps | null): R3DependencyMetadata[] | 'invalid' | null;
|
|
41
|
-
export declare function getValidConstructorDependencies(clazz: ClassDeclaration, reflector: ReflectionHost, isCore: boolean): R3DependencyMetadata[] | null;
|
|
42
|
-
/**
|
|
43
|
-
* Validate that `ConstructorDeps` does not have any invalid dependencies and convert them into the
|
|
44
|
-
* `R3DependencyMetadata` array if so, or raise a diagnostic if some deps are invalid.
|
|
45
|
-
*
|
|
46
|
-
* This is a companion function to `unwrapConstructorDependencies` which does not accept invalid
|
|
47
|
-
* deps.
|
|
48
|
-
*/
|
|
49
|
-
export declare function validateConstructorDependencies(clazz: ClassDeclaration, deps: ConstructorDeps | null): R3DependencyMetadata[] | null;
|
|
50
|
-
/** todo 需要改成自己的依赖包 */
|
|
51
20
|
export declare function isAngularCore(decorator: Decorator): decorator is Decorator & {
|
|
52
21
|
import: Import;
|
|
53
22
|
};
|
|
@@ -89,3 +58,4 @@ export declare function wrapFunctionExpressionsInParens(expression: ts.Expressio
|
|
|
89
58
|
*/
|
|
90
59
|
export declare function wrapTypeReference(reflector: ReflectionHost, clazz: ClassDeclaration): R3Reference;
|
|
91
60
|
export declare function toFactoryMetadata(meta: Omit<R3FactoryMetadata, 'target'>, target: FactoryTarget): R3FactoryMetadata;
|
|
61
|
+
export declare function isAbstractClassDeclaration(clazz: ClassDeclaration): boolean;
|
|
@@ -0,0 +1,181 @@
|
|
|
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 __importDefault = (this && this.__importDefault) || function (mod) {
|
|
10
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
11
|
+
};
|
|
12
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
+
exports.isAbstractClassDeclaration = exports.toFactoryMetadata = exports.wrapTypeReference = exports.wrapFunctionExpressionsInParens = exports.tryUnwrapForwardRef = exports.unwrapExpression = exports.isAngularDecorator = exports.findAngularDecorator = exports.isAngularCore = exports.valueReferenceToExpression = void 0;
|
|
14
|
+
const compiler_1 = require("static-injector/transform/compiler");
|
|
15
|
+
const typescript_1 = __importDefault(require("typescript"));
|
|
16
|
+
const reflection_1 = require("../../../reflection");
|
|
17
|
+
function valueReferenceToExpression(valueRef) {
|
|
18
|
+
if (valueRef.kind === 2 /* TypeValueReferenceKind.UNAVAILABLE */) {
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
else if (valueRef.kind === 0 /* TypeValueReferenceKind.LOCAL */) {
|
|
22
|
+
const expr = new compiler_1.WrappedNodeExpr(valueRef.expression);
|
|
23
|
+
return expr;
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
let importExpr = new compiler_1.ExternalExpr({
|
|
27
|
+
moduleName: valueRef.moduleName,
|
|
28
|
+
name: valueRef.importedName,
|
|
29
|
+
});
|
|
30
|
+
if (valueRef.nestedPath !== null) {
|
|
31
|
+
for (const property of valueRef.nestedPath) {
|
|
32
|
+
importExpr = new compiler_1.ReadPropExpr(importExpr, property);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return importExpr;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
exports.valueReferenceToExpression = valueReferenceToExpression;
|
|
39
|
+
function isAngularCore(decorator) {
|
|
40
|
+
return (decorator.import !== null && decorator.import.from === 'static-injector');
|
|
41
|
+
}
|
|
42
|
+
exports.isAngularCore = isAngularCore;
|
|
43
|
+
function findAngularDecorator(decorators, name, isCore) {
|
|
44
|
+
return decorators.find((decorator) => isAngularDecorator(decorator, name, isCore));
|
|
45
|
+
}
|
|
46
|
+
exports.findAngularDecorator = findAngularDecorator;
|
|
47
|
+
function isAngularDecorator(decorator, name, isCore) {
|
|
48
|
+
if (isCore) {
|
|
49
|
+
return decorator.name === name;
|
|
50
|
+
}
|
|
51
|
+
else if (isAngularCore(decorator)) {
|
|
52
|
+
return decorator.import.name === name;
|
|
53
|
+
}
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
exports.isAngularDecorator = isAngularDecorator;
|
|
57
|
+
/**
|
|
58
|
+
* Unwrap a `ts.Expression`, removing outer type-casts or parentheses until the expression is in its
|
|
59
|
+
* lowest level form.
|
|
60
|
+
*
|
|
61
|
+
* For example, the expression "(foo as Type)" unwraps to "foo".
|
|
62
|
+
*/
|
|
63
|
+
function unwrapExpression(node) {
|
|
64
|
+
while (typescript_1.default.isAsExpression(node) || typescript_1.default.isParenthesizedExpression(node)) {
|
|
65
|
+
node = node.expression;
|
|
66
|
+
}
|
|
67
|
+
return node;
|
|
68
|
+
}
|
|
69
|
+
exports.unwrapExpression = unwrapExpression;
|
|
70
|
+
function expandForwardRef(arg) {
|
|
71
|
+
arg = unwrapExpression(arg);
|
|
72
|
+
if (!typescript_1.default.isArrowFunction(arg) && !typescript_1.default.isFunctionExpression(arg)) {
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
const body = arg.body;
|
|
76
|
+
// Either the body is a ts.Expression directly, or a block with a single return statement.
|
|
77
|
+
if (typescript_1.default.isBlock(body)) {
|
|
78
|
+
// Block body - look for a single return statement.
|
|
79
|
+
if (body.statements.length !== 1) {
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
82
|
+
const stmt = body.statements[0];
|
|
83
|
+
if (!typescript_1.default.isReturnStatement(stmt) || stmt.expression === undefined) {
|
|
84
|
+
return null;
|
|
85
|
+
}
|
|
86
|
+
return stmt.expression;
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
// Shorthand body - return as an expression.
|
|
90
|
+
return body;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* If the given `node` is a forwardRef() expression then resolve its inner value, otherwise return
|
|
95
|
+
* `null`.
|
|
96
|
+
*
|
|
97
|
+
* @param node the forwardRef() expression to resolve
|
|
98
|
+
* @param reflector a ReflectionHost
|
|
99
|
+
* @returns the resolved expression, if the original expression was a forwardRef(), or `null`
|
|
100
|
+
* otherwise.
|
|
101
|
+
*/
|
|
102
|
+
function tryUnwrapForwardRef(node, reflector) {
|
|
103
|
+
node = unwrapExpression(node);
|
|
104
|
+
if (!typescript_1.default.isCallExpression(node) || node.arguments.length !== 1) {
|
|
105
|
+
return null;
|
|
106
|
+
}
|
|
107
|
+
const fn = typescript_1.default.isPropertyAccessExpression(node.expression)
|
|
108
|
+
? node.expression.name
|
|
109
|
+
: node.expression;
|
|
110
|
+
if (!typescript_1.default.isIdentifier(fn)) {
|
|
111
|
+
return null;
|
|
112
|
+
}
|
|
113
|
+
const expr = expandForwardRef(node.arguments[0]);
|
|
114
|
+
if (expr === null) {
|
|
115
|
+
return null;
|
|
116
|
+
}
|
|
117
|
+
const imp = reflector.getImportOfIdentifier(fn);
|
|
118
|
+
if (imp === null ||
|
|
119
|
+
imp.from !== 'static-injector' ||
|
|
120
|
+
imp.name !== 'forwardRef') {
|
|
121
|
+
return null;
|
|
122
|
+
}
|
|
123
|
+
return expr;
|
|
124
|
+
}
|
|
125
|
+
exports.tryUnwrapForwardRef = tryUnwrapForwardRef;
|
|
126
|
+
const parensWrapperTransformerFactory = (context) => {
|
|
127
|
+
const visitor = (node) => {
|
|
128
|
+
const visited = typescript_1.default.visitEachChild(node, visitor, context);
|
|
129
|
+
if (typescript_1.default.isArrowFunction(visited) || typescript_1.default.isFunctionExpression(visited)) {
|
|
130
|
+
return typescript_1.default.factory.createParenthesizedExpression(visited);
|
|
131
|
+
}
|
|
132
|
+
return visited;
|
|
133
|
+
};
|
|
134
|
+
return (node) => typescript_1.default.visitEachChild(node, visitor, context);
|
|
135
|
+
};
|
|
136
|
+
/**
|
|
137
|
+
* Wraps all functions in a given expression in parentheses. This is needed to avoid problems
|
|
138
|
+
* where Tsickle annotations added between analyse and transform phases in Angular may trigger
|
|
139
|
+
* automatic semicolon insertion, e.g. if a function is the expression in a `return` statement.
|
|
140
|
+
* More
|
|
141
|
+
* info can be found in Tsickle source code here:
|
|
142
|
+
* https://github.com/angular/tsickle/blob/d7974262571c8a17d684e5ba07680e1b1993afdd/src/jsdoc_transformer.ts#L1021
|
|
143
|
+
*
|
|
144
|
+
* @param expression Expression where functions should be wrapped in parentheses
|
|
145
|
+
*/
|
|
146
|
+
function wrapFunctionExpressionsInParens(expression) {
|
|
147
|
+
return typescript_1.default.transform(expression, [parensWrapperTransformerFactory])
|
|
148
|
+
.transformed[0];
|
|
149
|
+
}
|
|
150
|
+
exports.wrapFunctionExpressionsInParens = wrapFunctionExpressionsInParens;
|
|
151
|
+
/**
|
|
152
|
+
* Create an R3Reference for a class.
|
|
153
|
+
*
|
|
154
|
+
* The `value` is the exported declaration of the class from its source file.
|
|
155
|
+
* The `type` is an expression that would be used by ngcc in the typings (.d.ts) files.
|
|
156
|
+
*/
|
|
157
|
+
function wrapTypeReference(reflector, clazz) {
|
|
158
|
+
const dtsClass = reflector.getDtsDeclaration(clazz);
|
|
159
|
+
const value = new compiler_1.WrappedNodeExpr(clazz.name);
|
|
160
|
+
const type = dtsClass !== null && (0, reflection_1.isNamedClassDeclaration)(dtsClass)
|
|
161
|
+
? new compiler_1.WrappedNodeExpr(dtsClass.name)
|
|
162
|
+
: value;
|
|
163
|
+
return { value, type };
|
|
164
|
+
}
|
|
165
|
+
exports.wrapTypeReference = wrapTypeReference;
|
|
166
|
+
function toFactoryMetadata(meta, target) {
|
|
167
|
+
return {
|
|
168
|
+
name: meta.name,
|
|
169
|
+
type: meta.type,
|
|
170
|
+
internalType: meta.internalType,
|
|
171
|
+
typeArgumentCount: meta.typeArgumentCount,
|
|
172
|
+
deps: meta.deps,
|
|
173
|
+
target,
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
exports.toFactoryMetadata = toFactoryMetadata;
|
|
177
|
+
function isAbstractClassDeclaration(clazz) {
|
|
178
|
+
return (clazz.modifiers !== undefined &&
|
|
179
|
+
clazz.modifiers.some((mod) => mod.kind === typescript_1.default.SyntaxKind.AbstractKeyword));
|
|
180
|
+
}
|
|
181
|
+
exports.isAbstractClassDeclaration = isAbstractClassDeclaration;
|
|
@@ -5,17 +5,17 @@
|
|
|
5
5
|
* Use of this source code is governed by an MIT-style license that can be
|
|
6
6
|
* found in the LICENSE file at https://angular.io/license
|
|
7
7
|
*/
|
|
8
|
-
import { R3DependencyMetadata, R3InjectableMetadata } from '
|
|
8
|
+
import { R3DependencyMetadata, R3InjectableMetadata } from 'static-injector/transform/compiler';
|
|
9
9
|
import { ClassDeclaration, Decorator, ReflectionHost } from '../../reflection';
|
|
10
10
|
import { AnalysisOutput, CompileResult, DecoratorHandler, DetectResult } from '../../transform';
|
|
11
|
-
interface InjectableHandlerData {
|
|
11
|
+
export interface InjectableHandlerData {
|
|
12
12
|
meta: R3InjectableMetadata;
|
|
13
13
|
ctorDeps: R3DependencyMetadata[] | 'invalid' | null;
|
|
14
14
|
}
|
|
15
15
|
/**
|
|
16
16
|
* Adapts the `compileInjectable` compiler for `@Injectable` decorators to the Ivy compiler.
|
|
17
17
|
*/
|
|
18
|
-
export declare class InjectableDecoratorHandler implements DecoratorHandler<Decorator, InjectableHandlerData,
|
|
18
|
+
export declare class InjectableDecoratorHandler implements DecoratorHandler<Decorator, InjectableHandlerData, unknown> {
|
|
19
19
|
private reflector;
|
|
20
20
|
private isCore;
|
|
21
21
|
private strictCtorDeps;
|
|
@@ -39,4 +39,3 @@ export declare class InjectableDecoratorHandler implements DecoratorHandler<Deco
|
|
|
39
39
|
compileFull(node: ClassDeclaration, analysis: Readonly<InjectableHandlerData>): CompileResult[];
|
|
40
40
|
private compile;
|
|
41
41
|
}
|
|
42
|
-
export {};
|