refactor(compiler): better constant hoist/stringify checks

This commit is contained in:
Evan You
2020-11-20 19:26:07 -05:00
parent acba86ef45
commit 90bdf59f4c
22 changed files with 291 additions and 204 deletions

View File

@@ -3322,8 +3322,8 @@ Object {
"children": Array [
Object {
"content": Object {
"constType": 0,
"content": "a < b",
"isConstant": false,
"isStatic": false,
"loc": Object {
"end": Object {
@@ -6084,8 +6084,8 @@ Object {
"children": Array [
Object {
"content": Object {
"constType": 0,
"content": "'</div>'",
"isConstant": false,
"isStatic": false,
"loc": Object {
"end": Object {
@@ -6259,8 +6259,8 @@ Object {
"props": Array [
Object {
"arg": Object {
"constType": 0,
"content": "se",
"isConstant": false,
"isStatic": false,
"loc": Object {
"end": Object {
@@ -6593,8 +6593,8 @@ Object {
"children": Array [
Object {
"content": Object {
"constType": 0,
"content": "",
"isConstant": false,
"isStatic": false,
"loc": Object {
"end": Object {
@@ -6758,8 +6758,8 @@ Object {
"props": Array [
Object {
"arg": Object {
"constType": 3,
"content": "class",
"isConstant": true,
"isStatic": true,
"loc": Object {
"end": Object {
@@ -6777,8 +6777,8 @@ Object {
"type": 4,
},
"exp": Object {
"constType": 0,
"content": "{ some: condition }",
"isConstant": false,
"isStatic": false,
"loc": Object {
"end": Object {
@@ -6838,8 +6838,8 @@ Object {
"props": Array [
Object {
"arg": Object {
"constType": 3,
"content": "style",
"isConstant": true,
"isStatic": true,
"loc": Object {
"end": Object {
@@ -6857,8 +6857,8 @@ Object {
"type": 4,
},
"exp": Object {
"constType": 0,
"content": "{ color: 'red' }",
"isConstant": false,
"isStatic": false,
"loc": Object {
"end": Object {
@@ -6950,8 +6950,8 @@ Object {
"props": Array [
Object {
"arg": Object {
"constType": 3,
"content": "style",
"isConstant": true,
"isStatic": true,
"loc": Object {
"end": Object {
@@ -6969,8 +6969,8 @@ Object {
"type": 4,
},
"exp": Object {
"constType": 0,
"content": "{ color: 'red' }",
"isConstant": false,
"isStatic": false,
"loc": Object {
"end": Object {
@@ -7049,8 +7049,8 @@ Object {
"props": Array [
Object {
"arg": Object {
"constType": 3,
"content": "class",
"isConstant": true,
"isStatic": true,
"loc": Object {
"end": Object {
@@ -7068,8 +7068,8 @@ Object {
"type": 4,
},
"exp": Object {
"constType": 0,
"content": "{ some: condition }",
"isConstant": false,
"isStatic": false,
"loc": Object {
"end": Object {

View File

@@ -20,7 +20,8 @@ import {
IfConditionalExpression,
createVNodeCall,
VNodeCall,
DirectiveArguments
DirectiveArguments,
ConstantTypes
} from '../src'
import {
CREATE_VNODE,
@@ -304,7 +305,12 @@ describe('compiler: codegen', () => {
codegenNode: {
type: NodeTypes.FOR,
loc: locStub,
source: createSimpleExpression('1 + 2', false, locStub, true),
source: createSimpleExpression(
'1 + 2',
false,
locStub,
ConstantTypes.CAN_STRINGIFY
),
valueAlias: undefined,
keyAlias: undefined,
objectIndexAlias: undefined,

View File

@@ -9,7 +9,8 @@ import {
NodeTypes,
Position,
TextNode,
InterpolationNode
InterpolationNode,
ConstantTypes
} from '../src/ast'
describe('compiler: parse', () => {
@@ -177,7 +178,7 @@ describe('compiler: parse', () => {
type: NodeTypes.SIMPLE_EXPRESSION,
content: `message`,
isStatic: false,
isConstant: false,
constType: ConstantTypes.NOT_CONSTANT,
loc: {
start: { offset: 2, line: 1, column: 3 },
end: { offset: 9, line: 1, column: 10 },
@@ -202,7 +203,7 @@ describe('compiler: parse', () => {
type: NodeTypes.SIMPLE_EXPRESSION,
content: `a<b`,
isStatic: false,
isConstant: false,
constType: ConstantTypes.NOT_CONSTANT,
loc: {
start: { offset: 3, line: 1, column: 4 },
end: { offset: 6, line: 1, column: 7 },
@@ -228,7 +229,7 @@ describe('compiler: parse', () => {
type: NodeTypes.SIMPLE_EXPRESSION,
content: `a<b`,
isStatic: false,
isConstant: false,
constType: ConstantTypes.NOT_CONSTANT,
loc: {
start: { offset: 3, line: 1, column: 4 },
end: { offset: 6, line: 1, column: 7 },
@@ -247,7 +248,7 @@ describe('compiler: parse', () => {
content: {
type: NodeTypes.SIMPLE_EXPRESSION,
isStatic: false,
isConstant: false,
constType: ConstantTypes.NOT_CONSTANT,
content: 'c>d',
loc: {
start: { offset: 12, line: 1, column: 13 },
@@ -273,8 +274,8 @@ describe('compiler: parse', () => {
content: {
type: NodeTypes.SIMPLE_EXPRESSION,
isStatic: false,
// The `isConstant` is the default value and will be determined in `transformExpression`.
isConstant: false,
// The `constType` is the default value and will be determined in `transformExpression`.
constType: ConstantTypes.NOT_CONSTANT,
content: '"</div>"',
loc: {
start: { offset: 8, line: 1, column: 9 },
@@ -303,7 +304,7 @@ describe('compiler: parse', () => {
type: NodeTypes.SIMPLE_EXPRESSION,
content: `msg`,
isStatic: false,
isConstant: false,
constType: ConstantTypes.NOT_CONSTANT,
loc: {
start: { offset: 4, line: 1, column: 5 },
end: { offset: 7, line: 1, column: 8 },
@@ -1028,7 +1029,7 @@ describe('compiler: parse', () => {
type: NodeTypes.SIMPLE_EXPRESSION,
content: 'a',
isStatic: false,
isConstant: false,
constType: ConstantTypes.NOT_CONSTANT,
loc: {
start: { offset: 11, line: 1, column: 12 },
end: { offset: 12, line: 1, column: 13 },
@@ -1054,7 +1055,7 @@ describe('compiler: parse', () => {
type: NodeTypes.SIMPLE_EXPRESSION,
content: 'click',
isStatic: true,
isConstant: true,
constType: ConstantTypes.CAN_STRINGIFY,
loc: {
source: 'click',
@@ -1091,7 +1092,7 @@ describe('compiler: parse', () => {
type: NodeTypes.SIMPLE_EXPRESSION,
content: 'event',
isStatic: false,
isConstant: false,
constType: ConstantTypes.NOT_CONSTANT,
loc: {
source: '[event]',
@@ -1164,7 +1165,7 @@ describe('compiler: parse', () => {
type: NodeTypes.SIMPLE_EXPRESSION,
content: 'click',
isStatic: true,
isConstant: true,
constType: ConstantTypes.CAN_STRINGIFY,
loc: {
source: 'click',
@@ -1201,7 +1202,7 @@ describe('compiler: parse', () => {
type: NodeTypes.SIMPLE_EXPRESSION,
content: 'a.b',
isStatic: false,
isConstant: false,
constType: ConstantTypes.NOT_CONSTANT,
loc: {
source: '[a.b]',
@@ -1238,7 +1239,7 @@ describe('compiler: parse', () => {
type: NodeTypes.SIMPLE_EXPRESSION,
content: 'a',
isStatic: true,
isConstant: true,
constType: ConstantTypes.CAN_STRINGIFY,
loc: {
source: 'a',
@@ -1259,7 +1260,7 @@ describe('compiler: parse', () => {
type: NodeTypes.SIMPLE_EXPRESSION,
content: 'b',
isStatic: false,
isConstant: false,
constType: ConstantTypes.NOT_CONSTANT,
loc: {
start: { offset: 8, line: 1, column: 9 },
@@ -1286,7 +1287,7 @@ describe('compiler: parse', () => {
type: NodeTypes.SIMPLE_EXPRESSION,
content: 'a',
isStatic: true,
isConstant: true,
constType: ConstantTypes.CAN_STRINGIFY,
loc: {
source: 'a',
@@ -1307,7 +1308,7 @@ describe('compiler: parse', () => {
type: NodeTypes.SIMPLE_EXPRESSION,
content: 'b',
isStatic: false,
isConstant: false,
constType: ConstantTypes.NOT_CONSTANT,
loc: {
start: { offset: 13, line: 1, column: 14 },
@@ -1334,7 +1335,7 @@ describe('compiler: parse', () => {
type: NodeTypes.SIMPLE_EXPRESSION,
content: 'a',
isStatic: true,
isConstant: true,
constType: ConstantTypes.CAN_STRINGIFY,
loc: {
source: 'a',
@@ -1355,7 +1356,7 @@ describe('compiler: parse', () => {
type: NodeTypes.SIMPLE_EXPRESSION,
content: 'b',
isStatic: false,
isConstant: false,
constType: ConstantTypes.NOT_CONSTANT,
loc: {
start: { offset: 8, line: 1, column: 9 },
@@ -1382,7 +1383,7 @@ describe('compiler: parse', () => {
type: NodeTypes.SIMPLE_EXPRESSION,
content: 'a',
isStatic: true,
isConstant: true,
constType: ConstantTypes.CAN_STRINGIFY,
loc: {
source: 'a',
@@ -1403,7 +1404,7 @@ describe('compiler: parse', () => {
type: NodeTypes.SIMPLE_EXPRESSION,
content: 'b',
isStatic: false,
isConstant: false,
constType: ConstantTypes.NOT_CONSTANT,
loc: {
start: { offset: 14, line: 1, column: 15 },
@@ -1430,7 +1431,7 @@ describe('compiler: parse', () => {
type: NodeTypes.SIMPLE_EXPRESSION,
content: 'a',
isStatic: true,
isConstant: true,
constType: ConstantTypes.CAN_STRINGIFY,
loc: {
source: 'a',
start: {
@@ -1450,8 +1451,8 @@ describe('compiler: parse', () => {
type: NodeTypes.SIMPLE_EXPRESSION,
content: '{ b }',
isStatic: false,
// The `isConstant` is the default value and will be determined in transformExpression
isConstant: false,
// The `constType` is the default value and will be determined in transformExpression
constType: ConstantTypes.NOT_CONSTANT,
loc: {
start: { offset: 10, line: 1, column: 11 },
end: { offset: 15, line: 1, column: 16 },
@@ -1478,7 +1479,7 @@ describe('compiler: parse', () => {
type: NodeTypes.SIMPLE_EXPRESSION,
content: 'foo.bar',
isStatic: true,
isConstant: true,
constType: ConstantTypes.CAN_STRINGIFY,
loc: {
source: 'foo.bar',
start: {

View File

@@ -7,7 +7,8 @@ import {
VNodeCall,
IfNode,
ElementNode,
ForNode
ForNode,
ConstantTypes
} from '../../src'
import { FRAGMENT, RENDER_LIST, CREATE_TEXT } from '../../src/runtimeHelpers'
import { transformElement } from '../../src/transforms/transformElement'
@@ -469,7 +470,7 @@ describe('compiler: hoistStatic transform', () => {
content: {
content: `1`,
isStatic: false,
isConstant: true
constType: ConstantTypes.CAN_STRINGIFY
}
}
}
@@ -505,13 +506,13 @@ describe('compiler: hoistStatic transform', () => {
{
key: {
content: `class`,
isConstant: true,
isStatic: true
isStatic: true,
constType: ConstantTypes.CAN_STRINGIFY
},
value: {
content: `{ foo: true }`,
isConstant: true,
isStatic: false
isStatic: false,
constType: ConstantTypes.CAN_STRINGIFY
}
}
]
@@ -534,8 +535,8 @@ describe('compiler: hoistStatic transform', () => {
type: NodeTypes.INTERPOLATION,
content: {
content: `_ctx.bar`,
isConstant: false,
isStatic: false
isStatic: false,
constType: ConstantTypes.NOT_CONSTANT
}
},
patchFlag: `1 /* TEXT */`

View File

@@ -5,7 +5,8 @@ import {
DirectiveNode,
NodeTypes,
CompilerOptions,
InterpolationNode
InterpolationNode,
ConstantTypes
} from '../../src'
import { transformIf } from '../../src/transforms/vIf'
import { transformExpression } from '../../src/transforms/transformExpression'
@@ -408,7 +409,7 @@ describe('compiler: expression transform', () => {
type: NodeTypes.SIMPLE_EXPRESSION,
content: `13000n`,
isStatic: false,
isConstant: true
constType: ConstantTypes.CAN_STRINGIFY
})
})

View File

@@ -12,7 +12,8 @@ import {
SimpleExpressionNode,
ElementNode,
InterpolationNode,
ForCodegenNode
ForCodegenNode,
ConstantTypes
} from '../../src/ast'
import { ErrorCodes } from '../../src/errors'
import { CompilerOptions, generate } from '../../src'
@@ -760,7 +761,7 @@ describe('compiler: v-for', () => {
false /* disableTracking */
)
).toMatchObject({
source: { content: `10`, isConstant: true },
source: { content: `10`, constType: ConstantTypes.CAN_STRINGIFY },
params: [{ content: `item` }],
innerVNodeCall: {
tag: `"p"`,
@@ -772,7 +773,7 @@ describe('compiler: v-for', () => {
type: NodeTypes.SIMPLE_EXPRESSION,
content: 'item',
isStatic: false,
isConstant: false
constType: ConstantTypes.NOT_CONSTANT
}
},
patchFlag: genFlagText(PatchFlags.TEXT)