Skip to content

Commit

Permalink
Optimize the AST transform for top-level elements
Browse files Browse the repository at this point in the history
Don’t bother to convert top-level elements into dynamic calls if they
aren’t the top-level element *of a template*.

Consider a template like this:

```
{{#each posts as |post|}}
<div>hello</div>
{{/each}}
```

The previous version of the transform would convert the nested template
(the <div>) into a dynamic call, because the AST transformation didn’t
differentiate between top-level templates and nested templates. It
didn’t have any other effects (the dynamic call is required to restore
the semantics of the original static form) but it unnecessarily bloats
templates and makes execution slower.
  • Loading branch information
Godhuda committed Aug 11, 2015
1 parent 6f359d2 commit 6424797
Showing 1 changed file with 5 additions and 2 deletions.
Expand Up @@ -13,7 +13,7 @@ function TransformTopLevelComponents() {
TransformTopLevelComponents.prototype.transform = function TransformTopLevelComponents_transform(ast) {
let b = this.syntax.builders;

hasSingleComponentNode(ast.body, component => {
hasSingleComponentNode(ast, component => {
if (component.type === 'ComponentNode') {
component.tag = `@${component.tag}`;
component.isStatic = true;
Expand All @@ -35,7 +35,10 @@ TransformTopLevelComponents.prototype.transform = function TransformTopLevelComp
return ast;
};

function hasSingleComponentNode(body, componentCallback, elementCallback) {
function hasSingleComponentNode(program, componentCallback, elementCallback) {
let { loc, body } = program;
if (!loc || loc.start.line !== 1 || loc.start.column !== 0) { return; }

let lastComponentNode;
let lastIndex;
let nodeCount = 0;
Expand Down

0 comments on commit 6424797

Please sign in to comment.