Skip to content

Generate appropriate prop definitions for functional SFCs #22

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 2 commits into from
Nov 2, 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: 11 additions & 0 deletions lib/process-functional.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = function extractProps (content) {
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) => {
const propName = match.trim().replace(CHARS_TO_REMOVE, '')
return `'${propName}'`
})
return `[ ${props.join(', ')} ]`
}
5 changes: 4 additions & 1 deletion lib/process.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +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 splitRE = /\r?\n/g

Expand All @@ -32,7 +33,9 @@ function isFunctionalTemplate (parts) {
function changePartsIfFunctional (parts) {
if (isFunctionalTemplate(parts)) {
parts.lang = 'javascript'
parts.script = { type: 'script', content: 'export default { props: { props: Object } }' }
const functionalProps = extractPropsFromFunctionalTemplate(parts.template.content)
parts.template.content = parts.template.content.replace('props.', '')
parts.script = { type: 'script', content: `export default { props: ${functionalProps} }` }
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/FunctionalSFC.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import FunctionalSFC from './resources/FunctionalSFC.vue'

test('processes .vue file with functional template', () => {
const wrapper = shallow(FunctionalSFC, {
propsData: { props: { msg: 'Hello' }}
propsData: { msg: 'Hello' }
})
expect(wrapper.is('div')).toBe(true)
expect(wrapper.text().trim()).toBe('Hello')
Expand Down
7 changes: 7 additions & 0 deletions test/FunctionalSFCParent.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { mount } from 'vue-test-utils'
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')
})
15 changes: 15 additions & 0 deletions test/process-functional.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import extractProps from '../lib/process-functional'

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' ]")
})
})
1 change: 1 addition & 0 deletions test/resources/FunctionalSFC.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
{{ props.msg }}
</div>
</template>

14 changes: 14 additions & 0 deletions test/resources/FunctionalSFCParent.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<template>
<FunctionalSFC msg="TEST"/>
</template>

<script>
import Vue from 'vue'
import FunctionalSFC from './FunctionalSFC'

Vue.component('FunctionalSFC', FunctionalSFC)

export default {
components: FunctionalSFC
}
</script>