Skip to content

Commit

Permalink
refactor(compiler): better constant hoist/stringify checks
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Nov 21, 2020
1 parent acba86e commit 90bdf59
Show file tree
Hide file tree
Showing 22 changed files with 291 additions and 204 deletions.
24 changes: 12 additions & 12 deletions packages/compiler-core/__tests__/__snapshots__/parse.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3322,8 +3322,8 @@ Object {
"children": Array [
Object {
"content": Object {
"constType": 0,
"content": "a < b",
"isConstant": false,
"isStatic": false,
"loc": Object {
"end": Object {
Expand Down Expand Up @@ -6084,8 +6084,8 @@ Object {
"children": Array [
Object {
"content": Object {
"constType": 0,
"content": "'</div>'",
"isConstant": false,
"isStatic": false,
"loc": Object {
"end": Object {
Expand Down Expand Up @@ -6259,8 +6259,8 @@ Object {
"props": Array [
Object {
"arg": Object {
"constType": 0,
"content": "se",
"isConstant": false,
"isStatic": false,
"loc": Object {
"end": Object {
Expand Down Expand Up @@ -6593,8 +6593,8 @@ Object {
"children": Array [
Object {
"content": Object {
"constType": 0,
"content": "",
"isConstant": false,
"isStatic": false,
"loc": Object {
"end": Object {
Expand Down Expand Up @@ -6758,8 +6758,8 @@ Object {
"props": Array [
Object {
"arg": Object {
"constType": 3,
"content": "class",
"isConstant": true,
"isStatic": true,
"loc": Object {
"end": Object {
Expand All @@ -6777,8 +6777,8 @@ Object {
"type": 4,
},
"exp": Object {
"constType": 0,
"content": "{ some: condition }",
"isConstant": false,
"isStatic": false,
"loc": Object {
"end": Object {
Expand Down Expand Up @@ -6838,8 +6838,8 @@ Object {
"props": Array [
Object {
"arg": Object {
"constType": 3,
"content": "style",
"isConstant": true,
"isStatic": true,
"loc": Object {
"end": Object {
Expand All @@ -6857,8 +6857,8 @@ Object {
"type": 4,
},
"exp": Object {
"constType": 0,
"content": "{ color: 'red' }",
"isConstant": false,
"isStatic": false,
"loc": Object {
"end": Object {
Expand Down Expand Up @@ -6950,8 +6950,8 @@ Object {
"props": Array [
Object {
"arg": Object {
"constType": 3,
"content": "style",
"isConstant": true,
"isStatic": true,
"loc": Object {
"end": Object {
Expand All @@ -6969,8 +6969,8 @@ Object {
"type": 4,
},
"exp": Object {
"constType": 0,
"content": "{ color: 'red' }",
"isConstant": false,
"isStatic": false,
"loc": Object {
"end": Object {
Expand Down Expand Up @@ -7049,8 +7049,8 @@ Object {
"props": Array [
Object {
"arg": Object {
"constType": 3,
"content": "class",
"isConstant": true,
"isStatic": true,
"loc": Object {
"end": Object {
Expand All @@ -7068,8 +7068,8 @@ Object {
"type": 4,
},
"exp": Object {
"constType": 0,
"content": "{ some: condition }",
"isConstant": false,
"isStatic": false,
"loc": Object {
"end": Object {
Expand Down
10 changes: 8 additions & 2 deletions packages/compiler-core/__tests__/codegen.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import {
IfConditionalExpression,
createVNodeCall,
VNodeCall,
DirectiveArguments
DirectiveArguments,
ConstantTypes
} from '../src'
import {
CREATE_VNODE,
Expand Down Expand Up @@ -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,
Expand Down
51 changes: 26 additions & 25 deletions packages/compiler-core/__tests__/parse.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import {
NodeTypes,
Position,
TextNode,
InterpolationNode
InterpolationNode,
ConstantTypes
} from '../src/ast'

describe('compiler: parse', () => {
Expand Down Expand Up @@ -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 },
Expand All @@ -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 },
Expand All @@ -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 },
Expand All @@ -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 },
Expand All @@ -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 },
Expand Down Expand Up @@ -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 },
Expand Down Expand Up @@ -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 },
Expand All @@ -1054,7 +1055,7 @@ describe('compiler: parse', () => {
type: NodeTypes.SIMPLE_EXPRESSION,
content: 'click',
isStatic: true,
isConstant: true,
constType: ConstantTypes.CAN_STRINGIFY,

loc: {
source: 'click',
Expand Down Expand Up @@ -1091,7 +1092,7 @@ describe('compiler: parse', () => {
type: NodeTypes.SIMPLE_EXPRESSION,
content: 'event',
isStatic: false,
isConstant: false,
constType: ConstantTypes.NOT_CONSTANT,

loc: {
source: '[event]',
Expand Down Expand Up @@ -1164,7 +1165,7 @@ describe('compiler: parse', () => {
type: NodeTypes.SIMPLE_EXPRESSION,
content: 'click',
isStatic: true,
isConstant: true,
constType: ConstantTypes.CAN_STRINGIFY,

loc: {
source: 'click',
Expand Down Expand Up @@ -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]',
Expand Down Expand Up @@ -1238,7 +1239,7 @@ describe('compiler: parse', () => {
type: NodeTypes.SIMPLE_EXPRESSION,
content: 'a',
isStatic: true,
isConstant: true,
constType: ConstantTypes.CAN_STRINGIFY,

loc: {
source: 'a',
Expand All @@ -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 },
Expand All @@ -1286,7 +1287,7 @@ describe('compiler: parse', () => {
type: NodeTypes.SIMPLE_EXPRESSION,
content: 'a',
isStatic: true,
isConstant: true,
constType: ConstantTypes.CAN_STRINGIFY,

loc: {
source: 'a',
Expand All @@ -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 },
Expand All @@ -1334,7 +1335,7 @@ describe('compiler: parse', () => {
type: NodeTypes.SIMPLE_EXPRESSION,
content: 'a',
isStatic: true,
isConstant: true,
constType: ConstantTypes.CAN_STRINGIFY,

loc: {
source: 'a',
Expand All @@ -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 },
Expand All @@ -1382,7 +1383,7 @@ describe('compiler: parse', () => {
type: NodeTypes.SIMPLE_EXPRESSION,
content: 'a',
isStatic: true,
isConstant: true,
constType: ConstantTypes.CAN_STRINGIFY,

loc: {
source: 'a',
Expand All @@ -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 },
Expand All @@ -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: {
Expand All @@ -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 },
Expand All @@ -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: {
Expand Down
Loading

0 comments on commit 90bdf59

Please sign in to comment.