From a6996a9cddf1d7831a662684a8ad00b97ea8469b Mon Sep 17 00:00:00 2001 From: Chuck Jazdzewski Date: Wed, 1 Mar 2017 14:45:00 -0800 Subject: [PATCH] fix(tsc-wrapped): validate metadata in static members of a class (#14772) Fixes #13481 --- tools/@angular/tsc-wrapped/src/collector.ts | 10 ++++++++++ .../tsc-wrapped/test/collector.spec.ts | 20 +++++++++++++++++-- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/tools/@angular/tsc-wrapped/src/collector.ts b/tools/@angular/tsc-wrapped/src/collector.ts index dd4a019087..aed013536c 100644 --- a/tools/@angular/tsc-wrapped/src/collector.ts +++ b/tools/@angular/tsc-wrapped/src/collector.ts @@ -581,6 +581,16 @@ function validateMetadata( Object.getOwnPropertyNames(classData.members) .forEach(name => classData.members[name].forEach((m) => validateMember(classData, m))); } + if (classData.statics) { + Object.getOwnPropertyNames(classData.statics).forEach(name => { + const staticMember = classData.statics[name]; + if (isFunctionMetadata(staticMember)) { + validateExpression(staticMember.value); + } else { + validateExpression(staticMember); + } + }); + } } function validateFunction(functionDeclaration: FunctionMetadata) { diff --git a/tools/@angular/tsc-wrapped/test/collector.spec.ts b/tools/@angular/tsc-wrapped/test/collector.spec.ts index f4dec8aa24..396730aafe 100644 --- a/tools/@angular/tsc-wrapped/test/collector.spec.ts +++ b/tools/@angular/tsc-wrapped/test/collector.spec.ts @@ -28,6 +28,7 @@ describe('Collector', () => { '/promise.ts', '/unsupported-1.ts', '/unsupported-2.ts', + '/unsupported-3.ts', 'class-arity.ts', 'import-star.ts', 'exported-classes.ts', @@ -621,10 +622,16 @@ describe('Collector', () => { }); it('should throw for references to unexpected types', () => { - const unsupported1 = program.getSourceFile('/unsupported-2.ts'); - expect(() => collector.getMetadata(unsupported1, true)) + 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'); + expect(() => collector.getMetadata(unsupported3, true)) + .toThrowError(/Reference to a non-exported class/); + }); }); describe('with invalid input', () => { @@ -911,6 +918,15 @@ const FILES: Directory = { constructor(private f: Foo) {} } `, + 'unsupported-3.ts': ` + class Foo {} + + export class SomeClass { + static someStatic() { + return Foo; + } + } + `, 'import-star.ts': ` import {Injectable} from 'angular2/core'; import * as common from 'angular2/common';