Skip to content

Commit

Permalink
feat(compiler): transform vShow
Browse files Browse the repository at this point in the history
  • Loading branch information
rigor789 committed Apr 1, 2020
1 parent 927fdc9 commit 92021fd
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 3 deletions.
43 changes: 43 additions & 0 deletions packages/compiler/__tests__/transforms/vShow.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// todo: uncomment when vue-next#907 is released
test.todo('uncomment when vue-next#907 is released')
// import {
// baseParse as parse,
// transform,
// generate,
// CompilerOptions,
// transformElement
// } from '@vue/compiler-core'
// import { transformShow } from '../../src/transforms/vShow'
// import { DOMErrorCodes } from '../../src/errors'
//
// function transformWithShow(template: string, options: CompilerOptions = {}) {
// const ast = parse(template)
// transform(ast, {
// nodeTransforms: [transformElement],
// directiveTransforms: {
// show: transformShow
// },
// ...options
// })
// return ast
// }
//
// describe('compiler: v-show transform', () => {
// test('simple expression', () => {
// const ast = transformWithShow(`<Label v-show="a"/>`)
//
// expect(generate(ast).code).toMatchSnapshot()
// })
//
// test('should raise error if has no expression', () => {
// const onError = jest.fn()
// transformWithShow(`<Label v-show/>`, { onError })
//
// expect(onError).toHaveBeenCalledTimes(1)
// expect(onError).toHaveBeenCalledWith(
// expect.objectContaining({
// code: DOMErrorCodes.X_V_SHOW_NO_EXPRESSION
// })
// )
// })
// })
6 changes: 3 additions & 3 deletions packages/compiler/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { transformStyle } from './transforms/transformStyle'
import { transformVText } from './transforms/vText'
import { transformModel } from './transforms/vModel'
import { transformOn } from './transforms/vOn'
// import { transformShow } from './transforms/vShow'
import { transformShow } from './transforms/vShow'
// import { warnTransitionChildren } from './transforms/warnTransitionChildren'
// import { stringifyStatic } from './transforms/stringifyStatic'

Expand All @@ -30,8 +30,8 @@ export const DOMDirectiveTransforms: Record<string, DirectiveTransform> = {
cloak: noopDirectiveTransform,
text: transformVText,
model: transformModel, // override compiler-core todo: remove if not needed
on: transformOn // override compiler-core todo: remove if not needed
// show: transformShow
on: transformOn, // override compiler-core todo: remove if not needed
show: transformShow
}

export function compile(
Expand Down
17 changes: 17 additions & 0 deletions packages/compiler/src/transforms/vShow.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { DirectiveTransform } from '@vue/compiler-core'
import { createDOMCompilerError, DOMErrorCodes } from '../errors'
import { V_SHOW } from '../runtimeHelpers'

export const transformShow: DirectiveTransform = (dir, node, context) => {
const { exp, loc } = dir
if (!exp) {
context.onError(
createDOMCompilerError(DOMErrorCodes.X_V_SHOW_NO_EXPRESSION, loc)
)
}

return {
props: [],
needRuntime: context.helper(V_SHOW)
}
}

0 comments on commit 92021fd

Please sign in to comment.