ref: adapt qweb to output code closer to es5

This commit is contained in:
Géry Debongnie
2019-04-04 14:08:38 +02:00
parent fd26775a0f
commit 3e8682b741
3 changed files with 440 additions and 440 deletions
+17 -17
View File
@@ -58,7 +58,7 @@ export class Context {
constructor(name?: string) { constructor(name?: string) {
this.rootContext = this; this.rootContext = this;
this.templateName = name || "noname"; this.templateName = name || "noname";
this.addLine("let h = this.utils.h;"); this.addLine("var h = this.utils.h;");
} }
generateID(): number { generateID(): number {
@@ -404,7 +404,7 @@ export class QWeb {
// this is an unusual situation: this text node is the result of the // this is an unusual situation: this text node is the result of the
// template rendering. // template rendering.
let nodeID = ctx.generateID(); let nodeID = ctx.generateID();
ctx.addLine(`let vn${nodeID} = {text: \`${text}\`};`); ctx.addLine(`var vn${nodeID} = {text: \`${text}\`};`);
ctx.rootContext.rootNode = nodeID; ctx.rootContext.rootNode = nodeID;
ctx.rootContext.parentNode = nodeID; ctx.rootContext.parentNode = nodeID;
} }
@@ -534,7 +534,7 @@ export class QWeb {
!(<Element>node).getAttribute("t-attf-" + name) !(<Element>node).getAttribute("t-attf-" + name)
) { ) {
const attID = ctx.generateID(); const attID = ctx.generateID();
ctx.addLine(`let _${attID} = '${value}';`); ctx.addLine(`var _${attID} = '${value}';`);
if (!name.match(/^[a-zA-Z]+$/)) { if (!name.match(/^[a-zA-Z]+$/)) {
// attribute contains 'non letters' => we want to quote it // attribute contains 'non letters' => we want to quote it
name = '"' + name + '"'; name = '"' + name + '"';
@@ -563,14 +563,14 @@ export class QWeb {
const attValue = (<Element>node).getAttribute(attName); const attValue = (<Element>node).getAttribute(attName);
if (attValue) { if (attValue) {
const attValueID = ctx.generateID(); const attValueID = ctx.generateID();
ctx.addLine(`let _${attValueID} = ${formattedValue};`); ctx.addLine(`var _${attValueID} = ${formattedValue};`);
formattedValue = `'${attValue}' + (_${attValueID} ? ' ' + _${attValueID} : '')`; formattedValue = `'${attValue}' + (_${attValueID} ? ' ' + _${attValueID} : '')`;
const attrIndex = attrs.findIndex(att => const attrIndex = attrs.findIndex(att =>
att.startsWith(attName + ":") att.startsWith(attName + ":")
); );
attrs.splice(attrIndex, 1); attrs.splice(attrIndex, 1);
} }
ctx.addLine(`let _${attID} = ${formattedValue};`); ctx.addLine(`var _${attID} = ${formattedValue};`);
attrs.push(`${attName}: _${attID}`); attrs.push(`${attName}: _${attID}`);
handleBooleanProps(attName, attID); handleBooleanProps(attName, attID);
} }
@@ -589,10 +589,10 @@ export class QWeb {
let staticVal = (<Element>node).getAttribute(attName); let staticVal = (<Element>node).getAttribute(attName);
if (staticVal) { if (staticVal) {
ctx.addLine( ctx.addLine(
`let _${attID} = '${staticVal} ' + \`${formattedExpr}\`;` `var _${attID} = '${staticVal} ' + \`${formattedExpr}\`;`
); );
} else { } else {
ctx.addLine(`let _${attID} = \`${formattedExpr}\`;`); ctx.addLine(`var _${attID} = \`${formattedExpr}\`;`);
} }
attrs.push(`${attName}: _${attID}`); attrs.push(`${attName}: _${attID}`);
} }
@@ -600,7 +600,7 @@ export class QWeb {
// t-att= attributes // t-att= attributes
if (name === "t-att") { if (name === "t-att") {
let id = ctx.generateID(); let id = ctx.generateID();
ctx.addLine(`let _${id} = ${ctx.formatExpression(value!)};`); ctx.addLine(`var _${id} = ${ctx.formatExpression(value!)};`);
tattrs.push(id); tattrs.push(id);
} }
} }
@@ -616,7 +616,7 @@ export class QWeb {
parts.push(`on:{}`); parts.push(`on:{}`);
} }
ctx.addLine(`let c${nodeID} = [], p${nodeID} = {${parts.join(",")}};`); ctx.addLine(`var c${nodeID} = [], p${nodeID} = {${parts.join(",")}};`);
for (let id of tattrs) { for (let id of tattrs) {
ctx.addIf(`_${id} instanceof Array`); ctx.addIf(`_${id} instanceof Array`);
ctx.addLine(`p${nodeID}.attrs[_${id}[0]] = _${id}[1];`); ctx.addLine(`p${nodeID}.attrs[_${id}[0]] = _${id}[1];`);
@@ -629,7 +629,7 @@ export class QWeb {
ctx.closeIf(); ctx.closeIf();
} }
ctx.addLine( ctx.addLine(
`let vn${nodeID} = h('${node.nodeName}', p${nodeID}, c${nodeID});` `var vn${nodeID} = h('${node.nodeName}', p${nodeID}, c${nodeID});`
); );
if (ctx.parentNode) { if (ctx.parentNode) {
ctx.addLine(`c${ctx.parentNode}.push(vn${nodeID});`); ctx.addLine(`c${ctx.parentNode}.push(vn${nodeID});`);
@@ -678,7 +678,7 @@ function compileValueNode(value: any, node: Element, qweb: QWeb, ctx: Context) {
if (typeof value === "string") { if (typeof value === "string") {
const exprID = ctx.generateID(); const exprID = ctx.generateID();
ctx.addLine(`let e${exprID} = ${ctx.formatExpression(value)};`); ctx.addLine(`var e${exprID} = ${ctx.formatExpression(value)};`);
ctx.addIf(`e${exprID} || e${exprID} === 0`); ctx.addIf(`e${exprID} || e${exprID} === 0`);
let text = `e${exprID}`; let text = `e${exprID}`;
@@ -689,14 +689,14 @@ function compileValueNode(value: any, node: Element, qweb: QWeb, ctx: Context) {
ctx.addLine(`c${ctx.parentNode}.push({text: ${text}});`); ctx.addLine(`c${ctx.parentNode}.push({text: ${text}});`);
} else { } else {
let fragID = ctx.generateID(); let fragID = ctx.generateID();
ctx.addLine(`let frag${fragID} = this.utils.getFragment(e${exprID})`); ctx.addLine(`var frag${fragID} = this.utils.getFragment(e${exprID})`);
let tempNodeID = ctx.generateID(); let tempNodeID = ctx.generateID();
ctx.addLine(`let p${tempNodeID} = {hook: {`); ctx.addLine(`var p${tempNodeID} = {hook: {`);
ctx.addLine( ctx.addLine(
` insert: n => n.elm.parentNode.replaceChild(frag${fragID}, n.elm),` ` insert: n => n.elm.parentNode.replaceChild(frag${fragID}, n.elm),`
); );
ctx.addLine(`}};`); ctx.addLine(`}};`);
ctx.addLine(`let vn${tempNodeID} = h('div', p${tempNodeID})`); ctx.addLine(`var vn${tempNodeID} = h('div', p${tempNodeID})`);
ctx.addLine(`c${ctx.parentNode}.push(vn${tempNodeID});`); ctx.addLine(`c${ctx.parentNode}.push(vn${tempNodeID});`);
} }
if (node.childNodes.length) { if (node.childNodes.length) {
@@ -833,7 +833,7 @@ const forEachDirective: Directive = {
const elems = node.getAttribute("t-foreach")!; const elems = node.getAttribute("t-foreach")!;
const name = node.getAttribute("t-as")!; const name = node.getAttribute("t-as")!;
let arrayID = ctx.generateID(); let arrayID = ctx.generateID();
ctx.addLine(`let _${arrayID} = ${ctx.formatExpression(elems)};`); ctx.addLine(`var _${arrayID} = ${ctx.formatExpression(elems)};`);
ctx.addLine( ctx.addLine(
`if (!_${arrayID}) { throw new Error('QWeb error: Invalid loop expression')}` `if (!_${arrayID}) { throw new Error('QWeb error: Invalid loop expression')}`
); );
@@ -842,11 +842,11 @@ const forEachDirective: Directive = {
); );
let keysID = ctx.generateID(); let keysID = ctx.generateID();
ctx.addLine( ctx.addLine(
`let _${keysID} = _${arrayID} instanceof Array ? _${arrayID} : Object.keys(_${arrayID});` `var _${keysID} = _${arrayID} instanceof Array ? _${arrayID} : Object.keys(_${arrayID});`
); );
let valuesID = ctx.generateID(); let valuesID = ctx.generateID();
ctx.addLine( ctx.addLine(
`let _${valuesID} = _${arrayID} instanceof Array ? _${arrayID} : Object.values(_${arrayID});` `var _${valuesID} = _${arrayID} instanceof Array ? _${arrayID} : Object.values(_${arrayID});`
); );
ctx.addLine(`for (let i = 0; i < _${keysID}.length; i++) {`); ctx.addLine(`for (let i = 0; i < _${keysID}.length; i++) {`);
ctx.indent(); ctx.indent();
+12 -12
View File
@@ -5,14 +5,14 @@ exports[`composition sub widgets with some state rendered in a loop 1`] = `
) { ) {
let owner = context; let owner = context;
context = Object.create(context); context = Object.create(context);
let h = this.utils.h; var h = this.utils.h;
let c1 = [], p1 = {key:1}; var c1 = [], p1 = {key:1};
let vn1 = h('div', p1, c1); var vn1 = h('div', p1, c1);
let _2 = context['state'].numbers; var _2 = context['state'].numbers;
if (!_2) { throw new Error('QWeb error: Invalid loop expression')} if (!_2) { throw new Error('QWeb error: Invalid loop expression')}
if (typeof _2 === 'number') { _2 = Array.from(Array(_2).keys())} if (typeof _2 === 'number') { _2 = Array.from(Array(_2).keys())}
let _3 = _2 instanceof Array ? _2 : Object.keys(_2); var _3 = _2 instanceof Array ? _2 : Object.keys(_2);
let _4 = _2 instanceof Array ? _2 : Object.values(_2); var _4 = _2 instanceof Array ? _2 : Object.values(_2);
for (let i = 0; i < _3.length; i++) { for (let i = 0; i < _3.length; i++) {
context.number_first = i === 0; context.number_first = i === 0;
context.number_last = i === _3.length - 1; context.number_last = i === _3.length - 1;
@@ -65,9 +65,9 @@ exports[`random stuff/miscellaneous snapshotting compiled code 1`] = `
"function anonymous(context,extra "function anonymous(context,extra
) { ) {
let owner = context; let owner = context;
let h = this.utils.h; var h = this.utils.h;
let c1 = [], p1 = {key:1}; var c1 = [], p1 = {key:1};
let vn1 = h('div', p1, c1); var vn1 = h('div', p1, c1);
//WIDGET //WIDGET
let key5 = \\"somestring\\"; let key5 = \\"somestring\\";
let _2_index = c1.length; let _2_index = c1.length;
@@ -112,9 +112,9 @@ exports[`random stuff/miscellaneous t-props should not be undefined (snapshottin
"function anonymous(context,extra "function anonymous(context,extra
) { ) {
let owner = context; let owner = context;
let h = this.utils.h; var h = this.utils.h;
let c1 = [], p1 = {key:1}; var c1 = [], p1 = {key:1};
let vn1 = h('div', p1, c1); var vn1 = h('div', p1, c1);
//WIDGET //WIDGET
let _2_index = c1.length; let _2_index = c1.length;
c1.push(null); c1.push(null);
File diff suppressed because it is too large Load Diff