style(compiler-cli): reformat of codebase with new clang-format version (#36520)

This commit reformats the packages/compiler-cli tree using the new version
of clang-format.

PR Close #36520
This commit is contained in:
Alex Rickabaugh
2020-04-07 12:43:43 -07:00
committed by atscott
parent 33eee43263
commit d5aa6b5bd6
205 changed files with 2949 additions and 2122 deletions

View File

@ -9,9 +9,9 @@
import * as ts from 'typescript';
import {MetadataCollector} from '../../src/metadata/collector';
import {ClassMetadata, ConstructorMetadata, METADATA_VERSION, MetadataEntry, MetadataMap, MetadataSymbolicExpression, ModuleMetadata, isClassMetadata, isMetadataGlobalReferenceExpression} from '../../src/metadata/schema';
import {ClassMetadata, ConstructorMetadata, isClassMetadata, isMetadataGlobalReferenceExpression, METADATA_VERSION, MetadataEntry, MetadataMap, MetadataSymbolicExpression, ModuleMetadata} from '../../src/metadata/schema';
import {Directory, Host, expectValidSources} from './typescript.mocks';
import {Directory, expectValidSources, Host} from './typescript.mocks';
describe('Collector', () => {
const documentRegistry = ts.createDocumentRegistry();
@ -40,20 +40,22 @@ describe('Collector', () => {
'interface-reference.ts', 'static-type-check-members.ts',
]);
service = ts.createLanguageService(host, documentRegistry);
program = service.getProgram() !;
program = service.getProgram()!;
collector = new MetadataCollector({quotedNames: true});
});
it('should not have errors in test data', () => { expectValidSources(service, program); });
it('should not have errors in test data', () => {
expectValidSources(service, program);
});
it('should return undefined for modules that have no metadata', () => {
const sourceFile = program.getSourceFile('app/empty.ts') !;
const sourceFile = program.getSourceFile('app/empty.ts')!;
const metadata = collector.getMetadata(sourceFile);
expect(metadata).toBeUndefined();
});
it('should treat all symbols of .d.ts files as exported', () => {
const sourceFile = program.getSourceFile('declarations.d.ts') !;
const sourceFile = program.getSourceFile('declarations.d.ts')!;
const metadata = collector.getMetadata(sourceFile);
expect(metadata).toEqual({
__symbolic: 'module',
@ -66,7 +68,7 @@ describe('Collector', () => {
});
it('should return an interface reference for types', () => {
const sourceFile = program.getSourceFile('/exported-type.ts') !;
const sourceFile = program.getSourceFile('/exported-type.ts')!;
const metadata = collector.getMetadata(sourceFile);
expect(metadata).toEqual({
__symbolic: 'module',
@ -76,7 +78,7 @@ describe('Collector', () => {
});
it('should return an interface reference for interfaces', () => {
const sourceFile = program.getSourceFile('app/hero.ts') !;
const sourceFile = program.getSourceFile('app/hero.ts')!;
const metadata = collector.getMetadata(sourceFile);
expect(metadata).toEqual({
__symbolic: 'module',
@ -86,13 +88,13 @@ describe('Collector', () => {
});
it('should preserve module names from TypeScript sources', () => {
const sourceFile = program.getSourceFile('named-module.d.ts') !;
const sourceFile = program.getSourceFile('named-module.d.ts')!;
const metadata = collector.getMetadata(sourceFile);
expect(metadata !['importAs']).toEqual('some-named-module');
expect(metadata!['importAs']).toEqual('some-named-module');
});
it('should be able to collect a simple component\'s metadata', () => {
const sourceFile = program.getSourceFile('app/hero-detail.component.ts') !;
const sourceFile = program.getSourceFile('app/hero-detail.component.ts')!;
const metadata = collector.getMetadata(sourceFile);
expect(metadata).toEqual({
__symbolic: 'module',
@ -144,7 +146,7 @@ describe('Collector', () => {
});
it('should be able to get a more complicated component\'s metadata', () => {
const sourceFile = program.getSourceFile('/app/app.component.ts') !;
const sourceFile = program.getSourceFile('/app/app.component.ts')!;
const metadata = collector.getMetadata(sourceFile);
expect(metadata).toEqual({
__symbolic: 'module',
@ -236,7 +238,7 @@ describe('Collector', () => {
});
it('should return the values of exported variables', () => {
const sourceFile = program.getSourceFile('/app/mock-heroes.ts') !;
const sourceFile = program.getSourceFile('/app/mock-heroes.ts')!;
const metadata = collector.getMetadata(sourceFile);
expect(metadata).toEqual({
__symbolic: 'module',
@ -262,14 +264,14 @@ describe('Collector', () => {
let casesMetadata: ModuleMetadata;
beforeEach(() => {
casesFile = program.getSourceFile('/app/cases-data.ts') !;
casesMetadata = collector.getMetadata(casesFile) !;
casesFile = program.getSourceFile('/app/cases-data.ts')!;
casesMetadata = collector.getMetadata(casesFile)!;
});
it('should provide any reference for an any ctor parameter type', () => {
const casesAny = <ClassMetadata>casesMetadata.metadata['CaseAny'];
expect(casesAny).toBeTruthy();
const ctorData = casesAny.members !['__ctor__'] as ConstructorMetadata[];
const ctorData = casesAny.members!['__ctor__'] as ConstructorMetadata[];
expect(ctorData).toEqual([{
__symbolic: 'constructor',
parameters: [{__symbolic: 'reference', name: 'any'} as MetadataSymbolicExpression]
@ -300,7 +302,8 @@ describe('Collector', () => {
it('should record references to parameterized types', () => {
const casesForIn = <ClassMetadata>casesMetadata.metadata['NgFor'];
expect(casesForIn).toEqual({
__symbolic: 'class', decorators: [{
__symbolic: 'class',
decorators: [{
__symbolic: 'call',
expression: {
__symbolic: 'reference',
@ -310,21 +313,21 @@ describe('Collector', () => {
character: 7
}
}],
members: {
__ctor__: [{
__symbolic: 'constructor',
parameters: [{
__symbolic: 'reference',
name: 'ClassReference',
arguments: [{__symbolic: 'reference', name: 'NgForRow'}]
}]
}]
}
members: {
__ctor__: [{
__symbolic: 'constructor',
parameters: [{
__symbolic: 'reference',
name: 'ClassReference',
arguments: [{__symbolic: 'reference', name: 'NgForRow'}]
}]
}]
}
} as any as ClassMetadata); // TODO: Review use of `any` here (#19904)
});
it('should report errors for destructured imports', () => {
const unsupported1 = program.getSourceFile('/unsupported-1.ts') !;
const unsupported1 = program.getSourceFile('/unsupported-1.ts')!;
const metadata = collector.getMetadata(unsupported1);
expect(metadata).toEqual({
__symbolic: 'module',
@ -340,11 +343,11 @@ describe('Collector', () => {
});
it('should report an error for references to unexpected types', () => {
const unsupported1 = program.getSourceFile('/unsupported-2.ts') !;
const metadata = collector.getMetadata(unsupported1) !;
const unsupported1 = program.getSourceFile('/unsupported-2.ts')!;
const metadata = collector.getMetadata(unsupported1)!;
const barClass = <ClassMetadata>metadata.metadata['Bar'];
const ctor = <ConstructorMetadata>barClass.members !['__ctor__'][0];
const parameter = ctor.parameters ![0];
const ctor = <ConstructorMetadata>barClass.members!['__ctor__'][0];
const parameter = ctor.parameters![0];
expect(parameter).toEqual({
__symbolic: 'error',
message: 'Reference to non-exported class',
@ -355,18 +358,19 @@ describe('Collector', () => {
});
it('should be able to handle import star type references', () => {
const importStar = program.getSourceFile('/import-star.ts') !;
const metadata = collector.getMetadata(importStar) !;
const importStar = program.getSourceFile('/import-star.ts')!;
const metadata = collector.getMetadata(importStar)!;
const someClass = <ClassMetadata>metadata.metadata['SomeClass'];
const ctor = <ConstructorMetadata>someClass.members !['__ctor__'][0];
const ctor = <ConstructorMetadata>someClass.members!['__ctor__'][0];
const parameters = ctor.parameters;
expect(parameters).toEqual([{
__symbolic: 'reference', module: 'angular2/common', name: 'NgFor', line: 6, character: 29
} as MetadataSymbolicExpression]);
expect(parameters).toEqual([
{__symbolic: 'reference', module: 'angular2/common', name: 'NgFor', line: 6, character: 29} as
MetadataSymbolicExpression
]);
});
it('should record all exported classes', () => {
const sourceFile = program.getSourceFile('/exported-classes.ts') !;
const sourceFile = program.getSourceFile('/exported-classes.ts')!;
const metadata = collector.getMetadata(sourceFile);
expect(metadata).toEqual({
__symbolic: 'module',
@ -380,7 +384,7 @@ describe('Collector', () => {
});
it('should be able to record functions', () => {
const exportedFunctions = program.getSourceFile('/exported-functions.ts') !;
const exportedFunctions = program.getSourceFile('/exported-functions.ts')!;
const metadata = collector.getMetadata(exportedFunctions);
expect(metadata).toEqual({
__symbolic: 'module',
@ -440,26 +444,27 @@ describe('Collector', () => {
});
it('should be able to handle import star type references', () => {
const importStar = program.getSourceFile('/import-star.ts') !;
const metadata = collector.getMetadata(importStar) !;
const importStar = program.getSourceFile('/import-star.ts')!;
const metadata = collector.getMetadata(importStar)!;
const someClass = <ClassMetadata>metadata.metadata['SomeClass'];
const ctor = <ConstructorMetadata>someClass.members !['__ctor__'][0];
const ctor = <ConstructorMetadata>someClass.members!['__ctor__'][0];
const parameters = ctor.parameters;
expect(parameters).toEqual([{
__symbolic: 'reference', module: 'angular2/common', name: 'NgFor', line: 6, character: 29
} as MetadataSymbolicExpression]);
expect(parameters).toEqual([
{__symbolic: 'reference', module: 'angular2/common', name: 'NgFor', line: 6, character: 29} as
MetadataSymbolicExpression
]);
});
it('should be able to collect the value of an enum', () => {
const enumSource = program.getSourceFile('/exported-enum.ts') !;
const metadata = collector.getMetadata(enumSource) !;
const enumSource = program.getSourceFile('/exported-enum.ts')!;
const metadata = collector.getMetadata(enumSource)!;
const someEnum: any = metadata.metadata['SomeEnum'];
expect(someEnum).toEqual({A: 0, B: 1, C: 100, D: 101});
});
it('should ignore a non-export enum', () => {
const enumSource = program.getSourceFile('/private-enum.ts') !;
const metadata = collector.getMetadata(enumSource) !;
const enumSource = program.getSourceFile('/private-enum.ts')!;
const metadata = collector.getMetadata(enumSource)!;
const publicEnum: any = metadata.metadata['PublicEnum'];
const privateEnum: any = metadata.metadata['PrivateEnum'];
expect(publicEnum).toEqual({a: 0, b: 1, c: 2});
@ -467,8 +472,8 @@ describe('Collector', () => {
});
it('should be able to collect enums initialized from consts', () => {
const enumSource = program.getSourceFile('/exported-enum.ts') !;
const metadata = collector.getMetadata(enumSource) !;
const enumSource = program.getSourceFile('/exported-enum.ts')!;
const metadata = collector.getMetadata(enumSource)!;
const complexEnum: any = metadata.metadata['ComplexEnum'];
expect(complexEnum).toEqual({
A: 0,
@ -486,8 +491,8 @@ describe('Collector', () => {
});
it('should be able to collect a simple static method', () => {
const staticSource = program.getSourceFile('/static-method.ts') !;
const metadata = collector.getMetadata(staticSource) !;
const staticSource = program.getSourceFile('/static-method.ts')!;
const metadata = collector.getMetadata(staticSource)!;
expect(metadata).toBeDefined();
const classData = <ClassMetadata>metadata.metadata['MyModule'];
expect(classData).toBeDefined();
@ -504,43 +509,45 @@ describe('Collector', () => {
});
it('should be able to collect a call to a static method', () => {
const staticSource = program.getSourceFile('/static-method-call.ts') !;
const metadata = collector.getMetadata(staticSource) !;
const staticSource = program.getSourceFile('/static-method-call.ts')!;
const metadata = collector.getMetadata(staticSource)!;
expect(metadata).toBeDefined();
const classData = <ClassMetadata>metadata.metadata['Foo'];
expect(classData).toBeDefined();
expect(classData.decorators).toEqual([{
__symbolic: 'call',
expression: {
__symbolic: 'reference',
module: 'angular2/core',
name: 'Component',
line: 4,
character: 5
},
arguments: [{
providers: {
__symbolic: 'call',
expression: {
__symbolic: 'select',
expect(classData.decorators).toEqual([
{
__symbolic: 'call',
expression: {
__symbolic: 'reference',
module: 'angular2/core',
name: 'Component',
line: 4,
character: 5
},
arguments: [{
providers: {
__symbolic: 'call',
expression: {
__symbolic: 'reference',
module: './static-method',
name: 'MyModule',
line: 5,
character: 17
__symbolic: 'select',
expression: {
__symbolic: 'reference',
module: './static-method',
name: 'MyModule',
line: 5,
character: 17
},
member: 'with'
},
member: 'with'
},
arguments: ['a']
}
}]
}] as any as MetadataSymbolicExpression[]); // TODO: Review use of `any` here (#19904)
arguments: ['a']
}
}]
}
] as any as MetadataSymbolicExpression[]); // TODO: Review use of `any` here (#19904)
});
it('should be able to collect a static field', () => {
const staticSource = program.getSourceFile('/static-field.ts') !;
const metadata = collector.getMetadata(staticSource) !;
const staticSource = program.getSourceFile('/static-field.ts')!;
const metadata = collector.getMetadata(staticSource)!;
expect(metadata).toBeDefined();
const classData = <ClassMetadata>metadata.metadata['MyModule'];
expect(classData).toBeDefined();
@ -548,8 +555,8 @@ describe('Collector', () => {
});
it('should ignore static type check members without a value', () => {
const typeCheckMembers = program.getSourceFile('/static-type-check-members.ts') !;
const metadata = collector.getMetadata(typeCheckMembers) !;
const typeCheckMembers = program.getSourceFile('/static-type-check-members.ts')!;
const metadata = collector.getMetadata(typeCheckMembers)!;
const classData = <ClassMetadata>metadata.metadata['MyDirective'];
expect(classData.statics).toEqual({
foo: 'bar',
@ -560,42 +567,44 @@ describe('Collector', () => {
});
it('should be able to collect a reference to a static field', () => {
const staticSource = program.getSourceFile('/static-field-reference.ts') !;
const metadata = collector.getMetadata(staticSource) !;
const staticSource = program.getSourceFile('/static-field-reference.ts')!;
const metadata = collector.getMetadata(staticSource)!;
expect(metadata).toBeDefined();
const classData = <ClassMetadata>metadata.metadata['Foo'];
expect(classData).toBeDefined();
expect(classData.decorators).toEqual([{
__symbolic: 'call',
expression: {
__symbolic: 'reference',
module: 'angular2/core',
name: 'Component',
line: 4,
character: 5
},
arguments: [{
providers: [{
provide: 'a',
useValue: {
__symbolic: 'select',
expression: {
__symbolic: 'reference',
module: './static-field',
name: 'MyModule',
line: 5,
character: 45
},
member: 'VALUE'
}
expect(classData.decorators).toEqual([
{
__symbolic: 'call',
expression: {
__symbolic: 'reference',
module: 'angular2/core',
name: 'Component',
line: 4,
character: 5
},
arguments: [{
providers: [{
provide: 'a',
useValue: {
__symbolic: 'select',
expression: {
__symbolic: 'reference',
module: './static-field',
name: 'MyModule',
line: 5,
character: 45
},
member: 'VALUE'
}
}]
}]
}]
}] as any as MetadataSymbolicExpression[]); // TODO: Review use of `any` here (#19904)
}
] as any as MetadataSymbolicExpression[]); // TODO: Review use of `any` here (#19904)
});
it('should be able to collect a method with a conditional expression', () => {
const source = program.getSourceFile('/static-method-with-if.ts') !;
const metadata = collector.getMetadata(source) !;
const source = program.getSourceFile('/static-method-with-if.ts')!;
const metadata = collector.getMetadata(source)!;
expect(metadata).toBeDefined();
const classData = <ClassMetadata>metadata.metadata['MyModule'];
expect(classData).toBeDefined();
@ -619,8 +628,8 @@ describe('Collector', () => {
});
it('should be able to collect a method with a default parameter', () => {
const source = program.getSourceFile('/static-method-with-default.ts') !;
const metadata = collector.getMetadata(source) !;
const source = program.getSourceFile('/static-method-with-default.ts')!;
const metadata = collector.getMetadata(source)!;
expect(metadata).toBeDefined();
const classData = <ClassMetadata>metadata.metadata['MyModule'];
expect(classData).toBeDefined();
@ -648,8 +657,8 @@ describe('Collector', () => {
});
it('should be able to collect re-exported symbols', () => {
const source = program.getSourceFile('/re-exports.ts') !;
const metadata = collector.getMetadata(source) !;
const source = program.getSourceFile('/re-exports.ts')!;
const metadata = collector.getMetadata(source)!;
expect(metadata.exports).toEqual([
{from: './static-field', export: ['MyModule']},
{from: './static-field-reference', export: [{name: 'Foo', as: 'OtherModule'}]},
@ -658,14 +667,14 @@ describe('Collector', () => {
});
it('should be able to collect a export as symbol', () => {
const source = program.getSourceFile('export-as.d.ts') !;
const metadata = collector.getMetadata(source) !;
const source = program.getSourceFile('export-as.d.ts')!;
const metadata = collector.getMetadata(source)!;
expect(metadata.metadata).toEqual({SomeFunction: {__symbolic: 'function'}});
});
it('should be able to collect exports with no module specifier', () => {
const source = program.getSourceFile('/re-exports-2.ts') !;
const metadata = collector.getMetadata(source) !;
const source = program.getSourceFile('/re-exports-2.ts')!;
const metadata = collector.getMetadata(source)!;
expect(metadata.metadata).toEqual({
MyClass: Object({__symbolic: 'class'}),
OtherModule: {
@ -686,8 +695,8 @@ describe('Collector', () => {
});
it('should collect an error symbol if collecting a reference to a non-exported symbol', () => {
const source = program.getSourceFile('/local-symbol-ref.ts') !;
const metadata = collector.getMetadata(source) !;
const source = program.getSourceFile('/local-symbol-ref.ts')!;
const metadata = collector.getMetadata(source)!;
expect(metadata.metadata).toEqual({
REQUIRED_VALIDATOR: {
__symbolic: 'error',
@ -714,8 +723,8 @@ describe('Collector', () => {
});
it('should collect an error symbol if collecting a reference to a non-exported function', () => {
const source = program.getSourceFile('/local-function-ref.ts') !;
const metadata = collector.getMetadata(source) !;
const source = program.getSourceFile('/local-function-ref.ts')!;
const metadata = collector.getMetadata(source)!;
expect(metadata.metadata).toEqual({
REQUIRED_VALIDATOR: {
__symbolic: 'error',
@ -742,8 +751,8 @@ describe('Collector', () => {
});
it('should collect an error for a simple function that references a local variable', () => {
const source = program.getSourceFile('/local-symbol-ref-func.ts') !;
const metadata = collector.getMetadata(source) !;
const source = program.getSourceFile('/local-symbol-ref-func.ts')!;
const metadata = collector.getMetadata(source)!;
expect(metadata.metadata).toEqual({
foo: {
__symbolic: 'function',
@ -760,8 +769,8 @@ describe('Collector', () => {
});
it('should collect any for interface parameter reference', () => {
const source = program.getSourceFile('/interface-reference.ts') !;
const metadata = collector.getMetadata(source) !;
const source = program.getSourceFile('/interface-reference.ts')!;
const metadata = collector.getMetadata(source)!;
expect((metadata.metadata['SomeClass'] as ClassMetadata).members).toEqual({
__ctor__: [{
__symbolic: 'constructor',
@ -787,11 +796,13 @@ describe('Collector', () => {
return expect(metadata.metadata['value']);
}
it('should be able to collect a raw interpolated string',
() => { e('`simple value`').toBe('simple value'); });
it('should be able to collect a raw interpolated string', () => {
e('`simple value`').toBe('simple value');
});
it('should be able to interpolate a single value',
() => { e('`${foo}`', 'const foo = "foo value"').toBe('foo value'); });
it('should be able to interpolate a single value', () => {
e('`${foo}`', 'const foo = "foo value"').toBe('foo value');
});
it('should be able to interpolate multiple values', () => {
e('`foo:${foo}, bar:${bar}, end`', 'const foo = "foo"; const bar = "bar";')
@ -894,30 +905,30 @@ describe('Collector', () => {
toString(): string { return \`InjectionToken \${this._desc}\`; }
} as any;`,
ts.ScriptTarget.Latest, true);
const metadata = collector.getMetadata(source) !;
const metadata = collector.getMetadata(source)!;
expect(metadata.metadata).toEqual({InjectionToken: {__symbolic: 'class'}});
});
describe('in strict mode', () => {
it('should throw if an error symbol is collecting a reference to a non-exported symbol', () => {
const source = program.getSourceFile('/local-symbol-ref.ts') !;
const source = program.getSourceFile('/local-symbol-ref.ts')!;
expect(() => collector.getMetadata(source, true)).toThrowError(/Reference to a local symbol/);
});
it('should throw if an error if collecting a reference to a non-exported function', () => {
const source = program.getSourceFile('/local-function-ref.ts') !;
const source = program.getSourceFile('/local-function-ref.ts')!;
expect(() => collector.getMetadata(source, true))
.toThrowError(/Reference to a non-exported function/);
});
it('should throw for references to unexpected types', () => {
const unsupported2 = program.getSourceFile('/unsupported-2.ts') !;
const unsupported2 = program.getSourceFile('/unsupported-2.ts')!;
expect(() => collector.getMetadata(unsupported2, true))
.toThrowError(/Reference to non-exported class/);
});
it('should throw for errors in a static method', () => {
const unsupported3 = program.getSourceFile('/unsupported-3.ts') !;
const unsupported3 = program.getSourceFile('/unsupported-3.ts')!;
expect(() => collector.getMetadata(unsupported3, true))
.toThrowError(/Reference to a non-exported class/);
});
@ -927,34 +938,39 @@ describe('Collector', () => {
it('should not throw with a class with no name', () => {
const fileName = '/invalid-class.ts';
override(fileName, 'export class');
const invalidClass = program.getSourceFile(fileName) !;
const invalidClass = program.getSourceFile(fileName)!;
expect(() => collector.getMetadata(invalidClass)).not.toThrow();
});
it('should not throw with a function with no name', () => {
const fileName = '/invalid-function.ts';
override(fileName, 'export function');
const invalidFunction = program.getSourceFile(fileName) !;
const invalidFunction = program.getSourceFile(fileName)!;
expect(() => collector.getMetadata(invalidFunction)).not.toThrow();
});
});
describe('inheritance', () => {
it('should record `extends` clauses for declared classes', () => {
const metadata = collector.getMetadata(program.getSourceFile('/class-inheritance.ts') !) !;
expect(metadata.metadata['DeclaredChildClass'])
describe(
'inheritance', () => {
it('should record `extends` clauses for declared classes',
() => {
const metadata =
collector.getMetadata(program.getSourceFile('/class-inheritance.ts')!)!;
expect(metadata.metadata['DeclaredChildClass'])
.toEqual({__symbolic: 'class', extends: {__symbolic: 'reference', name: 'ParentClass'}});
});
});
it('should record `extends` clauses for classes in the same file', () => {
const metadata = collector.getMetadata(program.getSourceFile('/class-inheritance.ts') !) !;
expect(metadata.metadata['ChildClassSameFile'])
it('should record `extends` clauses for classes in the same file',
() => {
const metadata =
collector.getMetadata(program.getSourceFile('/class-inheritance.ts')!)!;
expect(metadata.metadata['ChildClassSameFile'])
.toEqual({__symbolic: 'class', extends: {__symbolic: 'reference', name: 'ParentClass'}});
});
});
it('should record `extends` clauses for classes in a different file', () => {
const metadata = collector.getMetadata(program.getSourceFile('/class-inheritance.ts') !) !;
expect(metadata.metadata['ChildClassOtherFile']).toEqual({
it('should record `extends` clauses for classes in a different file', () => {
const metadata = collector.getMetadata(program.getSourceFile('/class-inheritance.ts')!)!;
expect(metadata.metadata['ChildClassOtherFile']).toEqual({
__symbolic: 'class',
extends: {
__symbolic: 'reference',
@ -964,29 +980,29 @@ describe('Collector', () => {
character: 45,
}
});
});
});
function expectClass(entry: MetadataEntry): entry is ClassMetadata {
const result = isClassMetadata(entry);
expect(result).toBeTruthy();
return result;
}
function expectClass(entry: MetadataEntry): entry is ClassMetadata {
const result = isClassMetadata(entry);
expect(result).toBeTruthy();
return result;
}
it('should collect the correct arity for a class', () => {
const metadata = collector.getMetadata(program.getSourceFile('/class-arity.ts') !) !;
it('should collect the correct arity for a class', () => {
const metadata = collector.getMetadata(program.getSourceFile('/class-arity.ts')!)!;
const zero = metadata.metadata['Zero'];
if (expectClass(zero)) expect(zero.arity).toBeUndefined();
const one = metadata.metadata['One'];
if (expectClass(one)) expect(one.arity).toBe(1);
const two = metadata.metadata['Two'];
if (expectClass(two)) expect(two.arity).toBe(2);
const three = metadata.metadata['Three'];
if (expectClass(three)) expect(three.arity).toBe(3);
const nine = metadata.metadata['Nine'];
if (expectClass(nine)) expect(nine.arity).toBe(9);
});
});
const zero = metadata.metadata['Zero'];
if (expectClass(zero)) expect(zero.arity).toBeUndefined();
const one = metadata.metadata['One'];
if (expectClass(one)) expect(one.arity).toBe(1);
const two = metadata.metadata['Two'];
if (expectClass(two)) expect(two.arity).toBe(2);
const three = metadata.metadata['Three'];
if (expectClass(three)) expect(three.arity).toBe(3);
const nine = metadata.metadata['Nine'];
if (expectClass(nine)) expect(nine.arity).toBe(9);
});
});
describe('regression', () => {
it('should be able to collect a short-hand property value', () => {
@ -1053,11 +1069,12 @@ describe('Collector', () => {
expect((metadata.metadata.MyIf as any).statics.typeGuard)
.not.toBeUndefined('typeGuard was not collected');
});
});
describe('references', () => {
beforeEach(() => { collector = new MetadataCollector({quotedNames: true}); });
beforeEach(() => {
collector = new MetadataCollector({quotedNames: true});
});
it('should record a reference to an exported field of a useValue', () => {
const metadata = collectSource(`
@ -1113,13 +1130,13 @@ describe('Collector', () => {
}
return value;
});
expect(metadata !.metadata['a']).toEqual({__symbolic: 'reference', name: lambdaTemp});
expect(metadata!.metadata['a']).toEqual({__symbolic: 'reference', name: lambdaTemp});
});
it('should compose substitution functions', () => {
const collector = new MetadataCollector({
substituteExpression: (value, node) => isMetadataGlobalReferenceExpression(value) &&
value.name == lambdaTemp ?
substituteExpression: (value, node) =>
isMetadataGlobalReferenceExpression(value) && value.name == lambdaTemp ?
{__symbolic: 'reference', name: value.name + '2'} :
value
});
@ -1133,19 +1150,19 @@ describe('Collector', () => {
}
return value;
});
expect(metadata !.metadata['a']).toEqual({__symbolic: 'reference', name: lambdaTemp + '2'});
expect(metadata!.metadata['a']).toEqual({__symbolic: 'reference', name: lambdaTemp + '2'});
});
});
function override(fileName: string, content: string) {
host.overrideFile(fileName, content);
host.addFile(fileName);
program = service.getProgram() !;
program = service.getProgram()!;
}
function collectSource(content: string): ModuleMetadata {
const sourceFile = createSource(content);
return collector.getMetadata(sourceFile) !;
return collector.getMetadata(sourceFile)!;
}
});