Skip to content

Commit 2625719

Browse files
committed
refactor: rename to v-scope
1 parent 3e243d7 commit 2625719

File tree

13 files changed

+127
-117
lines changed

13 files changed

+127
-117
lines changed

packages/compiler-core/__tests__/transforms/__snapshots__/vLet.spec.ts.snap renamed to packages/compiler-core/__tests__/transforms/__snapshots__/vScope.spec.ts.snap

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,23 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`compiler: v-let transform complex expression 1`] = `
3+
exports[`compiler: v-scope transform complex expression 1`] = `
44
"import { toDisplayString as _toDisplayString, createTextVNode as _createTextVNode, createElementVNode as _createElementVNode, openBlock as _openBlock, createElementBlock as _createElementBlock } from "vue"
55
66
export function render(_ctx, _cache) {
77
return (_openBlock(), _createElementBlock("div", null, [
8-
((a=_ctx.foo+_ctx.bar) => _createElementVNode("div", null, [
9-
((b=a+_ctx.baz) => _createElementVNode("span", null, [
8+
((a=_ctx.foo + _ctx.bar) => _createElementVNode("div", null, [
9+
((b=a + _ctx.baz) => _createElementVNode("span", null, [
1010
_createTextVNode(_toDisplayString(b), 1 /* TEXT */)
1111
]))()
1212
]))(),
13-
((x=_ctx.y=_ctx.z) => _createElementVNode("div", null, [
14-
_createTextVNode(_toDisplayString(x) + _toDisplayString(_ctx.y) + _toDisplayString(_ctx.z), 1 /* TEXT */)
15-
]))(),
1613
((exp=_ctx.getExp()) => _createElementVNode("div", null, [
1714
_createTextVNode(_toDisplayString(exp), 1 /* TEXT */)
1815
]))()
1916
]))
2017
}"
2118
`;
2219

23-
exports[`compiler: v-let transform multiple declare 1`] = `
24-
"import { toDisplayString as _toDisplayString, createTextVNode as _createTextVNode, createElementVNode as _createElementVNode, openBlock as _openBlock, createElementBlock as _createElementBlock } from "vue"
25-
26-
export function render(_ctx, _cache) {
27-
return (_openBlock(), _createElementBlock("div", null, [
28-
((a=1, b=2) => _createElementVNode("div", null, [
29-
_createTextVNode(_toDisplayString(a) + " " + _toDisplayString(b), 1 /* TEXT */)
30-
]))()
31-
]))
32-
}"
33-
`;
34-
35-
exports[`compiler: v-let transform nested v-let 1`] = `
20+
exports[`compiler: v-scope transform nested v-scope 1`] = `
3621
"import { toDisplayString as _toDisplayString, createTextVNode as _createTextVNode, createElementVNode as _createElementVNode, openBlock as _openBlock, createElementBlock as _createElementBlock } from "vue"
3722
3823
export function render(_ctx, _cache) {
@@ -46,7 +31,7 @@ export function render(_ctx, _cache) {
4631
}"
4732
`;
4833

49-
exports[`compiler: v-let transform ok v-if 1`] = `
34+
exports[`compiler: v-scope transform ok v-if 1`] = `
5035
"import { toDisplayString as _toDisplayString, createTextVNode as _createTextVNode, openBlock as _openBlock, createElementBlock as _createElementBlock, createCommentVNode as _createCommentVNode } from "vue"
5136
5237
export function render(_ctx, _cache) {
@@ -60,7 +45,7 @@ export function render(_ctx, _cache) {
6045
}"
6146
`;
6247

63-
exports[`compiler: v-let transform on v-for 1`] = `
48+
exports[`compiler: v-scope transform on v-for 1`] = `
6449
"import { renderList as _renderList, Fragment as _Fragment, openBlock as _openBlock, createElementBlock as _createElementBlock, toDisplayString as _toDisplayString, createTextVNode as _createTextVNode, createElementVNode as _createElementVNode } from "vue"
6550
6651
export function render(_ctx, _cache) {
@@ -74,19 +59,19 @@ export function render(_ctx, _cache) {
7459
}"
7560
`;
7661

77-
exports[`compiler: v-let transform should work 1`] = `
62+
exports[`compiler: v-scope transform should work 1`] = `
7863
"import { toDisplayString as _toDisplayString, createTextVNode as _createTextVNode, createElementVNode as _createElementVNode, openBlock as _openBlock, createElementBlock as _createElementBlock } from "vue"
7964
8065
export function render(_ctx, _cache) {
8166
return (_openBlock(), _createElementBlock("div", null, [
82-
((a=1) => _createElementVNode("div", null, [
83-
_createTextVNode(_toDisplayString(a), 1 /* TEXT */)
67+
((a=1, b=2) => _createElementVNode("div", null, [
68+
_createTextVNode(_toDisplayString(a) + " " + _toDisplayString(b), 1 /* TEXT */)
8469
]))()
8570
]))
8671
}"
8772
`;
8873

89-
exports[`compiler: v-let transform work with variable 1`] = `
74+
exports[`compiler: v-scope transform work with variable 1`] = `
9075
"import { toDisplayString as _toDisplayString, createTextVNode as _createTextVNode, createElementVNode as _createElementVNode, openBlock as _openBlock, createElementBlock as _createElementBlock } from "vue"
9176
9277
export function render(_ctx, _cache) {

packages/compiler-core/__tests__/transforms/vLet.spec.ts renamed to packages/compiler-core/__tests__/transforms/vScope.spec.ts

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { baseCompile, CompilerOptions } from '../../src'
22

3-
describe('compiler: v-let transform', () => {
3+
describe('compiler: v-scope transform', () => {
44
function compile(content: string, options: CompilerOptions = {}) {
55
return baseCompile(`<div>${content}</div>`, {
66
mode: 'module',
@@ -10,24 +10,20 @@ describe('compiler: v-let transform', () => {
1010
}
1111

1212
test('should work', () => {
13-
expect(compile(`<div v-let="a=1">{{a}}</div>`)).toMatchSnapshot()
14-
})
15-
16-
test('multiple declare', () => {
1713
expect(
1814
compile(
19-
`<div v-let="a=1, b=2">
15+
`<div v-scope="{ a:1, b:2 }">
2016
{{a}} {{b}}
2117
</div>`
2218
)
2319
).toMatchSnapshot()
2420
})
2521

26-
test('nested v-let', () => {
22+
test('nested v-scope', () => {
2723
expect(
2824
compile(
29-
`<div v-let="a=1">
30-
<span v-let="b=1">{{a}}{{b}}</span>
25+
`<div v-scope="{ a:1 }">
26+
<span v-scope="{ b:1 }">{{ a }}{{ b }}</span>
3127
</div>`
3228
)
3329
).toMatchSnapshot()
@@ -36,8 +32,8 @@ describe('compiler: v-let transform', () => {
3632
test('work with variable', () => {
3733
expect(
3834
compile(
39-
`<div v-let="a=msg">
40-
<span v-let="b=a">{{b}}</span>
35+
`<div v-scope="{ a:msg }">
36+
<span v-scope="{ b:a }">{{ b }}</span>
4137
</div>`
4238
)
4339
).toMatchSnapshot()
@@ -46,19 +42,18 @@ describe('compiler: v-let transform', () => {
4642
test('complex expression', () => {
4743
expect(
4844
compile(`
49-
<div v-let="a=foo+bar">
50-
<span v-let="b=a+baz">{{b}}</span>
45+
<div v-scope="{ a:foo + bar }">
46+
<span v-scope="{ b:a + baz }">{{ b }}</span>
5147
</div>
52-
<div v-let="x=y=z">{{x}}{{y}}{{z}}</div>
53-
<div v-let="exp=getExp()">{{exp}}</div>
48+
<div v-scope="{ exp:getExp() }">{{ exp }}</div>
5449
`)
5550
).toMatchSnapshot()
5651
})
5752

5853
test('on v-for', () => {
5954
expect(
6055
compile(`
61-
<div v-for="i in [1,2,3]" v-let="a=i+1">
56+
<div v-for="i in [1,2,3]" v-scope="{ a:i+1 }">
6257
{{ a }}
6358
</div>
6459
`)
@@ -68,7 +63,7 @@ describe('compiler: v-let transform', () => {
6863
test('ok v-if', () => {
6964
expect(
7065
compile(`
71-
<div v-if="ok" v-let="a=true" >
66+
<div v-if="ok" v-scope="{ a:true }" >
7267
{{ a }}
7368
</div>
7469
`)
@@ -77,11 +72,11 @@ describe('compiler: v-let transform', () => {
7772

7873
test('error', () => {
7974
const onError = jest.fn()
80-
expect(compile(`<div v-let="a=,b=1">{{a}}</div>`, { onError }))
75+
expect(compile(`<div v-scope="{ a:, b:1 }">{{ a }}</div>`, { onError }))
8176
expect(onError.mock.calls).toMatchInlineSnapshot(`
8277
[
8378
[
84-
[SyntaxError: Error parsing JavaScript expression: Unexpected token (1:3)],
79+
[SyntaxError: Error parsing JavaScript expression: Unexpected token (1:5)],
8580
],
8681
]
8782
`)

packages/compiler-core/src/compile.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { transformModel } from './transforms/vModel'
1818
import { transformFilter } from './compat/transformFilter'
1919
import { defaultOnError, createCompilerError, ErrorCodes } from './errors'
2020
import { transformMemo } from './transforms/vMemo'
21-
import { trackVLetScopes, transformLet } from './transforms/vLet'
21+
import { trackVScopeScopes, transformScope } from './transforms/vScope'
2222

2323
export type TransformPreset = [
2424
NodeTransform[],
@@ -33,7 +33,7 @@ export function getBaseTransformPreset(
3333
transformOnce,
3434
transformIf,
3535
transformMemo,
36-
transformLet,
36+
transformScope,
3737
transformFor,
3838
...(__COMPAT__ ? [transformFilter] : []),
3939
...(!__BROWSER__ && prefixIdentifiers
@@ -48,7 +48,7 @@ export function getBaseTransformPreset(
4848
transformSlotOutlet,
4949
transformElement,
5050
trackSlotScopes,
51-
trackVLetScopes,
51+
trackVScopeScopes,
5252
transformText
5353
],
5454
{

packages/compiler-core/src/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,11 @@ export {
5252
trackVForSlotScopes,
5353
trackSlotScopes
5454
} from './transforms/vSlot'
55-
export { transformLet, trackVLetScopes } from './transforms/vLet'
55+
export {
56+
transformScope,
57+
trackVScopeScopes,
58+
transformScopeExpression
59+
} from './transforms/vScope'
5660
export {
5761
transformElement,
5862
resolveComponentType,

packages/compiler-core/src/transforms/transformExpression.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ export const transformExpression: NodeTransform = (node, context) => {
6969
dir.exp = processExpression(
7070
exp,
7171
context,
72-
// slot and let args must be processed as function params
73-
dir.name === 'slot' || dir.name === 'let'
72+
// slot args must be processed as function params
73+
dir.name === 'slot'
7474
)
7575
}
7676
if (arg && arg.type === NodeTypes.SIMPLE_EXPRESSION && !arg.isStatic) {

packages/compiler-core/src/transforms/vFor.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import {
4848
import { processExpression } from './transformExpression'
4949
import { validateBrowserExpression } from '../validateExpression'
5050
import { PatchFlags, PatchFlagNames } from '@vue/shared'
51+
import { transformScopeExpression } from './vScope'
5152

5253
export const transformFor = createStructuralDirectiveTransform(
5354
'for',
@@ -61,7 +62,7 @@ export const transformFor = createStructuralDirectiveTransform(
6162
]) as ForRenderListExpression
6263
const isTemplate = isTemplateNode(node)
6364
const memo = findDir(node, 'memo')
64-
const vLet = findDir(node, 'let')
65+
const vScope = findDir(node, 'scope')
6566
const keyProp = findProp(node, `key`)
6667
const keyExp =
6768
keyProp &&
@@ -230,9 +231,12 @@ export const transformFor = createStructuralDirectiveTransform(
230231
)
231232
} else {
232233
let child
233-
if (vLet) {
234+
if (vScope) {
234235
child = createCallExpression(
235-
createFunctionExpression([vLet.exp!], childBlock)
236+
createFunctionExpression(
237+
transformScopeExpression(vScope.exp!),
238+
childBlock
239+
)
236240
)
237241
} else {
238242
child = childBlock

packages/compiler-core/src/transforms/vIf.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ function createChildrenCodegenNode(
302302
| BlockCodegenNode
303303
| MemoExpression
304304

305-
const vnodeCall = findDir(firstChild, 'let')
305+
const vnodeCall = findDir(firstChild, 'scope')
306306
? (((ret as CallExpression).callee as FunctionExpression)
307307
.returns as VNodeCall)
308308
: getMemoedVNodeCall(ret)

packages/compiler-core/src/transforms/vLet.ts

Lines changed: 0 additions & 51 deletions
This file was deleted.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { NodeTransform } from '../transform'
2+
import { findDir } from '../utils'
3+
import {
4+
createCallExpression,
5+
createFunctionExpression,
6+
createSimpleExpression,
7+
ExpressionNode,
8+
NodeTypes,
9+
PlainElementNode,
10+
SimpleExpressionNode
11+
} from '../ast'
12+
import { stringifyExpression } from './transformExpression'
13+
14+
const seen = new WeakSet()
15+
const extractKeyValueRE = /(\w+)\s*:\s*"*(.*?)"*\s*[,}\n]/g
16+
17+
export const transformScope: NodeTransform = (node, context) => {
18+
if (node.type === NodeTypes.ELEMENT) {
19+
const dir = findDir(node, 'scope')
20+
if (!dir || seen.has(node)) {
21+
return
22+
}
23+
seen.add(node)
24+
return () => {
25+
const codegenNode =
26+
node.codegenNode ||
27+
(context.currentNode as PlainElementNode).codegenNode
28+
if (codegenNode && codegenNode.type === NodeTypes.VNODE_CALL) {
29+
node.codegenNode = createCallExpression(
30+
createFunctionExpression(
31+
transformScopeExpression(dir.exp!),
32+
codegenNode
33+
)
34+
)
35+
}
36+
}
37+
}
38+
}
39+
40+
export function transformScopeExpression(
41+
exp: string | ExpressionNode
42+
): ExpressionNode[] {
43+
const params: SimpleExpressionNode[] = []
44+
const rExpString = stringifyExpression(exp)
45+
let match
46+
while ((match = extractKeyValueRE.exec(rExpString))) {
47+
params.push(createSimpleExpression(`${match[1]}=${match[2]}`))
48+
}
49+
return params
50+
}
51+
52+
export const trackVScopeScopes: NodeTransform = (node, context) => {
53+
if (node.type === NodeTypes.ELEMENT) {
54+
const vLet = findDir(node, 'scope')
55+
if (vLet) {
56+
const keys: string[] = []
57+
let match
58+
while ((match = extractKeyValueRE.exec(vLet.exp!.loc.source))) {
59+
keys.push(match[1])
60+
}
61+
if (!__BROWSER__ && context.prefixIdentifiers) {
62+
keys.forEach(context.addIdentifiers)
63+
}
64+
return () => {
65+
if (!__BROWSER__ && context.prefixIdentifiers) {
66+
keys.forEach(context.removeIdentifiers)
67+
}
68+
}
69+
}
70+
}
71+
}

0 commit comments

Comments
 (0)