feat(compiler): assert that Component.style is an array

Part of #7481 (effort to improve error messages)

Closes #7559
This commit is contained in:
Brian Ford 2016-03-09 14:55:27 -08:00
parent 49527ab495
commit 6de68e2f1f
6 changed files with 45 additions and 0 deletions

View File

@ -0,0 +1,3 @@
library angular2.core.util.asserions;
void assertArrayOfStrings(String identifier, Object value) {}

View File

@ -0,0 +1,16 @@
import {isArray, isString, isBlank, assertionsEnabled} from '../facade/lang';
import {BaseException} from '../facade/exceptions';
export function assertArrayOfStrings(identifier: string, value: any) {
if (!assertionsEnabled() || isBlank(value)) {
return;
}
if (!isArray(value)) {
throw new BaseException(`Expected '${identifier}' to be an array of strings.`);
}
for (var i = 0; i < value.length; i += 1) {
if (!isString(value[i])) {
throw new BaseException(`Expected '${identifier}' to be an array of strings.`);
}
}
}

View File

@ -20,6 +20,7 @@ import {reflector} from 'angular2/src/core/reflection/reflection';
import {Injectable, Inject, Optional} from 'angular2/src/core/di'; import {Injectable, Inject, Optional} from 'angular2/src/core/di';
import {PLATFORM_DIRECTIVES, PLATFORM_PIPES} from 'angular2/src/core/platform_directives_and_pipes'; import {PLATFORM_DIRECTIVES, PLATFORM_PIPES} from 'angular2/src/core/platform_directives_and_pipes';
import {MODULE_SUFFIX} from './util'; import {MODULE_SUFFIX} from './util';
import {assertArrayOfStrings} from './assertions';
import {getUrlScheme} from 'angular2/src/compiler/url_resolver'; import {getUrlScheme} from 'angular2/src/compiler/url_resolver';
@Injectable() @Injectable()
@ -41,9 +42,11 @@ export class RuntimeMetadataResolver {
var changeDetectionStrategy = null; var changeDetectionStrategy = null;
if (dirMeta instanceof md.ComponentMetadata) { if (dirMeta instanceof md.ComponentMetadata) {
assertArrayOfStrings('styles', dirMeta.styles);
var cmpMeta = <md.ComponentMetadata>dirMeta; var cmpMeta = <md.ComponentMetadata>dirMeta;
moduleUrl = calcModuleUrl(directiveType, cmpMeta); moduleUrl = calcModuleUrl(directiveType, cmpMeta);
var viewMeta = this._viewResolver.resolve(directiveType); var viewMeta = this._viewResolver.resolve(directiveType);
assertArrayOfStrings('styles', viewMeta.styles);
templateMeta = new cpl.CompileTemplateMetadata({ templateMeta = new cpl.CompileTemplateMetadata({
encapsulation: viewMeta.encapsulation, encapsulation: viewMeta.encapsulation,
template: viewMeta.template, template: viewMeta.template,

View File

@ -0,0 +1,9 @@
library angular2.test.compiler.runtime_metadata_fixture;
import "package:angular2/core.dart" show Component;
// This component is not actually malformed; this fixture is here to
// make Dart not complain about a missing import for a test case that only
// matters in an JavaScript app.
@Component(template: "")
class MalformedStylesComponent {}

View File

@ -0,0 +1,5 @@
import {Component} from 'angular2/core';
@Component({styles:<any>('foo'), template: ''})
export class MalformedStylesComponent {
}

View File

@ -37,6 +37,7 @@ import {TEST_PROVIDERS} from './test_bindings';
import {MODULE_SUFFIX} from 'angular2/src/compiler/util'; import {MODULE_SUFFIX} from 'angular2/src/compiler/util';
import {IS_DART} from 'angular2/src/facade/lang'; import {IS_DART} from 'angular2/src/facade/lang';
import {PLATFORM_DIRECTIVES} from 'angular2/src/core/platform_directives_and_pipes'; import {PLATFORM_DIRECTIVES} from 'angular2/src/core/platform_directives_and_pipes';
import {MalformedStylesComponent} from './runtime_metadata_fixture';
export function main() { export function main() {
describe('RuntimeMetadataResolver', () => { describe('RuntimeMetadataResolver', () => {
@ -74,6 +75,14 @@ export function main() {
var expectedEndValue = IS_DART ? 'test/compiler/runtime_metadata_spec.dart' : './'; var expectedEndValue = IS_DART ? 'test/compiler/runtime_metadata_spec.dart' : './';
expect(value.endsWith(expectedEndValue)).toBe(true); expect(value.endsWith(expectedEndValue)).toBe(true);
})); }));
it('should throw when metadata is incorrectly typed',
inject([RuntimeMetadataResolver], (resolver: RuntimeMetadataResolver) => {
if (!IS_DART) {
expect(() => resolver.getDirectiveMetadata(MalformedStylesComponent))
.toThrowError(`Expected 'styles' to be an array of strings.`);
}
}));
}); });
describe('getViewDirectivesMetadata', () => { describe('getViewDirectivesMetadata', () => {