static-injector 2.0.0 → 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.
Files changed (72) hide show
  1. package/import/commonjs/index.js +6 -6
  2. package/import/es2015/di/r3_injector.js +6 -6
  3. package/import/fesm2015/index.js +6 -6
  4. package/import/typings/di/injectable.d.ts +1 -1
  5. package/import/typings/di/interface/provider.d.ts +2 -2
  6. package/import/typings/di/provider_collection.d.ts +1 -1
  7. package/import/typings/di/provider_token.d.ts +1 -1
  8. package/import/typings/di/r3_injector.d.ts +1 -1
  9. package/import/typings/di/scope.d.ts +1 -1
  10. package/import/typings/render3/definition_factory.d.ts +1 -1
  11. package/package.json +3 -3
  12. package/transform/compiler/compiler.d.ts +8 -0
  13. package/transform/compiler/compiler.js +28 -0
  14. package/transform/compiler/index.d.ts +8 -5
  15. package/transform/compiler/index.js +12 -5
  16. package/transform/compiler/public_api.d.ts +13 -0
  17. package/transform/compiler/public_api.js +30 -0
  18. package/transform/compiler/src/compiler.d.ts +27 -0
  19. package/transform/compiler/src/compiler.js +47 -0
  20. package/transform/compiler/src/output/output_ast.d.ts +1 -1
  21. package/transform/compiler/src/render3/partial/api.d.ts +1 -1
  22. package/transform/compiler/src/render3/partial/api.js +7 -0
  23. package/transform/compiler/src/render3/r3_factory.d.ts +8 -15
  24. package/transform/compiler/src/render3/r3_factory.js +37 -56
  25. package/transform/compiler/src/render3/view/util.d.ts +0 -8
  26. package/transform/compiler/src/render3/view/util.js +0 -8
  27. package/transform/compiler-cli/src/ngtsc/annotations/common/index.d.ts +10 -0
  28. package/transform/compiler-cli/src/ngtsc/annotations/common/index.js +26 -0
  29. package/transform/compiler-cli/src/ngtsc/annotations/common/src/di.d.ts +37 -0
  30. package/transform/compiler-cli/src/ngtsc/annotations/common/src/di.js +197 -0
  31. package/transform/compiler-cli/src/ngtsc/annotations/{src → common/src}/factory.d.ts +3 -3
  32. package/transform/compiler-cli/src/ngtsc/annotations/{src → common/src}/factory.js +2 -2
  33. package/transform/compiler-cli/src/ngtsc/annotations/common/src/util.d.ts +3 -34
  34. package/transform/compiler-cli/src/ngtsc/annotations/common/src/util.js +18 -216
  35. package/transform/compiler-cli/src/ngtsc/annotations/src/injectable.d.ts +3 -4
  36. package/transform/compiler-cli/src/ngtsc/annotations/src/injectable.js +31 -50
  37. package/transform/compiler-cli/src/ngtsc/diagnostics/index.d.ts +9 -2
  38. package/transform/compiler-cli/src/ngtsc/diagnostics/index.js +7 -0
  39. package/transform/compiler-cli/src/ngtsc/imports/index.d.ts +7 -0
  40. package/transform/compiler-cli/src/ngtsc/imports/index.js +7 -0
  41. package/transform/compiler-cli/src/ngtsc/imports/src/default.d.ts +19 -0
  42. package/transform/compiler-cli/src/ngtsc/imports/src/default.js +28 -0
  43. package/transform/compiler-cli/src/ngtsc/reflection/index.d.ts +10 -3
  44. package/transform/compiler-cli/src/ngtsc/reflection/index.js +7 -0
  45. package/transform/compiler-cli/src/ngtsc/reflection/src/host.d.ts +12 -13
  46. package/transform/compiler-cli/src/ngtsc/reflection/src/host.js +7 -28
  47. package/transform/compiler-cli/src/ngtsc/reflection/src/type_to_value.d.ts +1 -1
  48. package/transform/compiler-cli/src/ngtsc/reflection/src/type_to_value.js +17 -41
  49. package/transform/compiler-cli/src/ngtsc/reflection/src/typescript.d.ts +1 -1
  50. package/transform/compiler-cli/src/ngtsc/reflection/src/typescript.js +46 -65
  51. package/transform/compiler-cli/src/ngtsc/reflection/src/util.d.ts +2 -2
  52. package/transform/compiler-cli/src/ngtsc/reflection/src/util.js +7 -27
  53. package/transform/compiler-cli/src/ngtsc/transform/index.d.ts +9 -2
  54. package/transform/compiler-cli/src/ngtsc/transform/index.js +7 -0
  55. package/transform/compiler-cli/src/ngtsc/transform/src/api.d.ts +4 -4
  56. package/transform/compiler-cli/src/ngtsc/transform/src/utils.d.ts +1 -1
  57. package/transform/compiler-cli/src/ngtsc/transform/src/utils.js +15 -35
  58. package/transform/compiler-cli/src/ngtsc/translator/index.d.ts +12 -5
  59. package/transform/compiler-cli/src/ngtsc/translator/index.js +7 -0
  60. package/transform/compiler-cli/src/ngtsc/translator/src/api/ast_factory.d.ts +5 -5
  61. package/transform/compiler-cli/src/ngtsc/translator/src/import_manager.d.ts +2 -2
  62. package/transform/compiler-cli/src/ngtsc/translator/src/import_manager.js +4 -24
  63. package/transform/compiler-cli/src/ngtsc/translator/src/translator.d.ts +2 -2
  64. package/transform/compiler-cli/src/ngtsc/translator/src/translator.js +1 -1
  65. package/transform/compiler-cli/src/ngtsc/translator/src/typescript_ast_factory.d.ts +1 -1
  66. package/transform/compiler-cli/src/ngtsc/translator/src/typescript_ast_factory.js +76 -95
  67. package/transform/compiler-cli/src/ngtsc/translator/src/typescript_translator.d.ts +4 -4
  68. package/transform/compiler-cli/src/ngtsc/ts_compatibility/src/ts_cross_version_utils.d.ts +20 -126
  69. package/transform/compiler-cli/src/ngtsc/ts_compatibility/src/ts_cross_version_utils.js +23 -147
  70. package/transform/compiler-cli/src/ngtsc/util/src/typescript.d.ts +1 -8
  71. package/transform/compiler-cli/src/ngtsc/util/src/typescript.js +4 -31
  72. package/transform/injectable-transform.js +36 -7
@@ -1,11 +1,4 @@
1
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
2
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
10
3
  if (k2 === undefined) k2 = k;
11
4
  var desc = Object.getOwnPropertyDescriptor(m, k);
@@ -31,7 +24,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
31
24
  };
32
25
  Object.defineProperty(exports, "__esModule", { value: true });
33
26
  exports.isExpressionFactoryMetadata = exports.isDelegatedFactoryMetadata = exports.createFactoryType = exports.compileFactoryFunction = exports.FactoryTarget = exports.R3FactoryDelegateType = void 0;
34
- const outputAst = __importStar(require("../output/output_ast"));
27
+ const o = __importStar(require("../output/output_ast"));
35
28
  const r3_identifiers_1 = require("../render3/r3_identifiers");
36
29
  const util_1 = require("./util");
37
30
  var R3FactoryDelegateType;
@@ -51,7 +44,7 @@ var FactoryTarget;
51
44
  * Construct a factory function expression for the given `R3FactoryMetadata`.
52
45
  */
53
46
  function compileFactoryFunction(meta) {
54
- const t = outputAst.variable('t');
47
+ const t = o.variable('t');
55
48
  let baseFactoryVar = null;
56
49
  // The type to instantiate via constructor invocation. If there is no delegated factory, meaning
57
50
  // this type is always created by constructor invocation, then this is the type-to-create
@@ -59,29 +52,29 @@ function compileFactoryFunction(meta) {
59
52
  // delegated factory (which is used to create the current type) then this is only the type-to-
60
53
  // create parameter (t).
61
54
  const typeForCtor = !isDelegatedFactoryMetadata(meta)
62
- ? new outputAst.BinaryOperatorExpr(outputAst.BinaryOperator.Or, t, meta.internalType)
55
+ ? new o.BinaryOperatorExpr(o.BinaryOperator.Or, t, meta.internalType)
63
56
  : t;
64
57
  let ctorExpr = null;
65
58
  if (meta.deps !== null) {
66
59
  // There is a constructor (either explicitly or implicitly defined).
67
60
  if (meta.deps !== 'invalid') {
68
- ctorExpr = new outputAst.InstantiateExpr(typeForCtor, injectDependencies(meta.deps, meta.target));
61
+ ctorExpr = new o.InstantiateExpr(typeForCtor, injectDependencies(meta.deps, meta.target));
69
62
  }
70
63
  }
71
64
  else {
72
65
  // There is no constructor, use the base class' factory to construct typeForCtor.
73
- baseFactoryVar = outputAst.variable(`ɵ${meta.name}_BaseFactory`);
66
+ baseFactoryVar = o.variable(`ɵ${meta.name}_BaseFactory`);
74
67
  ctorExpr = baseFactoryVar.callFn([typeForCtor]);
75
68
  }
76
69
  const body = [];
77
70
  let retExpr = null;
78
71
  function makeConditionalFactory(nonCtorExpr) {
79
- const r = outputAst.variable('r');
80
- body.push(r.set(outputAst.NULL_EXPR).toDeclStmt());
72
+ const r = o.variable('r');
73
+ body.push(r.set(o.NULL_EXPR).toDeclStmt());
81
74
  const ctorStmt = ctorExpr !== null
82
75
  ? r.set(ctorExpr).toStmt()
83
- : outputAst.importExpr(r3_identifiers_1.Identifiers.invalidFactory).callFn([]).toStmt();
84
- body.push(outputAst.ifStmt(t, [ctorStmt], [r.set(nonCtorExpr).toStmt()]));
76
+ : o.importExpr(r3_identifiers_1.Identifiers.invalidFactory).callFn([]).toStmt();
77
+ body.push(o.ifStmt(t, [ctorStmt], [r.set(nonCtorExpr).toStmt()]));
85
78
  return r;
86
79
  }
87
80
  if (isDelegatedFactoryMetadata(meta)) {
@@ -90,8 +83,8 @@ function compileFactoryFunction(meta) {
90
83
  const delegateArgs = injectDependencies(meta.delegateDeps, meta.target);
91
84
  // Either call `new delegate(...)` or `delegate(...)` depending on meta.delegateType.
92
85
  const factoryExpr = new (meta.delegateType === R3FactoryDelegateType.Class
93
- ? outputAst.InstantiateExpr
94
- : outputAst.InvokeFunctionExpr)(meta.delegate, delegateArgs);
86
+ ? o.InstantiateExpr
87
+ : o.InvokeFunctionExpr)(meta.delegate, delegateArgs);
95
88
  retExpr = makeConditionalFactory(factoryExpr);
96
89
  }
97
90
  else if (isExpressionFactoryMetadata(meta)) {
@@ -103,29 +96,29 @@ function compileFactoryFunction(meta) {
103
96
  }
104
97
  if (retExpr === null) {
105
98
  // The expression cannot be formed so render an `ɵɵinvalidFactory()` call.
106
- body.push(outputAst.importExpr(r3_identifiers_1.Identifiers.invalidFactory).callFn([]).toStmt());
99
+ body.push(o.importExpr(r3_identifiers_1.Identifiers.invalidFactory).callFn([]).toStmt());
107
100
  }
108
101
  else if (baseFactoryVar !== null) {
109
102
  // This factory uses a base factory, so call `ɵɵgetInheritedFactory()` to compute it.
110
- const getInheritedFactoryCall = outputAst
103
+ const getInheritedFactoryCall = o
111
104
  .importExpr(r3_identifiers_1.Identifiers.getInheritedFactory)
112
105
  .callFn([meta.internalType]);
113
106
  // Memoize the base factoryFn: `baseFactory || (baseFactory = ɵɵgetInheritedFactory(...))`
114
- const baseFactory = new outputAst.BinaryOperatorExpr(outputAst.BinaryOperator.Or, baseFactoryVar, baseFactoryVar.set(getInheritedFactoryCall));
115
- body.push(new outputAst.ReturnStatement(baseFactory.callFn([typeForCtor])));
107
+ const baseFactory = new o.BinaryOperatorExpr(o.BinaryOperator.Or, baseFactoryVar, baseFactoryVar.set(getInheritedFactoryCall));
108
+ body.push(new o.ReturnStatement(baseFactory.callFn([typeForCtor])));
116
109
  }
117
110
  else {
118
111
  // This is straightforward factory, just return it.
119
- body.push(new outputAst.ReturnStatement(retExpr));
112
+ body.push(new o.ReturnStatement(retExpr));
120
113
  }
121
- let factoryFn = outputAst.fn([new outputAst.FnParam('t', outputAst.DYNAMIC_TYPE)], body, outputAst.INFERRED_TYPE, undefined, `${meta.name}_Factory`);
114
+ let factoryFn = o.fn([new o.FnParam('t', o.DYNAMIC_TYPE)], body, o.INFERRED_TYPE, undefined, `${meta.name}_Factory`);
122
115
  if (baseFactoryVar !== null) {
123
116
  // There is a base factory variable so wrap its declaration along with the factory function into
124
117
  // an IIFE.
125
- factoryFn = outputAst
118
+ factoryFn = o
126
119
  .fn([], [
127
- new outputAst.DeclareVarStmt(baseFactoryVar.name),
128
- new outputAst.ReturnStatement(factoryFn),
120
+ new o.DeclareVarStmt(baseFactoryVar.name),
121
+ new o.ReturnStatement(factoryFn),
129
122
  ])
130
123
  .callFn([], /* sourceSpan */ undefined, /* pure */ true);
131
124
  }
@@ -139,8 +132,8 @@ exports.compileFactoryFunction = compileFactoryFunction;
139
132
  function createFactoryType(meta) {
140
133
  const ctorDepsType = meta.deps !== null && meta.deps !== 'invalid'
141
134
  ? createCtorDepsType(meta.deps)
142
- : outputAst.NONE_TYPE;
143
- return outputAst.expressionType(outputAst.importExpr(r3_identifiers_1.Identifiers.FactoryDeclaration, [
135
+ : o.NONE_TYPE;
136
+ return o.expressionType(o.importExpr(r3_identifiers_1.Identifiers.FactoryDeclaration, [
144
137
  (0, util_1.typeWithParameters)(meta.type.type, meta.typeArgumentCount),
145
138
  ctorDepsType,
146
139
  ]));
@@ -152,29 +145,29 @@ function injectDependencies(deps, target) {
152
145
  function compileInjectDependency(dep, target, index) {
153
146
  // Interpret the dependency according to its resolved type.
154
147
  if (dep.token === null) {
155
- return outputAst
156
- .importExpr(r3_identifiers_1.Identifiers.invalidFactoryDep)
157
- .callFn([outputAst.literal(index)]);
148
+ return o.importExpr(r3_identifiers_1.Identifiers.invalidFactoryDep).callFn([o.literal(index)]);
158
149
  }
159
150
  else if (dep.attributeNameType === null) {
160
151
  // Build up the injection flags according to the metadata.
161
152
  const flags = 0 /* InjectFlags.Default */ |
162
153
  (dep.self ? 2 /* InjectFlags.Self */ : 0) |
163
154
  (dep.skipSelf ? 4 /* InjectFlags.SkipSelf */ : 0) |
164
- (dep.optional ? 8 /* InjectFlags.Optional */ : 0);
155
+ 0 |
156
+ (dep.optional ? 8 /* InjectFlags.Optional */ : 0) |
157
+ 0;
165
158
  // If this dependency is optional or otherwise has non-default flags, then additional
166
159
  // parameters describing how to inject the dependency must be passed to the inject function
167
160
  // that's being used.
168
- let flagsParam = flags !== 0 /* InjectFlags.Default */ || dep.optional
169
- ? outputAst.literal(flags)
170
- : null;
161
+ let flagsParam = flags !== 0 /* InjectFlags.Default */ || dep.optional ? o.literal(flags) : null;
171
162
  // Build up the arguments to the injectFn call.
172
163
  const injectArgs = [dep.token];
173
164
  if (flagsParam) {
174
165
  injectArgs.push(flagsParam);
175
166
  }
176
167
  const injectFn = getInjectFn(target);
177
- return outputAst.importExpr(injectFn).callFn(injectArgs);
168
+ return o.importExpr(injectFn).callFn(injectArgs);
169
+ }
170
+ else {
178
171
  }
179
172
  }
180
173
  function createCtorDepsType(deps) {
@@ -186,40 +179,28 @@ function createCtorDepsType(deps) {
186
179
  return type;
187
180
  }
188
181
  else {
189
- return outputAst.literal(null);
182
+ return o.literal(null);
190
183
  }
191
184
  });
192
185
  if (hasTypes) {
193
- return outputAst.expressionType(outputAst.literalArr(attributeTypes));
186
+ return o.expressionType(o.literalArr(attributeTypes));
194
187
  }
195
188
  else {
196
- return outputAst.NONE_TYPE;
189
+ return o.NONE_TYPE;
197
190
  }
198
191
  }
199
192
  function createCtorDepType(dep) {
200
193
  const entries = [];
201
194
  if (dep.optional) {
202
- entries.push({
203
- key: 'optional',
204
- value: outputAst.literal(true),
205
- quoted: false,
206
- });
195
+ entries.push({ key: 'optional', value: o.literal(true), quoted: false });
207
196
  }
208
197
  if (dep.self) {
209
- entries.push({
210
- key: 'self',
211
- value: outputAst.literal(true),
212
- quoted: false,
213
- });
198
+ entries.push({ key: 'self', value: o.literal(true), quoted: false });
214
199
  }
215
200
  if (dep.skipSelf) {
216
- entries.push({
217
- key: 'skipSelf',
218
- value: outputAst.literal(true),
219
- quoted: false,
220
- });
201
+ entries.push({ key: 'skipSelf', value: o.literal(true), quoted: false });
221
202
  }
222
- return entries.length > 0 ? outputAst.literalMap(entries) : null;
203
+ return entries.length > 0 ? o.literalMap(entries) : null;
223
204
  }
224
205
  function isDelegatedFactoryMetadata(meta) {
225
206
  return meta.delegateType !== undefined;
@@ -6,14 +6,6 @@
6
6
  * found in the LICENSE file at https://angular.io/license
7
7
  */
8
8
  import * as o from '../../output/output_ast';
9
- /**
10
- * Checks whether an object key contains potentially unsafe chars, thus the key should be wrapped in
11
- * quotes. Note: we do not wrap all keys into quotes, as it may have impact on minification and may
12
- * bot work in some cases when object keys are mangled by minifier.
13
- *
14
- * TODO(FW-1136): this is a temporary solution, we need to come up with a better way of working with
15
- * inputs that contain potentially unsafe chars.
16
- */
17
9
  /**
18
10
  * A representation for an object literal used during codegen of definition objects. The generic
19
11
  * type `T` allows to reference a documented type of the generated structure, such that the
@@ -32,14 +32,6 @@ var __importStar = (this && this.__importStar) || function (mod) {
32
32
  Object.defineProperty(exports, "__esModule", { value: true });
33
33
  exports.DefinitionMap = void 0;
34
34
  const o = __importStar(require("../../output/output_ast"));
35
- /**
36
- * Checks whether an object key contains potentially unsafe chars, thus the key should be wrapped in
37
- * quotes. Note: we do not wrap all keys into quotes, as it may have impact on minification and may
38
- * bot work in some cases when object keys are mangled by minifier.
39
- *
40
- * TODO(FW-1136): this is a temporary solution, we need to come up with a better way of working with
41
- * inputs that contain potentially unsafe chars.
42
- */
43
35
  /**
44
36
  * A representation for an object literal used during codegen of definition objects. The generic
45
37
  * type `T` allows to reference a documented type of the generated structure, such that the
@@ -0,0 +1,10 @@
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
+ export * from './src/di';
9
+ export * from './src/factory';
10
+ export * from './src/util';
@@ -0,0 +1,26 @@
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
+ var desc = Object.getOwnPropertyDescriptor(m, k);
12
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
13
+ desc = { enumerable: true, get: function() { return m[k]; } };
14
+ }
15
+ Object.defineProperty(o, k2, desc);
16
+ }) : (function(o, m, k, k2) {
17
+ if (k2 === undefined) k2 = k;
18
+ o[k2] = m[k];
19
+ }));
20
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
21
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
22
+ };
23
+ Object.defineProperty(exports, "__esModule", { value: true });
24
+ __exportStar(require("./src/di"), exports);
25
+ __exportStar(require("./src/factory"), exports);
26
+ __exportStar(require("./src/util"), exports);
@@ -0,0 +1,37 @@
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 } from 'static-injector/transform/compiler';
9
+ import { ClassDeclaration, CtorParameter, ReflectionHost, UnavailableValue } from '../../../reflection';
10
+ export type ConstructorDeps = {
11
+ deps: R3DependencyMetadata[];
12
+ } | {
13
+ deps: null;
14
+ errors: ConstructorDepError[];
15
+ };
16
+ export interface ConstructorDepError {
17
+ index: number;
18
+ param: CtorParameter;
19
+ reason: UnavailableValue;
20
+ }
21
+ export declare function getConstructorDependencies(clazz: ClassDeclaration, reflector: ReflectionHost, isCore: boolean): ConstructorDeps | null;
22
+ /**
23
+ * Convert `ConstructorDeps` into the `R3DependencyMetadata` array for those deps if they're valid,
24
+ * or into an `'invalid'` signal if they're not.
25
+ *
26
+ * This is a companion function to `validateConstructorDependencies` which accepts invalid deps.
27
+ */
28
+ export declare function unwrapConstructorDependencies(deps: ConstructorDeps | null): R3DependencyMetadata[] | 'invalid' | null;
29
+ export declare function getValidConstructorDependencies(clazz: ClassDeclaration, reflector: ReflectionHost, isCore: boolean): R3DependencyMetadata[] | null;
30
+ /**
31
+ * Validate that `ConstructorDeps` does not have any invalid dependencies and convert them into the
32
+ * `R3DependencyMetadata` array if so, or raise a diagnostic if some deps are invalid.
33
+ *
34
+ * This is a companion function to `unwrapConstructorDependencies` which does not accept invalid
35
+ * deps.
36
+ */
37
+ export declare function validateConstructorDependencies(clazz: ClassDeclaration, deps: ConstructorDeps | null): R3DependencyMetadata[] | null;
@@ -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 "../../../../../compiler";
9
- import { CompileResult } from "../../transform";
10
- export declare type CompileFactoryFn = (metadata: R3FactoryMetadata) => CompileResult;
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("../../../../../compiler");
11
+ const compiler_1 = require("static-injector/transform/compiler");
12
12
  function compileNgFactoryDefField(metadata) {
13
13
  const res = (0, compiler_1.compileFactoryFunction)(metadata);
14
14
  return {
15
- name: "ɵfac",
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, 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;
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
  };