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 | (function (factory) {
|
---|
9 | if (typeof module === "object" && typeof module.exports === "object") {
|
---|
10 | var v = factory(require, exports);
|
---|
11 | if (v !== undefined) module.exports = v;
|
---|
12 | }
|
---|
13 | else if (typeof define === "function" && define.amd) {
|
---|
14 | define("@angular/core/schematics/migrations/missing-injectable/providers_evaluator", ["require", "exports", "@angular/compiler-cli/src/ngtsc/annotations", "@angular/compiler-cli/src/ngtsc/partial_evaluator/src/interpreter"], factory);
|
---|
15 | }
|
---|
16 | })(function (require, exports) {
|
---|
17 | "use strict";
|
---|
18 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
19 | exports.ProvidersEvaluator = void 0;
|
---|
20 | const annotations_1 = require("@angular/compiler-cli/src/ngtsc/annotations");
|
---|
21 | const interpreter_1 = require("@angular/compiler-cli/src/ngtsc/partial_evaluator/src/interpreter");
|
---|
22 | /**
|
---|
23 | * Providers evaluator that extends the ngtsc static interpreter. This is necessary because
|
---|
24 | * the static interpreter by default only exposes the resolved value, but we are also interested
|
---|
25 | * in the TypeScript nodes that declare providers. It would be possible to manually traverse the
|
---|
26 | * AST to collect these nodes, but that would mean that we need to re-implement the static
|
---|
27 | * interpreter in order to handle all possible scenarios. (e.g. spread operator, function calls,
|
---|
28 | * callee scope). This can be avoided by simply extending the static interpreter and intercepting
|
---|
29 | * the "visitObjectLiteralExpression" method.
|
---|
30 | */
|
---|
31 | class ProvidersEvaluator extends interpreter_1.StaticInterpreter {
|
---|
32 | constructor() {
|
---|
33 | super(...arguments);
|
---|
34 | this._providerLiterals = [];
|
---|
35 | }
|
---|
36 | visitObjectLiteralExpression(node, context) {
|
---|
37 | const resolvedValue = super.visitObjectLiteralExpression(node, Object.assign(Object.assign({}, context), { insideProviderDef: true }));
|
---|
38 | // do not collect nested object literals. e.g. a provider could use a
|
---|
39 | // spread assignment (which resolves to another object literal). In that
|
---|
40 | // case the referenced object literal is not a provider object literal.
|
---|
41 | if (!context.insideProviderDef) {
|
---|
42 | this._providerLiterals.push({ node, resolvedValue });
|
---|
43 | }
|
---|
44 | return resolvedValue;
|
---|
45 | }
|
---|
46 | /**
|
---|
47 | * Evaluates the given expression and returns its statically resolved value
|
---|
48 | * and a list of object literals which define Angular providers.
|
---|
49 | */
|
---|
50 | evaluate(expr) {
|
---|
51 | this._providerLiterals = [];
|
---|
52 | const resolvedValue = this.visit(expr, {
|
---|
53 | originatingFile: expr.getSourceFile(),
|
---|
54 | absoluteModuleName: null,
|
---|
55 | resolutionContext: expr.getSourceFile().fileName,
|
---|
56 | scope: new Map(),
|
---|
57 | foreignFunctionResolver: annotations_1.forwardRefResolver
|
---|
58 | });
|
---|
59 | return { resolvedValue, literals: this._providerLiterals };
|
---|
60 | }
|
---|
61 | }
|
---|
62 | exports.ProvidersEvaluator = ProvidersEvaluator;
|
---|
63 | });
|
---|
64 | //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicHJvdmlkZXJzX2V2YWx1YXRvci5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uLy4uLy4uLy4uLy4uLy4uL3BhY2thZ2VzL2NvcmUvc2NoZW1hdGljcy9taWdyYXRpb25zL21pc3NpbmctaW5qZWN0YWJsZS9wcm92aWRlcnNfZXZhbHVhdG9yLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBOzs7Ozs7R0FNRzs7Ozs7Ozs7Ozs7OztJQUVILDZFQUErRTtJQUUvRSxtR0FBb0c7SUFRcEc7Ozs7Ozs7O09BUUc7SUFDSCxNQUFhLGtCQUFtQixTQUFRLCtCQUFpQjtRQUF6RDs7WUFDVSxzQkFBaUIsR0FBc0IsRUFBRSxDQUFDO1FBNkJwRCxDQUFDO1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