fix(compiler): collect non exported symbols in d.ts files (#19301)

TS treats all symbols in d.ts files as exported,
whether they have an `export` keyword or not.

PR Close #19301
This commit is contained in:
Tobias Bosch 2017-09-20 16:25:08 -07:00 committed by Igor Minar
parent b826e0c636
commit 62602b9bd8
2 changed files with 21 additions and 10 deletions

View File

@ -12,16 +12,7 @@ import {Evaluator, errorSymbol} from './evaluator';
import {ClassMetadata, ConstructorMetadata, FunctionMetadata, InterfaceMetadata, MemberMetadata, MetadataEntry, MetadataError, MetadataMap, MetadataSymbolicBinaryExpression, MetadataSymbolicCallExpression, MetadataSymbolicExpression, MetadataSymbolicIfExpression, MetadataSymbolicIndexExpression, MetadataSymbolicPrefixExpression, MetadataSymbolicReferenceExpression, MetadataSymbolicSelectExpression, MetadataSymbolicSpreadExpression, MetadataValue, MethodMetadata, ModuleExportMetadata, ModuleMetadata, VERSION, isClassMetadata, isConstructorMetadata, isFunctionMetadata, isMetadataError, isMetadataGlobalReferenceExpression, isMetadataSymbolicExpression, isMetadataSymbolicReferenceExpression, isMetadataSymbolicSelectExpression, isMethodMetadata} from './schema';
import {Symbols} from './symbols';
// In TypeScript 2.1 these flags moved
// These helpers work for both 2.0 and 2.1.
const isExport = (ts as any).ModifierFlags ?
((node: ts.Node) =>
!!((ts as any).getCombinedModifierFlags(node) & (ts as any).ModifierFlags.Export)) :
((node: ts.Node) => !!((node.flags & (ts as any).NodeFlags.Export)));
const isStatic = (ts as any).ModifierFlags ?
((node: ts.Node) =>
!!((ts as any).getCombinedModifierFlags(node) & (ts as any).ModifierFlags.Static)) :
((node: ts.Node) => !!((node.flags & (ts as any).NodeFlags.Static)));
const isStatic = (node: ts.Node) => ts.getCombinedModifierFlags(node) & ts.ModifierFlags.Static;
/**
* A set of collector options to use when collecting metadata.
@ -284,6 +275,8 @@ export class MetadataCollector {
}
});
const isExport = (node: ts.Node) =>
sourceFile.isDeclarationFile || ts.getCombinedModifierFlags(node) & ts.ModifierFlags.Export;
const isExportedIdentifier = (identifier?: ts.Identifier) =>
identifier && exportMap.has(identifier.text);
const isExported =

View File

@ -30,6 +30,7 @@ describe('Collector', () => {
'/unsupported-2.ts',
'/unsupported-3.ts',
'class-arity.ts',
'declarations.d.ts',
'import-star.ts',
'exported-classes.ts',
'exported-functions.ts',
@ -67,6 +68,19 @@ describe('Collector', () => {
expect(metadata).toBeUndefined();
});
it('should treat all symbols of .d.ts files as exported', () => {
const sourceFile = program.getSourceFile('declarations.d.ts');
const metadata = collector.getMetadata(sourceFile);
expect(metadata).toEqual({
__symbolic: 'module',
version: 3,
metadata: {
DeclaredClass: {__symbolic: 'class'},
declaredFn: {__symbolic: 'function'},
}
});
});
it('should return an interface reference for types', () => {
const sourceFile = program.getSourceFile('/exported-type.ts');
const metadata = collector.getMetadata(sourceFile);
@ -1237,6 +1251,10 @@ const FILES: Directory = {
constructor(private f: common.NgFor) {}
}
`,
'declarations.d.ts': `
declare class DeclaredClass {}
declare function declaredFn();
`,
'exported-classes.ts': `
export class SimpleClass {}
export abstract class AbstractClass {}