Skip to content

Commit

Permalink
feat(compiler-vapor): support expressionPlugins for generate (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhiyuanzmj authored Jan 13, 2024
1 parent 8d7d672 commit 0494323
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 27 deletions.
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(context: CodegenContext) {
const exp = oper.value
if (exp && exp.content.trim()) {
const isMemberExp = isMemberExpression(exp.content, {
// TODO: expression plugins
expressionPlugins: [],
})
const isMemberExp = isMemberExpression(exp.content, context)
const isInlineStatement = !(isMemberExp || fnExpRE.test(exp.content))
const hasMultipleStatements = exp.content.includes(`;`)

Expand Down

0 comments on commit 0494323

Please sign in to comment.