Skip to content

Commit

Permalink
Remove named blocks from included templates that extend
Browse files Browse the repository at this point in the history
[fixes #2436]
  • Loading branch information
ForbesLindesay committed May 1, 2017
1 parent bb8efc6 commit 11f1b8d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 180 deletions.
17 changes: 16 additions & 1 deletion packages/pug-linker/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ function link(ast) {
parent.declaredBlocks[name] = ast.declaredBlocks[name];
});
parent.nodes = mixins.concat(parent.nodes);
parent.hasExtends = true;
return parent;
}
return ast;
Expand Down Expand Up @@ -123,7 +124,21 @@ function applyIncludes(ast, child) {
}
}, function after(node, replace) {
if (node.type === 'Include') {
replace(applyYield(link(node.file.ast), node.block));
var childAST = link(node.file.ast);
if (childAST.hasExtends) {
childAST = removeBlocks(childAST);
}
replace(applyYield(childAST, node.block));
}
});
}
function removeBlocks(ast) {
return walk(ast, function (node, replace) {
if (node.type === 'NamedBlock') {
replace({
type: 'Block',
nodes: node.nodes
});
}
});
}
Expand Down
200 changes: 21 additions & 179 deletions packages/pug-linker/test/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -370,60 +370,7 @@ Object {

exports[`cases from pug include-extends-from-root.input.json 1`] = `
Object {
"declaredBlocks": Object {
"content": Array [
Object {
"filename": "auxiliary/layout.pug",
"line": 6,
"mode": "replace",
"name": "content",
"nodes": Array [
Object {
"declaredBlocks": Object {},
"filename": "auxiliary/include-from-root.pug",
"line": 0,
"nodes": Array [
Object {
"attributeBlocks": Array [],
"attrs": Array [],
"block": Object {
"filename": "auxiliary/include-from-root.pug",
"line": 1,
"nodes": Array [
Object {
"filename": "auxiliary/include-from-root.pug",
"line": 1,
"type": "Text",
"val": "hello",
},
],
"type": "Block",
},
"filename": "auxiliary/include-from-root.pug",
"isInline": false,
"line": 1,
"name": "h1",
"selfClosing": false,
"type": "Tag",
},
],
"type": "Block",
},
],
"type": "NamedBlock",
},
],
"head": Array [
Object {
"filename": "auxiliary/layout.pug",
"line": 4,
"mode": "replace",
"name": "head",
"nodes": Array [],
"type": "NamedBlock",
},
],
},
"declaredBlocks": Object {},
"filename": "include-extends-from-root.pug",
"line": 0,
"nodes": Array [
Expand Down Expand Up @@ -525,6 +472,7 @@ Object {
],
},
"filename": "auxiliary/layout.pug",
"hasExtends": true,
"line": 0,
"nodes": Array [
Object {
Expand Down Expand Up @@ -565,12 +513,8 @@ Object {
"type": "Tag",
},
Object {
"filename": "auxiliary/layout.pug",
"line": 4,
"mode": "replace",
"name": "head",
"nodes": Array [],
"type": "NamedBlock",
"type": "Block",
},
],
"type": "Block",
Expand All @@ -590,10 +534,6 @@ Object {
"line": 5,
"nodes": Array [
Object {
"filename": "auxiliary/layout.pug",
"line": 6,
"mode": "replace",
"name": "content",
"nodes": Array [
Object {
"declaredBlocks": Object {},
Expand Down Expand Up @@ -627,7 +567,7 @@ Object {
"type": "Block",
},
],
"type": "NamedBlock",
"type": "Block",
},
],
"type": "Block",
Expand Down Expand Up @@ -659,74 +599,7 @@ Object {

exports[`cases from pug include-extends-of-common-template.input.json 1`] = `
Object {
"declaredBlocks": Object {
"test": Array [
Object {
"filename": "auxiliary/empty-block.pug",
"line": 1,
"mode": "replace",
"name": "test",
"nodes": Array [
Object {
"attributeBlocks": Array [],
"attrs": Array [],
"block": Object {
"filename": "auxiliary/extends-empty-block-1.pug",
"line": 4,
"nodes": Array [
Object {
"filename": "auxiliary/extends-empty-block-1.pug",
"line": 4,
"type": "Text",
"val": "test1",
},
],
"type": "Block",
},
"filename": "auxiliary/extends-empty-block-1.pug",
"isInline": false,
"line": 4,
"name": "div",
"selfClosing": false,
"type": "Tag",
},
],
"type": "NamedBlock",
},
Object {
"filename": "auxiliary/empty-block.pug",
"line": 1,
"mode": "replace",
"name": "test",
"nodes": Array [
Object {
"attributeBlocks": Array [],
"attrs": Array [],
"block": Object {
"filename": "auxiliary/extends-empty-block-2.pug",
"line": 4,
"nodes": Array [
Object {
"filename": "auxiliary/extends-empty-block-2.pug",
"line": 4,
"type": "Text",
"val": "test2",
},
],
"type": "Block",
},
"filename": "auxiliary/extends-empty-block-2.pug",
"isInline": false,
"line": 4,
"name": "div",
"selfClosing": false,
"type": "Tag",
},
],
"type": "NamedBlock",
},
],
},
"declaredBlocks": Object {},
"filename": "include-extends-of-common-template.pug",
"line": 0,
"nodes": Array [
Expand Down Expand Up @@ -802,13 +675,10 @@ Object {
],
},
"filename": "auxiliary/empty-block.pug",
"hasExtends": true,
"line": 0,
"nodes": Array [
Object {
"filename": "auxiliary/empty-block.pug",
"line": 1,
"mode": "replace",
"name": "test",
"nodes": Array [
Object {
"attributeBlocks": Array [],
Expand All @@ -834,7 +704,7 @@ Object {
"type": "Tag",
},
],
"type": "NamedBlock",
"type": "Block",
},
],
"type": "Block",
Expand Down Expand Up @@ -911,13 +781,10 @@ Object {
],
},
"filename": "auxiliary/empty-block.pug",
"hasExtends": true,
"line": 0,
"nodes": Array [
Object {
"filename": "auxiliary/empty-block.pug",
"line": 1,
"mode": "replace",
"name": "test",
"nodes": Array [
Object {
"attributeBlocks": Array [],
Expand All @@ -943,7 +810,7 @@ Object {
"type": "Tag",
},
],
"type": "NamedBlock",
"type": "Block",
},
],
"type": "Block",
Expand All @@ -955,33 +822,7 @@ Object {

exports[`cases from pug include-extends-relative.input.json 1`] = `
Object {
"declaredBlocks": Object {
"content": Array [
Object {
"filename": "../cases-src/auxiliary/layout.pug",
"line": 6,
"mode": "replace",
"name": "content",
"nodes": Array [
Object {
"type": "Text",
"val": "h1 hello",
},
],
"type": "NamedBlock",
},
],
"head": Array [
Object {
"filename": "../cases-src/auxiliary/layout.pug",
"line": 4,
"mode": "replace",
"name": "head",
"nodes": Array [],
"type": "NamedBlock",
},
],
},
"declaredBlocks": Object {},
"filename": "include-extends-relative.pug",
"line": 0,
"nodes": Array [
Expand Down Expand Up @@ -1029,6 +870,7 @@ Object {
],
},
"filename": "../cases-src/auxiliary/layout.pug",
"hasExtends": true,
"line": 0,
"nodes": Array [
Object {
Expand Down Expand Up @@ -1069,12 +911,8 @@ Object {
"type": "Tag",
},
Object {
"filename": "../cases-src/auxiliary/layout.pug",
"line": 4,
"mode": "replace",
"name": "head",
"nodes": Array [],
"type": "NamedBlock",
"type": "Block",
},
],
"type": "Block",
Expand All @@ -1094,17 +932,13 @@ Object {
"line": 5,
"nodes": Array [
Object {
"filename": "../cases-src/auxiliary/layout.pug",
"line": 6,
"mode": "replace",
"name": "content",
"nodes": Array [
Object {
"type": "Text",
"val": "h1 hello",
},
],
"type": "NamedBlock",
"type": "Block",
},
],
"type": "Block",
Expand Down Expand Up @@ -1924,6 +1758,7 @@ Object {
],
},
"filename": "../fixtures/append/layout.pug",
"hasExtends": true,
"line": 0,
"nodes": Array [
Object {
Expand Down Expand Up @@ -2228,6 +2063,7 @@ Object {
],
},
"filename": "../fixtures/append-without-block/layout.pug",
"hasExtends": true,
"line": 0,
"nodes": Array [
Object {
Expand Down Expand Up @@ -2694,6 +2530,7 @@ Object {
],
},
"filename": "../fixtures/multi-append-prepend-block/root.pug",
"hasExtends": true,
"line": 0,
"nodes": Array [
Object {
Expand Down Expand Up @@ -3086,6 +2923,7 @@ Object {
],
},
"filename": "../fixtures/prepend/layout.pug",
"hasExtends": true,
"line": 0,
"nodes": Array [
Object {
Expand Down Expand Up @@ -3390,6 +3228,7 @@ Object {
],
},
"filename": "../fixtures/prepend-without-block/layout.pug",
"hasExtends": true,
"line": 0,
"nodes": Array [
Object {
Expand Down Expand Up @@ -3586,6 +3425,7 @@ exports[`special cases extending-empty.input.json 1`] = `
Object {
"declaredBlocks": Object {},
"filename": "../fixtures/empty.pug",
"hasExtends": true,
"line": 0,
"nodes": Array [],
"type": "Block",
Expand Down Expand Up @@ -3681,6 +3521,7 @@ Object {
],
},
"filename": "../fixtures/layout.pug",
"hasExtends": true,
"line": 0,
"nodes": Array [
Object {
Expand Down Expand Up @@ -3990,6 +3831,7 @@ Object {
],
},
"filename": "../fixtures/layout.pug",
"hasExtends": true,
"line": 0,
"nodes": Array [
Object {
Expand Down

0 comments on commit 11f1b8d

Please sign in to comment.