From 631da3ac2761c9ef13ef56654ceb4cbcee76443c Mon Sep 17 00:00:00 2001 From: Dylan Piercey Date: Tue, 1 Mar 2022 09:05:48 -0700 Subject: [PATCH] feat: throw errors when using statement tags if not at concise root --- src/states/TAG_NAME.ts | 26 +++++++++++++++---- .../statement-concise-only/expected.html | 1 + .../statement-concise-only/input.htmljs | 1 + .../statement-root-only/expected.html | 3 +++ .../autotest/statement-root-only/input.htmljs | 2 ++ 5 files changed, 28 insertions(+), 5 deletions(-) create mode 100644 test/autotest/statement-concise-only/expected.html create mode 100644 test/autotest/statement-concise-only/input.htmljs create mode 100644 test/autotest/statement-root-only/expected.html create mode 100644 test/autotest/statement-root-only/input.htmljs diff --git a/src/states/TAG_NAME.ts b/src/states/TAG_NAME.ts index 63cec8ca..27c50ed6 100644 --- a/src/states/TAG_NAME.ts +++ b/src/states/TAG_NAME.ts @@ -80,11 +80,27 @@ export const TAG_NAME: StateDefinition = { if (tagName.expressions.length === 0) { if (this.matchAnyAtPos(tagName, ONLY_OPEN_TAGS)) { tag.openTagOnly = true; - } else if ( - tag.concise && - this.blockStack[0] === tag && - this.matchAnyAtPos(tagName, EXPRESSION_TAGS) - ) { + } else if (this.matchAnyAtPos(tagName, EXPRESSION_TAGS)) { + if (!tag.concise) { + return this.emitError( + tagName, + "RESERVED_TAG_NAME", + `The "${this.read( + tagName + )}" tag is reserved as a statement and cannot be used as an HTML tag.` + ); + } + + if (this.blockStack[0] !== tag) { + return this.emitError( + tagName, + "ROOT_TAG_ONLY", + `The "${this.read( + tagName + )}" statement can only be used at the root of the template.` + ); + } + tag.statement = true; tag.openTagOnly = true; this.enterState(STATE.EXPRESSION, { terminatedByEOL: true }); diff --git a/test/autotest/statement-concise-only/expected.html b/test/autotest/statement-concise-only/expected.html new file mode 100644 index 00000000..7fcb4a05 --- /dev/null +++ b/test/autotest/statement-concise-only/expected.html @@ -0,0 +1 @@ +error:"The \"static\" tag is reserved as a statement and cannot be used as an HTML tag." (code: "RESERVED_TAG_NAME") diff --git a/test/autotest/statement-concise-only/input.htmljs b/test/autotest/statement-concise-only/input.htmljs new file mode 100644 index 00000000..b1ef471c --- /dev/null +++ b/test/autotest/statement-concise-only/input.htmljs @@ -0,0 +1 @@ + diff --git a/test/autotest/statement-root-only/expected.html b/test/autotest/statement-root-only/expected.html new file mode 100644 index 00000000..485d8100 --- /dev/null +++ b/test/autotest/statement-root-only/expected.html @@ -0,0 +1,3 @@ +
+ error:"The \"static\" statement can only be used at the root of the template." (code: "ROOT_TAG_ONLY") +
diff --git a/test/autotest/statement-root-only/input.htmljs b/test/autotest/statement-root-only/input.htmljs new file mode 100644 index 00000000..adc69e09 --- /dev/null +++ b/test/autotest/statement-root-only/input.htmljs @@ -0,0 +1,2 @@ +div + static const x = 1