static-injector 2.0.0 → 2.1.1

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 (107) hide show
  1. package/import/commonjs/index.js +425 -163
  2. package/import/es2015/di/create_injector.js +5 -1
  3. package/import/es2015/di/forward_ref.js +3 -3
  4. package/import/es2015/di/injection_token.js +18 -11
  5. package/import/es2015/di/injector_compatibility.js +19 -10
  6. package/import/es2015/di/injector_token.js +3 -3
  7. package/import/es2015/di/interface/defs.js +21 -12
  8. package/import/es2015/di/interface/provider.js +3 -1
  9. package/import/es2015/di/null_injector.js +3 -3
  10. package/import/es2015/di/provider_collection.js +196 -1
  11. package/import/es2015/di/r3_injector.js +31 -28
  12. package/import/es2015/error_details_base_url.js +19 -0
  13. package/import/es2015/errors.js +42 -0
  14. package/import/es2015/interface/type.js +14 -1
  15. package/import/es2015/render3/definition_factory.js +2 -3
  16. package/import/es2015/render3/errors_di.js +2 -3
  17. package/import/es2015/render3/instructions/di.js +0 -7
  18. package/import/es2015/render3/util/stringify_utils.js +5 -5
  19. package/import/es2015/util/array_utils.js +0 -7
  20. package/import/es2015/util/decorators.js +24 -1
  21. package/import/es2015/util/empty.js +4 -0
  22. package/import/es2015/util/stringify.js +5 -5
  23. package/import/fesm2015/index.js +422 -161
  24. package/import/typings/di/forward_ref.d.ts +1 -1
  25. package/import/typings/di/injectable.d.ts +5 -4
  26. package/import/typings/di/injection_token.d.ts +21 -6
  27. package/import/typings/di/injector.d.ts +16 -0
  28. package/import/typings/di/injector_compatibility.d.ts +11 -5
  29. package/import/typings/di/injector_token.d.ts +2 -2
  30. package/import/typings/di/interface/defs.d.ts +15 -8
  31. package/import/typings/di/interface/provider.d.ts +62 -2
  32. package/import/typings/di/null_injector.d.ts +1 -1
  33. package/import/typings/di/provider_collection.d.ts +55 -2
  34. package/import/typings/di/provider_token.d.ts +3 -3
  35. package/import/typings/di/r3_injector.d.ts +3 -8
  36. package/import/typings/di/scope.d.ts +1 -1
  37. package/import/typings/error_details_base_url.d.ts +19 -0
  38. package/import/typings/errors.d.ts +89 -0
  39. package/import/typings/interface/type.d.ts +45 -0
  40. package/import/typings/render3/definition_factory.d.ts +1 -1
  41. package/import/typings/render3/errors_di.d.ts +7 -0
  42. package/import/typings/render3/instructions/di.d.ts +0 -7
  43. package/import/typings/util/array_utils.d.ts +15 -7
  44. package/import/typings/util/decorators.d.ts +2 -1
  45. package/import/typings/util/empty.d.ts +1 -0
  46. package/package.json +3 -3
  47. package/transform/compiler/compiler.d.ts +8 -0
  48. package/transform/compiler/compiler.js +28 -0
  49. package/transform/compiler/index.d.ts +8 -5
  50. package/transform/compiler/index.js +12 -5
  51. package/transform/compiler/public_api.d.ts +13 -0
  52. package/transform/compiler/public_api.js +30 -0
  53. package/transform/compiler/src/compiler.d.ts +27 -0
  54. package/transform/compiler/src/compiler.js +47 -0
  55. package/transform/compiler/src/output/output_ast.d.ts +1 -1
  56. package/transform/compiler/src/render3/partial/api.d.ts +1 -1
  57. package/transform/compiler/src/render3/partial/api.js +7 -0
  58. package/transform/compiler/src/render3/r3_factory.d.ts +8 -15
  59. package/transform/compiler/src/render3/r3_factory.js +37 -56
  60. package/transform/compiler/src/render3/view/util.d.ts +0 -8
  61. package/transform/compiler/src/render3/view/util.js +0 -8
  62. package/transform/compiler-cli/src/ngtsc/annotations/common/index.d.ts +10 -0
  63. package/transform/compiler-cli/src/ngtsc/annotations/common/index.js +26 -0
  64. package/transform/compiler-cli/src/ngtsc/annotations/common/src/di.d.ts +37 -0
  65. package/transform/compiler-cli/src/ngtsc/annotations/common/src/di.js +197 -0
  66. package/transform/compiler-cli/src/ngtsc/annotations/{src → common/src}/factory.d.ts +3 -3
  67. package/transform/compiler-cli/src/ngtsc/annotations/{src → common/src}/factory.js +2 -2
  68. package/transform/compiler-cli/src/ngtsc/annotations/common/src/util.d.ts +3 -34
  69. package/transform/compiler-cli/src/ngtsc/annotations/common/src/util.js +18 -216
  70. package/transform/compiler-cli/src/ngtsc/annotations/src/injectable.d.ts +3 -4
  71. package/transform/compiler-cli/src/ngtsc/annotations/src/injectable.js +31 -50
  72. package/transform/compiler-cli/src/ngtsc/diagnostics/index.d.ts +9 -2
  73. package/transform/compiler-cli/src/ngtsc/diagnostics/index.js +7 -0
  74. package/transform/compiler-cli/src/ngtsc/imports/index.d.ts +7 -0
  75. package/transform/compiler-cli/src/ngtsc/imports/index.js +7 -0
  76. package/transform/compiler-cli/src/ngtsc/imports/src/default.d.ts +19 -0
  77. package/transform/compiler-cli/src/ngtsc/imports/src/default.js +28 -0
  78. package/transform/compiler-cli/src/ngtsc/reflection/index.d.ts +10 -3
  79. package/transform/compiler-cli/src/ngtsc/reflection/index.js +7 -0
  80. package/transform/compiler-cli/src/ngtsc/reflection/src/host.d.ts +12 -13
  81. package/transform/compiler-cli/src/ngtsc/reflection/src/host.js +7 -28
  82. package/transform/compiler-cli/src/ngtsc/reflection/src/type_to_value.d.ts +1 -1
  83. package/transform/compiler-cli/src/ngtsc/reflection/src/type_to_value.js +17 -41
  84. package/transform/compiler-cli/src/ngtsc/reflection/src/typescript.d.ts +1 -1
  85. package/transform/compiler-cli/src/ngtsc/reflection/src/typescript.js +46 -65
  86. package/transform/compiler-cli/src/ngtsc/reflection/src/util.d.ts +2 -2
  87. package/transform/compiler-cli/src/ngtsc/reflection/src/util.js +7 -27
  88. package/transform/compiler-cli/src/ngtsc/transform/index.d.ts +9 -2
  89. package/transform/compiler-cli/src/ngtsc/transform/index.js +7 -0
  90. package/transform/compiler-cli/src/ngtsc/transform/src/api.d.ts +4 -4
  91. package/transform/compiler-cli/src/ngtsc/transform/src/utils.d.ts +1 -1
  92. package/transform/compiler-cli/src/ngtsc/transform/src/utils.js +15 -35
  93. package/transform/compiler-cli/src/ngtsc/translator/index.d.ts +12 -5
  94. package/transform/compiler-cli/src/ngtsc/translator/index.js +7 -0
  95. package/transform/compiler-cli/src/ngtsc/translator/src/api/ast_factory.d.ts +5 -5
  96. package/transform/compiler-cli/src/ngtsc/translator/src/import_manager.d.ts +2 -2
  97. package/transform/compiler-cli/src/ngtsc/translator/src/import_manager.js +4 -24
  98. package/transform/compiler-cli/src/ngtsc/translator/src/translator.d.ts +2 -2
  99. package/transform/compiler-cli/src/ngtsc/translator/src/translator.js +1 -1
  100. package/transform/compiler-cli/src/ngtsc/translator/src/typescript_ast_factory.d.ts +1 -1
  101. package/transform/compiler-cli/src/ngtsc/translator/src/typescript_ast_factory.js +76 -95
  102. package/transform/compiler-cli/src/ngtsc/translator/src/typescript_translator.d.ts +4 -4
  103. package/transform/compiler-cli/src/ngtsc/ts_compatibility/src/ts_cross_version_utils.d.ts +20 -126
  104. package/transform/compiler-cli/src/ngtsc/ts_compatibility/src/ts_cross_version_utils.js +23 -147
  105. package/transform/compiler-cli/src/ngtsc/util/src/typescript.d.ts +1 -8
  106. package/transform/compiler-cli/src/ngtsc/util/src/typescript.js +4 -31
  107. package/transform/injectable-transform.js +36 -7
@@ -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
  };
@@ -6,96 +6,14 @@
6
6
  * Use of this source code is governed by an MIT-style license that can be
7
7
  * found in the LICENSE file at https://angular.io/license
8
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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
21
- Object.defineProperty(o, "default", { enumerable: true, value: v });
22
- }) : function(o, v) {
23
- o["default"] = v;
24
- });
25
- var __importStar = (this && this.__importStar) || function (mod) {
26
- if (mod && mod.__esModule) return mod;
27
- var result = {};
28
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
29
- __setModuleDefault(result, mod);
30
- return result;
9
+ var __importDefault = (this && this.__importDefault) || function (mod) {
10
+ return (mod && mod.__esModule) ? mod : { "default": mod };
31
11
  };
32
12
  Object.defineProperty(exports, "__esModule", { value: true });
33
- exports.isAbstractClassDeclaration = exports.toFactoryMetadata = exports.wrapTypeReference = exports.wrapFunctionExpressionsInParens = exports.tryUnwrapForwardRef = exports.unwrapExpression = exports.isAngularDecorator = exports.findAngularDecorator = exports.isAngularCore = exports.validateConstructorDependencies = exports.getValidConstructorDependencies = exports.unwrapConstructorDependencies = exports.valueReferenceToExpression = exports.getConstructorDependencies = void 0;
34
- const compiler_1 = require("../../../../../../compiler");
35
- const ts = __importStar(require("typescript"));
36
- const diagnostics_1 = require("../../../diagnostics");
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"));
37
16
  const reflection_1 = require("../../../reflection");
38
- function getConstructorDependencies(clazz, reflector, isCore) {
39
- const deps = [];
40
- const errors = [];
41
- let ctorParams = reflector.getConstructorParameters(clazz);
42
- if (ctorParams === null) {
43
- if (reflector.hasBaseClass(clazz)) {
44
- return null;
45
- }
46
- else {
47
- ctorParams = [];
48
- }
49
- }
50
- ctorParams.forEach((param, idx) => {
51
- let token = valueReferenceToExpression(param.typeValueReference);
52
- let attributeNameType = null;
53
- let optional = false, self = false, skipSelf = false;
54
- (param.decorators || [])
55
- .filter((dec) => isCore || isAngularCore(dec))
56
- .forEach((dec) => {
57
- const name = isCore || dec.import === null ? dec.name : dec.import.name;
58
- if (name === 'Inject') {
59
- if (dec.args === null || dec.args.length !== 1) {
60
- throw new diagnostics_1.FatalDiagnosticError(diagnostics_1.ErrorCode.DECORATOR_ARITY_WRONG, reflection_1.Decorator.nodeForError(dec), `Unexpected number of arguments to @Inject().`);
61
- }
62
- token = new compiler_1.WrappedNodeExpr(dec.args[0]);
63
- }
64
- else if (name === 'Optional') {
65
- optional = true;
66
- }
67
- else if (name === 'SkipSelf') {
68
- skipSelf = true;
69
- }
70
- else if (name === 'Self') {
71
- self = true;
72
- }
73
- else {
74
- throw new diagnostics_1.FatalDiagnosticError(diagnostics_1.ErrorCode.DECORATOR_UNEXPECTED, reflection_1.Decorator.nodeForError(dec), `Unexpected decorator ${name} on parameter.`);
75
- }
76
- });
77
- if (token === null) {
78
- if (param.typeValueReference.kind !== 2 /* TypeValueReferenceKind.UNAVAILABLE */) {
79
- throw new Error('Illegal state: expected value reference to be unavailable if no token is present');
80
- }
81
- errors.push({
82
- index: idx,
83
- param,
84
- reason: param.typeValueReference.reason,
85
- });
86
- }
87
- else {
88
- deps.push({ token, attributeNameType, optional, self, skipSelf });
89
- }
90
- });
91
- if (errors.length === 0) {
92
- return { deps };
93
- }
94
- else {
95
- return { deps: null, errors };
96
- }
97
- }
98
- exports.getConstructorDependencies = getConstructorDependencies;
99
17
  function valueReferenceToExpression(valueRef) {
100
18
  if (valueRef.kind === 2 /* TypeValueReferenceKind.UNAVAILABLE */) {
101
19
  return null;
@@ -118,121 +36,6 @@ function valueReferenceToExpression(valueRef) {
118
36
  }
119
37
  }
120
38
  exports.valueReferenceToExpression = valueReferenceToExpression;
121
- /**
122
- * Convert `ConstructorDeps` into the `R3DependencyMetadata` array for those deps if they're valid,
123
- * or into an `'invalid'` signal if they're not.
124
- *
125
- * This is a companion function to `validateConstructorDependencies` which accepts invalid deps.
126
- */
127
- function unwrapConstructorDependencies(deps) {
128
- if (deps === null) {
129
- return null;
130
- }
131
- else if (deps.deps !== null) {
132
- // These constructor dependencies are valid.
133
- return deps.deps;
134
- }
135
- else {
136
- // These deps are invalid.
137
- return 'invalid';
138
- }
139
- }
140
- exports.unwrapConstructorDependencies = unwrapConstructorDependencies;
141
- function getValidConstructorDependencies(clazz, reflector, isCore) {
142
- return validateConstructorDependencies(clazz, getConstructorDependencies(clazz, reflector, isCore));
143
- }
144
- exports.getValidConstructorDependencies = getValidConstructorDependencies;
145
- /**
146
- * Validate that `ConstructorDeps` does not have any invalid dependencies and convert them into the
147
- * `R3DependencyMetadata` array if so, or raise a diagnostic if some deps are invalid.
148
- *
149
- * This is a companion function to `unwrapConstructorDependencies` which does not accept invalid
150
- * deps.
151
- */
152
- function validateConstructorDependencies(clazz, deps) {
153
- if (deps === null) {
154
- return null;
155
- }
156
- else if (deps.deps !== null) {
157
- return deps.deps;
158
- }
159
- else {
160
- // TODO(alxhub): this cast is necessary because the g3 typescript version doesn't narrow here.
161
- // There is at least one error.
162
- const error = deps.errors[0];
163
- throw createUnsuitableInjectionTokenError(clazz, error);
164
- }
165
- }
166
- exports.validateConstructorDependencies = validateConstructorDependencies;
167
- /**
168
- * Creates a fatal error with diagnostic for an invalid injection token.
169
- * @param clazz The class for which the injection token was unavailable.
170
- * @param error The reason why no valid injection token is available.
171
- */
172
- function createUnsuitableInjectionTokenError(clazz, error) {
173
- const { param, index, reason } = error;
174
- let chainMessage = undefined;
175
- let hints = undefined;
176
- switch (reason.kind) {
177
- case 5 /* ValueUnavailableKind.UNSUPPORTED */:
178
- chainMessage =
179
- 'Consider using the @Inject decorator to specify an injection token.';
180
- hints = [
181
- (0, diagnostics_1.makeRelatedInformation)(reason.typeNode, 'This type is not supported as injection token.'),
182
- ];
183
- break;
184
- case 1 /* ValueUnavailableKind.NO_VALUE_DECLARATION */:
185
- chainMessage =
186
- 'Consider using the @Inject decorator to specify an injection token.';
187
- hints = [
188
- (0, diagnostics_1.makeRelatedInformation)(reason.typeNode, 'This type does not have a value, so it cannot be used as injection token.'),
189
- ];
190
- if (reason.decl !== null) {
191
- hints.push((0, diagnostics_1.makeRelatedInformation)(reason.decl, 'The type is declared here.'));
192
- }
193
- break;
194
- case 2 /* ValueUnavailableKind.TYPE_ONLY_IMPORT */:
195
- chainMessage =
196
- 'Consider changing the type-only import to a regular import, or use the @Inject decorator to specify an injection token.';
197
- hints = [
198
- (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.'),
199
- (0, diagnostics_1.makeRelatedInformation)(reason.node, 'The type-only import occurs here.'),
200
- ];
201
- break;
202
- case 4 /* ValueUnavailableKind.NAMESPACE */:
203
- chainMessage =
204
- 'Consider using the @Inject decorator to specify an injection token.';
205
- hints = [
206
- (0, diagnostics_1.makeRelatedInformation)(reason.typeNode, 'This type corresponds with a namespace, which cannot be used as injection token.'),
207
- (0, diagnostics_1.makeRelatedInformation)(reason.importClause, 'The namespace import occurs here.'),
208
- ];
209
- break;
210
- case 3 /* ValueUnavailableKind.UNKNOWN_REFERENCE */:
211
- chainMessage = 'The type should reference a known declaration.';
212
- hints = [
213
- (0, diagnostics_1.makeRelatedInformation)(reason.typeNode, 'This type could not be resolved.'),
214
- ];
215
- break;
216
- case 0 /* ValueUnavailableKind.MISSING_TYPE */:
217
- chainMessage =
218
- 'Consider adding a type to the parameter or use the @Inject decorator to specify an injection token.';
219
- break;
220
- }
221
- const chain = {
222
- messageText: `No suitable injection token for parameter '${param.name || index}' of class '${clazz.name.text}'.`,
223
- category: ts.DiagnosticCategory.Error,
224
- code: 0,
225
- next: [
226
- {
227
- messageText: chainMessage,
228
- category: ts.DiagnosticCategory.Message,
229
- code: 0,
230
- },
231
- ],
232
- };
233
- return new diagnostics_1.FatalDiagnosticError(diagnostics_1.ErrorCode.PARAM_MISSING_TOKEN, param.nameNode, chain, hints);
234
- }
235
- /** todo 需要改成自己的依赖包 */
236
39
  function isAngularCore(decorator) {
237
40
  return (decorator.import !== null && decorator.import.from === 'static-injector');
238
41
  }
@@ -258,7 +61,7 @@ exports.isAngularDecorator = isAngularDecorator;
258
61
  * For example, the expression "(foo as Type)" unwraps to "foo".
259
62
  */
260
63
  function unwrapExpression(node) {
261
- while (ts.isAsExpression(node) || ts.isParenthesizedExpression(node)) {
64
+ while (typescript_1.default.isAsExpression(node) || typescript_1.default.isParenthesizedExpression(node)) {
262
65
  node = node.expression;
263
66
  }
264
67
  return node;
@@ -266,18 +69,18 @@ function unwrapExpression(node) {
266
69
  exports.unwrapExpression = unwrapExpression;
267
70
  function expandForwardRef(arg) {
268
71
  arg = unwrapExpression(arg);
269
- if (!ts.isArrowFunction(arg) && !ts.isFunctionExpression(arg)) {
72
+ if (!typescript_1.default.isArrowFunction(arg) && !typescript_1.default.isFunctionExpression(arg)) {
270
73
  return null;
271
74
  }
272
75
  const body = arg.body;
273
76
  // Either the body is a ts.Expression directly, or a block with a single return statement.
274
- if (ts.isBlock(body)) {
77
+ if (typescript_1.default.isBlock(body)) {
275
78
  // Block body - look for a single return statement.
276
79
  if (body.statements.length !== 1) {
277
80
  return null;
278
81
  }
279
82
  const stmt = body.statements[0];
280
- if (!ts.isReturnStatement(stmt) || stmt.expression === undefined) {
83
+ if (!typescript_1.default.isReturnStatement(stmt) || stmt.expression === undefined) {
281
84
  return null;
282
85
  }
283
86
  return stmt.expression;
@@ -298,13 +101,13 @@ function expandForwardRef(arg) {
298
101
  */
299
102
  function tryUnwrapForwardRef(node, reflector) {
300
103
  node = unwrapExpression(node);
301
- if (!ts.isCallExpression(node) || node.arguments.length !== 1) {
104
+ if (!typescript_1.default.isCallExpression(node) || node.arguments.length !== 1) {
302
105
  return null;
303
106
  }
304
- const fn = ts.isPropertyAccessExpression(node.expression)
107
+ const fn = typescript_1.default.isPropertyAccessExpression(node.expression)
305
108
  ? node.expression.name
306
109
  : node.expression;
307
- if (!ts.isIdentifier(fn)) {
110
+ if (!typescript_1.default.isIdentifier(fn)) {
308
111
  return null;
309
112
  }
310
113
  const expr = expandForwardRef(node.arguments[0]);
@@ -312,7 +115,6 @@ function tryUnwrapForwardRef(node, reflector) {
312
115
  return null;
313
116
  }
314
117
  const imp = reflector.getImportOfIdentifier(fn);
315
- //todo 更换包名
316
118
  if (imp === null ||
317
119
  imp.from !== 'static-injector' ||
318
120
  imp.name !== 'forwardRef') {
@@ -323,13 +125,13 @@ function tryUnwrapForwardRef(node, reflector) {
323
125
  exports.tryUnwrapForwardRef = tryUnwrapForwardRef;
324
126
  const parensWrapperTransformerFactory = (context) => {
325
127
  const visitor = (node) => {
326
- const visited = ts.visitEachChild(node, visitor, context);
327
- if (ts.isArrowFunction(visited) || ts.isFunctionExpression(visited)) {
328
- return ts.createParen(visited);
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);
329
131
  }
330
132
  return visited;
331
133
  };
332
- return (node) => ts.visitEachChild(node, visitor, context);
134
+ return (node) => typescript_1.default.visitEachChild(node, visitor, context);
333
135
  };
334
136
  /**
335
137
  * Wraps all functions in a given expression in parentheses. This is needed to avoid problems
@@ -342,7 +144,7 @@ const parensWrapperTransformerFactory = (context) => {
342
144
  * @param expression Expression where functions should be wrapped in parentheses
343
145
  */
344
146
  function wrapFunctionExpressionsInParens(expression) {
345
- return ts.transform(expression, [parensWrapperTransformerFactory])
147
+ return typescript_1.default.transform(expression, [parensWrapperTransformerFactory])
346
148
  .transformed[0];
347
149
  }
348
150
  exports.wrapFunctionExpressionsInParens = wrapFunctionExpressionsInParens;
@@ -374,6 +176,6 @@ function toFactoryMetadata(meta, target) {
374
176
  exports.toFactoryMetadata = toFactoryMetadata;
375
177
  function isAbstractClassDeclaration(clazz) {
376
178
  return (clazz.modifiers !== undefined &&
377
- clazz.modifiers.some((mod) => mod.kind === ts.SyntaxKind.AbstractKeyword));
179
+ clazz.modifiers.some((mod) => mod.kind === typescript_1.default.SyntaxKind.AbstractKeyword));
378
180
  }
379
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 '../../../../../compiler';
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, null, unknown> {
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 {};