import { ElementNode, ErrorCodes } from '@vue/compiler-core' import { compile } from '../src' import { MPErrorCodes } from '../src/errors' import { CompilerOptions } from '../src/options' import { assert, miniProgram } from './testUtils' function parseWithVBind(template: string, options: CompilerOptions = {}) { const { ast, code } = compile(template, { generatorOpts: { concise: true, }, ...options, }) return { code, node: ast.children[0] as ElementNode, } } describe('compiler: transform v-bind', () => { test('basic', () => { assert( ``, ``, `(_ctx, _cache) => { return { a: _ctx.id } }`, {} ) }) test('dynamic arg', () => { const onError = jest.fn() parseWithVBind(``, { onError, filename: 'foo.vue', miniProgram: { ...miniProgram, emitFile({ source }) { expect(source).toBe(``) return '' }, }, }) expect(onError.mock.calls[0][0]).toMatchObject({ code: MPErrorCodes.X_V_BIND_DYNAMIC_ARGUMENT, loc: { start: { line: 1, column: 7, }, end: { line: 1, column: 23, }, }, }) parseWithVBind(``, { onError, filename: 'foo.vue', miniProgram: { ...miniProgram, emitFile({ source }) { expect(source).toBe(``) return '' }, }, }) expect(onError.mock.calls[1][0]).toMatchObject({ code: MPErrorCodes.X_V_BIND_DYNAMIC_ARGUMENT, loc: { start: { line: 1, column: 7, }, end: { line: 1, column: 27, }, }, }) }) test('should error if no expression', () => { const onError = jest.fn() parseWithVBind(``, { onError }) expect(onError.mock.calls[0][0]).toMatchObject({ code: MPErrorCodes.X_V_BIND_NO_ARGUMENT, loc: { start: { line: 1, column: 7, }, end: { line: 1, column: 13, }, }, }) parseWithVBind(``, { onError }) expect(onError.mock.calls[1][0]).toMatchObject({ code: ErrorCodes.X_V_BIND_NO_EXPRESSION, loc: { start: { line: 1, column: 7, }, end: { line: 1, column: 17, }, }, }) }) test('.camel modifier', () => { assert( ``, '', `(_ctx, _cache) => { return { a: _ctx.id } }` ) }) test('.camel modifier w/ dynamic arg', () => { const onError = jest.fn() parseWithVBind(``, { onError, prefixIdentifiers: false, filename: 'foo.vue', miniProgram: { ...miniProgram, emitFile({ source }) { expect(source).toBe(``) return '' }, }, }) expect(onError.mock.calls[0][0]).toMatchObject({ code: MPErrorCodes.X_V_BIND_DYNAMIC_ARGUMENT, loc: { start: { line: 1, column: 7, }, end: { line: 1, column: 30, }, }, }) }) test('.camel modifier w/ dynamic arg + prefixIdentifiers', () => { const onError = jest.fn() parseWithVBind(``, { onError, prefixIdentifiers: true, filename: 'foo.vue', miniProgram: { ...miniProgram, emitFile({ source }) { expect(source).toBe(``) return '' }, }, }) expect(onError.mock.calls[0][0]).toMatchObject({ code: MPErrorCodes.X_V_BIND_DYNAMIC_ARGUMENT, loc: { start: { line: 1, column: 7, }, end: { line: 1, column: 30, }, }, }) }) test('.prop modifier', () => { const onWarn = jest.fn() parseWithVBind(``, { onWarn }) expect(onWarn.mock.calls[0][0]).toMatchObject({ code: MPErrorCodes.X_V_BIND_MODIFIER_PROP, loc: { start: { line: 1, column: 7, }, end: { line: 1, column: 30, }, }, }) }) test('.prop modifier w/ dynamic arg', () => { const onError = jest.fn() parseWithVBind(``, { onError, filename: 'foo.vue', prefixIdentifiers: false, miniProgram: { ...miniProgram, emitFile({ source }) { expect(source).toBe(``) return '' }, }, }) expect(onError.mock.calls[0][0]).toMatchObject({ code: MPErrorCodes.X_V_BIND_DYNAMIC_ARGUMENT, loc: { start: { line: 1, column: 7, }, end: { line: 1, column: 32, }, }, }) }) test('.prop modifier w/ dynamic arg + prefixIdentifiers', () => { const onError = jest.fn() parseWithVBind(``, { onError, prefixIdentifiers: true, filename: 'foo.vue', miniProgram: { ...miniProgram, emitFile({ source }) { expect(source).toBe(``) return '' }, }, }) expect(onError.mock.calls[0][0]).toMatchObject({ code: MPErrorCodes.X_V_BIND_DYNAMIC_ARGUMENT, loc: { start: { line: 1, column: 7, }, end: { line: 1, column: 34, }, }, }) }) test('.prop modifier (shorthand)', () => { const onWarn = jest.fn() parseWithVBind(``, { onWarn, filename: 'foo.vue', miniProgram: { ...miniProgram, emitFile({ source }) { expect(source).toBe(``) return '' }, }, }) expect(onWarn.mock.calls[0][0]).toMatchObject({ code: MPErrorCodes.X_V_BIND_MODIFIER_PROP, loc: { start: { line: 1, column: 7, }, end: { line: 1, column: 19, }, }, }) }) test('.attr modifier', () => { const onWarn = jest.fn() parseWithVBind(``, { onWarn, filename: 'foo.vue', miniProgram: { ...miniProgram, emitFile({ source }) { expect(source).toBe(``) return '' }, }, }) expect(onWarn.mock.calls[0][0]).toMatchObject({ code: MPErrorCodes.X_V_BIND_MODIFIER_ATTR, loc: { start: { line: 1, column: 7, }, end: { line: 1, column: 31, }, }, }) }) })