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,11 @@
|
|
|
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 { R3FactoryMetadata } from "../../../../../compiler";
|
|
9
|
+
import { CompileResult } from "../../transform";
|
|
10
|
+
export declare type CompileFactoryFn = (metadata: R3FactoryMetadata) => CompileResult;
|
|
11
|
+
export declare function compileNgFactoryDefField(metadata: R3FactoryMetadata): CompileResult;
|
|
@@ -0,0 +1,21 @@
|
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.compileNgFactoryDefField = void 0;
|
|
11
|
+
const compiler_1 = require("../../../../../compiler");
|
|
12
|
+
function compileNgFactoryDefField(metadata) {
|
|
13
|
+
const res = compiler_1.compileFactoryFunction(metadata);
|
|
14
|
+
return {
|
|
15
|
+
name: "ɵfac",
|
|
16
|
+
initializer: res.expression,
|
|
17
|
+
statements: res.statements,
|
|
18
|
+
type: res.type,
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
exports.compileNgFactoryDefField = compileNgFactoryDefField;
|
|
@@ -0,0 +1,42 @@
|
|
|
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 { R3DependencyMetadata, R3InjectableMetadata } from '../../../../../compiler';
|
|
9
|
+
import { ClassDeclaration, Decorator, ReflectionHost } from '../../reflection';
|
|
10
|
+
import { AnalysisOutput, CompileResult, DecoratorHandler, DetectResult } from '../../transform';
|
|
11
|
+
interface InjectableHandlerData {
|
|
12
|
+
meta: R3InjectableMetadata;
|
|
13
|
+
ctorDeps: R3DependencyMetadata[] | 'invalid' | null;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Adapts the `compileInjectable` compiler for `@Injectable` decorators to the Ivy compiler.
|
|
17
|
+
*/
|
|
18
|
+
export declare class InjectableDecoratorHandler implements DecoratorHandler<Decorator, InjectableHandlerData, null, unknown> {
|
|
19
|
+
private reflector;
|
|
20
|
+
private isCore;
|
|
21
|
+
private strictCtorDeps;
|
|
22
|
+
/**
|
|
23
|
+
* What to do if the injectable already contains a ɵprov property.
|
|
24
|
+
*
|
|
25
|
+
* If true then an error diagnostic is reported.
|
|
26
|
+
* If false then there is no error and a new ɵprov property is not added.
|
|
27
|
+
*/
|
|
28
|
+
private errorOnDuplicateProv;
|
|
29
|
+
constructor(reflector: ReflectionHost, isCore: boolean, strictCtorDeps: boolean,
|
|
30
|
+
/**
|
|
31
|
+
* What to do if the injectable already contains a ɵprov property.
|
|
32
|
+
*
|
|
33
|
+
* If true then an error diagnostic is reported.
|
|
34
|
+
* If false then there is no error and a new ɵprov property is not added.
|
|
35
|
+
*/
|
|
36
|
+
errorOnDuplicateProv?: boolean);
|
|
37
|
+
detect(node: ClassDeclaration, decorators: Decorator[] | null): DetectResult<Decorator> | undefined;
|
|
38
|
+
analyze(node: ClassDeclaration, decorator: Readonly<Decorator>): AnalysisOutput<InjectableHandlerData>;
|
|
39
|
+
compileFull(node: ClassDeclaration, analysis: Readonly<InjectableHandlerData>): CompileResult[];
|
|
40
|
+
private compile;
|
|
41
|
+
}
|
|
42
|
+
export {};
|
|
@@ -0,0 +1,278 @@
|
|
|
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.InjectableDecoratorHandler = void 0;
|
|
30
|
+
const compiler_1 = require("../../../../../compiler");
|
|
31
|
+
const ts = __importStar(require("typescript"));
|
|
32
|
+
const diagnostics_1 = require("../../diagnostics");
|
|
33
|
+
const reflection_1 = require("../../reflection");
|
|
34
|
+
const factory_1 = require("./factory");
|
|
35
|
+
const util_1 = require("./util");
|
|
36
|
+
/**
|
|
37
|
+
* Adapts the `compileInjectable` compiler for `@Injectable` decorators to the Ivy compiler.
|
|
38
|
+
*/
|
|
39
|
+
class InjectableDecoratorHandler {
|
|
40
|
+
constructor(reflector, isCore, strictCtorDeps,
|
|
41
|
+
/**
|
|
42
|
+
* What to do if the injectable already contains a ɵprov property.
|
|
43
|
+
*
|
|
44
|
+
* If true then an error diagnostic is reported.
|
|
45
|
+
* If false then there is no error and a new ɵprov property is not added.
|
|
46
|
+
*/
|
|
47
|
+
errorOnDuplicateProv = true) {
|
|
48
|
+
this.reflector = reflector;
|
|
49
|
+
this.isCore = isCore;
|
|
50
|
+
this.strictCtorDeps = strictCtorDeps;
|
|
51
|
+
this.errorOnDuplicateProv = errorOnDuplicateProv;
|
|
52
|
+
}
|
|
53
|
+
detect(node, decorators) {
|
|
54
|
+
if (!decorators) {
|
|
55
|
+
return undefined;
|
|
56
|
+
}
|
|
57
|
+
const decorator = util_1.findAngularDecorator(decorators, 'Injectable', this.isCore);
|
|
58
|
+
if (decorator !== undefined) {
|
|
59
|
+
return {
|
|
60
|
+
trigger: decorator.node,
|
|
61
|
+
decorator: decorator,
|
|
62
|
+
metadata: decorator,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
return undefined;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
analyze(node, decorator) {
|
|
70
|
+
const meta = extractInjectableMetadata(node, decorator, this.reflector);
|
|
71
|
+
return {
|
|
72
|
+
analysis: {
|
|
73
|
+
meta,
|
|
74
|
+
ctorDeps: extractInjectableCtorDeps(node, meta, decorator, this.reflector, this.isCore, this.strictCtorDeps),
|
|
75
|
+
// Avoid generating multiple factories if a class has
|
|
76
|
+
// more Angular decorators, apart from Injectable.
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
compileFull(node, analysis) {
|
|
81
|
+
return this.compile(factory_1.compileNgFactoryDefField, (meta) => compiler_1.compileInjectable(meta, false), node, analysis);
|
|
82
|
+
}
|
|
83
|
+
compile(compileFactoryFn, compileInjectableFn, node, analysis) {
|
|
84
|
+
const results = [];
|
|
85
|
+
const meta = analysis.meta;
|
|
86
|
+
const factoryRes = compileFactoryFn(util_1.toFactoryMetadata(Object.assign(Object.assign({}, meta), { deps: analysis.ctorDeps }), compiler_1.FactoryTarget.Injectable));
|
|
87
|
+
results.push(factoryRes);
|
|
88
|
+
const ɵprov = this.reflector
|
|
89
|
+
.getMembersOfClass(node)
|
|
90
|
+
.find((member) => member.name === 'ɵprov');
|
|
91
|
+
if (ɵprov !== undefined && this.errorOnDuplicateProv) {
|
|
92
|
+
throw new diagnostics_1.FatalDiagnosticError(diagnostics_1.ErrorCode.INJECTABLE_DUPLICATE_PROV, ɵprov.nameNode || ɵprov.node || node, 'Injectables cannot contain a static ɵprov property, because the compiler is going to generate one.');
|
|
93
|
+
}
|
|
94
|
+
if (ɵprov === undefined) {
|
|
95
|
+
// Only add a new ɵprov if there is not one already
|
|
96
|
+
const res = compileInjectableFn(analysis.meta);
|
|
97
|
+
results.push({
|
|
98
|
+
name: 'ɵprov',
|
|
99
|
+
initializer: res.expression,
|
|
100
|
+
statements: res.statements,
|
|
101
|
+
type: res.type,
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
return results;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
exports.InjectableDecoratorHandler = InjectableDecoratorHandler;
|
|
108
|
+
/**
|
|
109
|
+
* Read metadata from the `@Injectable` decorator and produce the `IvyInjectableMetadata`, the
|
|
110
|
+
* input metadata needed to run `compileInjectable`.
|
|
111
|
+
*
|
|
112
|
+
* A `null` return value indicates this is @Injectable has invalid data.
|
|
113
|
+
*/
|
|
114
|
+
function extractInjectableMetadata(clazz, decorator, reflector) {
|
|
115
|
+
const name = clazz.name.text;
|
|
116
|
+
const type = util_1.wrapTypeReference(reflector, clazz);
|
|
117
|
+
const internalType = new compiler_1.WrappedNodeExpr(reflector.getInternalNameOfClass(clazz));
|
|
118
|
+
const typeArgumentCount = reflector.getGenericArityOfClass(clazz) || 0;
|
|
119
|
+
if (decorator.args === null) {
|
|
120
|
+
throw new diagnostics_1.FatalDiagnosticError(diagnostics_1.ErrorCode.DECORATOR_NOT_CALLED, reflection_1.Decorator.nodeForError(decorator), '@Injectable must be called');
|
|
121
|
+
}
|
|
122
|
+
if (decorator.args.length === 0) {
|
|
123
|
+
return {
|
|
124
|
+
name,
|
|
125
|
+
type,
|
|
126
|
+
typeArgumentCount,
|
|
127
|
+
internalType,
|
|
128
|
+
providedIn: compiler_1.createR3ProviderExpression(new compiler_1.LiteralExpr(null), false),
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
else if (decorator.args.length === 1) {
|
|
132
|
+
const metaNode = decorator.args[0];
|
|
133
|
+
// Firstly make sure the decorator argument is an inline literal - if not, it's illegal to
|
|
134
|
+
// transport references from one location to another. This is the problem that lowering
|
|
135
|
+
// used to solve - if this restriction proves too undesirable we can re-implement lowering.
|
|
136
|
+
if (!ts.isObjectLiteralExpression(metaNode)) {
|
|
137
|
+
throw new diagnostics_1.FatalDiagnosticError(diagnostics_1.ErrorCode.DECORATOR_ARG_NOT_LITERAL, metaNode, `@Injectable argument must be an object literal`);
|
|
138
|
+
}
|
|
139
|
+
// Resolve the fields of the literal into a map of field name to expression.
|
|
140
|
+
const meta = reflection_1.reflectObjectLiteral(metaNode);
|
|
141
|
+
const providedIn = meta.has('providedIn')
|
|
142
|
+
? getProviderExpression(meta.get('providedIn'), reflector)
|
|
143
|
+
: compiler_1.createR3ProviderExpression(new compiler_1.LiteralExpr(null), false);
|
|
144
|
+
let deps = undefined;
|
|
145
|
+
if ((meta.has('useClass') || meta.has('useFactory')) && meta.has('deps')) {
|
|
146
|
+
const depsExpr = meta.get('deps');
|
|
147
|
+
if (!ts.isArrayLiteralExpression(depsExpr)) {
|
|
148
|
+
throw new diagnostics_1.FatalDiagnosticError(diagnostics_1.ErrorCode.VALUE_NOT_LITERAL, depsExpr, `@Injectable deps metadata must be an inline array`);
|
|
149
|
+
}
|
|
150
|
+
deps = depsExpr.elements.map((dep) => getDep(dep, reflector));
|
|
151
|
+
}
|
|
152
|
+
const result = {
|
|
153
|
+
name,
|
|
154
|
+
type,
|
|
155
|
+
typeArgumentCount,
|
|
156
|
+
internalType,
|
|
157
|
+
providedIn,
|
|
158
|
+
};
|
|
159
|
+
if (meta.has('useValue')) {
|
|
160
|
+
result.useValue = getProviderExpression(meta.get('useValue'), reflector);
|
|
161
|
+
}
|
|
162
|
+
else if (meta.has('useExisting')) {
|
|
163
|
+
result.useExisting = getProviderExpression(meta.get('useExisting'), reflector);
|
|
164
|
+
}
|
|
165
|
+
else if (meta.has('useClass')) {
|
|
166
|
+
result.useClass = getProviderExpression(meta.get('useClass'), reflector);
|
|
167
|
+
result.deps = deps;
|
|
168
|
+
}
|
|
169
|
+
else if (meta.has('useFactory')) {
|
|
170
|
+
result.useFactory = new compiler_1.WrappedNodeExpr(meta.get('useFactory'));
|
|
171
|
+
result.deps = deps;
|
|
172
|
+
}
|
|
173
|
+
return result;
|
|
174
|
+
}
|
|
175
|
+
else {
|
|
176
|
+
throw new diagnostics_1.FatalDiagnosticError(diagnostics_1.ErrorCode.DECORATOR_ARITY_WRONG, decorator.args[2], 'Too many arguments to @Injectable');
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Get the `R3ProviderExpression` for this `expression`.
|
|
181
|
+
*
|
|
182
|
+
* The `useValue`, `useExisting` and `useClass` properties might be wrapped in a `ForwardRef`, which
|
|
183
|
+
* needs to be unwrapped. This function will do that unwrapping and set a flag on the returned
|
|
184
|
+
* object to indicate whether the value needed unwrapping.
|
|
185
|
+
*/
|
|
186
|
+
function getProviderExpression(expression, reflector) {
|
|
187
|
+
const forwardRefValue = util_1.tryUnwrapForwardRef(expression, reflector);
|
|
188
|
+
return compiler_1.createR3ProviderExpression(new compiler_1.WrappedNodeExpr(forwardRefValue !== null && forwardRefValue !== void 0 ? forwardRefValue : expression), forwardRefValue !== null);
|
|
189
|
+
}
|
|
190
|
+
function extractInjectableCtorDeps(clazz, meta, decorator, reflector, isCore, strictCtorDeps) {
|
|
191
|
+
if (decorator.args === null) {
|
|
192
|
+
throw new diagnostics_1.FatalDiagnosticError(diagnostics_1.ErrorCode.DECORATOR_NOT_CALLED, reflection_1.Decorator.nodeForError(decorator), '@Injectable must be called');
|
|
193
|
+
}
|
|
194
|
+
let ctorDeps = null;
|
|
195
|
+
if (decorator.args.length === 0) {
|
|
196
|
+
// Ideally, using @Injectable() would have the same effect as using @Injectable({...}), and be
|
|
197
|
+
// subject to the same validation. However, existing Angular code abuses @Injectable, applying
|
|
198
|
+
// it to things like abstract classes with constructors that were never meant for use with
|
|
199
|
+
// Angular's DI.
|
|
200
|
+
//
|
|
201
|
+
// To deal with this, @Injectable() without an argument is more lenient, and if the
|
|
202
|
+
// constructor signature does not work for DI then a factory definition (ɵfac) that throws is
|
|
203
|
+
// generated.
|
|
204
|
+
if (strictCtorDeps) {
|
|
205
|
+
ctorDeps = util_1.getValidConstructorDependencies(clazz, reflector, isCore);
|
|
206
|
+
}
|
|
207
|
+
else {
|
|
208
|
+
ctorDeps = util_1.unwrapConstructorDependencies(util_1.getConstructorDependencies(clazz, reflector, isCore));
|
|
209
|
+
}
|
|
210
|
+
return ctorDeps;
|
|
211
|
+
}
|
|
212
|
+
else if (decorator.args.length === 1) {
|
|
213
|
+
const rawCtorDeps = util_1.getConstructorDependencies(clazz, reflector, isCore);
|
|
214
|
+
if (strictCtorDeps &&
|
|
215
|
+
meta.useValue === undefined &&
|
|
216
|
+
meta.useExisting === undefined &&
|
|
217
|
+
meta.useClass === undefined &&
|
|
218
|
+
meta.useFactory === undefined) {
|
|
219
|
+
// Since use* was not provided, validate the deps according to strictCtorDeps.
|
|
220
|
+
ctorDeps = util_1.validateConstructorDependencies(clazz, rawCtorDeps);
|
|
221
|
+
}
|
|
222
|
+
else {
|
|
223
|
+
ctorDeps = util_1.unwrapConstructorDependencies(rawCtorDeps);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
return ctorDeps;
|
|
227
|
+
}
|
|
228
|
+
function getDep(dep, reflector) {
|
|
229
|
+
const meta = {
|
|
230
|
+
token: new compiler_1.WrappedNodeExpr(dep),
|
|
231
|
+
attributeNameType: null,
|
|
232
|
+
optional: false,
|
|
233
|
+
self: false,
|
|
234
|
+
skipSelf: false,
|
|
235
|
+
};
|
|
236
|
+
function maybeUpdateDecorator(dec, reflector, token) {
|
|
237
|
+
const source = reflector.getImportOfIdentifier(dec);
|
|
238
|
+
if (source === null || source.from !== 'static-injector') {
|
|
239
|
+
return false;
|
|
240
|
+
}
|
|
241
|
+
switch (source.name) {
|
|
242
|
+
case 'Inject':
|
|
243
|
+
if (token !== undefined) {
|
|
244
|
+
meta.token = new compiler_1.WrappedNodeExpr(token);
|
|
245
|
+
}
|
|
246
|
+
break;
|
|
247
|
+
case 'Optional':
|
|
248
|
+
meta.optional = true;
|
|
249
|
+
break;
|
|
250
|
+
case 'SkipSelf':
|
|
251
|
+
meta.skipSelf = true;
|
|
252
|
+
break;
|
|
253
|
+
case 'Self':
|
|
254
|
+
meta.self = true;
|
|
255
|
+
break;
|
|
256
|
+
default:
|
|
257
|
+
return false;
|
|
258
|
+
}
|
|
259
|
+
return true;
|
|
260
|
+
}
|
|
261
|
+
if (ts.isArrayLiteralExpression(dep)) {
|
|
262
|
+
dep.elements.forEach((el) => {
|
|
263
|
+
let isParametersDecorator = false;
|
|
264
|
+
if (ts.isIdentifier(el)) {
|
|
265
|
+
isParametersDecorator = maybeUpdateDecorator(el, reflector);
|
|
266
|
+
}
|
|
267
|
+
else if (ts.isNewExpression(el) && ts.isIdentifier(el.expression)) {
|
|
268
|
+
const token = (el.arguments && el.arguments.length > 0 && el.arguments[0]) ||
|
|
269
|
+
undefined;
|
|
270
|
+
isParametersDecorator = maybeUpdateDecorator(el.expression, reflector, token);
|
|
271
|
+
}
|
|
272
|
+
if (!isParametersDecorator) {
|
|
273
|
+
meta.token = new compiler_1.WrappedNodeExpr(el);
|
|
274
|
+
}
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
return meta;
|
|
278
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
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 { Expression, R3DependencyMetadata, R3Reference } from '../../../../../compiler';
|
|
9
|
+
import { R3FactoryMetadata } from '../../../../../compiler';
|
|
10
|
+
import { FactoryTarget } from '../../../../../compiler/src/render3/partial/api';
|
|
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;
|
|
25
|
+
/**
|
|
26
|
+
* Convert a `TypeValueReference` to an `Expression` which refers to the type as a value.
|
|
27
|
+
*
|
|
28
|
+
* Local references are converted to a `WrappedNodeExpr` of the TypeScript expression, and non-local
|
|
29
|
+
* references are converted to an `ExternalExpr`. Note that this is only valid in the context of the
|
|
30
|
+
* file in which the `TypeValueReference` originated.
|
|
31
|
+
*/
|
|
32
|
+
export declare function valueReferenceToExpression(valueRef: LocalTypeValueReference | ImportedTypeValueReference): Expression;
|
|
33
|
+
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
|
+
export declare function isAngularCore(decorator: Decorator): decorator is Decorator & {
|
|
52
|
+
import: Import;
|
|
53
|
+
};
|
|
54
|
+
export declare function findAngularDecorator(decorators: Decorator[], name: string, isCore: boolean): Decorator | undefined;
|
|
55
|
+
export declare function isAngularDecorator(decorator: Decorator, name: string, isCore: boolean): boolean;
|
|
56
|
+
/**
|
|
57
|
+
* Unwrap a `ts.Expression`, removing outer type-casts or parentheses until the expression is in its
|
|
58
|
+
* lowest level form.
|
|
59
|
+
*
|
|
60
|
+
* For example, the expression "(foo as Type)" unwraps to "foo".
|
|
61
|
+
*/
|
|
62
|
+
export declare function unwrapExpression(node: ts.Expression): ts.Expression;
|
|
63
|
+
/**
|
|
64
|
+
* If the given `node` is a forwardRef() expression then resolve its inner value, otherwise return
|
|
65
|
+
* `null`.
|
|
66
|
+
*
|
|
67
|
+
* @param node the forwardRef() expression to resolve
|
|
68
|
+
* @param reflector a ReflectionHost
|
|
69
|
+
* @returns the resolved expression, if the original expression was a forwardRef(), or `null`
|
|
70
|
+
* otherwise.
|
|
71
|
+
*/
|
|
72
|
+
export declare function tryUnwrapForwardRef(node: ts.Expression, reflector: ReflectionHost): ts.Expression | null;
|
|
73
|
+
/**
|
|
74
|
+
* Wraps all functions in a given expression in parentheses. This is needed to avoid problems
|
|
75
|
+
* where Tsickle annotations added between analyse and transform phases in Angular may trigger
|
|
76
|
+
* automatic semicolon insertion, e.g. if a function is the expression in a `return` statement.
|
|
77
|
+
* More
|
|
78
|
+
* info can be found in Tsickle source code here:
|
|
79
|
+
* https://github.com/angular/tsickle/blob/d7974262571c8a17d684e5ba07680e1b1993afdd/src/jsdoc_transformer.ts#L1021
|
|
80
|
+
*
|
|
81
|
+
* @param expression Expression where functions should be wrapped in parentheses
|
|
82
|
+
*/
|
|
83
|
+
export declare function wrapFunctionExpressionsInParens(expression: ts.Expression): ts.Expression;
|
|
84
|
+
/**
|
|
85
|
+
* Create an R3Reference for a class.
|
|
86
|
+
*
|
|
87
|
+
* The `value` is the exported declaration of the class from its source file.
|
|
88
|
+
* The `type` is an expression that would be used by ngcc in the typings (.d.ts) files.
|
|
89
|
+
*/
|
|
90
|
+
export declare function wrapTypeReference(reflector: ReflectionHost, clazz: ClassDeclaration): R3Reference;
|
|
91
|
+
export declare function toFactoryMetadata(meta: Omit<R3FactoryMetadata, 'target'>, target: FactoryTarget): R3FactoryMetadata;
|