diff --git a/packages/compiler-dom/__tests__/transforms/__snapshots__/vShow.spec.ts.snap b/packages/compiler-dom/__tests__/transforms/__snapshots__/vShow.spec.ts.snap new file mode 100644 index 00000000..8ec0bbbc --- /dev/null +++ b/packages/compiler-dom/__tests__/transforms/__snapshots__/vShow.spec.ts.snap @@ -0,0 +1,15 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`compiler: v-show transform simple expression 1`] = ` +"const _Vue = Vue + +return function render() { + with (this) { + const { vShow: _vShow, createVNode: _createVNode, withDirectives: _withDirectives, createBlock: _createBlock, openBlock: _openBlock } = _Vue + + return (_openBlock(), _withDirectives(_createBlock(\\"div\\", null, null, 32 /* NEED_PATCH */), [ + [_vShow, a] + ])) + } +}" +`; diff --git a/packages/compiler-dom/__tests__/transforms/vShow.spec.ts b/packages/compiler-dom/__tests__/transforms/vShow.spec.ts new file mode 100644 index 00000000..3bac6bbe --- /dev/null +++ b/packages/compiler-dom/__tests__/transforms/vShow.spec.ts @@ -0,0 +1,36 @@ +import { parse, transform, generate, CompilerOptions } from '@vue/compiler-core' +import { transformElement } from '../../../compiler-core/src/transforms/transformElement' +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(`
`) + + expect(generate(ast).code).toMatchSnapshot() + }) + + test('should raise error if has no expression', () => { + const onError = jest.fn() + transformWithShow(``, { onError }) + + expect(onError).toHaveBeenCalledTimes(1) + expect(onError).toHaveBeenCalledWith( + expect.objectContaining({ + code: DOMErrorCodes.X_V_SHOW_NO_EXPRESSION + }) + ) + }) +}) diff --git a/packages/compiler-dom/src/errors.ts b/packages/compiler-dom/src/errors.ts index b59ee0c0..7dd83fce 100644 --- a/packages/compiler-dom/src/errors.ts +++ b/packages/compiler-dom/src/errors.ts @@ -27,7 +27,8 @@ export const enum DOMErrorCodes { X_V_TEXT_WITH_CHILDREN, X_V_MODEL_ON_INVALID_ELEMENT, X_V_MODEL_ARG_ON_ELEMENT, - X_V_MODEL_ON_FILE_INPUT_ELEMENT + X_V_MODEL_ON_FILE_INPUT_ELEMENT, + X_V_SHOW_NO_EXPRESSION } export const DOMErrorMessages: { [code: number]: string } = { @@ -37,5 +38,6 @@ export const DOMErrorMessages: { [code: number]: string } = { [DOMErrorCodes.X_V_TEXT_WITH_CHILDREN]: `v-text will override element children.`, [DOMErrorCodes.X_V_MODEL_ON_INVALID_ELEMENT]: `v-model can only be used on ,