better template names

This commit is contained in:
Samuel Degueldre
2023-07-31 07:41:18 +02:00
parent 9d99f8936b
commit e60a24f222
6 changed files with 22 additions and 9 deletions
+11 -1
View File
@@ -43,6 +43,7 @@ export interface Config {
export interface CodeGenOptions extends Config { export interface CodeGenOptions extends Config {
hasSafeContext?: boolean; hasSafeContext?: boolean;
name?: string; name?: string;
alias?: string;
} }
// using a non-html document so that <inner/outer>HTML serializes as XML instead // using a non-html document so that <inner/outer>HTML serializes as XML instead
@@ -197,6 +198,7 @@ class CodeTarget {
hasRefWrapper: boolean = false; hasRefWrapper: boolean = false;
constructor(name: string, on?: EventHandlers | null) { constructor(name: string, on?: EventHandlers | null) {
debugger
this.name = name; this.name = name;
this.on = on || null; 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 TRANSLATABLE_ATTRS = ["label", "title", "placeholder", "alt"];
const translationRE = /^(\s*)([\s\S]+?)(\s*)$/; const translationRE = /^(\s*)([\s\S]+?)(\s*)$/;
@@ -252,7 +261,7 @@ export class CodeGenerator {
hasSafeContext: boolean; hasSafeContext: boolean;
isDebug: boolean = false; isDebug: boolean = false;
targets: CodeTarget[] = []; targets: CodeTarget[] = [];
target = new CodeTarget("template"); target: CodeTarget;
templateName?: string; templateName?: string;
dev: boolean; dev: boolean;
translateFn: (s: string) => string; translateFn: (s: string) => string;
@@ -263,6 +272,7 @@ export class CodeGenerator {
helpers: Set<string> = new Set(); helpers: Set<string> = new Set();
constructor(ast: AST, options: CodeGenOptions) { constructor(ast: AST, options: CodeGenOptions) {
this.target = new CodeTarget(toFnName(options.name, options.alias));
this.translateFn = options.translateFn || ((s: string) => s); this.translateFn = options.translateFn || ((s: string) => s);
if (options.translatableAttributes) { if (options.translatableAttributes) {
const attrs = new Set(TRANSLATABLE_ATTRS); const attrs = new Set(TRANSLATABLE_ATTRS);
+1
View File
@@ -10,6 +10,7 @@ export type TemplateFunction = (app: TemplateSet, bdom: any, helpers: any) => Te
interface CompileOptions extends Config { interface CompileOptions extends Config {
name?: string; name?: string;
alias?: string;
} }
export function compile( export function compile(
template: string | Element, template: string | Element,
+3 -1
View File
@@ -5,10 +5,12 @@ export * from "./runtime";
TemplateSet.prototype._compileTemplate = function _compileTemplate( TemplateSet.prototype._compileTemplate = function _compileTemplate(
name: string, name: string,
template: string | Element template: string | Element,
alias?: string
) { ) {
return compile(template, { return compile(template, {
name, name,
alias,
dev: this.dev, dev: this.dev,
translateFn: this.translateFn, translateFn: this.translateFn,
translatableAttributes: this.translatableAttributes, translatableAttributes: this.translatableAttributes,
+1 -1
View File
@@ -116,7 +116,7 @@ export class ComponentNode<P extends Props = any, E = any> implements VNode<Comp
} }
this.component = new C(props, env, this); this.component = new C(props, env, this);
const ctx = Object.assign(Object.create(this.component), { this: this.component }); const ctx = Object.assign(Object.create(this.component), { this: this.component });
this.renderFn = app.getTemplate(C.template).bind(this.component, ctx, this); this.renderFn = app.getTemplate(C.template, C.name).bind(this.component, ctx, this);
this.component.setup(); this.component.setup();
currentNode = null; currentNode = null;
} }
+4 -4
View File
@@ -94,7 +94,7 @@ export class TemplateSet {
} }
} }
getTemplate(name: string): Template { getTemplate(name: string, alias?: string): Template {
if (!(name in this.templates)) { if (!(name in this.templates)) {
const rawTemplate = this.rawTemplates[name]; const rawTemplate = this.rawTemplates[name];
if (rawTemplate === undefined) { if (rawTemplate === undefined) {
@@ -106,7 +106,7 @@ export class TemplateSet {
throw new OwlError(`Missing template: "${name}"${extraInfo}`); throw new OwlError(`Missing template: "${name}"${extraInfo}`);
} }
const isFn = typeof rawTemplate === "function" && !(rawTemplate instanceof Element); const isFn = typeof rawTemplate === "function" && !(rawTemplate instanceof Element);
const templateFn = isFn ? rawTemplate : this._compileTemplate(name, rawTemplate); const templateFn = isFn ? rawTemplate : this._compileTemplate(name, rawTemplate, alias);
// first add a function to lazily get the template, in case there is a // first add a function to lazily get the template, in case there is a
// recursive call to the template name // recursive call to the template name
const templates = this.templates; const templates = this.templates;
@@ -119,7 +119,7 @@ export class TemplateSet {
return this.templates[name]; return this.templates[name];
} }
_compileTemplate(name: string, template: string | Element): ReturnType<typeof compile> { _compileTemplate(name: string, template: string | Element, alias?: string): ReturnType<typeof compile> {
throw new OwlError(`Unable to compile a template. Please use owl full build instead`); 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 const globalTemplates: { [key: string]: string | Element | TemplateFunction } = {};
export function xml(...args: Parameters<typeof String.raw>) { export function xml(...args: Parameters<typeof String.raw>) {
const name = `__template__${xml.nextId++}`; const name = `xml_template_${xml.nextId++}`;
const value = String.raw(...args); const value = String.raw(...args);
globalTemplates[name] = value; globalTemplates[name] = value;
return name; return name;
+2 -2
View File
@@ -128,8 +128,8 @@ export function snapshotEverything() {
}); });
const originalCompileTemplate = TemplateSet.prototype._compileTemplate; const originalCompileTemplate = TemplateSet.prototype._compileTemplate;
TemplateSet.prototype._compileTemplate = function (name: string, template: string | Element) { TemplateSet.prototype._compileTemplate = function (name: string, template: string | Element, alias?: string) {
const fn = originalCompileTemplate.call(this, "", template); const fn = originalCompileTemplate.call(this, name, template, alias);
if (!globalTemplateNames.has(name)) { if (!globalTemplateNames.has(name)) {
expect(fn.toString()).toMatchSnapshot(); expect(fn.toString()).toMatchSnapshot();
} }