fix(compiler): don’t lower property accesses of exported symbols (#19301)
E.g. this allows the following to work: ``` @Decorator({ useValue: MyClass.someMethod }) class MyClass { static someMethod() {} } ``` PR Close #19301
This commit is contained in:
parent
62602b9bd8
commit
45747ed531
|
@ -292,6 +292,15 @@ export class LowerMetadataCache implements RequestsMap {
|
|||
};
|
||||
})();
|
||||
|
||||
const isExportedPropertyAccess = (node: ts.Node) => {
|
||||
if (node.kind === ts.SyntaxKind.PropertyAccessExpression) {
|
||||
const pae = node as ts.PropertyAccessExpression;
|
||||
if (isExportedSymbol(pae.expression)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
};
|
||||
const replaceNode = (node: ts.Node) => {
|
||||
const name = freshIdent();
|
||||
requests.set(node.pos, {name, kind: node.kind, location: node.pos, end: node.end});
|
||||
|
@ -306,14 +315,15 @@ export class LowerMetadataCache implements RequestsMap {
|
|||
return replaceNode(node);
|
||||
}
|
||||
if (isLiteralFieldNamed(node, LOWERABLE_FIELD_NAMES) && shouldLower(node) &&
|
||||
!isExportedSymbol(node)) {
|
||||
!isExportedSymbol(node) && !isExportedPropertyAccess(node)) {
|
||||
return replaceNode(node);
|
||||
}
|
||||
}
|
||||
return value;
|
||||
};
|
||||
|
||||
const metadata = this.collector.getMetadata(sourceFile, this.strict, substituteExpression);
|
||||
const metadata = this.collector.getMetadata(
|
||||
sourceFile, this.strict, sourceFile.isDeclarationFile ? undefined : substituteExpression);
|
||||
|
||||
return {metadata, requests};
|
||||
}
|
||||
|
|
|
@ -54,6 +54,20 @@ describe('Expression lowering', () => {
|
|||
.toBeTruthy('did not find the useValue');
|
||||
});
|
||||
|
||||
it('should not request a lowering for useValue with a reference to a static property', () => {
|
||||
const collected = collect(`
|
||||
import {Component} from '@angular/core';
|
||||
|
||||
@Component({
|
||||
provider: [{provide: 'someToken', useValue:◊value: MyClass.someMethod◊}]
|
||||
})
|
||||
export class MyClass {
|
||||
static someMethod() {}
|
||||
}
|
||||
`);
|
||||
expect(collected.requests.size).toBe(0);
|
||||
});
|
||||
|
||||
it('should request a lowering for useFactory', () => {
|
||||
const collected = collect(`
|
||||
import {Component} from '@angular/core';
|
||||
|
|
Loading…
Reference in New Issue