[IMP] component,qweb: props validation

We re-add the possibility to validate props when dev mode is active.
No change in the API right Now. The dev mode is activated via the
configure method of App class.
This commit is contained in:
Mathieu Duckerts-Antoine
2021-10-20 16:08:31 +02:00
committed by Géry Debongnie
parent d1425c7100
commit 6e0834ce5e
15 changed files with 1308 additions and 714 deletions
+11
View File
@@ -32,6 +32,7 @@ export interface CompileOptions {
name?: string;
translateFn?: (s: string) => string;
translatableAttributes?: string[];
dev?: boolean;
}
export function compileTemplate(template: string, options?: CompileOptions): TemplateFunction {
@@ -177,6 +178,7 @@ export class QWebCompiler {
target = new CodeTarget("main");
templateName: string;
template: string;
dev: boolean;
translateFn: (s: string) => string;
translatableAttributes: string[];
ast: AST;
@@ -186,6 +188,7 @@ export class QWebCompiler {
this.template = template;
this.translateFn = options.translateFn || ((s: string) => s);
this.translatableAttributes = options.translatableAttributes || TRANSLATABLE_ATTRS;
this.dev = options.dev || false;
this.ast = parse(template);
if (options.name) {
this.templateName = options.name;
@@ -925,6 +928,14 @@ export class QWebCompiler {
} else {
expr = `\`${ast.name}\``;
}
if (this.dev) {
const propVar = this.generateId("props");
this.addLine(`const ${propVar} = ${propString}`);
this.addLine(`helpers.validateProps(${expr}, ${propVar}, ctx)`);
propString = propVar;
}
let blockArgs = `${expr}, ${propString}, key + \`${key}\`, node, ctx`;
// slots
+4
View File
@@ -1,6 +1,7 @@
import { BDom, createBlock, html, list, multi, text, toggler } from "../blockdom";
import { compileTemplate, Template } from "./compiler";
import { component } from "../component/component_node";
import { validateProps } from "../component/props_validation";
const bdom = { text, createBlock, list, multi, html, toggler, component };
@@ -94,6 +95,7 @@ export const UTILS = {
prepareList,
setContextValue,
shallowEqual,
validateProps,
};
export class TemplateSet {
@@ -102,6 +104,7 @@ export class TemplateSet {
translateFn?: (s: string) => string;
translatableAttributes?: string[];
utils: typeof UTILS;
dev?: boolean;
constructor() {
const call = (subTemplate: string, ctx: any, parent: any) => {
@@ -128,6 +131,7 @@ export class TemplateSet {
}
const templateFn = compileTemplate(rawTemplate, {
name,
dev: this.dev,
translateFn: this.translateFn,
translatableAttributes: this.translatableAttributes,
});