Skip to content

Add callback and array prop detection in functional template #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Nov 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions lib/process-functional.js → lib/extract-props.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
module.exports = function extractProps (content) {
const DETECT_PROP_DEFINITIONS = /(props\..*?)(}| |\.)/g
const CHARS_TO_REMOVE = /(\.|}| |props)/g
const DETECT_PROP_DEFINITIONS = /(props\..*?)(}| |\.|\[)/g
const CHARS_TO_REMOVE = /(\.|}| |props|\(|\[)/g
const propDefinitions = content.match(DETECT_PROP_DEFINITIONS)
if (!propDefinitions) return '{}'
const props = propDefinitions.map((match) => {
let props = propDefinitions.map((match) => {
const propName = match.trim().replace(CHARS_TO_REMOVE, '')
return `'${propName}'`
})
props = removeDuplicates(props)
return `[ ${props.join(', ')} ]`
}

function removeDuplicates (props) {
return [...new Set(props)]
}
15 changes: 4 additions & 11 deletions lib/process.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const addTemplateMapping = require('./add-template-mapping')
const compileBabel = require('./compilers/babel-compiler')
const compileTypescript = require('./compilers/typescript-compiler')
const compileCoffeeScript = require('./compilers/coffee-compiler')
const extractPropsFromFunctionalTemplate = require('./process-functional')
const extractPropsFromFunctionalTemplate = require('./extract-props')

const splitRE = /\r?\n/g

Expand All @@ -22,19 +22,12 @@ function processScript (scriptPart) {
return compileBabel(scriptPart.content)
}

function isFunctionalTemplate (parts) {
try {
if (parts.template.attrs.functional === true) return true
} catch (error) {
return false
}
}

function changePartsIfFunctional (parts) {
if (isFunctionalTemplate(parts)) {
const isFunctional = parts.template && parts.template.attrs && parts.template.attrs.functional
if (isFunctional) {
parts.lang = 'javascript'
const functionalProps = extractPropsFromFunctionalTemplate(parts.template.content)
parts.template.content = parts.template.content.replace('props.', '')
parts.template.content = parts.template.content.replace(/props./g, '')
parts.script = { type: 'script', content: `export default { props: ${functionalProps} }` }
}
}
Expand Down
21 changes: 16 additions & 5 deletions test/FunctionalSFC.spec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
import { shallow } from 'vue-test-utils'
import FunctionalSFC from './resources/FunctionalSFC.vue'

test('processes .vue file with functional template', () => {
const wrapper = shallow(FunctionalSFC, {
propsData: { msg: 'Hello' }
let wrapper
const clickSpy = jest.fn()
beforeEach(() => {
wrapper = shallow(FunctionalSFC, {
propsData: { msg: { id: 1, title: 'foo' }, onClick: clickSpy }
})
})

describe('Processes .vue file with functional template', () => {
it('with nested props', () => {
expect(wrapper.text().trim()).toBe('foo')
})

it('with callback prop', () => {
wrapper.trigger('click')
expect(clickSpy).toHaveBeenCalledWith(1)
})
expect(wrapper.is('div')).toBe(true)
expect(wrapper.text().trim()).toBe('Hello')
})
2 changes: 1 addition & 1 deletion test/FunctionalSFCParent.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ import FunctionalSFCParent from './resources/FunctionalSFCParent.vue'

test('processes .vue file with functional template from parent', () => {
const wrapper = mount(FunctionalSFCParent)
expect(wrapper.text().trim()).toBe('TEST')
expect(wrapper.text().trim()).toBe('foo')
})
31 changes: 31 additions & 0 deletions test/extract-props.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import extractProps from '../lib/extract-props'

describe('when extracting props with props. prefix from functional template content', () => {
it('extracts interpolated props ', () => {
const content = '<div> {{props.msg1 }} {{props.msg2}}</div>'

expect(extractProps(content)).toBe("[ 'msg1', 'msg2' ]")
})

it('extracts props used in v-for', () => {
const content = '<div v-for="bar in props.foo.bar"> {{ bar }}} </div>'

expect(extractProps(content)).toBe("[ 'foo' ]")
})

it('extracts props with nested structure', () => {
const content = '<div> {{props.msg1.foo }} {{props.msg1.bar}}</div>'

expect(extractProps(content)).toBe("[ 'msg1' ]")
})

it('extracts callback props', () => {
const content = '<button @click="props.onClick(props.msg)">{{props.msg.title}}</button>'
expect(extractProps(content)).toBe("[ 'onClick', 'msg' ]")
})

it('extracts array props', () => {
const content = '<div>{{props.msg[title]}}</div>'
expect(extractProps(content)).toBe("[ 'msg' ]")
})
})
15 changes: 0 additions & 15 deletions test/process-functional.spec.js

This file was deleted.

5 changes: 1 addition & 4 deletions test/resources/FunctionalSFC.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
<template functional>
<div class="hello">
{{ props.msg }}
</div>
<button @click="props.onClick(props.msg.id)">{{props.msg.title}}</button>
</template>

2 changes: 1 addition & 1 deletion test/resources/FunctionalSFCParent.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<FunctionalSFC msg="TEST"/>
<FunctionalSFC :msg="{id: 1, title: 'foo'}" :onClick="() => {}"/>
</template>

<script>
Expand Down