Skip to content

fix: add support for multiple statements in v-on snippets #36

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ Install package:

```sh
# npm
npm install vue-sfc-transformer
npm install vue-sfc-transformer vue @vue/compiler-core esbuild

# pnpm
pnpm install vue-sfc-transformer
pnpm install vue-sfc-transformer vue @vue/compiler-core esbuild
```

```js
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"test:types": "tsc --noEmit"
},
"peerDependencies": {
"@vue/compiler-core": "^3.5.13",
"esbuild": "*",
"vue": "^3.5.13"
},
Expand All @@ -55,6 +56,7 @@
"@babel/types": "7.27.0",
"@types/node": "22.14.1",
"@vitest/coverage-v8": "3.1.2",
"@vue/compiler-core": "3.5.13",
"@vue/compiler-dom": "3.5.13",
"bumpp": "10.1.0",
"changelogithub": "13.13.0",
Expand Down
21 changes: 12 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 42 additions & 10 deletions src/utils/template.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { AttributeNode, DirectiveNode, ExpressionNode, ForParseResult, ParentNode, RootNode, SourceLocation, TemplateChildNode, TextNode } from '@vue/compiler-dom'
import type { AttributeNode, DirectiveNode, ExpressionNode, ParentNode, RootNode, SourceLocation, TemplateChildNode, TextNode } from '@vue/compiler-dom'
import { isFnExpressionBrowser as isFnExpression, isMemberExpressionBrowser as isMemberExpression } from '@vue/compiler-core'

// copy from `@vue/compiler-dom`
enum NodeTypes {
Expand Down Expand Up @@ -37,14 +38,8 @@ enum NodeTypes {
JS_RETURN_STATEMENT,
}

interface ExpressionTrack {
type: NodeTypes
name?: string
forParseResult?: ForParseResult
}

interface Expression {
track: ExpressionTrack[]
track: VueTemplateNode[]
loc: SourceLocation
src: string
replacement?: string
Expand All @@ -60,7 +55,7 @@ type VueTemplateNode =
function handleNode(
node: VueTemplateNode | undefined,
addExpression: (...expressions: Expression[]) => void,
track: ExpressionTrack[],
track: VueTemplateNode[],
) {
if (!node) {
return
Expand Down Expand Up @@ -264,6 +259,43 @@ const defaultSnippetHandler: SnippetHandler = {
standalone: false,
}

const multipleStatementsSnippetHandler: SnippetHandler = {
key: (node) => {
const key = `multipleStatements$:${node.src}`
const secondLastTrack = node.track.at(-2)
const lastTrack = node.track.at(-1)

if (
lastTrack?.type === NodeTypes.SIMPLE_EXPRESSION
&& secondLastTrack?.type === NodeTypes.DIRECTIVE
&& secondLastTrack.name === 'on'
) {
const isMemberExp = isMemberExpression(lastTrack)
const isInlineStatement = !(isMemberExp || isFnExpression(lastTrack))

const hasMultipleStatements = node.src.includes(';')

if ((isInlineStatement || isMemberExp) && hasMultipleStatements) {
return key
}
}

return null
},
prepare: (node, id) => `wrapper_${id}(() => {${node.src}});`,
parse: (code) => {
const wrapperRegex = /^(wrapper_\d+)\(\(\) => \{([\s\S]*?)\}\);$/

const [_, wrapperName, res] = code.trim().match(wrapperRegex) ?? []
if (!wrapperName || !res) {
return undefined
}

return res.trim().replace(/;$/, '')
},
standalone: false,
}

const destructureSnippetHandler: SnippetHandler = {
key: (node) => {
const key = `destructure$:${node.src}`
Expand Down Expand Up @@ -299,7 +331,7 @@ const destructureSnippetHandler: SnippetHandler = {
standalone: true,
}

const snippetHandlers = [destructureSnippetHandler, defaultSnippetHandler]
const snippetHandlers = [destructureSnippetHandler, multipleStatementsSnippetHandler, defaultSnippetHandler]
function getKey(expression: Expression) {
for (const handler of snippetHandlers) {
const key = handler.key(expression)
Expand Down
16 changes: 16 additions & 0 deletions test/template.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,22 @@ describe('transform typescript template', () => {
ping();
}" />"
`)

// https://github.com/nuxt/module-builder/issues/587#issuecomment-2820414064
expect(
await fixture(`<div @click="a(); b()" />`),
).toMatchInlineSnapshot(`
"<div @click="a();
b()" />"
`)
expect(
await fixture(`<div @click="a(); () => {}; b()" />`),
).toMatchInlineSnapshot(`
"<div @click="a();
() => {
};
b()" />"
`)
})

it('v-slot', async () => {
Expand Down