Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit c5e9d0a

Browse filesBrowse files
committed
fix(compiler-cli): compile dep without Array when use in @Injectable parameters (#42987)
if node is not decorator node ,then think maybe is a token node.
1 parent 8ce1ac6 commit c5e9d0a
Copy full SHA for c5e9d0a

File tree

Expand file treeCollapse file tree

2 files changed

+73
-4
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+73
-4
lines changed

‎packages/compiler-cli/src/ngtsc/annotations/src/injectable.ts

Copy file name to clipboardExpand all lines: packages/compiler-cli/src/ngtsc/annotations/src/injectable.ts
+11-4Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -280,10 +280,10 @@ function getDep(dep: ts.Expression, reflector: ReflectionHost): R3DependencyMeta
280280
};
281281

282282
function maybeUpdateDecorator(
283-
dec: ts.Identifier, reflector: ReflectionHost, token?: ts.Expression): void {
283+
dec: ts.Identifier, reflector: ReflectionHost, token?: ts.Expression): boolean {
284284
const source = reflector.getImportOfIdentifier(dec);
285285
if (source === null || source.from !== '@angular/core') {
286-
return;
286+
return false;
287287
}
288288
switch (source.name) {
289289
case 'Inject':
@@ -300,16 +300,23 @@ function getDep(dep: ts.Expression, reflector: ReflectionHost): R3DependencyMeta
300300
case 'Self':
301301
meta.self = true;
302302
break;
303+
default:
304+
return false;
303305
}
306+
return true;
304307
}
305308

306309
if (ts.isArrayLiteralExpression(dep)) {
307310
dep.elements.forEach(el => {
311+
let isDecorator = false;
308312
if (ts.isIdentifier(el)) {
309-
maybeUpdateDecorator(el, reflector);
313+
isDecorator = maybeUpdateDecorator(el, reflector);
310314
} else if (ts.isNewExpression(el) && ts.isIdentifier(el.expression)) {
311315
const token = el.arguments && el.arguments.length > 0 && el.arguments[0] || undefined;
312-
maybeUpdateDecorator(el.expression, reflector, token);
316+
isDecorator = maybeUpdateDecorator(el.expression, reflector, token);
317+
}
318+
if (!isDecorator) {
319+
meta.token = new WrappedNodeExpr(el);
313320
}
314321
});
315322
}

‎packages/compiler-cli/src/ngtsc/annotations/test/injectable_spec.ts

Copy file name to clipboardExpand all lines: packages/compiler-cli/src/ngtsc/annotations/test/injectable_spec.ts
+62Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8+
import {WrappedNodeExpr} from '@angular/compiler';
9+
import * as ts from 'typescript';
810
import {ErrorCode, FatalDiagnosticError, ngErrorCode} from '../../diagnostics';
911
import {absoluteFrom} from '../../file_system';
1012
import {runInEachFileSystem} from '../../file_system/testing';
@@ -43,6 +45,46 @@ runInEachFileSystem(() => {
4345
expect(res).not.toContain(jasmine.objectContaining({name: 'ɵprov'}));
4446
});
4547
});
48+
49+
describe('fix', () => {
50+
it('should compile dep without Array in @Injectable', () => {
51+
const {handler, TestClass, reflectionHost} =
52+
fixHandler(`export const Injectable: any; export const Self: any;`, `
53+
import { Injectable,Self } from '@angular/core';
54+
declare const someToken;
55+
@Injectable({
56+
providedIn: 'any',
57+
useFactory: (token) => new TestClass(token),
58+
deps: [[Self, 'stringToken'], [new Self(), someToken], [1]]
59+
})
60+
export class TestClass {
61+
constructor(token){}
62+
}`);
63+
let detected =
64+
handler.detect(TestClass, reflectionHost.getDecoratorsOfDeclaration(TestClass));
65+
if (detected === undefined) {
66+
throw new Error('Failed to recognize TestClass');
67+
}
68+
let {analysis} = handler.analyze(TestClass, detected.metadata);
69+
if (analysis === undefined) {
70+
throw new Error('Failed to analyze TestClass');
71+
}
72+
if (analysis.meta.deps === undefined) {
73+
throw new Error('Failed to have deps');
74+
}
75+
analysis.meta.deps.forEach((item) => {
76+
let nodeExpr =
77+
item.token! as WrappedNodeExpr<ts.Identifier|ts.StringLiteral|ts.NumericLiteral>;
78+
if (ts.isArrayLiteralExpression(nodeExpr.node)) {
79+
throw new Error('dep can not be ArrayLiteralExpression!');
80+
}
81+
expect(
82+
ts.isStringLiteral(nodeExpr.node) || ts.isIdentifier(nodeExpr.node) ||
83+
ts.isNumericLiteral(nodeExpr.node))
84+
.toBe(true);
85+
});
86+
});
87+
});
4688
});
4789
});
4890

@@ -86,3 +128,23 @@ function setupHandler(errorOnDuplicateProv: boolean) {
86128
}
87129
return {handler, TestClass, ɵprov, analysis};
88130
}
131+
132+
function fixHandler(angularCoreContent: string, entryFileContent: string) {
133+
const ENTRY_FILE = absoluteFrom('/entry.ts');
134+
const ANGULAR_CORE = absoluteFrom('/node_modules/@angular/core/index.d.ts');
135+
const {program} = makeProgram([
136+
{
137+
name: ANGULAR_CORE,
138+
contents: angularCoreContent,
139+
},
140+
{name: ENTRY_FILE, contents: entryFileContent},
141+
]);
142+
const checker = program.getTypeChecker();
143+
const reflectionHost = new TypeScriptReflectionHost(checker);
144+
const injectableRegistry = new InjectableClassRegistry(reflectionHost);
145+
const handler = new InjectableDecoratorHandler(
146+
reflectionHost, false, false, injectableRegistry, NOOP_PERF_RECORDER, false);
147+
const TestClass = getDeclaration(program, ENTRY_FILE, 'TestClass', isNamedClassDeclaration);
148+
149+
return {TestClass, reflectionHost, handler};
150+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.