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 6 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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ npm install --save @vuedoc/parser
```

## Options
- `filename` (*required*) The filename to parse
- `filename` (*required* unless `filecontent` is passed) The filename to parse
- `filecontent` (*required* unless `filename` is passed) The string to parse
- `encoding` (*optional*) `default: utf8`
- `ignoreName` (*optional*) `default: false` Set `true` to ignore the component name in the result
- `ignoreDescription` (*optional*) `default: false` Set `true` to ignore the component description in the result
- `methodsDefaultPrivate` (*optional*) `default: false` Set `true` to treat methods as `@private` unless otherwise specified
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting... What do you think to rename this option to defaultMethodVisibility with public as a default value?


## Usage
```js
Expand Down
8 changes: 4 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ module.exports.parse = (options) => new Promise((resolve) => {
})
})

function loadSourceFromFileContent(filecontent) {
function loadSourceFromFileContent (filecontent) {
const $ = cheerio.load(filecontent)
return {
template: $('template').html(),
script: $('script').html()
}
template: $('template').html(),
script: $('script').html()
}
}
21 changes: 13 additions & 8 deletions lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,19 @@ const hasModelDecoration = (text) => {
return RE_MODEL.test(text)
}

const getCommentVisibility = (text) => {
const getCommentVisibility = (text, defaultVisibility) => {
const matches = RE_VISIBILITY.exec(text)

if (matches) {
return matches[1]
}

return DEFAULT_VISIBILITY
return defaultVisibility
}

const parseComment = (text) => {
const parseComment = (text, defaultVisibility = DEFAULT_VISIBILITY) => {
const parsedComment = {
visibility: getCommentVisibility(text),
visibility: getCommentVisibility(text, defaultVisibility),
describeModel: hasModelDecoration(text),
description: text.replace(RE_VISIBILITY, '')
}
Expand All @@ -57,7 +57,7 @@ const parseComment = (text) => {
return parsedComment
}

const getComment = (property) => {
const getComment = (property, defaultVisibility = DEFAULT_VISIBILITY) => {
let lastComment = null

if (property.leadingComments) {
Expand All @@ -69,11 +69,11 @@ const getComment = (property) => {
}

if (lastComment) {
return parseComment(lastComment)
return parseComment(lastComment, defaultVisibility)
}

return {
visibility: DEFAULT_VISIBILITY,
visibility: defaultVisibility,
description: null
}
}
Expand Down Expand Up @@ -210,13 +210,18 @@ class Parser extends EventEmitter {
this.template = options.source.template
this.filename = options.filename
this.eventsEmmited = {}
this.methodsDefaultPrivate = options.methodsDefaultPrivate
}

extractProperties (property) {
switch (property.value.type) {
case 'ObjectExpression':
property.value.properties.forEach((p) => {
const entry = getComment(p)
let defaultVisibility = DEFAULT_VISIBILITY
if (property.key.name === 'methods' && this.methodsDefaultPrivate) {
defaultVisibility = 'private'
}
const entry = getComment(p, defaultVisibility)

entry.value = value(p)
entry.name = Object.keys(entry.value)[0]
Expand Down
52 changes: 52 additions & 0 deletions test/fixtures/checkboxMethods.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<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>

/**
* A simple checkbox component
*/
export default {
name: 'checkbox',

methods: {
/**
* @private
*/
privateMethod () {},

/**
* @public
*/
publicMethod () {},

/**
* Check the checkbox
*/
defaultMethod () {},

/**
* @protected
*/
protectedMethod () {},

uncommentedMethod () {}
}
}
</script>

<style lang="css" scoped></style>
73 changes: 68 additions & 5 deletions test/parser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ describe('component_no-top-level-constant', () => testComponent(optionsNoTopLeve

describe('component_filesource', () => testComponent(optionsWithFileSource))

function testComponent(optionsToParse) {
function testComponent (optionsToParse) {
let component = {}

it('should parse without error', (done) => {
Expand Down Expand Up @@ -104,7 +104,7 @@ describe('component.props_module.exports', () => testComponentProps(optionsForMo

describe('component.props_filesource', () => testComponentProps(optionsWithFileSource))

function testComponentProps(optionsToParse) {
function testComponentProps (optionsToParse) {
let component = {}

parser.parse(optionsToParse)
Expand Down Expand Up @@ -154,7 +154,7 @@ describe('component.slots_module.exports', () => testComponentSlots(optionsForMo

describe('component.slots_filesource', () => testComponentSlots(optionsWithFileSource))

function testComponentSlots(optionsToParse) {
function testComponentSlots (optionsToParse) {
let component = {}

parser.parse(options)
Expand Down Expand Up @@ -200,7 +200,7 @@ describe('component.events_module.exports', () => testComponentEvents(optionsFor

describe('component.events_filesource', () => testComponentEvents(optionsWithFileSource))

function testComponentEvents(optionsToParse) {
function testComponentEvents (optionsToParse) {
let component = {}

parser.parse(options)
Expand Down Expand Up @@ -242,7 +242,7 @@ describe('component.methods_module.exports', () => testComponentMethods(optionsF

describe('component.methods_filesource', () => testComponentMethods(optionsWithFileSource))

function testComponentMethods(optionsToParse) {
function testComponentMethods (optionsToParse) {
let component = {}

parser.parse(options)
Expand Down Expand Up @@ -280,3 +280,66 @@ function testComponentMethods(optionsToParse) {
assert.notEqual(typeof item, 'undefined')
})
}

describe('component.methods_visibility_default', () => {
let component = {}

parser.parse({
filename: f('checkboxMethods.vue'),
encoding: 'utf8'
})
.then((_component) => (component = _component))
.catch((err) => {
throw err
})

it('public method should be public', () => {
const item = component.methods.find(
(item) => item.name === 'publicMethod')
assert.equal(item.visibility, 'public')
})

it('uncommented method should be public', () => {
const item = component.methods.find(
(item) => item.name === 'uncommentedMethod')
assert.equal(item.visibility, 'public')
})

it('default method should be public', () => {
const item = component.methods.find(
(item) => item.name === 'defaultMethod')
assert.equal(item.visibility, 'public')
})
})

describe('component.methods_visibility_private', () => {
let component = {}

parser.parse({
filename: f('checkboxMethods.vue'),
encoding: 'utf8',
methodsDefaultPrivate: true
})
.then((_component) => (component = _component))
.catch((err) => {
throw err
})

it('public method should be public', () => {
const item = component.methods.find(
(item) => item.name === 'publicMethod')
assert.equal(item.visibility, 'public')
})

it('uncommented method should not exist', () => {
const item = component.methods.find(
(item) => item.name === 'uncommentedMethod')
assert.equal(item, undefined)
})

it('default method should not exist', () => {
const item = component.methods.find(
(item) => item.name === 'defaultMethod')
assert.equal(item, undefined)
})
})