Skip to content
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

feat(compiler-vapor): support expressionPlugins for generate #91

Merged
merged 2 commits into from
Jan 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
33 changes: 15 additions & 18 deletions packages/compiler-vapor/__tests__/transforms/vOn.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,25 +269,22 @@ describe('v-on', () => {
expect(code).contains('_on(n1, "click", $event => _ctx.foo($event))')
})

test.fails(
'should NOT wrap as function if expression is already function expression (with Typescript)',
() => {
const { ir, code } = compileWithVOn(
`<div @click="(e: any): any => foo(e)"/>`,
{ expressionPlugins: ['typescript'] },
)

expect(ir.operation).toMatchObject([
{
type: IRNodeTypes.SET_EVENT,
value: { content: '(e: any): any => foo(e)' },
},
])
test('should NOT wrap as function if expression is already function expression (with Typescript)', () => {
const { ir, code } = compileWithVOn(
`<div @click="(e: any): any => foo(e)"/>`,
{ expressionPlugins: ['typescript'] },
)

expect(code).matchSnapshot()
expect(code).contains('_on(n1, "click", e => _ctx.foo(e))')
},
)
expect(ir.operation).toMatchObject([
{
type: IRNodeTypes.SET_EVENT,
value: { content: '(e: any): any => foo(e)' },
},
])

expect(code).matchSnapshot()
expect(code).contains('_on(n1, "click", (e: any): any => _ctx.foo(e))')
})

test('should NOT wrap as function if expression is already function expression (with newlines)', () => {
const { ir, code } = compileWithVOn(
Expand Down
7 changes: 5 additions & 2 deletions packages/compiler-vapor/src/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,16 @@ export function compile(
if (!__BROWSER__ && options.isTS) {
const { expressionPlugins } = options
if (!expressionPlugins || !expressionPlugins.includes('typescript')) {
options.expressionPlugins = [...(expressionPlugins || []), 'typescript']
resolvedOptions.expressionPlugins = [
...(expressionPlugins || []),
'typescript',
]
}
}

const ir = transform(
ast,
extend({}, options, {
extend({}, resolvedOptions, {
nodeTransforms: [
...nodeTransforms,
...(options.nodeTransforms || []), // user transforms
Expand Down
18 changes: 11 additions & 7 deletions packages/compiler-vapor/src/generate.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
type CodegenOptions as BaseCodegenOptions,
BindingTypes,
type CodegenOptions,
type CodegenResult,
NewlineType,
type Position,
Expand Down Expand Up @@ -33,6 +33,11 @@ import {
import { SourceMapGenerator } from 'source-map-js'
import { camelize, isGloballyAllowed, isString, makeMap } from '@vue/shared'
import type { Identifier } from '@babel/types'
import type { ParserPlugin } from '@babel/parser'

interface CodegenOptions extends BaseCodegenOptions {
expressionPlugins?: ParserPlugin[]
}

// TODO: share this with compiler-core
const fnExpRE =
Expand Down Expand Up @@ -94,6 +99,7 @@ function createCodegenContext(
inSSR = false,
inline = false,
bindingMetadata = {},
expressionPlugins = [],
}: CodegenOptions,
) {
const { helpers, vaporHelpers } = ir
Expand All @@ -111,6 +117,7 @@ function createCodegenContext(
isTS,
inSSR,
bindingMetadata,
expressionPlugins,
inline,

source: ir.source,
Expand Down Expand Up @@ -513,7 +520,7 @@ function genSetEvent(oper: SetEventIRNode, context: CodegenContext) {

;(keys.length ? pushWithKeys : pushNoop)(() =>
(nonKeys.length ? pushWithModifiers : pushNoop)(() => {
genEventHandler()
genEventHandler(context)
}),
)
},
Expand All @@ -522,13 +529,10 @@ function genSetEvent(oper: SetEventIRNode, context: CodegenContext) {
(() => push(`{ ${options.map((v) => `${v}: true`).join(', ')} }`)),
)

function genEventHandler() {
function genEventHandler(content: CodegenContext) {
const exp = oper.value
if (exp && exp.content.trim()) {
const isMemberExp = isMemberExpression(exp.content, {
// TODO: expression plugins
expressionPlugins: [],
})
const isMemberExp = isMemberExpression(exp.content, content)
const isInlineStatement = !(isMemberExp || fnExpRE.test(exp.content))
const hasMultipleStatements = exp.content.includes(`;`)

Expand Down