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.
Files changed (146) hide show
  1. package/import/commonjs/index.js +1248 -0
  2. package/import/es2015/decorator/injectable.js +14 -0
  3. package/import/es2015/decorator/interface/provider.js +8 -0
  4. package/import/es2015/di/forward_ref.js +53 -0
  5. package/import/es2015/di/inject_switch.js +31 -0
  6. package/import/es2015/di/injection_token.js +71 -0
  7. package/import/es2015/di/injector.js +63 -0
  8. package/import/es2015/di/injector_compatibility.js +172 -0
  9. package/import/es2015/di/injector_marker.js +8 -0
  10. package/import/es2015/di/injector_token.js +21 -0
  11. package/import/es2015/di/interface/defs.js +72 -0
  12. package/import/es2015/di/interface/injector.js +25 -0
  13. package/import/es2015/di/interface/provider.js +8 -0
  14. package/import/es2015/di/metadata.js +49 -0
  15. package/import/es2015/di/null_injector.js +19 -0
  16. package/import/es2015/di/provider_token.js +8 -0
  17. package/import/es2015/di/r3_injector.js +382 -0
  18. package/import/es2015/di/scope.js +14 -0
  19. package/import/es2015/index.js +11 -0
  20. package/import/es2015/interface/lifecycle_hooks.js +8 -0
  21. package/import/es2015/interface/type.js +8 -0
  22. package/import/es2015/render3/definition_factory.js +15 -0
  23. package/import/es2015/render3/error_code.js +29 -0
  24. package/import/es2015/render3/errors_di.js +14 -0
  25. package/import/es2015/render3/fields.js +11 -0
  26. package/import/es2015/render3/instructions/di.js +23 -0
  27. package/import/es2015/render3/util/stringify_utils.js +37 -0
  28. package/import/es2015/util/array_utils.js +17 -0
  29. package/import/es2015/util/closure.js +19 -0
  30. package/import/es2015/util/decorators.js +30 -0
  31. package/import/es2015/util/empty.js +14 -0
  32. package/import/es2015/util/property.js +15 -0
  33. package/import/es2015/util/stringify.js +30 -0
  34. package/import/fesm2015/index.js +1211 -0
  35. package/import/typings/decorator/injectable.d.ts +82 -0
  36. package/import/typings/decorator/interface/provider.d.ts +312 -0
  37. package/import/typings/di/forward_ref.d.ts +49 -0
  38. package/import/typings/di/inject_switch.d.ts +17 -0
  39. package/import/typings/di/injection_token.d.ts +57 -0
  40. package/import/typings/di/injector.d.ts +76 -0
  41. package/import/typings/di/injector_compatibility.d.ts +77 -0
  42. package/import/typings/di/injector_marker.d.ts +23 -0
  43. package/import/typings/di/injector_token.d.ts +18 -0
  44. package/import/typings/di/interface/defs.d.ts +155 -0
  45. package/import/typings/di/interface/injector.d.ts +48 -0
  46. package/import/typings/di/interface/provider.d.ts +312 -0
  47. package/import/typings/di/metadata.d.ts +176 -0
  48. package/import/typings/di/null_injector.d.ts +11 -0
  49. package/import/typings/di/provider_token.d.ts +17 -0
  50. package/import/typings/di/r3_injector.d.ts +92 -0
  51. package/import/typings/di/scope.d.ts +14 -0
  52. package/import/typings/index.d.ts +11 -0
  53. package/import/typings/interface/lifecycle_hooks.d.ts +28 -0
  54. package/import/typings/interface/type.d.ts +21 -0
  55. package/import/typings/render3/definition_factory.d.ts +24 -0
  56. package/import/typings/render3/error_code.d.ts +24 -0
  57. package/import/typings/render3/errors_di.d.ts +2 -0
  58. package/import/typings/render3/fields.d.ts +8 -0
  59. package/import/typings/render3/instructions/di.d.ts +20 -0
  60. package/import/typings/render3/util/stringify_utils.d.ts +20 -0
  61. package/import/typings/util/array_utils.d.ts +10 -0
  62. package/import/typings/util/closure.d.ts +17 -0
  63. package/import/typings/util/decorators.d.ts +27 -0
  64. package/import/typings/util/empty.d.ts +14 -0
  65. package/import/typings/util/property.d.ts +8 -0
  66. package/import/typings/util/stringify.d.ts +8 -0
  67. package/package.json +34 -0
  68. package/readme.md +27 -0
  69. package/transform/compiler/index.d.ts +5 -0
  70. package/transform/compiler/index.js +17 -0
  71. package/transform/compiler/src/core.d.ts +19 -0
  72. package/transform/compiler/src/core.js +9 -0
  73. package/transform/compiler/src/injectable_compiler_2.d.ts +53 -0
  74. package/transform/compiler/src/injectable_compiler_2.js +159 -0
  75. package/transform/compiler/src/output/output_ast.d.ts +534 -0
  76. package/transform/compiler/src/output/output_ast.js +986 -0
  77. package/transform/compiler/src/parse_util.d.ts +50 -0
  78. package/transform/compiler/src/parse_util.js +61 -0
  79. package/transform/compiler/src/render3/partial/api.d.ts +166 -0
  80. package/transform/compiler/src/render3/partial/api.js +11 -0
  81. package/transform/compiler/src/render3/partial/util.d.ts +16 -0
  82. package/transform/compiler/src/render3/partial/util.js +44 -0
  83. package/transform/compiler/src/render3/r3_factory.d.ts +97 -0
  84. package/transform/compiler/src/render3/r3_factory.js +239 -0
  85. package/transform/compiler/src/render3/r3_identifiers.d.ts +20 -0
  86. package/transform/compiler/src/render3/r3_identifiers.js +51 -0
  87. package/transform/compiler/src/render3/util.d.ts +21 -0
  88. package/transform/compiler/src/render3/util.js +41 -0
  89. package/transform/compiler/src/render3/view/util.d.ts +30 -0
  90. package/transform/compiler/src/render3/view/util.js +57 -0
  91. package/transform/compiler-cli/src/ngtsc/annotations/src/factory.d.ts +11 -0
  92. package/transform/compiler-cli/src/ngtsc/annotations/src/factory.js +21 -0
  93. package/transform/compiler-cli/src/ngtsc/annotations/src/injectable.d.ts +42 -0
  94. package/transform/compiler-cli/src/ngtsc/annotations/src/injectable.js +278 -0
  95. package/transform/compiler-cli/src/ngtsc/annotations/src/util.d.ts +91 -0
  96. package/transform/compiler-cli/src/ngtsc/annotations/src/util.js +370 -0
  97. package/transform/compiler-cli/src/ngtsc/diagnostics/error.d.ts +24 -0
  98. package/transform/compiler-cli/src/ngtsc/diagnostics/error.js +76 -0
  99. package/transform/compiler-cli/src/ngtsc/diagnostics/error_code.d.ts +188 -0
  100. package/transform/compiler-cli/src/ngtsc/diagnostics/error_code.js +206 -0
  101. package/transform/compiler-cli/src/ngtsc/diagnostics/index.d.ts +2 -0
  102. package/transform/compiler-cli/src/ngtsc/diagnostics/index.js +14 -0
  103. package/transform/compiler-cli/src/ngtsc/imports/index.d.ts +1 -0
  104. package/transform/compiler-cli/src/ngtsc/imports/index.js +13 -0
  105. package/transform/compiler-cli/src/ngtsc/imports/src/core.d.ts +36 -0
  106. package/transform/compiler-cli/src/ngtsc/imports/src/core.js +25 -0
  107. package/transform/compiler-cli/src/ngtsc/reflection/index.d.ts +3 -0
  108. package/transform/compiler-cli/src/ngtsc/reflection/index.js +15 -0
  109. package/transform/compiler-cli/src/ngtsc/reflection/src/host.d.ts +667 -0
  110. package/transform/compiler-cli/src/ngtsc/reflection/src/host.js +98 -0
  111. package/transform/compiler-cli/src/ngtsc/reflection/src/type_to_value.d.ts +24 -0
  112. package/transform/compiler-cli/src/ngtsc/reflection/src/type_to_value.js +271 -0
  113. package/transform/compiler-cli/src/ngtsc/reflection/src/typescript.d.ts +50 -0
  114. package/transform/compiler-cli/src/ngtsc/reflection/src/typescript.js +396 -0
  115. package/transform/compiler-cli/src/ngtsc/reflection/src/util.d.ts +12 -0
  116. package/transform/compiler-cli/src/ngtsc/reflection/src/util.js +45 -0
  117. package/transform/compiler-cli/src/ngtsc/transform/index.d.ts +2 -0
  118. package/transform/compiler-cli/src/ngtsc/transform/index.js +14 -0
  119. package/transform/compiler-cli/src/ngtsc/transform/src/api.d.ts +115 -0
  120. package/transform/compiler-cli/src/ngtsc/transform/src/api.js +30 -0
  121. package/transform/compiler-cli/src/ngtsc/transform/src/utils.d.ts +15 -0
  122. package/transform/compiler-cli/src/ngtsc/transform/src/utils.js +84 -0
  123. package/transform/compiler-cli/src/ngtsc/translator/index.d.ts +5 -0
  124. package/transform/compiler-cli/src/ngtsc/translator/index.js +17 -0
  125. package/transform/compiler-cli/src/ngtsc/translator/src/api/ast_factory.d.ts +274 -0
  126. package/transform/compiler-cli/src/ngtsc/translator/src/api/ast_factory.js +9 -0
  127. package/transform/compiler-cli/src/ngtsc/translator/src/api/import_generator.d.ts +27 -0
  128. package/transform/compiler-cli/src/ngtsc/translator/src/api/import_generator.js +9 -0
  129. package/transform/compiler-cli/src/ngtsc/translator/src/context.d.ts +18 -0
  130. package/transform/compiler-cli/src/ngtsc/translator/src/context.js +27 -0
  131. package/transform/compiler-cli/src/ngtsc/translator/src/import_manager.d.ts +29 -0
  132. package/transform/compiler-cli/src/ngtsc/translator/src/import_manager.js +70 -0
  133. package/transform/compiler-cli/src/ngtsc/translator/src/translator.d.ts +58 -0
  134. package/transform/compiler-cli/src/ngtsc/translator/src/translator.js +270 -0
  135. package/transform/compiler-cli/src/ngtsc/translator/src/typescript_ast_factory.d.ts +54 -0
  136. package/transform/compiler-cli/src/ngtsc/translator/src/typescript_ast_factory.js +245 -0
  137. package/transform/compiler-cli/src/ngtsc/translator/src/typescript_translator.d.ts +13 -0
  138. package/transform/compiler-cli/src/ngtsc/translator/src/typescript_translator.js +21 -0
  139. package/transform/compiler-cli/src/ngtsc/util/src/typescript.d.ts +11 -0
  140. package/transform/compiler-cli/src/ngtsc/util/src/typescript.js +39 -0
  141. package/transform/index.d.ts +1 -0
  142. package/transform/index.js +13 -0
  143. package/transform/injectable-transform.d.ts +41 -0
  144. package/transform/injectable-transform.js +195 -0
  145. package/transform/node-Iteration.d.ts +2 -0
  146. package/transform/node-Iteration.js +12 -0
@@ -0,0 +1,9 @@
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 });
@@ -0,0 +1,27 @@
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
+ /**
9
+ * The symbol name and import namespace of an imported symbol,
10
+ * which has been registered through the ImportGenerator.
11
+ */
12
+ export interface NamedImport<TExpression> {
13
+ /** The import namespace containing this imported symbol. */
14
+ moduleImport: TExpression | null;
15
+ /** The (possibly rewritten) name of the imported symbol. */
16
+ symbol: string;
17
+ }
18
+ /**
19
+ * Generate import information based on the context of the code being generated.
20
+ *
21
+ * Implementations of these methods return a specific identifier that corresponds to the imported
22
+ * module.
23
+ */
24
+ export interface ImportGenerator<TExpression> {
25
+ generateNamespaceImport(moduleName: string): TExpression;
26
+ generateNamedImport(moduleName: string, originalSymbol: string): NamedImport<TExpression>;
27
+ }
@@ -0,0 +1,9 @@
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 });
@@ -0,0 +1,18 @@
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
+ /**
9
+ * The current context of a translator visitor as it traverses the AST tree.
10
+ *
11
+ * It tracks whether we are in the process of outputting a statement or an expression.
12
+ */
13
+ export declare class Context {
14
+ readonly isStatement: boolean;
15
+ constructor(isStatement: boolean);
16
+ get withExpressionMode(): Context;
17
+ get withStatementMode(): Context;
18
+ }
@@ -0,0 +1,27 @@
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.Context = void 0;
11
+ /**
12
+ * The current context of a translator visitor as it traverses the AST tree.
13
+ *
14
+ * It tracks whether we are in the process of outputting a statement or an expression.
15
+ */
16
+ class Context {
17
+ constructor(isStatement) {
18
+ this.isStatement = isStatement;
19
+ }
20
+ get withExpressionMode() {
21
+ return this.isStatement ? new Context(false) : this;
22
+ }
23
+ get withStatementMode() {
24
+ return !this.isStatement ? new Context(true) : this;
25
+ }
26
+ }
27
+ exports.Context = Context;
@@ -0,0 +1,29 @@
1
+ /**
2
+ * @license
3
+ * Copyright Google LLC All Rights Reserved.
4
+ *
5
+ * Use of this source code is governed by an MIT-style license that can be
6
+ * found in the LICENSE file at https://angular.io/license
7
+ */
8
+ import * as ts from "typescript";
9
+ import { ImportRewriter } from "../../imports";
10
+ import { ImportGenerator, NamedImport } from "./api/import_generator";
11
+ /**
12
+ * Information about an import that has been added to a module.
13
+ */
14
+ export interface Import {
15
+ /** The name of the module that has been imported. */
16
+ specifier: string;
17
+ /** The `ts.Identifer` by which the imported module is known. */
18
+ qualifier: ts.Identifier;
19
+ }
20
+ export declare class ImportManager implements ImportGenerator<ts.Identifier> {
21
+ protected rewriter: ImportRewriter;
22
+ private prefix;
23
+ private specifierToIdentifier;
24
+ private nextIndex;
25
+ constructor(rewriter?: ImportRewriter, prefix?: string);
26
+ generateNamespaceImport(moduleName: string): ts.Identifier;
27
+ generateNamedImport(moduleName: string, originalSymbol: string): NamedImport<ts.Identifier>;
28
+ getAllImports(contextPath: string): Import[];
29
+ }
@@ -0,0 +1,70 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
11
+ }) : function(o, v) {
12
+ o["default"] = v;
13
+ });
14
+ var __importStar = (this && this.__importStar) || function (mod) {
15
+ if (mod && mod.__esModule) return mod;
16
+ var result = {};
17
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
+ __setModuleDefault(result, mod);
19
+ return result;
20
+ };
21
+ Object.defineProperty(exports, "__esModule", { value: true });
22
+ exports.ImportManager = void 0;
23
+ /**
24
+ * @license
25
+ * Copyright Google LLC All Rights Reserved.
26
+ *
27
+ * Use of this source code is governed by an MIT-style license that can be
28
+ * found in the LICENSE file at https://angular.io/license
29
+ */
30
+ const ts = __importStar(require("typescript"));
31
+ const imports_1 = require("../../imports");
32
+ class ImportManager {
33
+ constructor(rewriter = new imports_1.NoopImportRewriter(), prefix = "i") {
34
+ this.rewriter = rewriter;
35
+ this.prefix = prefix;
36
+ this.specifierToIdentifier = new Map();
37
+ this.nextIndex = 0;
38
+ }
39
+ generateNamespaceImport(moduleName) {
40
+ if (!this.specifierToIdentifier.has(moduleName)) {
41
+ this.specifierToIdentifier.set(moduleName, ts.createIdentifier(`${this.prefix}${this.nextIndex++}`));
42
+ }
43
+ return this.specifierToIdentifier.get(moduleName);
44
+ }
45
+ generateNamedImport(moduleName, originalSymbol) {
46
+ // First, rewrite the symbol name.
47
+ const symbol = this.rewriter.rewriteSymbol(originalSymbol, moduleName);
48
+ // Ask the rewriter if this symbol should be imported at all. If not, it can be referenced
49
+ // directly (moduleImport: null).
50
+ if (!this.rewriter.shouldImportSymbol(symbol, moduleName)) {
51
+ // The symbol should be referenced directly.
52
+ return { moduleImport: null, symbol };
53
+ }
54
+ // If not, this symbol will be imported using a generated namespace import.
55
+ const moduleImport = this.generateNamespaceImport(moduleName);
56
+ return { moduleImport, symbol };
57
+ }
58
+ getAllImports(contextPath) {
59
+ const imports = [];
60
+ for (const [originalSpecifier, qualifier] of this.specifierToIdentifier) {
61
+ const specifier = this.rewriter.rewriteSpecifier(originalSpecifier, contextPath);
62
+ imports.push({
63
+ specifier,
64
+ qualifier,
65
+ });
66
+ }
67
+ return imports;
68
+ }
69
+ }
70
+ exports.ImportManager = ImportManager;
@@ -0,0 +1,58 @@
1
+ /**
2
+ * @license
3
+ * Copyright Google LLC All Rights Reserved.
4
+ *
5
+ * Use of this source code is governed by an MIT-style license that can be
6
+ * found in the LICENSE file at https://angular.io/license
7
+ */
8
+ import * as o from '../../../../../compiler';
9
+ import { AstFactory } from './api/ast_factory';
10
+ import { ImportGenerator } from './api/import_generator';
11
+ import { Context } from './context';
12
+ export declare type RecordWrappedNodeFn<TExpression> = (node: o.WrappedNodeExpr<TExpression>) => void;
13
+ export interface TranslatorOptions<TExpression> {
14
+ downlevelTaggedTemplates?: boolean;
15
+ downlevelVariableDeclarations?: boolean;
16
+ recordWrappedNode?: RecordWrappedNodeFn<TExpression>;
17
+ annotateForClosureCompiler?: boolean;
18
+ }
19
+ export declare class ExpressionTranslatorVisitor<TStatement, TExpression> implements o.ExpressionVisitor, o.StatementVisitor {
20
+ private factory;
21
+ private imports;
22
+ private downlevelTaggedTemplates;
23
+ private downlevelVariableDeclarations;
24
+ private recordWrappedNode;
25
+ constructor(factory: AstFactory<TStatement, TExpression>, imports: ImportGenerator<TExpression>, options: TranslatorOptions<TExpression>);
26
+ visitDeclareVarStmt(stmt: o.DeclareVarStmt, context: Context): TStatement;
27
+ visitDeclareFunctionStmt(stmt: o.DeclareFunctionStmt, context: Context): TStatement;
28
+ visitExpressionStmt(stmt: o.ExpressionStatement, context: Context): TStatement;
29
+ visitReturnStmt(stmt: o.ReturnStatement, context: Context): TStatement;
30
+ visitDeclareClassStmt(_stmt: o.ClassStmt, _context: Context): never;
31
+ visitIfStmt(stmt: o.IfStmt, context: Context): TStatement;
32
+ visitReadVarExpr(ast: o.ReadVarExpr, _context: Context): TExpression;
33
+ visitWriteVarExpr(expr: o.WriteVarExpr, context: Context): TExpression;
34
+ visitWriteKeyExpr(expr: o.WriteKeyExpr, context: Context): TExpression;
35
+ visitWritePropExpr(expr: o.WritePropExpr, context: Context): TExpression;
36
+ visitInvokeMethodExpr(ast: o.InvokeMethodExpr, context: Context): TExpression;
37
+ visitInvokeFunctionExpr(ast: o.InvokeFunctionExpr, context: Context): TExpression;
38
+ visitInstantiateExpr(ast: o.InstantiateExpr, context: Context): TExpression;
39
+ visitLiteralExpr(ast: o.LiteralExpr, _context: Context): TExpression;
40
+ visitExternalExpr(ast: o.ExternalExpr, _context: Context): TExpression;
41
+ visitConditionalExpr(ast: o.ConditionalExpr, context: Context): TExpression;
42
+ visitNotExpr(ast: o.NotExpr, context: Context): TExpression;
43
+ visitAssertNotNullExpr(ast: o.AssertNotNull, context: Context): TExpression;
44
+ visitCastExpr(ast: o.CastExpr, context: Context): TExpression;
45
+ visitFunctionExpr(ast: o.FunctionExpr, context: Context): TExpression;
46
+ visitBinaryOperatorExpr(ast: o.BinaryOperatorExpr, context: Context): TExpression;
47
+ visitReadPropExpr(ast: o.ReadPropExpr, context: Context): TExpression;
48
+ visitReadKeyExpr(ast: o.ReadKeyExpr, context: Context): TExpression;
49
+ visitLiteralArrayExpr(ast: o.LiteralArrayExpr, context: Context): TExpression;
50
+ visitLiteralMapExpr(ast: o.LiteralMapExpr, context: Context): TExpression;
51
+ visitCommaExpr(ast: o.CommaExpr, context: Context): never;
52
+ visitWrappedNodeExpr(ast: o.WrappedNodeExpr<any>, _context: Context): any;
53
+ visitTypeofExpr(ast: o.TypeofExpr, context: Context): TExpression;
54
+ visitUnaryOperatorExpr(ast: o.UnaryOperatorExpr, context: Context): TExpression;
55
+ private visitStatements;
56
+ private setSourceMapRange;
57
+ private attachComments;
58
+ }
@@ -0,0 +1,270 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
11
+ }) : function(o, v) {
12
+ o["default"] = v;
13
+ });
14
+ var __importStar = (this && this.__importStar) || function (mod) {
15
+ if (mod && mod.__esModule) return mod;
16
+ var result = {};
17
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
+ __setModuleDefault(result, mod);
19
+ return result;
20
+ };
21
+ Object.defineProperty(exports, "__esModule", { value: true });
22
+ exports.ExpressionTranslatorVisitor = void 0;
23
+ /**
24
+ * @license
25
+ * Copyright Google LLC All Rights Reserved.
26
+ *
27
+ * Use of this source code is governed by an MIT-style license that can be
28
+ * found in the LICENSE file at https://angular.io/license
29
+ */
30
+ const o = __importStar(require("../../../../../compiler"));
31
+ const UNARY_OPERATORS = new Map([
32
+ [o.UnaryOperator.Minus, '-'],
33
+ [o.UnaryOperator.Plus, '+'],
34
+ ]);
35
+ const BINARY_OPERATORS = new Map([
36
+ [o.BinaryOperator.And, '&&'],
37
+ [o.BinaryOperator.Bigger, '>'],
38
+ [o.BinaryOperator.BiggerEquals, '>='],
39
+ [o.BinaryOperator.BitwiseAnd, '&'],
40
+ [o.BinaryOperator.Divide, '/'],
41
+ [o.BinaryOperator.Equals, '=='],
42
+ [o.BinaryOperator.Identical, '==='],
43
+ [o.BinaryOperator.Lower, '<'],
44
+ [o.BinaryOperator.LowerEquals, '<='],
45
+ [o.BinaryOperator.Minus, '-'],
46
+ [o.BinaryOperator.Modulo, '%'],
47
+ [o.BinaryOperator.Multiply, '*'],
48
+ [o.BinaryOperator.NotEquals, '!='],
49
+ [o.BinaryOperator.NotIdentical, '!=='],
50
+ [o.BinaryOperator.Or, '||'],
51
+ [o.BinaryOperator.Plus, '+'],
52
+ [o.BinaryOperator.NullishCoalesce, '??'],
53
+ ]);
54
+ class ExpressionTranslatorVisitor {
55
+ constructor(factory, imports, options) {
56
+ this.factory = factory;
57
+ this.imports = imports;
58
+ this.downlevelTaggedTemplates = options.downlevelTaggedTemplates === true;
59
+ this.downlevelVariableDeclarations =
60
+ options.downlevelVariableDeclarations === true;
61
+ this.recordWrappedNode = options.recordWrappedNode || (() => { });
62
+ }
63
+ visitDeclareVarStmt(stmt, context) {
64
+ var _a;
65
+ const varType = this.downlevelVariableDeclarations
66
+ ? 'var'
67
+ : stmt.hasModifier(o.StmtModifier.Final)
68
+ ? 'const'
69
+ : 'let';
70
+ return this.attachComments(this.factory.createVariableDeclaration(stmt.name, (_a = stmt.value) === null || _a === void 0 ? void 0 : _a.visitExpression(this, context.withExpressionMode), varType), stmt.leadingComments);
71
+ }
72
+ visitDeclareFunctionStmt(stmt, context) {
73
+ return this.attachComments(this.factory.createFunctionDeclaration(stmt.name, stmt.params.map((param) => param.name), this.factory.createBlock(this.visitStatements(stmt.statements, context.withStatementMode))), stmt.leadingComments);
74
+ }
75
+ visitExpressionStmt(stmt, context) {
76
+ return this.attachComments(this.factory.createExpressionStatement(stmt.expr.visitExpression(this, context.withStatementMode)), stmt.leadingComments);
77
+ }
78
+ visitReturnStmt(stmt, context) {
79
+ return this.attachComments(this.factory.createReturnStatement(stmt.value.visitExpression(this, context.withExpressionMode)), stmt.leadingComments);
80
+ }
81
+ visitDeclareClassStmt(_stmt, _context) {
82
+ throw new Error('Method not implemented.');
83
+ }
84
+ visitIfStmt(stmt, context) {
85
+ return this.attachComments(this.factory.createIfStatement(stmt.condition.visitExpression(this, context), this.factory.createBlock(this.visitStatements(stmt.trueCase, context.withStatementMode)), stmt.falseCase.length > 0
86
+ ? this.factory.createBlock(this.visitStatements(stmt.falseCase, context.withStatementMode))
87
+ : null), stmt.leadingComments);
88
+ }
89
+ visitReadVarExpr(ast, _context) {
90
+ const identifier = this.factory.createIdentifier(ast.name);
91
+ this.setSourceMapRange(identifier, ast.sourceSpan);
92
+ return identifier;
93
+ }
94
+ visitWriteVarExpr(expr, context) {
95
+ const assignment = this.factory.createAssignment(this.setSourceMapRange(this.factory.createIdentifier(expr.name), expr.sourceSpan), expr.value.visitExpression(this, context));
96
+ return context.isStatement
97
+ ? assignment
98
+ : this.factory.createParenthesizedExpression(assignment);
99
+ }
100
+ visitWriteKeyExpr(expr, context) {
101
+ const exprContext = context.withExpressionMode;
102
+ const target = this.factory.createElementAccess(expr.receiver.visitExpression(this, exprContext), expr.index.visitExpression(this, exprContext));
103
+ const assignment = this.factory.createAssignment(target, expr.value.visitExpression(this, exprContext));
104
+ return context.isStatement
105
+ ? assignment
106
+ : this.factory.createParenthesizedExpression(assignment);
107
+ }
108
+ visitWritePropExpr(expr, context) {
109
+ const target = this.factory.createPropertyAccess(expr.receiver.visitExpression(this, context), expr.name);
110
+ return this.factory.createAssignment(target, expr.value.visitExpression(this, context));
111
+ }
112
+ visitInvokeMethodExpr(ast, context) {
113
+ const target = ast.receiver.visitExpression(this, context);
114
+ return this.setSourceMapRange(this.factory.createCallExpression(ast.name !== null
115
+ ? this.factory.createPropertyAccess(target, ast.name)
116
+ : target, ast.args.map((arg) => arg.visitExpression(this, context)),
117
+ /* pure */ false), ast.sourceSpan);
118
+ }
119
+ visitInvokeFunctionExpr(ast, context) {
120
+ return this.setSourceMapRange(this.factory.createCallExpression(ast.fn.visitExpression(this, context), ast.args.map((arg) => arg.visitExpression(this, context)), ast.pure), ast.sourceSpan);
121
+ }
122
+ visitInstantiateExpr(ast, context) {
123
+ return this.factory.createNewExpression(ast.classExpr.visitExpression(this, context), ast.args.map((arg) => arg.visitExpression(this, context)));
124
+ }
125
+ visitLiteralExpr(ast, _context) {
126
+ return this.setSourceMapRange(this.factory.createLiteral(ast.value), ast.sourceSpan);
127
+ }
128
+ visitExternalExpr(ast, _context) {
129
+ if (ast.value.name === null) {
130
+ if (ast.value.moduleName === null) {
131
+ throw new Error('Invalid import without name nor moduleName');
132
+ }
133
+ return this.imports.generateNamespaceImport(ast.value.moduleName);
134
+ }
135
+ // If a moduleName is specified, this is a normal import. If there's no module name, it's a
136
+ // reference to a global/ambient symbol.
137
+ if (ast.value.moduleName !== null) {
138
+ // This is a normal import. Find the imported module.
139
+ const { moduleImport, symbol } = this.imports.generateNamedImport(ast.value.moduleName, ast.value.name);
140
+ if (moduleImport === null) {
141
+ // The symbol was ambient after all.
142
+ return this.factory.createIdentifier(symbol);
143
+ }
144
+ else {
145
+ return this.factory.createPropertyAccess(moduleImport, symbol);
146
+ }
147
+ }
148
+ else {
149
+ // The symbol is ambient, so just reference it.
150
+ return this.factory.createIdentifier(ast.value.name);
151
+ }
152
+ }
153
+ visitConditionalExpr(ast, context) {
154
+ let cond = ast.condition.visitExpression(this, context);
155
+ // Ordinarily the ternary operator is right-associative. The following are equivalent:
156
+ // `a ? b : c ? d : e` => `a ? b : (c ? d : e)`
157
+ //
158
+ // However, occasionally Angular needs to produce a left-associative conditional, such as in
159
+ // the case of a null-safe navigation production: `{{a?.b ? c : d}}`. This template produces
160
+ // a ternary of the form:
161
+ // `a == null ? null : rest of expression`
162
+ // If the rest of the expression is also a ternary though, this would produce the form:
163
+ // `a == null ? null : a.b ? c : d`
164
+ // which, if left as right-associative, would be incorrectly associated as:
165
+ // `a == null ? null : (a.b ? c : d)`
166
+ //
167
+ // In such cases, the left-associativity needs to be enforced with parentheses:
168
+ // `(a == null ? null : a.b) ? c : d`
169
+ //
170
+ // Such parentheses could always be included in the condition (guaranteeing correct behavior) in
171
+ // all cases, but this has a code size cost. Instead, parentheses are added only when a
172
+ // conditional expression is directly used as the condition of another.
173
+ //
174
+ // TODO(alxhub): investigate better logic for precendence of conditional operators
175
+ if (ast.condition instanceof o.ConditionalExpr) {
176
+ // The condition of this ternary needs to be wrapped in parentheses to maintain
177
+ // left-associativity.
178
+ cond = this.factory.createParenthesizedExpression(cond);
179
+ }
180
+ return this.factory.createConditional(cond, ast.trueCase.visitExpression(this, context), ast.falseCase.visitExpression(this, context));
181
+ }
182
+ visitNotExpr(ast, context) {
183
+ return this.factory.createUnaryExpression('!', ast.condition.visitExpression(this, context));
184
+ }
185
+ visitAssertNotNullExpr(ast, context) {
186
+ return ast.condition.visitExpression(this, context);
187
+ }
188
+ visitCastExpr(ast, context) {
189
+ return ast.value.visitExpression(this, context);
190
+ }
191
+ visitFunctionExpr(ast, context) {
192
+ var _a;
193
+ return this.factory.createFunctionExpression((_a = ast.name) !== null && _a !== void 0 ? _a : null, ast.params.map((param) => param.name), this.factory.createBlock(this.visitStatements(ast.statements, context)));
194
+ }
195
+ visitBinaryOperatorExpr(ast, context) {
196
+ if (!BINARY_OPERATORS.has(ast.operator)) {
197
+ throw new Error(`Unknown binary operator: ${o.BinaryOperator[ast.operator]}`);
198
+ }
199
+ return this.factory.createBinaryExpression(ast.lhs.visitExpression(this, context), BINARY_OPERATORS.get(ast.operator), ast.rhs.visitExpression(this, context));
200
+ }
201
+ visitReadPropExpr(ast, context) {
202
+ return this.factory.createPropertyAccess(ast.receiver.visitExpression(this, context), ast.name);
203
+ }
204
+ visitReadKeyExpr(ast, context) {
205
+ return this.factory.createElementAccess(ast.receiver.visitExpression(this, context), ast.index.visitExpression(this, context));
206
+ }
207
+ visitLiteralArrayExpr(ast, context) {
208
+ return this.factory.createArrayLiteral(ast.entries.map((expr) => this.setSourceMapRange(expr.visitExpression(this, context), ast.sourceSpan)));
209
+ }
210
+ visitLiteralMapExpr(ast, context) {
211
+ const properties = ast.entries.map((entry) => {
212
+ return {
213
+ propertyName: entry.key,
214
+ quoted: entry.quoted,
215
+ value: entry.value.visitExpression(this, context),
216
+ };
217
+ });
218
+ return this.setSourceMapRange(this.factory.createObjectLiteral(properties), ast.sourceSpan);
219
+ }
220
+ visitCommaExpr(ast, context) {
221
+ throw new Error('Method not implemented.');
222
+ }
223
+ visitWrappedNodeExpr(ast, _context) {
224
+ this.recordWrappedNode(ast);
225
+ return ast.node;
226
+ }
227
+ visitTypeofExpr(ast, context) {
228
+ return this.factory.createTypeOfExpression(ast.expr.visitExpression(this, context));
229
+ }
230
+ visitUnaryOperatorExpr(ast, context) {
231
+ if (!UNARY_OPERATORS.has(ast.operator)) {
232
+ throw new Error(`Unknown unary operator: ${o.UnaryOperator[ast.operator]}`);
233
+ }
234
+ return this.factory.createUnaryExpression(UNARY_OPERATORS.get(ast.operator), ast.expr.visitExpression(this, context));
235
+ }
236
+ visitStatements(statements, context) {
237
+ return statements
238
+ .map((stmt) => stmt.visitStatement(this, context))
239
+ .filter((stmt) => stmt !== undefined);
240
+ }
241
+ setSourceMapRange(ast, span) {
242
+ return this.factory.setSourceMapRange(ast, createRange(span));
243
+ }
244
+ attachComments(statement, leadingComments) {
245
+ if (leadingComments !== undefined) {
246
+ this.factory.attachComments(statement, leadingComments);
247
+ }
248
+ return statement;
249
+ }
250
+ }
251
+ exports.ExpressionTranslatorVisitor = ExpressionTranslatorVisitor;
252
+ /**
253
+ * Convert an OutputAST source-span into a range that can be used by the AST factories.
254
+ */
255
+ function createRange(span) {
256
+ if (span === null) {
257
+ return null;
258
+ }
259
+ const { start, end } = span;
260
+ const { url, content } = start.file;
261
+ if (!url) {
262
+ return null;
263
+ }
264
+ return {
265
+ url,
266
+ content,
267
+ start: { offset: start.offset, line: start.line, column: start.col },
268
+ end: { offset: end.offset, line: end.line, column: end.col },
269
+ };
270
+ }