From e60a24f22208384d1ce9570450bdf83663689a45 Mon Sep 17 00:00:00 2001 From: Samuel Degueldre Date: Mon, 31 Jul 2023 07:41:18 +0200 Subject: [PATCH] better template names --- src/compiler/code_generator.ts | 12 +++++++++++- src/compiler/index.ts | 1 + src/index.ts | 4 +++- src/runtime/component_node.ts | 2 +- src/runtime/template_set.ts | 8 ++++---- tests/helpers.ts | 4 ++-- 6 files changed, 22 insertions(+), 9 deletions(-) diff --git a/src/compiler/code_generator.ts b/src/compiler/code_generator.ts index 3c0d6498..969b182d 100644 --- a/src/compiler/code_generator.ts +++ b/src/compiler/code_generator.ts @@ -43,6 +43,7 @@ export interface Config { export interface CodeGenOptions extends Config { hasSafeContext?: boolean; name?: string; + alias?: string; } // using a non-html document so that HTML serializes as XML instead @@ -197,6 +198,7 @@ class CodeTarget { hasRefWrapper: boolean = false; constructor(name: string, on?: EventHandlers | null) { + debugger this.name = name; this.on = on || null; } @@ -243,6 +245,13 @@ class CodeTarget { } } +function toFnName(name: string = "", alias?: string) { + if (alias) { + name += `_${alias}`; + } + return name.replace(/[^A-Za-z_$0-9]+/g, "_") || "template"; +} + const TRANSLATABLE_ATTRS = ["label", "title", "placeholder", "alt"]; const translationRE = /^(\s*)([\s\S]+?)(\s*)$/; @@ -252,7 +261,7 @@ export class CodeGenerator { hasSafeContext: boolean; isDebug: boolean = false; targets: CodeTarget[] = []; - target = new CodeTarget("template"); + target: CodeTarget; templateName?: string; dev: boolean; translateFn: (s: string) => string; @@ -263,6 +272,7 @@ export class CodeGenerator { helpers: Set = new Set(); constructor(ast: AST, options: CodeGenOptions) { + this.target = new CodeTarget(toFnName(options.name, options.alias)); this.translateFn = options.translateFn || ((s: string) => s); if (options.translatableAttributes) { const attrs = new Set(TRANSLATABLE_ATTRS); diff --git a/src/compiler/index.ts b/src/compiler/index.ts index d79e9534..5d820efd 100644 --- a/src/compiler/index.ts +++ b/src/compiler/index.ts @@ -10,6 +10,7 @@ export type TemplateFunction = (app: TemplateSet, bdom: any, helpers: any) => Te interface CompileOptions extends Config { name?: string; + alias?: string; } export function compile( template: string | Element, diff --git a/src/index.ts b/src/index.ts index 83d84e89..191cda8b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,10 +5,12 @@ export * from "./runtime"; TemplateSet.prototype._compileTemplate = function _compileTemplate( name: string, - template: string | Element + template: string | Element, + alias?: string ) { return compile(template, { name, + alias, dev: this.dev, translateFn: this.translateFn, translatableAttributes: this.translatableAttributes, diff --git a/src/runtime/component_node.ts b/src/runtime/component_node.ts index 98f580a4..d4cdccc4 100644 --- a/src/runtime/component_node.ts +++ b/src/runtime/component_node.ts @@ -116,7 +116,7 @@ export class ComponentNode

implements VNode { + _compileTemplate(name: string, template: string | Element, alias?: string): ReturnType { throw new OwlError(`Unable to compile a template. Please use owl full build instead`); } @@ -135,7 +135,7 @@ export class TemplateSet { export const globalTemplates: { [key: string]: string | Element | TemplateFunction } = {}; export function xml(...args: Parameters) { - const name = `__template__${xml.nextId++}`; + const name = `xml_template_${xml.nextId++}`; const value = String.raw(...args); globalTemplates[name] = value; return name; diff --git a/tests/helpers.ts b/tests/helpers.ts index eaef3d8b..2984765f 100644 --- a/tests/helpers.ts +++ b/tests/helpers.ts @@ -128,8 +128,8 @@ export function snapshotEverything() { }); const originalCompileTemplate = TemplateSet.prototype._compileTemplate; - TemplateSet.prototype._compileTemplate = function (name: string, template: string | Element) { - const fn = originalCompileTemplate.call(this, "", template); + TemplateSet.prototype._compileTemplate = function (name: string, template: string | Element, alias?: string) { + const fn = originalCompileTemplate.call(this, name, template, alias); if (!globalTemplateNames.has(name)) { expect(fn.toString()).toMatchSnapshot(); }