Skip to content

Commit 8dc8cf8

Browse files
committed
fix(compiler-core): treat floating point numbers as constants
close #8295
1 parent c454b9d commit 8dc8cf8

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

packages/compiler-core/__tests__/transforms/transformExpressions.spec.ts

+10
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,16 @@ describe('compiler: expression transform', () => {
431431
})
432432
})
433433

434+
// #8295
435+
test('should treat floating point number literals as constant', () => {
436+
const node = parseWithExpressionTransform(
437+
`{{ [1, 2.1] }}`
438+
) as InterpolationNode
439+
expect(node.content).toMatchObject({
440+
constType: ConstantTypes.CAN_STRINGIFY
441+
})
442+
})
443+
434444
describe('ES Proposals support', () => {
435445
test('bigInt', () => {
436446
const node = parseWithExpressionTransform(

packages/compiler-core/src/transforms/transformExpression.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ import { BindingTypes } from '../options'
4545

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

48+
// a heuristic safeguard to bail constant expressions on presence of
49+
// likely function invocation and member access
50+
const constantBailRE = /\w\s*\(|\.[^\d]/
51+
4852
export const transformExpression: NodeTransform = (node, context) => {
4953
if (node.type === NodeTypes.INTERPOLATION) {
5054
node.content = processExpression(
@@ -217,7 +221,7 @@ export function processExpression(
217221
// fast path if expression is a simple identifier.
218222
const rawExp = node.content
219223
// bail constant on parens (function invocation) and dot (member access)
220-
const bailConstant = rawExp.indexOf(`(`) > -1 || rawExp.indexOf('.') > 0
224+
const bailConstant = constantBailRE.test(rawExp)
221225

222226
if (isSimpleIdentifier(rawExp)) {
223227
const isScopeVarReference = context.identifiers[rawExp]

0 commit comments

Comments
 (0)