From 9f2e2bcc663891fbad4f7c7a46457dbe3ff79294 Mon Sep 17 00:00:00 2001 From: Samuel Degueldre Date: Wed, 22 Dec 2021 12:33:17 +0100 Subject: [PATCH] [IMP] compiler: scope generated ids to their prefix This means that unrelated ids (eg the id of a template, variable or key) not longer share the same incrementing counter, meaning that you no longer see a variable named "v2" unless another variable "v1" was generated previously, this is also true for block data. --- src/compiler/code_generator.ts | 15 +- .../__snapshots__/event_handling.test.ts.snap | 24 +- .../compiler/__snapshots__/misc.test.ts.snap | 124 ++++----- .../__snapshots__/t_call.test.ts.snap | 136 +++++----- .../__snapshots__/t_debug_log.test.ts.snap | 4 +- .../compiler/__snapshots__/t_esc.test.ts.snap | 4 +- .../__snapshots__/t_foreach.test.ts.snap | 8 +- .../compiler/__snapshots__/t_out.test.ts.snap | 8 +- .../compiler/__snapshots__/t_ref.test.ts.snap | 12 +- .../compiler/__snapshots__/t_set.test.ts.snap | 8 +- .../__snapshots__/template_set.test.ts.snap | 8 +- .../__snapshots__/basics.test.ts.snap | 30 +- .../__snapshots__/concurrency.test.ts.snap | 4 +- .../__snapshots__/error_handling.test.ts.snap | 56 ++-- .../__snapshots__/event_handling.test.ts.snap | 8 +- .../higher_order_component.test.ts.snap | 4 +- .../__snapshots__/hooks.test.ts.snap | 4 +- .../__snapshots__/lifecycle.test.ts.snap | 4 +- .../__snapshots__/props.test.ts.snap | 2 +- .../props_validation.test.ts.snap | 138 +++++----- .../__snapshots__/refs.test.ts.snap | 10 +- .../__snapshots__/slots.test.ts.snap | 256 +++++++++--------- .../__snapshots__/style_class.test.ts.snap | 4 +- .../__snapshots__/t_call.test.ts.snap | 34 +-- .../__snapshots__/t_component.test.ts.snap | 28 +- .../__snapshots__/t_foreach.test.ts.snap | 2 +- .../__snapshots__/t_key.test.ts.snap | 30 +- .../__snapshots__/t_model.test.ts.snap | 126 ++++----- .../__snapshots__/t_on.test.ts.snap | 12 +- .../__snapshots__/t_set.test.ts.snap | 4 +- tests/misc/__snapshots__/memo.test.ts.snap | 10 +- tests/misc/__snapshots__/portal.test.ts.snap | 68 ++--- 32 files changed, 595 insertions(+), 590 deletions(-) diff --git a/src/compiler/code_generator.ts b/src/compiler/code_generator.ts index 548b989b..a2f5bedd 100644 --- a/src/compiler/code_generator.ts +++ b/src/compiler/code_generator.ts @@ -44,7 +44,11 @@ const xmlDoc = document.implementation.createDocument(null, null, null); class BlockDescription { static nextBlockId = 1; - static nextDataId = 1; + static nextDataIds: { [key: string]: number } = {}; + static generateId(prefix: string) { + this.nextDataIds[prefix] = (this.nextDataIds[prefix] || 0) + 1; + return prefix + this.nextDataIds[prefix]; + } varName: string; blockName: string; @@ -70,7 +74,7 @@ class BlockDescription { } insertData(str: string, prefix: string = "d"): number { - const id = prefix + BlockDescription.nextDataId++; + const id = BlockDescription.generateId(prefix); this.target.addLine(`let ${id} = ${str};`); return this.data.push(id) - 1; } @@ -189,7 +193,7 @@ const translationRE = /^(\s*)([\s\S]+?)(\s*)$/; export class CodeGenerator { blocks: BlockDescription[] = []; - nextId = 1; + ids: { [key: string]: number } = {}; nextBlockId = 1; hasSafeContext: boolean; isDebug: boolean = false; @@ -216,7 +220,7 @@ export class CodeGenerator { const ast = this.ast; this.isDebug = ast.type === ASTType.TDebug; BlockDescription.nextBlockId = 1; - BlockDescription.nextDataId = 1; + BlockDescription.nextDataIds = {}; this.compileAST(ast, { block: null, index: 0, @@ -282,7 +286,8 @@ export class CodeGenerator { } generateId(prefix: string = ""): string { - return `${prefix}${this.nextId++}`; + this.ids[prefix] = (this.ids[prefix] || 0) + 1; + return prefix + this.ids[prefix]; } generateBlockName(): string { diff --git a/tests/compiler/__snapshots__/event_handling.test.ts.snap b/tests/compiler/__snapshots__/event_handling.test.ts.snap index 003988a2..6428dcbb 100644 --- a/tests/compiler/__snapshots__/event_handling.test.ts.snap +++ b/tests/compiler/__snapshots__/event_handling.test.ts.snap @@ -157,10 +157,10 @@ exports[`t-on handler is bound to proper owner, part 3 1`] = ` ) { let { text, createBlock, list, multi, html, toggler, component, comment } = bdom; let { getTemplate } = helpers; - const callTemplate_2 = getTemplate(\`sub\`); + const callTemplate_1 = getTemplate(\`sub\`); return function template(ctx, node, key = \\"\\") { - return callTemplate_2.call(this, ctx, node, key + \`__1\`); + return callTemplate_1.call(this, ctx, node, key + \`__1\`); } }" `; @@ -184,7 +184,7 @@ exports[`t-on handler is bound to proper owner, part 4 1`] = ` ) { let { text, createBlock, list, multi, html, toggler, component, comment } = bdom; let { prepareList, getTemplate, withKey } = helpers; - const callTemplate_2 = getTemplate(\`sub\`); + const callTemplate_1 = getTemplate(\`sub\`); return function template(ctx, node, key = \\"\\") { ctx = Object.create(ctx); @@ -196,7 +196,7 @@ exports[`t-on handler is bound to proper owner, part 4 1`] = ` ctx[\`value_index\`] = i1; ctx[\`value_value\`] = k_block1[i1]; let key1 = ctx['value']; - c_block1[i1] = withKey(callTemplate_2.call(this, ctx, node, key + \`__1__\${key1}\`), key1); + c_block1[i1] = withKey(callTemplate_1.call(this, ctx, node, key + \`__1__\${key1}\`), key1); } return list(c_block1); } @@ -255,8 +255,8 @@ exports[`t-on t-on modifiers (native listener) t-on combined with t-esc 1`] = ` return function template(ctx, node, key = \\"\\") { let hdlr1 = [ctx['onClick'], ctx]; - let txt2 = ctx['text']; - return block1([hdlr1, txt2]); + let txt1 = ctx['text']; + return block1([hdlr1, txt1]); } }" `; @@ -354,8 +354,8 @@ exports[`t-on t-on modifiers (native listener) t-on with prevent modifier in t-f const v1 = ctx['onEdit']; const v2 = ctx['project']; let hdlr1 = [\\"prevent\\", ev=>v1(v2.id,ev), ctx]; - let txt2 = ctx['project'].name; - c_block2[i1] = withKey(block3([hdlr1, txt2]), key1); + let txt1 = ctx['project'].name; + c_block2[i1] = withKey(block3([hdlr1, txt1]), key1); } let b2 = list(c_block2); return block1([], [b2]); @@ -473,12 +473,12 @@ exports[`t-on t-on with t-call 1`] = ` ) { let { text, createBlock, list, multi, html, toggler, component, comment } = bdom; let { getTemplate } = helpers; - const callTemplate_2 = getTemplate(\`sub\`); + const callTemplate_1 = getTemplate(\`sub\`); let block1 = createBlock(\`
\`); return function template(ctx, node, key = \\"\\") { - let b2 = callTemplate_2.call(this, ctx, node, key + \`__1\`); + let b2 = callTemplate_1.call(this, ctx, node, key + \`__1\`); return block1([], [b2]); } }" @@ -503,12 +503,12 @@ exports[`t-on t-on, with arguments and t-call 1`] = ` ) { let { text, createBlock, list, multi, html, toggler, component, comment } = bdom; let { getTemplate } = helpers; - const callTemplate_2 = getTemplate(\`sub\`); + const callTemplate_1 = getTemplate(\`sub\`); let block1 = createBlock(\`
\`); return function template(ctx, node, key = \\"\\") { - let b2 = callTemplate_2.call(this, ctx, node, key + \`__1\`); + let b2 = callTemplate_1.call(this, ctx, node, key + \`__1\`); return block1([], [b2]); } }" diff --git a/tests/compiler/__snapshots__/misc.test.ts.snap b/tests/compiler/__snapshots__/misc.test.ts.snap index da4c59da..9663a1ba 100644 --- a/tests/compiler/__snapshots__/misc.test.ts.snap +++ b/tests/compiler/__snapshots__/misc.test.ts.snap @@ -22,7 +22,7 @@ exports[`misc complex template 1`] = ` let attr2 = \`card bg-\${ctx['klass']}-light\`; let attr3 = \`/runbot/batch/\${ctx['batch'].id}\`; let attr4 = \`badge badge-\${ctx['batch'].has_warning?'warning':'light'}\`; - let txt5 = ctx['batch'].formated_age; + let txt1 = ctx['batch'].formated_age; if (ctx['batch'].has_warning) { b2 = block2(); } @@ -53,8 +53,8 @@ exports[`misc complex template 1`] = ` ctx[\`commit_link\`] = v_block8[i1]; let key1 = ctx['commit_link'].id; let b10,b11,b12,b13; - let attr6 = \`/runbot/commit/\${ctx['commit_link'].commit_id}\`; - let attr7 = \`badge badge-light batch_commit match_type_\${ctx['commit_link'].match_type}\`; + let attr5 = \`/runbot/commit/\${ctx['commit_link'].commit_id}\`; + let attr6 = \`badge badge-light batch_commit match_type_\${ctx['commit_link'].match_type}\`; if (ctx['commit_link'].match_type=='new') { b10 = block10(); } @@ -67,13 +67,13 @@ exports[`misc complex template 1`] = ` if (ctx['commit_link'].match_type=='base_head') { b13 = block13(); } - let txt8 = ctx['commit_link'].commit_dname; - let attr9 = 'https://%s/commit/%s'%(ctx['commit_link'].commit_remote_url,ctx['commit_link'].commit_name); - let txt10 = ctx['commit_link'].commit_subject; - c_block8[i1] = withKey(block9([attr6, attr7, txt8, attr9, txt10], [b10, b11, b12, b13]), key1); + let txt2 = ctx['commit_link'].commit_dname; + let attr7 = 'https://%s/commit/%s'%(ctx['commit_link'].commit_remote_url,ctx['commit_link'].commit_name); + let txt3 = ctx['commit_link'].commit_subject; + c_block8[i1] = withKey(block9([attr5, attr6, txt2, attr7, txt3], [b10, b11, b12, b13]), key1); } b8 = list(c_block8); - return block1([attr1, attr2, attr3, attr4, txt5], [b2, b3, b4, b6, b8]); + return block1([attr1, attr2, attr3, attr4, txt1], [b2, b3, b4, b6, b8]); } }" `; @@ -83,11 +83,11 @@ exports[`misc global 1`] = ` ) { let { text, createBlock, list, multi, html, toggler, component, comment } = bdom; let { prepareList, isBoundary, withDefault, setContextValue, getTemplate, zero, withKey } = helpers; + const callTemplate_1 = getTemplate(\`_callee-uses-foo\`); const callTemplate_2 = getTemplate(\`_callee-uses-foo\`); - const callTemplate_4 = getTemplate(\`_callee-uses-foo\`); - const callTemplate_6 = getTemplate(\`_callee-uses-foo\`); - const callTemplate_8 = getTemplate(\`_callee-asc\`); - const callTemplate_10 = getTemplate(\`_callee-asc-toto\`); + const callTemplate_3 = getTemplate(\`_callee-uses-foo\`); + const callTemplate_4 = getTemplate(\`_callee-asc\`); + const callTemplate_5 = getTemplate(\`_callee-asc-toto\`); let block1 = createBlock(\`
\`); let block4 = createBlock(\`\`); @@ -111,20 +111,20 @@ exports[`misc global 1`] = ` ctx = Object.create(ctx); ctx[isBoundary] = 1; setContextValue(ctx, \\"foo\\", 'aaa'); - let b6 = callTemplate_2.call(this, ctx, node, key + \`__1__\${key1}\`); + let b6 = callTemplate_1.call(this, ctx, node, key + \`__1__\${key1}\`); ctx = ctx.__proto__; - let b7 = callTemplate_4.call(this, ctx, node, key + \`__3__\${key1}\`); + let b7 = callTemplate_2.call(this, ctx, node, key + \`__2__\${key1}\`); setContextValue(ctx, \\"foo\\", 'bbb'); - let b8 = callTemplate_6.call(this, ctx, node, key + \`__5__\${key1}\`); + let b8 = callTemplate_3.call(this, ctx, node, key + \`__3__\${key1}\`); let b5 = multi([b6, b7, b8]); ctx[zero] = b5; - let b9 = callTemplate_8.call(this, ctx, node, key + \`__7__\${key1}\`); + let b9 = callTemplate_4.call(this, ctx, node, key + \`__4__\${key1}\`); ctx = ctx.__proto__; c_block2[i1] = withKey(multi([b4, b9]), key1); } ctx = ctx.__proto__; let b2 = list(c_block2); - let b10 = callTemplate_10.call(this, ctx, node, key + \`__9\`); + let b10 = callTemplate_5.call(this, ctx, node, key + \`__5\`); return block1([], [b2, b10]); } }" @@ -182,7 +182,7 @@ exports[`misc other complex template 1`] = ` ) { let { text, createBlock, list, multi, html, toggler, component, comment } = bdom; let { prepareList, withKey, getTemplate } = helpers; - const callTemplate_2 = getTemplate(\`LOAD_INFOS_TEMPLATE\`); + const callTemplate_1 = getTemplate(\`LOAD_INFOS_TEMPLATE\`); let block1 = createBlock(\`