Skip to content

Commit

Permalink
Fix test for extends not at top level and ensure second extends alway…
Browse files Browse the repository at this point in the history
…s triggers an early exception (#2791)
  • Loading branch information
ForbesLindesay authored Apr 26, 2017
1 parent 5c704b2 commit df7d661
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 12 deletions.
4 changes: 2 additions & 2 deletions packages/pug-linker/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ function link(ast) {
var extendsNode = null;
if (ast.nodes.length) {
var hasExtends = ast.nodes[0].type === 'Extends';
checkExtendPosition(ast, hasExtends);
if (hasExtends) {
extendsNode = ast.nodes.shift();
}
checkExtendPosition(ast, hasExtends);
}
ast = applyIncludes(ast);
ast.declaredBlocks = findDeclaredBlocks(ast);
Expand Down Expand Up @@ -164,7 +164,7 @@ function checkExtendPosition(ast, hasExtends) {
if (hasExtends && !legitExtendsReached) {
legitExtendsReached = true;
} else {
error('EXTENDS_NOT_FIRST', 'Declaration of template inheritance ("extends") should be the first thing in the file.', node);
error('EXTENDS_NOT_FIRST', 'Declaration of template inheritance ("extends") should be the first thing in the file. There can only be one extends statement per file.', node);
}
}
});
Expand Down
2 changes: 1 addition & 1 deletion packages/pug-linker/test/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3570,7 +3570,7 @@ exports[`error handling extends-not-first.input.json 1`] = `
Object {
"code": "PUG:EXTENDS_NOT_FIRST",
"line": 4,
"msg": "Declaration of template inheritance (\"extends\") should be the first thing in the file.",
"msg": "Declaration of template inheritance (\"extends\") should be the first thing in the file. There can only be one extends statement per file.",
}
`;
Expand Down
File renamed without changes.
2 changes: 2 additions & 0 deletions packages/pug/test/extends-not-top-level/duplicate.pug
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
extends default
extends default
19 changes: 19 additions & 0 deletions packages/pug/test/extends-not-top-level/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const pug = require('../../');

// regression test for #2404

test('extends not top level should throw an error', () => {
expect(
() => pug.compileFile(
__dirname + '/index.pug'
)
).toThrow('Declaration of template inheritance ("extends") should be the first thing in the file. There can only be one extends statement per file.');
});

test('duplicate extends should throw an error', () => {
expect(
() => pug.compileFile(
__dirname + '/duplicate.pug'
)
).toThrow('Declaration of template inheritance ("extends") should be the first thing in the file. There can only be one extends statement per file.');
});
9 changes: 0 additions & 9 deletions packages/pug/test/pug.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -890,15 +890,6 @@ describe('pug', function(){
it('does not produce warnings for issue-1593', function () {
pug.compileFile(__dirname + '/fixtures/issue-1593/index.pug');
});
it('does throw error for issue-2404', function () {
var message = '';
try {
pug.compileFile(__dirname + '/fixtures/issue-2404/mixin.call.extends.pug');
} catch (e) {
message = e.message;
}
assert(message.indexOf('Declaration of template inheritance ("extends") should be the first thing in the file.') !== -1);
});
it('should support caching (pass 1)', function () {
fs.writeFileSync(__dirname + '/temp/input-compileFile.pug', '.foo bar');
var fn = pug.compileFile(__dirname + '/temp/input-compileFile.pug',
Expand Down

0 comments on commit df7d661

Please sign in to comment.