Skip to content

Commit 2ac8ebd

Browse files
refactor(directive_parser): improve code readability
Closes angular#2876
1 parent 871267d commit 2ac8ebd

1 file changed

Lines changed: 18 additions & 22 deletions

File tree

modules/angular2/src/render/dom/compiler/directive_parser.ts

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
1-
import {
2-
isPresent,
3-
isBlank,
4-
BaseException,
5-
assertionsEnabled,
6-
RegExpWrapper,
7-
StringWrapper
8-
} from 'angular2/src/facade/lang';
1+
import {isPresent, isBlank, BaseException, StringWrapper} from 'angular2/src/facade/lang';
92
import {List, MapWrapper, ListWrapper} from 'angular2/src/facade/collection';
103
import {DOM} from 'angular2/src/dom/dom_adapter';
114
import {Parser} from 'angular2/change_detection';
@@ -18,7 +11,7 @@ import {CompileControl} from './compile_control';
1811

1912
import {DirectiveMetadata} from '../../api';
2013
import {dashCaseToCamelCase, camelCaseToDashCase, EVENT_TARGET_SEPARATOR} from '../util';
21-
import {DirectiveBuilder} from '../view/proto_view_builder';
14+
import {DirectiveBuilder, ElementBinderBuilder} from '../view/proto_view_builder';
2215

2316
/**
2417
* Parses the directives on a single element. Assumes ViewSplitter has already created
@@ -47,36 +40,32 @@ export class DirectiveParser implements CompileStep {
4740
process(parent: CompileElement, current: CompileElement, control: CompileControl) {
4841
var attrs = current.attrs();
4942
var classList = current.classList();
50-
5143
var cssSelector = new CssSelector();
52-
var nodeName = DOM.nodeName(current.element);
53-
cssSelector.setElement(nodeName);
44+
var foundDirectiveIndices = [];
45+
var elementBinder: ElementBinderBuilder = null;
46+
47+
cssSelector.setElement(DOM.nodeName(current.element));
5448
for (var i = 0; i < classList.length; i++) {
5549
cssSelector.addClassName(classList[i]);
5650
}
57-
5851
MapWrapper.forEach(attrs,
5952
(attrValue, attrName) => { cssSelector.addAttribute(attrName, attrValue); });
6053

61-
var componentDirective;
62-
var foundDirectiveIndices = [];
63-
var elementBinder = null;
6454
this._selectorMatcher.match(cssSelector, (selector, directiveIndex) => {
65-
elementBinder = current.bindElement();
6655
var directive = this._directives[directiveIndex];
56+
57+
elementBinder = current.bindElement();
6758
if (directive.type === DirectiveMetadata.COMPONENT_TYPE) {
59+
this._ensureHasOnlyOneComponent(elementBinder, current.elementDescription);
60+
6861
// components need to go first, so it is easier to locate them in the result.
6962
ListWrapper.insert(foundDirectiveIndices, 0, directiveIndex);
70-
if (isPresent(componentDirective)) {
71-
throw new BaseException(
72-
`Only one component directive is allowed per element - check ${current.elementDescription}`);
73-
}
74-
componentDirective = directive;
7563
elementBinder.setComponentId(directive.id);
7664
} else {
7765
foundDirectiveIndices.push(directiveIndex);
7866
}
7967
});
68+
8069
ListWrapper.forEach(foundDirectiveIndices, (directiveIndex) => {
8170
var dirMetadata = this._directives[directiveIndex];
8271
var directiveBinderBuilder = elementBinder.bindDirective(directiveIndex);
@@ -108,6 +97,13 @@ export class DirectiveParser implements CompileStep {
10897
});
10998
}
11099

100+
_ensureHasOnlyOneComponent(elementBinder: ElementBinderBuilder, elDescription: string): void {
101+
if (isPresent(elementBinder.componentId)) {
102+
throw new BaseException(
103+
`Only one component directive is allowed per element - check ${elDescription}`);
104+
}
105+
}
106+
111107
_bindDirectiveProperty(bindConfig: string, compileElement: CompileElement,
112108
directiveBinderBuilder: DirectiveBuilder) {
113109
// Name of the property on the directive

0 commit comments

Comments
 (0)