Skip to content

Commit ff2313e

Browse files
committed
wip(compiler): adjust statement positions
1 parent b43f3b6 commit ff2313e

File tree

5 files changed

+43
-20
lines changed

5 files changed

+43
-20
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,10 @@ return function render() {
7171
`;
7272

7373
exports[`compiler: codegen function mode preamble 1`] = `
74-
"const { helperOne, helperTwo } = Vue
75-
74+
"const _Vue = Vue
7675
return function render() {
7776
with (this) {
77+
const { helperOne, helperTwo } = _Vue
7878
return null
7979
}
8080
}"
@@ -147,10 +147,10 @@ return function render() {
147147
exports[`compiler: codegen statements 1`] = `
148148
"
149149
return function render() {
150-
const a = 1
151-
const b = 2
152-
153150
with (this) {
151+
const a = 1
152+
const b = 2
153+
154154
return null
155155
}
156156
}"

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ describe('compiler: codegen', () => {
5555
imports: [`helperOne`, `helperTwo`]
5656
})
5757
const { code } = generate(root, { mode: 'function' })
58-
expect(code).toMatch(`const { helperOne, helperTwo } = Vue`)
58+
expect(code).toMatch(`const _Vue = Vue`)
59+
expect(code).toMatch(`const { helperOne, helperTwo } = _Vue`)
5960
expect(code).toMatchSnapshot()
6061
})
6162

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ test('basic source map support', async () => {
99
filename: `foo.vue`
1010
})
1111
expect(code).toMatch(
12-
`const { toString } = Vue
13-
12+
`const _Vue = Vue
1413
return function render() {
1514
with (this) {
15+
const { toString } = _Vue
1616
return [
1717
"hello ",
1818
toString(world)

packages/compiler-core/src/codegen.ts

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,20 @@ export function generate(
145145
const context = createCodegenContext(ast, options)
146146
const { mode, push, prefixIdentifiers, indent, deindent, newline } = context
147147
const imports = ast.imports.join(', ')
148+
149+
// preambles
148150
if (mode === 'function') {
149-
// generate const declarations for helpers
151+
// Generate const declaration for helpers
152+
// In prefix mode, we place the const declaration at top so it's done
153+
// only once; But if we not prefixing, we place the decalration inside the
154+
// with block so it doesn't incur the `in` check cost for every helper access.
150155
if (imports) {
151-
push(`const { ${imports} } = Vue\n`)
156+
if (prefixIdentifiers) {
157+
push(`const { ${imports} } = Vue\n`)
158+
} else {
159+
// save Vue in a separate variable to avoid collision
160+
push(`const _Vue = Vue`)
161+
}
152162
}
153163
genHoists(ast.hoists, context)
154164
push(`return `)
@@ -160,8 +170,24 @@ export function generate(
160170
genHoists(ast.hoists, context)
161171
push(`export default `)
162172
}
173+
174+
// enter render function
163175
push(`function render() {`)
164176
indent()
177+
178+
if (!prefixIdentifiers) {
179+
push(`with (this) {`)
180+
indent()
181+
// function mode const declarations should be inside with block
182+
if (mode === 'function' && imports) {
183+
push(`const { ${imports} } = _Vue`)
184+
newline()
185+
}
186+
} else {
187+
push(`const _ctx = this`)
188+
newline()
189+
}
190+
165191
// generate asset resolution statements
166192
if (ast.statements.length) {
167193
ast.statements.forEach(s => {
@@ -170,13 +196,8 @@ export function generate(
170196
})
171197
newline()
172198
}
173-
if (!prefixIdentifiers) {
174-
push(`with (this) {`)
175-
indent()
176-
} else {
177-
push(`const _ctx = this`)
178-
newline()
179-
}
199+
200+
// generate the VNode tree expression
180201
push(`return `)
181202
genChildren(ast.children, context, true /* asRoot */)
182203
if (!prefixIdentifiers) {

packages/runtime-core/src/componentPublicInstanceProxy.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,10 @@ export const PublicInstanceProxyHandlers = {
8282
const { renderContext, data, props } = target
8383
// TODO handle $xxx properties
8484
return (
85-
(data !== EMPTY_OBJ && hasOwn(data, key)) ||
86-
hasOwn(renderContext, key) ||
87-
hasOwn(props, key)
85+
key[0] !== '_' &&
86+
((data !== EMPTY_OBJ && hasOwn(data, key)) ||
87+
hasOwn(renderContext, key) ||
88+
hasOwn(props, key))
8889
)
8990
},
9091
set(target: ComponentInternalInstance, key: string, value: any): boolean {

0 commit comments

Comments
 (0)