feat(compiler-cli): reflect static methods added to classes in metadata (#21926)
PR Close #21926
This commit is contained in:

committed by
Alex Rickabaugh

parent
1aa2947f70
commit
eb8ddd2983
@ -8,8 +8,9 @@
|
||||
|
||||
import * as ts from 'typescript';
|
||||
|
||||
import {ModuleMetadata} from '../../src/metadata/index';
|
||||
import {LowerMetadataCache, LoweringRequest, RequestLocationMap, getExpressionLoweringTransformFactory} from '../../src/transformers/lower_expressions';
|
||||
import {MetadataCollector, ModuleMetadata} from '../../src/metadata/index';
|
||||
import {LowerMetadataTransform, LoweringRequest, RequestLocationMap, getExpressionLoweringTransformFactory} from '../../src/transformers/lower_expressions';
|
||||
import {MetadataCache} from '../../src/transformers/metadata_cache';
|
||||
import {Directory, MockAotContext, MockCompilerHost} from '../mocks';
|
||||
|
||||
describe('Expression lowering', () => {
|
||||
@ -110,7 +111,8 @@ describe('Expression lowering', () => {
|
||||
});
|
||||
|
||||
it('should throw a validation exception for invalid files', () => {
|
||||
const cache = new LowerMetadataCache({}, /* strict */ true);
|
||||
const cache = new MetadataCache(
|
||||
new MetadataCollector({}), /* strict */ true, [new LowerMetadataTransform()]);
|
||||
const sourceFile = ts.createSourceFile(
|
||||
'foo.ts', `
|
||||
import {Injectable} from '@angular/core';
|
||||
@ -126,7 +128,8 @@ describe('Expression lowering', () => {
|
||||
});
|
||||
|
||||
it('should not report validation errors on a .d.ts file', () => {
|
||||
const cache = new LowerMetadataCache({}, /* strict */ true);
|
||||
const cache = new MetadataCache(
|
||||
new MetadataCollector({}), /* strict */ true, [new LowerMetadataTransform()]);
|
||||
const dtsFile = ts.createSourceFile(
|
||||
'foo.d.ts', `
|
||||
import {Injectable} from '@angular/core';
|
||||
@ -241,11 +244,12 @@ function normalizeResult(result: string): string {
|
||||
|
||||
function collect(annotatedSource: string) {
|
||||
const {annotations, unannotatedSource} = getAnnotations(annotatedSource);
|
||||
const cache = new LowerMetadataCache({});
|
||||
const transformer = new LowerMetadataTransform();
|
||||
const cache = new MetadataCache(new MetadataCollector({}), false, [transformer]);
|
||||
const sourceFile = ts.createSourceFile(
|
||||
'someName.ts', unannotatedSource, ts.ScriptTarget.Latest, /* setParentNodes */ true);
|
||||
return {
|
||||
metadata: cache.getMetadata(sourceFile),
|
||||
requests: cache.getRequests(sourceFile), annotations
|
||||
requests: transformer.getRequests(sourceFile), annotations
|
||||
};
|
||||
}
|
@ -0,0 +1,58 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {ClassField, ClassMethod, ClassStmt, PartialModule, Statement, StmtModifier} from '@angular/compiler';
|
||||
import * as ts from 'typescript';
|
||||
|
||||
import {MetadataCollector, isClassMetadata} from '../../src/metadata/index';
|
||||
import {MetadataCache} from '../../src/transformers/metadata_cache';
|
||||
import {PartialModuleMetadataTransformer} from '../../src/transformers/r3_metadata_transform';
|
||||
|
||||
describe('r3_transform_spec', () => {
|
||||
|
||||
it('should add a static method to collected metadata', () => {
|
||||
const fileName = '/some/directory/someFileName.ts';
|
||||
const className = 'SomeClass';
|
||||
const newFieldName = 'newStaticField';
|
||||
const source = `
|
||||
export class ${className} {
|
||||
myMethod(): void {}
|
||||
}
|
||||
`;
|
||||
|
||||
const sourceFile =
|
||||
ts.createSourceFile(fileName, source, ts.ScriptTarget.Latest, /* setParentNodes */ true);
|
||||
const partialModule: PartialModule = {
|
||||
fileName,
|
||||
statements: [new ClassStmt(
|
||||
className, /* parent */ null, /* fields */[new ClassField(
|
||||
/* name */ newFieldName, /* type */ null, /* modifiers */[StmtModifier.Static])],
|
||||
/* getters */[],
|
||||
/* constructorMethod */ new ClassMethod(/* name */ null, /* params */[], /* body */[]),
|
||||
/* methods */[])]
|
||||
};
|
||||
|
||||
const cache = new MetadataCache(
|
||||
new MetadataCollector(), /* strict */ true,
|
||||
[new PartialModuleMetadataTransformer([partialModule])]);
|
||||
const metadata = cache.getMetadata(sourceFile);
|
||||
expect(metadata).toBeDefined('Expected metadata from test source file');
|
||||
if (metadata) {
|
||||
const classData = metadata.metadata[className];
|
||||
expect(classData && isClassMetadata(classData))
|
||||
.toBeDefined(`Expected metadata to contain data for "${className}"`);
|
||||
if (classData && isClassMetadata(classData)) {
|
||||
const statics = classData.statics;
|
||||
expect(statics).toBeDefined(`Expected "${className}" metadata to contain statics`);
|
||||
if (statics) {
|
||||
expect(statics[newFieldName]).toEqual({}, 'Expected new field to recorded as a function');
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
Reference in New Issue
Block a user