From df7d66119413c16616195ddc3bac3a02505d4cff Mon Sep 17 00:00:00 2001 From: Forbes Lindesay Date: Wed, 26 Apr 2017 20:19:06 +0200 Subject: [PATCH] Fix test for extends not at top level and ensure second extends always triggers an early exception (#2791) --- packages/pug-linker/index.js | 4 ++-- .../test/__snapshots__/index.test.js.snap | 2 +- .../test/extends-not-top-level}/default.pug | 0 .../test/extends-not-top-level/duplicate.pug | 2 ++ .../pug/test/extends-not-top-level/index.pug | 0 .../test/extends-not-top-level/index.test.js | 19 +++++++++++++++++++ packages/pug/test/pug.test.js | 9 --------- 7 files changed, 24 insertions(+), 12 deletions(-) rename {test/fixtures/issue-2404 => packages/pug/test/extends-not-top-level}/default.pug (100%) create mode 100644 packages/pug/test/extends-not-top-level/duplicate.pug rename test/fixtures/issue-2404/mixin.call.extends.pug => packages/pug/test/extends-not-top-level/index.pug (100%) create mode 100644 packages/pug/test/extends-not-top-level/index.test.js diff --git a/packages/pug-linker/index.js b/packages/pug-linker/index.js index 98cba5e4f..7e82003ed 100644 --- a/packages/pug-linker/index.js +++ b/packages/pug-linker/index.js @@ -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); @@ -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); } } }); diff --git a/packages/pug-linker/test/__snapshots__/index.test.js.snap b/packages/pug-linker/test/__snapshots__/index.test.js.snap index 786f13182..a36cdebc8 100644 --- a/packages/pug-linker/test/__snapshots__/index.test.js.snap +++ b/packages/pug-linker/test/__snapshots__/index.test.js.snap @@ -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.", } `; diff --git a/test/fixtures/issue-2404/default.pug b/packages/pug/test/extends-not-top-level/default.pug similarity index 100% rename from test/fixtures/issue-2404/default.pug rename to packages/pug/test/extends-not-top-level/default.pug diff --git a/packages/pug/test/extends-not-top-level/duplicate.pug b/packages/pug/test/extends-not-top-level/duplicate.pug new file mode 100644 index 000000000..3786c4c55 --- /dev/null +++ b/packages/pug/test/extends-not-top-level/duplicate.pug @@ -0,0 +1,2 @@ +extends default +extends default diff --git a/test/fixtures/issue-2404/mixin.call.extends.pug b/packages/pug/test/extends-not-top-level/index.pug similarity index 100% rename from test/fixtures/issue-2404/mixin.call.extends.pug rename to packages/pug/test/extends-not-top-level/index.pug diff --git a/packages/pug/test/extends-not-top-level/index.test.js b/packages/pug/test/extends-not-top-level/index.test.js new file mode 100644 index 000000000..43877a026 --- /dev/null +++ b/packages/pug/test/extends-not-top-level/index.test.js @@ -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.'); +}); diff --git a/packages/pug/test/pug.test.js b/packages/pug/test/pug.test.js index d0dadd1f5..608419d47 100644 --- a/packages/pug/test/pug.test.js +++ b/packages/pug/test/pug.test.js @@ -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',