From 4084674e011c6e9a140caf9016eb41816de9ef49 Mon Sep 17 00:00:00 2001 From: fxy060608 Date: Wed, 20 Oct 2021 14:24:30 +0800 Subject: [PATCH] wip(mp): ref --- .../uni-mp-compiler/__tests__/ref.spec.ts | 25 +++++++++++++++++++ .../uni-mp-compiler/__tests__/test.spec.ts | 4 +-- .../src/transforms/transformElement.ts | 19 ++++++++++++-- .../uni-mp-compiler/src/transforms/vFor.ts | 1 + 4 files changed, 45 insertions(+), 4 deletions(-) diff --git a/packages/uni-mp-compiler/__tests__/ref.spec.ts b/packages/uni-mp-compiler/__tests__/ref.spec.ts index 5680a258e..55d1432d9 100644 --- a/packages/uni-mp-compiler/__tests__/ref.spec.ts +++ b/packages/uni-mp-compiler/__tests__/ref.spec.ts @@ -7,6 +7,13 @@ describe('compiler: transform ref', () => { ``, `(_ctx, _cache) => { return {} +}` + ) + assert( + ``, + ``, + `(_ctx, _cache) => { + return {} }` ) }) @@ -16,6 +23,24 @@ describe('compiler: transform ref', () => { ``, `(_ctx, _cache) => { return { a: _vFor(_ctx.items, item => { return {}; }) } +}` + ) + }) + test('static ref', () => { + assert( + ``, + ``, + `(_ctx, _cache) => { + return {} +}` + ) + }) + test('dynamic ref', () => { + assert( + ``, + ``, + `(_ctx, _cache) => { + return { a: _ctx.custom } }` ) }) diff --git a/packages/uni-mp-compiler/__tests__/test.spec.ts b/packages/uni-mp-compiler/__tests__/test.spec.ts index 41868a044..d52b3c899 100644 --- a/packages/uni-mp-compiler/__tests__/test.spec.ts +++ b/packages/uni-mp-compiler/__tests__/test.spec.ts @@ -37,10 +37,10 @@ function assert( describe('compiler', () => { test('scope', () => { assert( - ``, + ``, ``, `(_ctx, _cache) => { - return { a: _ctx.ok, ...(_ctx.ok ? {} : {}) } + return { a: \`\${_ctx.green}px\` } }` ) }) diff --git a/packages/uni-mp-compiler/src/transforms/transformElement.ts b/packages/uni-mp-compiler/src/transforms/transformElement.ts index fa7375ed5..1fed011b6 100644 --- a/packages/uni-mp-compiler/src/transforms/transformElement.ts +++ b/packages/uni-mp-compiler/src/transforms/transformElement.ts @@ -95,7 +95,7 @@ function addVueRef(node: ElementNode, context: TransformContext) { function processComponent(node: ElementNode, context: TransformContext) { const { tag } = node if (context.bindingComponents[tag]) { - return + return addVueRef(node, context) } // 1. dynamic component @@ -217,7 +217,12 @@ function processProps(node: ElementNode, context: TransformContext) { for (let i = 0; i < props.length; i++) { const prop = props[i] - if (prop.type === NodeTypes.DIRECTIVE) { + if (prop.type === NodeTypes.ATTRIBUTE) { + // => + if (prop.name === 'ref') { + prop.name = 'data-ref' + } + } else { // directives const { name, arg, loc } = prop const isVBind = name === 'bind' @@ -276,6 +281,16 @@ function processProps(node: ElementNode, context: TransformContext) { } } + if (isVBind) { + // => + if ( + arg?.type === NodeTypes.SIMPLE_EXPRESSION && + arg.content === 'ref' + ) { + arg.content = 'data-ref' + } + } + const directiveTransform = context.directiveTransforms[name] if (directiveTransform) { prop.exp = directiveTransform(prop, node, context).props[0] diff --git a/packages/uni-mp-compiler/src/transforms/vFor.ts b/packages/uni-mp-compiler/src/transforms/vFor.ts index ea0f48d46..81d5dc967 100644 --- a/packages/uni-mp-compiler/src/transforms/vFor.ts +++ b/packages/uni-mp-compiler/src/transforms/vFor.ts @@ -131,6 +131,7 @@ export const transformFor = createStructuralDirectiveTransform( scopes.vFor++ return () => { + scopes.vFor-- if (isTemplateNode(node)) { node.children.some((c) => { if (c.type === NodeTypes.ELEMENT && !isForElementNode(c)) { -- GitLab