Skip to content

Commit

Permalink
fix(compiler-core): treat floating point numbers as constants
Browse files Browse the repository at this point in the history
close #8295
  • Loading branch information
yyx990803 committed May 12, 2023
1 parent c454b9d commit 8dc8cf8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,16 @@ describe('compiler: expression transform', () => {
})
})

// #8295
test('should treat floating point number literals as constant', () => {
const node = parseWithExpressionTransform(
`{{ [1, 2.1] }}`
) as InterpolationNode
expect(node.content).toMatchObject({
constType: ConstantTypes.CAN_STRINGIFY
})
})

describe('ES Proposals support', () => {
test('bigInt', () => {
const node = parseWithExpressionTransform(
Expand Down
6 changes: 5 additions & 1 deletion packages/compiler-core/src/transforms/transformExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ import { BindingTypes } from '../options'

const isLiteralWhitelisted = /*#__PURE__*/ makeMap('true,false,null,this')

// a heuristic safeguard to bail constant expressions on presence of
// likely function invocation and member access
const constantBailRE = /\w\s*\(|\.[^\d]/

export const transformExpression: NodeTransform = (node, context) => {
if (node.type === NodeTypes.INTERPOLATION) {
node.content = processExpression(
Expand Down Expand Up @@ -217,7 +221,7 @@ export function processExpression(
// fast path if expression is a simple identifier.
const rawExp = node.content
// bail constant on parens (function invocation) and dot (member access)
const bailConstant = rawExp.indexOf(`(`) > -1 || rawExp.indexOf('.') > 0
const bailConstant = constantBailRE.test(rawExp)

if (isSimpleIdentifier(rawExp)) {
const isScopeVarReference = context.identifiers[rawExp]
Expand Down

0 comments on commit 8dc8cf8

Please sign in to comment.