Skip to content
This repository
Browse code

Merge pull request #177 from dart-lang/issue_174_codegen

fixes #174 - need to have IDs for things that we use query on
  • Loading branch information...
commit 63d100013d774cdcecd9b9dc3551da3bbdb63aef 2 parents c3c089c + d9f002c
sigmundch sigmundch authored

Showing 2 changed files with 38 additions and 11 deletions. Show diff stats Hide diff stats

  1. +31 1 lib/src/analyzer.dart
  2. +7 10 lib/src/info.dart
32 lib/src/analyzer.dart
@@ -88,6 +88,8 @@ class _Analyzer extends TreeVisitor {
88 88 void visitElementInfo(ElementInfo info) {
89 89 var node = info.node;
90 90
  91 + _ensureParentHasQuery(info);
  92 +
91 93 if (node.id != '') info.identifier = '_${toCamelCase(node.id)}';
92 94 if (node.tagName == 'body' || (_currentInfo is ComponentInfo
93 95 && (_currentInfo as ComponentInfo).template == node)) {
@@ -127,7 +129,7 @@ class _Analyzer extends TreeVisitor {
127 129
128 130 _parent = savedParent;
129 131
130   - if (info.needsIdentifier && info.identifier == null) {
  132 + if (info.identifier == null && _needsIdentifier(info)) {
131 133 var id = '__e-$_uniqueId';
132 134 info.identifier = toCamelCase(id);
133 135 // If it's not created in code, we'll query the element by it's id.
@@ -136,6 +138,34 @@ class _Analyzer extends TreeVisitor {
136 138 }
137 139 }
138 140
  141 + /**
  142 + * If this [info] is not created in code, ensure that whichever parent element
  143 + * is created in code has been marked appropriately, so we get an identifier.
  144 + */
  145 + static void _ensureParentHasQuery(ElementInfo info) {
  146 + if (info.isRoot || info.createdInCode) return;
  147 +
  148 + for (var p = info.parent; p != null; p = p.parent) {
  149 + if (p.createdInCode) {
  150 + p.hasQuery = true;
  151 + return;
  152 + }
  153 + }
  154 + }
  155 +
  156 + /**
  157 + * Whether code generators need to create a field to store a reference to this
  158 + * element. This is typically true whenever we need to access the element
  159 + * (e.g. to add event listeners, update values on data-bound watchers, etc).
  160 + */
  161 + static bool _needsIdentifier(ElementInfo info) {
  162 + if (info.isRoot) return false;
  163 +
  164 + return info.hasDataBinding || info.hasIfCondition || info.hasIterate
  165 + || info.hasQuery || info.component != null || info.values.length > 0 ||
  166 + info.events.length > 0;
  167 + }
  168 +
139 169 void _bindExtends(ComponentInfo component) {
140 170 if (component.extendsTag == null) {
141 171 // TODO(jmesserly): is web components spec going to have a default
17 lib/src/info.dart
@@ -340,15 +340,6 @@ class ElementInfo extends NodeInfo<Element> {
340 340 // adding/removing a node updates the parent pointer.
341 341 final List<NodeInfo> children = [];
342 342
343   - /**
344   - * Whether code generators need to create a field to store a reference to this
345   - * element. This is typically true whenever we need to access the element
346   - * (e.g. to add event listeners, update values on data-bound watchers, etc).
347   - */
348   - bool get needsIdentifier => !isRoot &&
349   - (hasDataBinding || hasIfCondition || hasIterate
350   - || component != null || values.length > 0 || events.length > 0);
351   -
352 343 bool get createdInCode => parent != null && parent.childrenCreatedInCode;
353 344
354 345 /**
@@ -366,6 +357,12 @@ class ElementInfo extends NodeInfo<Element> {
366 357 /** Whether this node represents "body" or the shadow root of a component. */
367 358 bool isRoot = false;
368 359
  360 + /**
  361 + * True if some descendant needs to query starting from this element.
  362 + * If this is true, we will generate a variable for this node.
  363 + */
  364 + bool hasQuery = false;
  365 +
369 366 // Note: we're using sorted maps so items are enumerated in a consistent order
370 367 // between runs, resulting in less "diff" in the generated code.
371 368 // TODO(jmesserly): An alternative approach would be to use LinkedHashMap to
@@ -394,12 +391,12 @@ class ElementInfo extends NodeInfo<Element> {
394 391
395 392 String toString() => '#<ElementInfo '
396 393 'identifier: $identifier, '
397   - 'needsIdentifier: $needsIdentifier, '
398 394 'createdInCode: $createdInCode, '
399 395 'component: $component, '
400 396 'hasIterate: $hasIterate, '
401 397 'hasIfCondition: $hasIfCondition, '
402 398 'hasDataBinding: $hasDataBinding, '
  399 + 'hasQuery: $hasQuery, '
403 400 'attributes: $attributes, '
404 401 'events: $events>';
405 402 }

0 comments on commit 63d1000

Please sign in to comment.
Something went wrong with that request. Please try again.