diff --git a/addDependencyTracker.js b/addDependencyTracker.js index 042a3ed7..24417b44 100644 --- a/addDependencyTracker.js +++ b/addDependencyTracker.js @@ -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; @@ -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; diff --git a/node-tests/ast_plugins_test.js b/node-tests/ast_plugins_test.js index 901af352..32bfecc8 100644 --- a/node-tests/ast_plugins_test.js +++ b/node-tests/ast_plugins_test.js @@ -49,6 +49,7 @@ describe('AST plugins', function(){ let rewriterCallCount; function DivRewriterImpl(env) { + let programStackDepth = 0; let rewriter = { name: "test-div-rewriter", tagNameFile: undefined, @@ -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) { diff --git a/node-tests/fixtures/template-with-bom.hbs b/node-tests/fixtures/template-with-bom.hbs index c538b9ac..485ae614 100644 --- a/node-tests/fixtures/template-with-bom.hbs +++ b/node-tests/fixtures/template-with-bom.hbs @@ -1,3 +1,5 @@ 
- {{ name }} + {{#foo-bar}} + {{ name }} + {{/foo-bar}}
diff --git a/node-tests/fixtures/template.hbs b/node-tests/fixtures/template.hbs index fc697a1a..cc0bb261 100644 --- a/node-tests/fixtures/template.hbs +++ b/node-tests/fixtures/template.hbs @@ -1,3 +1,5 @@
- {{ name }} + {{#foo-bar}} + {{ name }} + {{/foo-bar}}