import { parse } from 'compiler/parser/index' import { optimize } from 'compiler/optimizer' import { generate } from 'compiler/codegen' import { isObject, extend } from 'shared/util' import { isReservedTag } from 'web/util/index' import { baseOptions } from 'web/compiler/options' function assertCodegen (template, generatedCode, ...args) { let staticRenderFnCodes = [] let generateOptions = baseOptions let proc = null let len = args.length while (len--) { const arg = args[len] if (Array.isArray(arg)) { staticRenderFnCodes = arg } else if (isObject(arg)) { generateOptions = arg } else if (typeof arg === 'function') { proc = arg } } const ast = parse(template, baseOptions) optimize(ast, baseOptions) proc && proc(ast) const res = generate(ast, generateOptions) expect(res.render).toBe(generatedCode) expect(res.staticRenderFns).toEqual(staticRenderFnCodes) } /* eslint-disable quotes */ describe('codegen', () => { it('generate directive', () => { assertCodegen( '
', `with(this){return _c('p',{directives:[{name:"custom1",rawName:"v-custom1:arg1.modifier",value:(value1),expression:"value1",arg:"arg1",modifiers:{"modifier":true}},{name:"custom2",rawName:"v-custom2"}]})}` ) }) it('generate filters', () => { assertCodegen( 'hello
', `with(this){return (show)?_c('p',[_v("hello")]):_e()}` ) }) it('generate v-else directive', () => { assertCodegen( 'hello
world
hello
world
hello
world
bye
hello
world
elseif
bye
{{hello}}
hello world
', `with(this){return _c('p',{attrs:{"slot":"one"},slot:"one"},[_v("hello world")])}` ) }) it('generate scoped slot', () => { assertCodegen( 'hello world
', `with(this){return _c('p',{staticClass:"class1"},[_v("hello world")])}`, ) // dynamic assertCodegen( 'hello world
', `with(this){return _c('p',{class:class1},[_v("hello world")])}` ) }) it('generate style binding', () => { assertCodegen( 'hello world
', `with(this){return _c('p',{style:(error)},[_v("hello world")])}` ) }) it('generate v-show directive', () => { assertCodegen( 'hello world
', `with(this){return _c('p',{directives:[{name:"show",rawName:"v-show",value:(shown),expression:"shown"}]},[_v("hello world")])}` ) }) it('generate DOM props with v-bind directive', () => { // input + value assertCodegen( '', `with(this){return _c('input',{domProps:{"value":msg}})}` ) // non input assertCodegen( '', `with(this){return _c('p',{attrs:{"value":msg}})}` ) }) it('generate attrs with v-bind directive', () => { assertCodegen( '', `with(this){return _c('input',{attrs:{"name":field1}})}` ) }) it('generate static attrs', () => { assertCodegen( '', `with(this){return _c('input',{attrs:{"name":"field1"}})}` ) }) it('generate events with v-on directive', () => { assertCodegen( '', `with(this){return _c('input',{on:{"input":onInput}})}` ) }) it('generate events with method call', () => { assertCodegen( '', `with(this){return _c('input',{on:{"input":function($event){onInput($event);}}})}` ) // empty arguments assertCodegen( '', `with(this){return _c('input',{on:{"input":function($event){onInput();}}})}` ) // without semicolon assertCodegen( '', `with(this){return _c('input',{on:{"input":function($event){onInput($event)}}})}` ) // multiple args assertCodegen( '', `with(this){return _c('input',{on:{"input":function($event){onInput($event, 'abc', 5);}}})}` ) // expression in args assertCodegen( '', `with(this){return _c('input',{on:{"input":function($event){onInput($event, 2+2);}}})}` ) // tricky symbols in args assertCodegen( '', `with(this){return _c('input',{on:{"input":function($event){onInput(');[\'());');}}})}` ) }) it('generate events with multiple statements', () => { // normal function assertCodegen( '', `with(this){return _c('input',{on:{"input":function($event){onInput1();onInput2()}}})}` ) // function with multiple args assertCodegen( '', `with(this){return _c('input',{on:{"input":function($event){onInput1($event, 'text');onInput2('text2', $event)}}})}` ) }) it('generate events with keycode', () => { assertCodegen( '', `with(this){return _c('input',{on:{"input":function($event){if(!('button' in $event)&&_k($event.keyCode,"enter",13,$event.key,"Enter"))return null;return onInput($event)}}})}` ) // multiple keycodes (delete) assertCodegen( '', `with(this){return _c('input',{on:{"input":function($event){if(!('button' in $event)&&_k($event.keyCode,"delete",[8,46],$event.key,["Backspace","Delete"]))return null;return onInput($event)}}})}` ) // multiple keycodes (chained) assertCodegen( '', `with(this){return _c('input',{on:{"keydown":function($event){if(!('button' in $event)&&_k($event.keyCode,"enter",13,$event.key,"Enter")&&_k($event.keyCode,"delete",[8,46],$event.key,["Backspace","Delete"]))return null;return onInput($event)}}})}` ) // number keycode assertCodegen( '', `with(this){return _c('input',{on:{"input":function($event){if(!('button' in $event)&&$event.keyCode!==13)return null;return onInput($event)}}})}` ) // custom keycode assertCodegen( '', `with(this){return _c('input',{on:{"input":function($event){if(!('button' in $event)&&_k($event.keyCode,"custom",undefined,$event.key,undefined))return null;return onInput($event)}}})}` ) }) it('generate events with generic modifiers', () => { assertCodegen( '', `with(this){return _c('input',{on:{"input":function($event){$event.stopPropagation();return onInput($event)}}})}` ) assertCodegen( '', `with(this){return _c('input',{on:{"input":function($event){$event.preventDefault();return onInput($event)}}})}` ) assertCodegen( '', `with(this){return _c('input',{on:{"input":function($event){if($event.target !== $event.currentTarget)return null;return onInput($event)}}})}` ) }) // GitHub Issues #5146 it('generate events with generic modifiers and keycode correct order', () => { assertCodegen( '', `with(this){return _c('input',{on:{"keydown":function($event){if(!('button' in $event)&&_k($event.keyCode,"enter",13,$event.key,"Enter"))return null;$event.preventDefault();return onInput($event)}}})}` ) assertCodegen( '', `with(this){return _c('input',{on:{"keydown":function($event){if(!('button' in $event)&&_k($event.keyCode,"enter",13,$event.key,"Enter"))return null;$event.stopPropagation();return onInput($event)}}})}` ) }) it('generate events with mouse event modifiers', () => { assertCodegen( '', `with(this){return _c('input',{on:{"click":function($event){if(!$event.ctrlKey)return null;return onClick($event)}}})}` ) assertCodegen( '', `with(this){return _c('input',{on:{"click":function($event){if(!$event.shiftKey)return null;return onClick($event)}}})}` ) assertCodegen( '', `with(this){return _c('input',{on:{"click":function($event){if(!$event.altKey)return null;return onClick($event)}}})}` ) assertCodegen( '', `with(this){return _c('input',{on:{"click":function($event){if(!$event.metaKey)return null;return onClick($event)}}})}` ) assertCodegen( '', `with(this){return _c('input',{on:{"click":function($event){if($event.ctrlKey||$event.shiftKey||$event.altKey||$event.metaKey)return null;return onClick($event)}}})}` ) assertCodegen( '', `with(this){return _c('input',{on:{"click":function($event){if(!$event.ctrlKey)return null;if($event.shiftKey||$event.altKey||$event.metaKey)return null;return onClick($event)}}})}` ) }) it('generate events with multiple modifiers', () => { assertCodegen( '', `with(this){return _c('input',{on:{"input":function($event){$event.stopPropagation();$event.preventDefault();if($event.target !== $event.currentTarget)return null;return onInput($event)}}})}` ) }) it('generate events with capture modifier', () => { assertCodegen( '', `with(this){return _c('input',{on:{"!input":function($event){return onInput($event)}}})}` ) }) it('generate events with once modifier', () => { assertCodegen( '', `with(this){return _c('input',{on:{"~input":function($event){return onInput($event)}}})}` ) }) it('generate events with capture and once modifier', () => { assertCodegen( '', `with(this){return _c('input',{on:{"~!input":function($event){return onInput($event)}}})}` ) }) it('generate events with once and capture modifier', () => { assertCodegen( '', `with(this){return _c('input',{on:{"~!input":function($event){return onInput($event)}}})}` ) }) it('generate events with inline statement', () => { assertCodegen( '', `with(this){return _c('input',{on:{"input":function($event){current++}}})}` ) }) it('generate events with inline function expression', () => { // normal function assertCodegen( '', `with(this){return _c('input',{on:{"input":function () { current++ }}})}` ) // arrow with no args assertCodegen( 'current++">', `with(this){return _c('input',{on:{"input":()=>current++}})}` ) // arrow with parens, single arg assertCodegen( ' current++">', `with(this){return _c('input',{on:{"input":(e) => current++}})}` ) // arrow with parens, multi args assertCodegen( ' current++">', `with(this){return _c('input',{on:{"input":(a, b, c) => current++}})}` ) // arrow with destructuring assertCodegen( ' current++">', `with(this){return _c('input',{on:{"input":({ a, b }) => current++}})}` ) // arrow single arg no parens assertCodegen( 'current++">', `with(this){return _c('input',{on:{"input":e=>current++}})}` ) // with modifiers assertCodegen( `current++">`, `with(this){return _c('input',{on:{"keyup":function($event){if(!('button' in $event)&&_k($event.keyCode,"enter",13,$event.key,"Enter"))return null;return (e=>current++)($event)}}})}` ) }) // #3893 it('should not treat handler with unexpected whitespace as inline statement', () => { assertCodegen( '', `with(this){return _c('input',{on:{"input":onInput}})}` ) }) it('generate unhandled events', () => { assertCodegen( '', `with(this){return _c('input',{on:{"input":function(){}}})}`, ast => { ast.events.input = undefined } ) }) it('generate multiple event handlers', () => { assertCodegen( '', `with(this){return _c('input',{on:{"input":[function($event){current++},function($event){$event.stopPropagation();return onInput($event)}]}})}` ) }) it('generate component', () => { assertCodegen( 'hello world
hello world
', `with(this){return (show)?_c('p',[_v("hello world")]):_e()}`, { isReservedTag } ) }) }) /* eslint-enable quotes */