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:
Tobias Bosch 2017-09-20 16:26:11 -07:00 committed by Igor Minar
parent 62602b9bd8
commit 45747ed531
2 changed files with 26 additions and 2 deletions

View File

@ -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 replaceNode = (node: ts.Node) => {
const name = freshIdent(); const name = freshIdent();
requests.set(node.pos, {name, kind: node.kind, location: node.pos, end: node.end}); 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); return replaceNode(node);
} }
if (isLiteralFieldNamed(node, LOWERABLE_FIELD_NAMES) && shouldLower(node) && if (isLiteralFieldNamed(node, LOWERABLE_FIELD_NAMES) && shouldLower(node) &&
!isExportedSymbol(node)) { !isExportedSymbol(node) && !isExportedPropertyAccess(node)) {
return replaceNode(node); return replaceNode(node);
} }
} }
return value; 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}; return {metadata, requests};
} }

View File

@ -54,6 +54,20 @@ describe('Expression lowering', () => {
.toBeTruthy('did not find the useValue'); .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', () => { it('should request a lowering for useFactory', () => {
const collected = collect(` const collected = collect(`
import {Component} from '@angular/core'; import {Component} from '@angular/core';