diff --git a/packages/compiler/src/Compiler.ts b/packages/compiler/src/Compiler.ts index 04e7c3ded..8467339ba 100644 --- a/packages/compiler/src/Compiler.ts +++ b/packages/compiler/src/Compiler.ts @@ -51,14 +51,10 @@ export class Compiler { parseTemplates(sourceFile: ts.SourceFile): ReadonlyArray { const templates: Array = [] - getTaggedTemplateLiteralExpressions(sourceFile).forEach((expression) => { - const tag = expression.tag.getText() - // Only parse html tagged templates - if (tag === "html") { - const literal = expression.template - const [template, parts] = this.parseTemplateFromNode(literal) - templates.push({ literal, template, parts }) - } + getHtmlTags(sourceFile).forEach((expression) => { + const literal = expression.template + const [template, parts] = this.parseTemplateFromNode(literal) + templates.push({ literal, template, parts }) }) return templates.sort(sortParsedTemplates) @@ -95,12 +91,13 @@ export class Compiler { const type = this.project.getType(part) return { index, - kind: this.getPartType(type), + kind: this.getPartType(part, type), type } } - private getPartType(type: ts.Type): ParsedPart["kind"] { + private getPartType(node: ts.Node, type: ts.Type): ParsedPart["kind"] { + if (node.kind === ts.SyntaxKind.TaggedTemplateExpression) return "template" if (this.isPrimitiveType(type)) return "primitive" const properties = type.getProperties().map((p) => p.name) @@ -147,19 +144,18 @@ export interface ParsedTemplate { export interface ParsedPart { readonly index: number - readonly kind: "placeholder" | "fxEffect" | "fx" | "effect" | "primitive" | "directive" + readonly kind: "placeholder" | "fxEffect" | "fx" | "effect" | "primitive" | "directive" | "template" readonly type: ts.Type } -function getTaggedTemplateLiteralExpressions(node: ts.SourceFile) { +function getHtmlTags(node: ts.SourceFile) { const toProcess: Array = node.getChildren() const matches: Array = [] while (toProcess.length) { const node = toProcess.shift()! - - if (node.kind === ts.SyntaxKind.TaggedTemplateExpression) { - matches.push(node as ts.TaggedTemplateExpression) + if (isHtmlTag(node)) { + matches.push(node) } toProcess.push(...node.getChildren()) @@ -167,3 +163,12 @@ function getTaggedTemplateLiteralExpressions(node: ts.SourceFile) { return matches } + +function isHtmlTag(node: ts.Node): node is ts.TaggedTemplateExpression { + if (node.kind === ts.SyntaxKind.TaggedTemplateExpression) { + const expr = node as ts.TaggedTemplateExpression + return expr.tag.getText() === "html" + } + + return false +} diff --git a/packages/compiler/test/index.ts b/packages/compiler/test/index.ts index 1981e04d2..883b4be2d 100644 --- a/packages/compiler/test/index.ts +++ b/packages/compiler/test/index.ts @@ -158,6 +158,7 @@ describe("Compiler", () => { equalTemplates(p.template, expectedP) equalTemplates(div.template, expectedDiv) + equalParts(div.parts, { index: 0, kind: "template" }) }) })