Skip to content

Commit

Permalink
Templates can have nested Program nodes. Since we can't rely on the T…
Browse files Browse the repository at this point in the history
…emplate node yet, we have to keep a stack counter of Program nodes that we've seen so far.
  • Loading branch information
chriseppstein committed Jun 27, 2019
1 parent 5c9a6d6 commit 2e7e581
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 19 deletions.
21 changes: 16 additions & 5 deletions addDependencyTracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ function addDependencyTracker(plugin, enableInvalidation) {
// we can access.
let lastDependencies = {};
let trackedPlugin = (env) => {
let templateStackDepth = 0;
let realPlugin = plugin(env);
let visitor = realPlugin.visitor;
let origProgram = visitor.Program;
Expand All @@ -31,20 +32,30 @@ function addDependencyTracker(plugin, enableInvalidation) {
origKeys = origProgram.keys;
}
}
// Ideally we'd use visitor.Template but we still support versions of
// handlebars where the template node didn't exist yet. Templates can have
// nested Program nodes. Since we can't rely on the Template node yet, we
// have to keep a stack counter of Program nodes that we've seen so far.
visitor.Program = {
keys: origKeys,
enter: (node) => {
if (realPlugin.resetDependencies) {
realPlugin.resetDependencies(env.meta.moduleName);
templateStackDepth++;
if (templateStackDepth === 1) {
if (realPlugin.resetDependencies) {
realPlugin.resetDependencies(env.meta.moduleName);
}
delete lastDependencies[env.meta.moduleName];
}
delete lastDependencies[env.meta.moduleName];
if (origEnter) origEnter(node);
},
exit: (node) => {
if (realPlugin.dependencies) {
lastDependencies[env.meta.moduleName] = realPlugin.dependencies(env.meta.moduleName);
if (templateStackDepth === 1) {
if (realPlugin.dependencies) {
lastDependencies[env.meta.moduleName] = realPlugin.dependencies(env.meta.moduleName);
}
}
if (origExit) origExit(node)
templateStackDepth--;
}
};
return realPlugin;
Expand Down
33 changes: 21 additions & 12 deletions node-tests/ast_plugins_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ describe('AST plugins', function(){

let rewriterCallCount;
function DivRewriterImpl(env) {
let programStackDepth = 0;
let rewriter = {
name: "test-div-rewriter",
tagNameFile: undefined,
Expand All @@ -61,19 +62,27 @@ describe('AST plugins', function(){
return rewriter.tagNameFile ? [rewriter.tagNameFile] : [];
},
visitor: {
Program() {
let sourceFile = env.meta.moduleName;
let pathInfo = sourceFile && path.parse(sourceFile);
if (pathInfo) {
if (pathInfo.base === "template.hbs") {
rewriterCallCount++;
}
let tagNameFile = input.path(`${pathInfo.name}.tagname`);
if (fs.existsSync(tagNameFile)) {
let tagName = fs.readFileSync(tagNameFile, "utf-8").trim();
rewriter.tagName = tagName;
rewriter.tagNameFile = tagNameFile;
Program: {
enter() {
programStackDepth++;
if (programStackDepth === 1) {
let sourceFile = env.meta.moduleName;
let pathInfo = sourceFile && path.parse(sourceFile);
if (pathInfo) {
if (pathInfo.base === "template.hbs") {
rewriterCallCount++;
}
let tagNameFile = input.path(`${pathInfo.name}.tagname`);
if (fs.existsSync(tagNameFile)) {
let tagName = fs.readFileSync(tagNameFile, "utf-8").trim();
rewriter.tagName = tagName;
rewriter.tagNameFile = tagNameFile;
}
}
}
},
exit() {
programStackDepth--;
}
},
ElementNode(node) {
Expand Down
4 changes: 3 additions & 1 deletion node-tests/fixtures/template-with-bom.hbs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
<div>
{{ name }}
{{#foo-bar}}
{{ name }}
{{/foo-bar}}
</div>
4 changes: 3 additions & 1 deletion node-tests/fixtures/template.hbs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
<div>
{{ name }}
{{#foo-bar}}
{{ name }}
{{/foo-bar}}
</div>

0 comments on commit 2e7e581

Please sign in to comment.