Skip to content

Commit 1c0a2c6

Browse files
committed
refactor(compiler): use shorter helpers for text and comment nodes
1 parent eb20730 commit 1c0a2c6

19 files changed

+126
-108
lines changed

packages/compiler-core/__tests__/__snapshots__/codegen.spec.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ exports[`compiler: codegen comment 1`] = `
9090
"
9191
return function render() {
9292
with (this) {
93-
return _createVNode(_Comment, null, \\"foo\\")
93+
return _createCommentVNode(\\"foo\\")
9494
}
9595
}"
9696
`;

packages/compiler-core/__tests__/__snapshots__/compile.spec.ts.snap

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ exports[`compiler: integration tests function mode 1`] = `
55
66
return function render() {
77
with (this) {
8-
const { toString: _toString, openBlock: _openBlock, createVNode: _createVNode, createBlock: _createBlock, Comment: _Comment, Fragment: _Fragment, renderList: _renderList, Text: _Text } = _Vue
8+
const { toString: _toString, openBlock: _openBlock, createVNode: _createVNode, createBlock: _createBlock, createCommentVNode: _createCommentVNode, Fragment: _Fragment, renderList: _renderList, createTextVNode: _createTextVNode } = _Vue
99
1010
return (_openBlock(), _createBlock(\\"div\\", {
1111
id: \\"foo\\",
1212
class: bar.baz
1313
}, [
14-
_createVNode(_Text, null, _toString(world.burn()) + \\" \\", 1 /* TEXT */),
14+
_createTextVNode(_toString(world.burn()) + \\" \\", 1 /* TEXT */),
1515
(_openBlock(), ok
1616
? _createBlock(\\"div\\", { key: 0 }, \\"yes\\")
1717
: _createBlock(_Fragment, { key: 1 }, [\\"no\\"])),
@@ -26,15 +26,15 @@ return function render() {
2626
`;
2727

2828
exports[`compiler: integration tests function mode w/ prefixIdentifiers: true 1`] = `
29-
"const { toString, openBlock, createVNode, createBlock, Comment, Fragment, renderList, Text } = Vue
29+
"const { toString, openBlock, createVNode, createBlock, createCommentVNode, Fragment, renderList, createTextVNode } = Vue
3030
3131
return function render() {
3232
const _ctx = this
3333
return (openBlock(), createBlock(\\"div\\", {
3434
id: \\"foo\\",
3535
class: _ctx.bar.baz
3636
}, [
37-
createVNode(Text, null, toString(_ctx.world.burn()) + \\" \\", 1 /* TEXT */),
37+
createTextVNode(toString(_ctx.world.burn()) + \\" \\", 1 /* TEXT */),
3838
(openBlock(), (_ctx.ok)
3939
? createBlock(\\"div\\", { key: 0 }, \\"yes\\")
4040
: createBlock(Fragment, { key: 1 }, [\\"no\\"])),
@@ -48,15 +48,15 @@ return function render() {
4848
`;
4949

5050
exports[`compiler: integration tests module mode 1`] = `
51-
"import { toString, openBlock, createVNode, createBlock, Comment, Fragment, renderList, Text } from \\"vue\\"
51+
"import { toString, openBlock, createVNode, createBlock, createCommentVNode, Fragment, renderList, createTextVNode } from \\"vue\\"
5252
5353
export default function render() {
5454
const _ctx = this
5555
return (openBlock(), createBlock(\\"div\\", {
5656
id: \\"foo\\",
5757
class: _ctx.bar.baz
5858
}, [
59-
createVNode(Text, null, toString(_ctx.world.burn()) + \\" \\", 1 /* TEXT */),
59+
createTextVNode(toString(_ctx.world.burn()) + \\" \\", 1 /* TEXT */),
6060
(openBlock(), (_ctx.ok)
6161
? createBlock(\\"div\\", { key: 0 }, \\"yes\\")
6262
: createBlock(Fragment, { key: 1 }, [\\"no\\"])),

packages/compiler-core/__tests__/codegen.spec.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ import {
1818
} from '../src'
1919
import {
2020
CREATE_VNODE,
21-
COMMENT,
2221
TO_STRING,
2322
RESOLVE_DIRECTIVE,
2423
helperNameMap,
25-
RESOLVE_COMPONENT
24+
RESOLVE_COMPONENT,
25+
CREATE_COMMENT
2626
} from '../src/runtimeHelpers'
2727
import { createElementWithCodegen } from './testUtils'
2828
import { PatchFlags } from '@vue/shared'
@@ -149,7 +149,6 @@ describe('compiler: codegen', () => {
149149
codegenNode: {
150150
type: NodeTypes.TEXT,
151151
content: 'hello',
152-
isEmpty: false,
153152
loc: locStub
154153
}
155154
})
@@ -178,11 +177,7 @@ describe('compiler: codegen', () => {
178177
}
179178
})
180179
)
181-
expect(code).toMatch(
182-
`return _${helperNameMap[CREATE_VNODE]}(_${
183-
helperNameMap[COMMENT]
184-
}, null, "foo")`
185-
)
180+
expect(code).toMatch(`return _${helperNameMap[CREATE_COMMENT]}("foo")`)
186181
expect(code).toMatchSnapshot()
187182
})
188183

packages/compiler-core/__tests__/parse.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1612,6 +1612,14 @@ foo
16121612
expect(ast.children.every(c => c.type === NodeTypes.ELEMENT)).toBe(true)
16131613
})
16141614

1615+
it('should remove whitespaces adjacent to comments', () => {
1616+
const ast = parse(`<div/> \n <!--foo--> <div/>`)
1617+
expect(ast.children.length).toBe(3)
1618+
expect(ast.children[0].type).toBe(NodeTypes.ELEMENT)
1619+
expect(ast.children[1].type).toBe(NodeTypes.COMMENT)
1620+
expect(ast.children[2].type).toBe(NodeTypes.ELEMENT)
1621+
})
1622+
16151623
it('should remove whitespaces w/ newline between comments and elements', () => {
16161624
const ast = parse(`<div/> \n <!--foo--> \n <div/>`)
16171625
expect(ast.children.length).toBe(3)

packages/compiler-core/__tests__/transform.spec.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,12 @@ import {
99
import { ErrorCodes, createCompilerError } from '../src/errors'
1010
import {
1111
TO_STRING,
12-
CREATE_VNODE,
13-
COMMENT,
1412
OPEN_BLOCK,
1513
CREATE_BLOCK,
1614
FRAGMENT,
1715
RENDER_SLOT,
18-
WITH_DIRECTIVES
16+
WITH_DIRECTIVES,
17+
CREATE_COMMENT
1918
} from '../src/runtimeHelpers'
2019
import { transformIf } from '../src/transforms/vIf'
2120
import { transformFor } from '../src/transforms/vFor'
@@ -232,8 +231,7 @@ describe('compiler: transform', () => {
232231
test('should inject createVNode and Comment for comments', () => {
233232
const ast = parse(`<!--foo-->`)
234233
transform(ast, {})
235-
expect(ast.helpers).toContain(CREATE_VNODE)
236-
expect(ast.helpers).toContain(COMMENT)
234+
expect(ast.helpers).toContain(CREATE_COMMENT)
237235
})
238236

239237
describe('root codegenNode', () => {

packages/compiler-core/__tests__/transforms/__snapshots__/hoistStatic.spec.ts.snap

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,15 @@ return function render() {
4040
exports[`compiler: hoistStatic transform hoist nested static tree with comments 1`] = `
4141
"const _Vue = Vue
4242
const _createVNode = Vue.createVNode
43-
const _Comment = Vue.Comment
43+
const _createCommentVNode = Vue.createCommentVNode
4444
4545
const _hoisted_1 = _createVNode(\\"div\\", null, [
46-
_createVNode(_Comment, null, \\"comment\\")
46+
_createCommentVNode(\\"comment\\")
4747
])
4848
4949
return function render() {
5050
with (this) {
51-
const { createVNode: _createVNode, Comment: _Comment, createBlock: _createBlock, openBlock: _openBlock } = _Vue
51+
const { createCommentVNode: _createCommentVNode, createVNode: _createVNode, createBlock: _createBlock, openBlock: _openBlock } = _Vue
5252
5353
return (_openBlock(), _createBlock(\\"div\\", null, [
5454
_hoisted_1
@@ -372,7 +372,7 @@ return function render() {
372372
exports[`compiler: hoistStatic transform should hoist v-if props/children if static 1`] = `
373373
"const _Vue = Vue
374374
const _createVNode = Vue.createVNode
375-
const _Comment = Vue.Comment
375+
const _createCommentVNode = Vue.createCommentVNode
376376
377377
const _hoisted_1 = {
378378
key: 0,
@@ -382,14 +382,14 @@ const _hoisted_2 = _createVNode(\\"span\\")
382382
383383
return function render() {
384384
with (this) {
385-
const { openBlock: _openBlock, createVNode: _createVNode, createBlock: _createBlock, Comment: _Comment } = _Vue
385+
const { openBlock: _openBlock, createVNode: _createVNode, createBlock: _createBlock, createCommentVNode: _createCommentVNode } = _Vue
386386
387387
return (_openBlock(), _createBlock(\\"div\\", null, [
388388
(_openBlock(), ok
389389
? _createBlock(\\"div\\", _hoisted_1, [
390390
_hoisted_2
391391
])
392-
: _createBlock(_Comment))
392+
: _createCommentVNode())
393393
]))
394394
}
395395
}"

packages/compiler-core/__tests__/transforms/__snapshots__/transformText.spec.ts.snap

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ exports[`compiler: transform text consecutive text between elements 1`] = `
1717
1818
return function render() {
1919
with (this) {
20-
const { createVNode: _createVNode, toString: _toString, Text: _Text, createBlock: _createBlock, Fragment: _Fragment, openBlock: _openBlock } = _Vue
20+
const { createVNode: _createVNode, toString: _toString, createTextVNode: _createTextVNode, createBlock: _createBlock, Fragment: _Fragment, openBlock: _openBlock } = _Vue
2121
2222
return (_openBlock(), _createBlock(_Fragment, null, [
2323
_createVNode(\\"div\\"),
24-
_createVNode(_Text, null, _toString(foo) + \\" bar \\" + _toString(baz), 1 /* TEXT */),
24+
_createTextVNode(_toString(foo) + \\" bar \\" + _toString(baz), 1 /* TEXT */),
2525
_createVNode(\\"div\\")
2626
]))
2727
}
@@ -33,13 +33,13 @@ exports[`compiler: transform text consecutive text mixed with elements 1`] = `
3333
3434
return function render() {
3535
with (this) {
36-
const { createVNode: _createVNode, toString: _toString, Text: _Text, createBlock: _createBlock, Fragment: _Fragment, openBlock: _openBlock } = _Vue
36+
const { createVNode: _createVNode, toString: _toString, createTextVNode: _createTextVNode, createBlock: _createBlock, Fragment: _Fragment, openBlock: _openBlock } = _Vue
3737
3838
return (_openBlock(), _createBlock(_Fragment, null, [
3939
_createVNode(\\"div\\"),
40-
_createVNode(_Text, null, _toString(foo) + \\" bar \\" + _toString(baz), 1 /* TEXT */),
40+
_createTextVNode(_toString(foo) + \\" bar \\" + _toString(baz), 1 /* TEXT */),
4141
_createVNode(\\"div\\"),
42-
_createVNode(_Text, null, \\"hello\\"),
42+
_createTextVNode(\\"hello\\"),
4343
_createVNode(\\"div\\")
4444
]))
4545
}
@@ -63,11 +63,11 @@ exports[`compiler: transform text text between elements (static) 1`] = `
6363
6464
return function render() {
6565
with (this) {
66-
const { createVNode: _createVNode, Text: _Text, createBlock: _createBlock, Fragment: _Fragment, openBlock: _openBlock } = _Vue
66+
const { createVNode: _createVNode, createTextVNode: _createTextVNode, createBlock: _createBlock, Fragment: _Fragment, openBlock: _openBlock } = _Vue
6767
6868
return (_openBlock(), _createBlock(_Fragment, null, [
6969
_createVNode(\\"div\\"),
70-
_createVNode(_Text, null, \\"hello\\"),
70+
_createTextVNode(\\"hello\\"),
7171
_createVNode(\\"div\\")
7272
]))
7373
}

packages/compiler-core/__tests__/transforms/__snapshots__/vFor.spec.ts.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,13 +155,13 @@ exports[`compiler: v-for codegen v-if + v-for 1`] = `
155155
156156
return function render() {
157157
with (this) {
158-
const { openBlock: _openBlock, renderList: _renderList, createBlock: _createBlock, Fragment: _Fragment, createVNode: _createVNode, Comment: _Comment } = _Vue
158+
const { openBlock: _openBlock, renderList: _renderList, createBlock: _createBlock, Fragment: _Fragment, createVNode: _createVNode, createCommentVNode: _createCommentVNode } = _Vue
159159
160160
return (_openBlock(), ok
161161
? _createBlock(_Fragment, { key: 0 }, _renderList(list, (i) => {
162162
return (_openBlock(), _createBlock(\\"div\\"))
163163
}), 128 /* UNKEYED_FRAGMENT */)
164-
: _createBlock(_Comment))
164+
: _createCommentVNode())
165165
}
166166
}"
167167
`;

packages/compiler-core/__tests__/transforms/__snapshots__/vIf.spec.ts.snap

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ exports[`compiler: v-if codegen basic v-if 1`] = `
55
66
return function render() {
77
with (this) {
8-
const { openBlock: _openBlock, createVNode: _createVNode, createBlock: _createBlock, Comment: _Comment } = _Vue
8+
const { openBlock: _openBlock, createVNode: _createVNode, createBlock: _createBlock, createCommentVNode: _createCommentVNode } = _Vue
99
1010
return (_openBlock(), ok
1111
? _createBlock(\\"div\\", { key: 0 })
12-
: _createBlock(_Comment))
12+
: _createCommentVNode())
1313
}
1414
}"
1515
`;
@@ -19,15 +19,15 @@ exports[`compiler: v-if codegen template v-if 1`] = `
1919
2020
return function render() {
2121
with (this) {
22-
const { openBlock: _openBlock, createVNode: _createVNode, Fragment: _Fragment, createBlock: _createBlock, Comment: _Comment } = _Vue
22+
const { openBlock: _openBlock, createVNode: _createVNode, Fragment: _Fragment, createBlock: _createBlock, createCommentVNode: _createCommentVNode } = _Vue
2323
2424
return (_openBlock(), ok
2525
? _createBlock(_Fragment, { key: 0 }, [
2626
_createVNode(\\"div\\"),
2727
\\"hello\\",
2828
_createVNode(\\"p\\")
2929
])
30-
: _createBlock(_Comment))
30+
: _createCommentVNode())
3131
}
3232
}"
3333
`;
@@ -37,11 +37,11 @@ exports[`compiler: v-if codegen template v-if w/ single <slot/> child 1`] = `
3737
3838
return function render() {
3939
with (this) {
40-
const { openBlock: _openBlock, renderSlot: _renderSlot, createBlock: _createBlock, Comment: _Comment } = _Vue
40+
const { openBlock: _openBlock, renderSlot: _renderSlot, createCommentVNode: _createCommentVNode } = _Vue
4141
4242
return (_openBlock(), ok
4343
? _renderSlot($slots, \\"default\\", { key: 0 })
44-
: _createBlock(_Comment))
44+
: _createCommentVNode())
4545
}
4646
}"
4747
`;
@@ -51,7 +51,7 @@ exports[`compiler: v-if codegen v-if + v-else 1`] = `
5151
5252
return function render() {
5353
with (this) {
54-
const { openBlock: _openBlock, createVNode: _createVNode, createBlock: _createBlock, Comment: _Comment } = _Vue
54+
const { openBlock: _openBlock, createVNode: _createVNode, createBlock: _createBlock, createCommentVNode: _createCommentVNode } = _Vue
5555
5656
return (_openBlock(), ok
5757
? _createBlock(\\"div\\", { key: 0 })
@@ -65,7 +65,7 @@ exports[`compiler: v-if codegen v-if + v-else-if + v-else 1`] = `
6565
6666
return function render() {
6767
with (this) {
68-
const { openBlock: _openBlock, createVNode: _createVNode, createBlock: _createBlock, Comment: _Comment, Fragment: _Fragment } = _Vue
68+
const { openBlock: _openBlock, createVNode: _createVNode, createBlock: _createBlock, createCommentVNode: _createCommentVNode, Fragment: _Fragment } = _Vue
6969
7070
return (_openBlock(), ok
7171
? _createBlock(\\"div\\", { key: 0 })
@@ -81,13 +81,13 @@ exports[`compiler: v-if codegen v-if + v-else-if 1`] = `
8181
8282
return function render() {
8383
with (this) {
84-
const { openBlock: _openBlock, createVNode: _createVNode, createBlock: _createBlock, Comment: _Comment } = _Vue
84+
const { openBlock: _openBlock, createVNode: _createVNode, createBlock: _createBlock, createCommentVNode: _createCommentVNode } = _Vue
8585
8686
return (_openBlock(), ok
8787
? _createBlock(\\"div\\", { key: 0 })
8888
: orNot
8989
? _createBlock(\\"p\\", { key: 1 })
90-
: _createBlock(_Comment))
90+
: _createCommentVNode())
9191
}
9292
}"
9393
`;
@@ -97,11 +97,11 @@ exports[`compiler: v-if codegen v-if on <slot/> 1`] = `
9797
9898
return function render() {
9999
with (this) {
100-
const { openBlock: _openBlock, renderSlot: _renderSlot, createBlock: _createBlock, Comment: _Comment } = _Vue
100+
const { openBlock: _openBlock, renderSlot: _renderSlot, createCommentVNode: _createCommentVNode } = _Vue
101101
102102
return (_openBlock(), ok
103103
? _renderSlot($slots, \\"default\\", { key: 0 })
104-
: _createBlock(_Comment))
104+
: _createCommentVNode())
105105
}
106106
}"
107107
`;

packages/compiler-core/__tests__/transforms/transformText.spec.ts

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
import { transformText } from '../../src/transforms/transformText'
99
import { transformExpression } from '../../src/transforms/transformExpression'
1010
import { transformElement } from '../../src/transforms/transformElement'
11-
import { CREATE_VNODE, TEXT } from '../../src/runtimeHelpers'
11+
import { CREATE_TEXT } from '../../src/runtimeHelpers'
1212
import { genFlagText } from '../testUtils'
1313
import { PatchFlags } from '@vue/shared'
1414

@@ -62,10 +62,8 @@ describe('compiler: transform text', () => {
6262
type: NodeTypes.TEXT_CALL,
6363
codegenNode: {
6464
type: NodeTypes.JS_CALL_EXPRESSION,
65-
callee: CREATE_VNODE,
65+
callee: CREATE_TEXT,
6666
arguments: [
67-
TEXT,
68-
`null`,
6967
{
7068
type: NodeTypes.COMPOUND_EXPRESSION,
7169
children: [
@@ -93,10 +91,8 @@ describe('compiler: transform text', () => {
9391
type: NodeTypes.TEXT_CALL,
9492
codegenNode: {
9593
type: NodeTypes.JS_CALL_EXPRESSION,
96-
callee: CREATE_VNODE,
94+
callee: CREATE_TEXT,
9795
arguments: [
98-
TEXT,
99-
`null`,
10096
{
10197
type: NodeTypes.TEXT,
10298
content: `hello`
@@ -119,10 +115,8 @@ describe('compiler: transform text', () => {
119115
type: NodeTypes.TEXT_CALL,
120116
codegenNode: {
121117
type: NodeTypes.JS_CALL_EXPRESSION,
122-
callee: CREATE_VNODE,
118+
callee: CREATE_TEXT,
123119
arguments: [
124-
TEXT,
125-
`null`,
126120
{
127121
type: NodeTypes.COMPOUND_EXPRESSION,
128122
children: [
@@ -142,10 +136,8 @@ describe('compiler: transform text', () => {
142136
type: NodeTypes.TEXT_CALL,
143137
codegenNode: {
144138
type: NodeTypes.JS_CALL_EXPRESSION,
145-
callee: CREATE_VNODE,
139+
callee: CREATE_TEXT,
146140
arguments: [
147-
TEXT,
148-
`null`,
149141
{
150142
type: NodeTypes.TEXT,
151143
content: `hello`

0 commit comments

Comments
 (0)