X Tutup
Skip to content

Commit 6de68e2

Browse files
committed
feat(compiler): assert that Component.style is an array
Part of angular#7481 (effort to improve error messages) Closes angular#7559
1 parent 49527ab commit 6de68e2

File tree

6 files changed

+45
-0
lines changed

6 files changed

+45
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
library angular2.core.util.asserions;
2+
3+
void assertArrayOfStrings(String identifier, Object value) {}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import {isArray, isString, isBlank, assertionsEnabled} from '../facade/lang';
2+
import {BaseException} from '../facade/exceptions';
3+
4+
export function assertArrayOfStrings(identifier: string, value: any) {
5+
if (!assertionsEnabled() || isBlank(value)) {
6+
return;
7+
}
8+
if (!isArray(value)) {
9+
throw new BaseException(`Expected '${identifier}' to be an array of strings.`);
10+
}
11+
for (var i = 0; i < value.length; i += 1) {
12+
if (!isString(value[i])) {
13+
throw new BaseException(`Expected '${identifier}' to be an array of strings.`);
14+
}
15+
}
16+
}

modules/angular2/src/compiler/runtime_metadata.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {reflector} from 'angular2/src/core/reflection/reflection';
2020
import {Injectable, Inject, Optional} from 'angular2/src/core/di';
2121
import {PLATFORM_DIRECTIVES, PLATFORM_PIPES} from 'angular2/src/core/platform_directives_and_pipes';
2222
import {MODULE_SUFFIX} from './util';
23+
import {assertArrayOfStrings} from './assertions';
2324
import {getUrlScheme} from 'angular2/src/compiler/url_resolver';
2425

2526
@Injectable()
@@ -41,9 +42,11 @@ export class RuntimeMetadataResolver {
4142
var changeDetectionStrategy = null;
4243

4344
if (dirMeta instanceof md.ComponentMetadata) {
45+
assertArrayOfStrings('styles', dirMeta.styles);
4446
var cmpMeta = <md.ComponentMetadata>dirMeta;
4547
moduleUrl = calcModuleUrl(directiveType, cmpMeta);
4648
var viewMeta = this._viewResolver.resolve(directiveType);
49+
assertArrayOfStrings('styles', viewMeta.styles);
4750
templateMeta = new cpl.CompileTemplateMetadata({
4851
encapsulation: viewMeta.encapsulation,
4952
template: viewMeta.template,
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
library angular2.test.compiler.runtime_metadata_fixture;
2+
3+
import "package:angular2/core.dart" show Component;
4+
5+
// This component is not actually malformed; this fixture is here to
6+
// make Dart not complain about a missing import for a test case that only
7+
// matters in an JavaScript app.
8+
@Component(template: "")
9+
class MalformedStylesComponent {}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import {Component} from 'angular2/core';
2+
3+
@Component({styles:<any>('foo'), template: ''})
4+
export class MalformedStylesComponent {
5+
}

modules/angular2/test/compiler/runtime_metadata_spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import {TEST_PROVIDERS} from './test_bindings';
3737
import {MODULE_SUFFIX} from 'angular2/src/compiler/util';
3838
import {IS_DART} from 'angular2/src/facade/lang';
3939
import {PLATFORM_DIRECTIVES} from 'angular2/src/core/platform_directives_and_pipes';
40+
import {MalformedStylesComponent} from './runtime_metadata_fixture';
4041

4142
export function main() {
4243
describe('RuntimeMetadataResolver', () => {
@@ -74,6 +75,14 @@ export function main() {
7475
var expectedEndValue = IS_DART ? 'test/compiler/runtime_metadata_spec.dart' : './';
7576
expect(value.endsWith(expectedEndValue)).toBe(true);
7677
}));
78+
79+
it('should throw when metadata is incorrectly typed',
80+
inject([RuntimeMetadataResolver], (resolver: RuntimeMetadataResolver) => {
81+
if (!IS_DART) {
82+
expect(() => resolver.getDirectiveMetadata(MalformedStylesComponent))
83+
.toThrowError(`Expected 'styles' to be an array of strings.`);
84+
}
85+
}));
7786
});
7887

7988
describe('getViewDirectivesMetadata', () => {

0 commit comments

Comments
 (0)
X Tutup