feat(compiler): hoist static trees

This commit is contained in:
Evan You
2019-10-03 23:30:25 -04:00
parent 2e2b6924da
commit 095f5edf8d
11 changed files with 149 additions and 21 deletions

View File

@@ -5,7 +5,8 @@ import {
ObjectExpression,
CompilerOptions,
ErrorCodes,
NodeTypes
NodeTypes,
CallExpression
} from '../../src'
import { transformOn } from '../../src/transforms/vOn'
import { transformElement } from '../../src/transforms/transformElement'
@@ -29,7 +30,8 @@ function parseWithVOn(
describe('compiler: transform v-on', () => {
test('basic', () => {
const node = parseWithVOn(`<div v-on:click="onClick"/>`)
const props = node.codegenNode!.arguments[1] as ObjectExpression
const props = (node.codegenNode as CallExpression)
.arguments[1] as ObjectExpression
expect(props.properties[0]).toMatchObject({
key: {
content: `onClick`,
@@ -64,7 +66,8 @@ describe('compiler: transform v-on', () => {
test('dynamic arg', () => {
const node = parseWithVOn(`<div v-on:[event]="handler"/>`)
const props = node.codegenNode!.arguments[1] as ObjectExpression
const props = (node.codegenNode as CallExpression)
.arguments[1] as ObjectExpression
expect(props.properties[0]).toMatchObject({
key: {
type: NodeTypes.COMPOUND_EXPRESSION,
@@ -82,7 +85,8 @@ describe('compiler: transform v-on', () => {
const node = parseWithVOn(`<div v-on:[event]="handler"/>`, {
prefixIdentifiers: true
})
const props = node.codegenNode!.arguments[1] as ObjectExpression
const props = (node.codegenNode as CallExpression)
.arguments[1] as ObjectExpression
expect(props.properties[0]).toMatchObject({
key: {
type: NodeTypes.COMPOUND_EXPRESSION,
@@ -100,7 +104,8 @@ describe('compiler: transform v-on', () => {
const node = parseWithVOn(`<div v-on:[event(foo)]="handler"/>`, {
prefixIdentifiers: true
})
const props = node.codegenNode!.arguments[1] as ObjectExpression
const props = (node.codegenNode as CallExpression)
.arguments[1] as ObjectExpression
expect(props.properties[0]).toMatchObject({
key: {
type: NodeTypes.COMPOUND_EXPRESSION,
@@ -123,7 +128,8 @@ describe('compiler: transform v-on', () => {
test('should wrap as function if expression is inline statement', () => {
const node = parseWithVOn(`<div @click="i++"/>`)
const props = node.codegenNode!.arguments[1] as ObjectExpression
const props = (node.codegenNode as CallExpression)
.arguments[1] as ObjectExpression
expect(props.properties[0]).toMatchObject({
key: { content: `onClick` },
value: {
@@ -137,7 +143,8 @@ describe('compiler: transform v-on', () => {
const node = parseWithVOn(`<div @click="foo($event)"/>`, {
prefixIdentifiers: true
})
const props = node.codegenNode!.arguments[1] as ObjectExpression
const props = (node.codegenNode as CallExpression)
.arguments[1] as ObjectExpression
expect(props.properties[0]).toMatchObject({
key: { content: `onClick` },
value: {
@@ -157,7 +164,8 @@ describe('compiler: transform v-on', () => {
test('should NOT wrap as function if expression is already function expression', () => {
const node = parseWithVOn(`<div @click="$event => foo($event)"/>`)
const props = node.codegenNode!.arguments[1] as ObjectExpression
const props = (node.codegenNode as CallExpression)
.arguments[1] as ObjectExpression
expect(props.properties[0]).toMatchObject({
key: { content: `onClick` },
value: {
@@ -171,7 +179,8 @@ describe('compiler: transform v-on', () => {
const node = parseWithVOn(`<div @click="e => foo(e)"/>`, {
prefixIdentifiers: true
})
const props = node.codegenNode!.arguments[1] as ObjectExpression
const props = (node.codegenNode as CallExpression)
.arguments[1] as ObjectExpression
expect(props.properties[0]).toMatchObject({
key: { content: `onClick` },
value: {

View File

@@ -6,7 +6,8 @@ import {
ElementNode,
NodeTypes,
ErrorCodes,
ForNode
ForNode,
CallExpression
} from '../../src'
import { transformElement } from '../../src/transforms/transformElement'
import { transformOn } from '../../src/transforms/vOn'
@@ -44,7 +45,7 @@ function parseWithSlots(template: string, options: CompilerOptions = {}) {
root: ast,
slots:
ast.children[0].type === NodeTypes.ELEMENT
? ast.children[0].codegenNode!.arguments[2]
? (ast.children[0].codegenNode as CallExpression).arguments[2]
: null
}
}