From 452e648848ecaf9c44044005ea57eebd1e29eba5 Mon Sep 17 00:00:00 2001 From: Harminder virk Date: Fri, 17 Apr 2020 23:59:41 +0530 Subject: [PATCH] improvement: raise exception when super is used outside the section tag --- src/Compiler/index.ts | 1 + src/Tags/Super.ts | 14 +++++++++++--- test/compiler.spec.ts | 1 - 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/Compiler/index.ts b/src/Compiler/index.ts index d187e31..3e844a4 100644 --- a/src/Compiler/index.ts +++ b/src/Compiler/index.ts @@ -104,6 +104,7 @@ export class Compiler implements CompilerContract { * Concat children when super was called */ if (extendedNode.children.length && lexerUtils.isTag(extendedNode.children[0], 'super')) { + extendedNode.children.shift() extendedNode.children = node.children.concat(extendedNode.children) } diff --git a/src/Tags/Super.ts b/src/Tags/Super.ts index c555481..a3b1072 100644 --- a/src/Tags/Super.ts +++ b/src/Tags/Super.ts @@ -7,6 +7,7 @@ * file that was distributed with this source code. */ +import { EdgeError } from 'edge-error' import { TagContract } from '../Contracts' /** @@ -21,8 +22,15 @@ export const superTag: TagContract = { seekable: false, tagName: 'super', - compile () { - // The super tag is handled by the compiler itself. I am just a way to - // tell lexer to parse me as an inline node + compile (_, __, token) { + throw new EdgeError( + '@super tag must appear as top level tag inside the @section tag', + 'E_ORPHAN_SUPER_TAG', + { + line: token.loc.start.line, + col: token.loc.start.col, + filename: token.filename, + }, + ) }, } diff --git a/test/compiler.spec.ts b/test/compiler.spec.ts index 6153ea6..4291282 100644 --- a/test/compiler.spec.ts +++ b/test/compiler.spec.ts @@ -530,7 +530,6 @@ test.group('Compiler | Compile', (group) => { try { const fn = compiler.compile('index.edge').template - console.log(fn.toString()) new Function('state', 'ctx', fn)({}, new Context()) } catch (error) { assert.equal(error.message, 'getUserName is not a function')