Skip to content
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

various updates #2

Merged
merged 13 commits into from
Sep 5, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Adding support for module.exports in vue files
  • Loading branch information
magali-br committed Jun 7, 2017
commit 2ce2c7c4f3190312414c2857c6d9579e710b3727
86 changes: 46 additions & 40 deletions lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,56 +212,62 @@ class Parser extends EventEmitter {
this.eventsEmmited = {}
}

extractProperties (property) {
switch (property.value.type) {
case 'ObjectExpression':
property.value.properties.forEach((p) => {
const entry = getComment(p)

entry.value = value(p)
entry.name = Object.keys(entry.value)[0]
entry.value = entry.value[entry.name]

if (entry.value instanceof NodeFunction) {
entry.params = entry.value.params
} else if (property.key.name === 'props') {
if (entry.describeModel) {
entry.name = 'v-model'
} else {
entry.name = unCamelcase(entry.name)
}
}

delete entry.describeModel

this.emit(property.key.name, entry)
this.subWalk(entry.value)
})
break

default:
const propertyValue = value(property)
const entryValue = first(propertyValue)

if (propertyValue.hasOwnProperty('name') && !this.componentName) {
this.componentName = propertyValue.name
break
}

this.subWalk(entryValue)
}
}

walk () {
process.nextTick(() => {
this.ast.body.forEach((body) => {
if (body.type !== 'ExportDefaultDeclaration') {
if (body.type !== 'ExportDefaultDeclaration' && body.type !== 'ExpressionStatement') {
const entry = getComment(body)
const description = entry ? entry.description : null

this.emit('description', description)
return
}

body.declaration.properties.forEach((property) => {
switch (property.value.type) {
case 'ObjectExpression':
property.value.properties.forEach((p) => {
const entry = getComment(p)

entry.value = value(p)
entry.name = Object.keys(entry.value)[0]
entry.value = entry.value[entry.name]

if (entry.value instanceof NodeFunction) {
entry.params = entry.value.params
} else if (property.key.name === 'props') {
if (entry.describeModel) {
entry.name = 'v-model'
} else {
entry.name = unCamelcase(entry.name)
}
}

delete entry.describeModel

this.emit(property.key.name, entry)
this.subWalk(entry.value)
})
break

default:
const propertyValue = value(property)
const entryValue = first(propertyValue)

if (propertyValue.hasOwnProperty('name') && !this.componentName) {
this.componentName = propertyValue.name
break
}

this.subWalk(entryValue)
}
})
if (body.declaration != null) {
body.declaration.properties.forEach((property) => this.extractProperties(property))
} else if (body.expression != null) {
body.expression.right.properties.forEach((property) => this.extractProperties(property))
}

if (this.componentName === null && this.filename) {
const filename = path.parse(this.filename).name
Expand Down
139 changes: 139 additions & 0 deletions test/fixtures/checkboxModuleExports.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
<template>
<label>
<input :disabled="disabled" type="text" v-model="checkbox">
<!-- Default slot -->
<slot></slot>
<!-- Use this slot to set the checkbox label -->
<slot name="label">Unamed checkbox</slot>
<!--
This
is multiline description
-->
<slot name="multiline">Unamed checkbox</slot>
<slot name="undescribed"></slot>
<template></template>
</label>
</template>

<script>
const dynamic2 = 'dynamicMode'

/**
* A simple checkbox component
*/
module.exports = {
name: 'checkbox',
props: {
/**
* The checkbox model
* @model
*/
model: {
type: Array,
required: true,
twoWay: true
},

/**
* Initial checkbox state
*/
disabled: Boolean,

/**
* Initial checkbox value
*/
checked: {
type: Boolean,
default: true
},

// Prop with camel name
propWithCamel: {
type: Object,
default: () => ({ name: 'X'})
}
},

data () {
return {}
},

created () {
/**
* Emit when the component has been loaded
*/
this.$emit('loaded')
},

methods: {
/**
* @private
*/
privateMethod () {
console.log('check')

const name = 'check'
const value = 'event value'

if (name) {
console.log('>', name)
}

/**
* Event with identifier name
*/
this.$emit(name, value)
},

/**
* Check the checkbox
*/
check () {
console.log('check')

let eventName = 'check'
const value = 'event value'

if (eventName) {
console.log('>', eventName)
}

eventName = 'renamed'

/**
* Event with renamed identifier name
*/
this.$emit(eventName, value)
},

/**
* @protected
*/
recursiveIdentifierValue () {
console.log('check')

let recursiveValue = 'recursive'
const value = 'event value'

if (eventName) {
console.log('>', eventName)
}

eventName = recursiveValue

/**
* Event with recursive identifier name
*/
this.$emit(eventName, value)
},

uncommentedMethod () {}
}
}
</script>

<style lang="css" scoped>
label {
font-size: .9em
}
</style>
54 changes: 40 additions & 14 deletions test/parser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,16 @@ const options = {
ignoredVisibilities: []
}

const optionsForModuleExports = {
filename: f('checkboxModuleExports.vue'),
encoding: 'utf8',
ignoredVisibilities: []
}

/* global describe it */

describe('options', () => {
it('should faild to parse with missing options.filename', (done) => {
it('should fail to parse with missing options.filename', (done) => {
parser.parse({})
.catch((err) => {
assert.ok(/required/.test(err.message))
Expand All @@ -35,7 +41,7 @@ describe('options', () => {
.catch(done)
})

it('should faild with missing options.filename', (done) => {
it('should fail with missing options.filename', (done) => {
parser.parse({})
.catch((err) => {
assert.ok(/required/.test(err.message))
Expand All @@ -44,11 +50,15 @@ describe('options', () => {
})
})

describe('component', () => {
describe('component', () => testComponent(options))

describe('component_module.exports', () => testComponent(optionsForModuleExports))

function testComponent(optionsToParse) {
let component = {}

it('should parse without error', (done) => {
parser.parse(options)
parser.parse(optionsToParse)
.then((_component) => {
component = _component
done()
Expand All @@ -70,12 +80,16 @@ describe('component', () => {

it('should have a description', () =>
assert.equal(component.description, 'A simple checkbox component'))
})
}

describe('component.props', () => testComponentProps(options))

describe('component.props_module.exports', () => testComponentProps(optionsForModuleExports))

describe('component.props', () => {
function testComponentProps(optionsToParse) {
let component = {}

parser.parse(options)
parser.parse(optionsToParse)
.then((_component) => (component = _component))
.catch((err) => { throw err })

Expand Down Expand Up @@ -114,9 +128,13 @@ describe('component.props', () => {
assert.equal(item.value.default.type, 'ArrowFunctionExpression')
assert.equal(item.description, 'Prop with camel name')
})
})
}

describe('component.slots', () => {
describe('component.slots', () => testComponentSlots(options))

describe('component.slots_module.exports', () => testComponentSlots(optionsForModuleExports))

function testComponentSlots(optionsToParse) {
let component = {}

parser.parse(options)
Expand Down Expand Up @@ -154,9 +172,13 @@ describe('component.slots', () => {
assert.notEqual(typeof item, 'undefined')
assert.equal(item.description, null)
})
})
}

describe('component.events', () => testComponentEvents(options))

describe('component.events_module.exports', () => testComponentEvents(optionsForModuleExports))

describe('component.events', () => {
function testComponentEvents(optionsToParse) {
let component = {}

parser.parse(options)
Expand Down Expand Up @@ -190,9 +212,13 @@ describe('component.events', () => {
assert.notEqual(typeof item, 'undefined')
assert.equal(item.description, 'Event with recursive identifier name')
})
})
}

describe('component.methods', () => testComponentMethods(options))

describe('component.methods', () => {
describe('component.methods_module.exports', () => testComponentMethods(optionsForModuleExports))

function testComponentMethods(optionsToParse) {
let component = {}

parser.parse(options)
Expand Down Expand Up @@ -229,4 +255,4 @@ describe('component.methods', () => {

assert.notEqual(typeof item, 'undefined')
})
})
}
Loading