Skip to content

Commit

Permalink
fix(compat): correctly transform non-identifier expressions in legacy…
Browse files Browse the repository at this point in the history
… filter syntax (#10896)

close #10852
  • Loading branch information
haoqunjiang authored May 27, 2024
1 parent 37f9ef8 commit 07b3c4b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/compiler-core/src/compat/transformFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ function parseFilter(node: SimpleExpressionNode, context: TransformContext) {
expression = wrapFilter(expression, filters[i], context)
}
node.content = expression
// reset ast since the content is replaced
node.ast = undefined
}
}

Expand Down
31 changes: 31 additions & 0 deletions packages/compiler-sfc/__tests__/compileTemplate.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { type RawSourceMap, SourceMapConsumer } from 'source-map-js'
import { parse as babelParse } from '@babel/parser'
import {
type SFCTemplateCompileOptions,
compileTemplate,
Expand Down Expand Up @@ -452,6 +453,36 @@ test('prefixing edge case for reused AST ssr mode', () => {
).not.toThrowError()
})

// #10852
test('non-identifier expression in legacy filter syntax', () => {
const src = `
<template>
<div>
Today is
{{ new Date() | formatDate }}
</div>
</template>
`

const { descriptor } = parse(src)
const compilationResult = compileTemplate({
id: 'xxx',
filename: 'test.vue',
ast: descriptor.template!.ast,
source: descriptor.template!.content,
ssr: false,
compilerOptions: {
compatConfig: {
MODE: 2,
},
},
})

expect(() => {
babelParse(compilationResult.code, { sourceType: 'module' })
}).not.toThrow()
})

interface Pos {
line: number
column: number
Expand Down

0 comments on commit 07b3c4b

Please sign in to comment.