Skip to content

Commit

Permalink
Merge pull request #1 from Kokosso/feature/add_tag_event
Browse files Browse the repository at this point in the history
Add event tag for jsdoc-vuejs
  • Loading branch information
Kokosso committed Jan 8, 2019
2 parents e143554 + eb8ebf0 commit 9410183
Show file tree
Hide file tree
Showing 12 changed files with 155 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"_isVueDoc",
"_vueProps",
"_vueData",
"_vueComputed"
"_vueComputed",
"_vueEvent"
]
}]
}
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Update your .vue files with one of the following tags:
- `@vue-prop`
- `@vue-data`
- `@vue-computed`
- `@vue-event`

All of those tags work the same way than [`@param` tag](http://usejsdoc.org/tags-param.html).

Expand Down
5 changes: 4 additions & 1 deletion __tests__/core/renderer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ describe('code.renderer', () => {
it('should call ejs render method', () => {
const cb = () => {};

renderer('my-template', { props: ['props'], data: ['data'], computed: ['computed'] }, cb);
renderer('my-template', {
props: ['props'], data: ['data'], computed: ['computed'], event: ['event'],
}, cb);

expect(ejs.renderFile).toHaveBeenCalledTimes(1);
expect(ejs.renderFile).toHaveBeenCalledWith(
Expand All @@ -18,6 +20,7 @@ describe('code.renderer', () => {
props: ['props'],
data: ['data'],
computed: ['computed'],
event: ['event'],
// an helper function, it should be under keys "utils" or "helpers" btw
renderType: expect.any(Function),
},
Expand Down
33 changes: 33 additions & 0 deletions cypress/integration/templates/default.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,39 @@ describe('Template: default', () => {
});
});

it('should renders event correctly', () => {
const events = [
{ name: '<code>increment</code>', type: 'Number', description: 'Emit value of counter after increment' },
{ name: '<code>decrement</code>', type: 'Number', description: 'Emit value of counter after decrement' },
];

cy.get('[data-jsdoc-vuejs="section-event"]').contains('Events');
cy.get('[data-jsdoc-vuejs="table-event"]').as('table-event');

cy
.get('@table-event')
.find('> thead > tr > th')
.contains('Name')
.next().contains('Payload-type')
.next().contains('Description');

cy
.get('@table-event')
.find('> tbody > tr')
.then(($rows) => {
expect($rows).to.have.length(3);

events.forEach((event, i) => {
const $row = $rows.eq(i);
const $children = $row.children();

expect($children.eq(0).html()).to.eq(event.name);
expect($children.eq(1).html()).to.eq(event.type);
expect($children.eq(2).html()).to.eq(event.description);
});
});
});

it('should render methods properly', () => {
cy.contains('h3', 'Methods').should('have.attr', 'class', 'subsection-title');

Expand Down
8 changes: 6 additions & 2 deletions example/src/better-components/BetterCounter.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
* @vue-computed {Array.<String>} fooList - A list of foo
* @vue-computed {Array.<String>} barList - A list of bar
* @vue-computed {String} message A message
* @vue-event {Number} increment - Emit value of counter after increment
* @vue-event {Number} decrement - Emit value of counter after decrement
*/
export default {
props: {
Expand All @@ -41,17 +43,19 @@
},
methods: {
/**
* Increment counter.
* Increment counter and emit event 'increment'
*/
increment() {
this.counter += this.step;
this.$emit('increment', this.counter);
},
/**
* Decrement counter.
* Decrement counter and emit event 'decrement'
*/
decrement() {
this.counter -= this.step;
this.$emit('decrement', this.counter);
},
/**
Expand Down
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const { isSingleFileComponent, isJSComponent } = require('./lib/core/issers');
const vueDataTag = require('./lib/tags/vue-data');
const vuePropTag = require('./lib/tags/vue-prop');
const vueComputedTag = require('./lib/tags/vue-computed');
const vueEventTag = require('./lib/tags/vue-event');

// Used to compute good line number for Vue methods
const exportDefaultLines = {};
Expand All @@ -22,7 +23,6 @@ exports.handlers = {
newDoclet(e) {
const fileIsSingleFileComponent = isSingleFileComponent(e.doclet);
const fileIsJSComponent = isJSComponent(e.doclet);

if (!fileIsSingleFileComponent && !fileIsJSComponent) {
return;
}
Expand Down Expand Up @@ -53,6 +53,7 @@ exports.handlers = {
props: e.doclet._vueProps || [],
data: e.doclet._vueData || [],
computed: e.doclet._vueComputed || [],
event: e.doclet._vueEvent || [],
};

render(template, data, (err, str) => {
Expand Down Expand Up @@ -84,4 +85,5 @@ exports.defineTags = function defineTags(dictionary) {
dictionary.defineTag(vueDataTag.name, vueDataTag.options);
dictionary.defineTag(vuePropTag.name, vuePropTag.options);
dictionary.defineTag(vueComputedTag.name, vueComputedTag.options);
dictionary.defineTag(vueEventTag.name, vueEventTag.options);
};
5 changes: 4 additions & 1 deletion lib/core/renderer.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
const ejs = require('ejs');
const renderType = require('../templates/utils/renderType');

module.exports = function render(template, { props, data, computed }, cb) {
module.exports = function render(template, {
props, data, computed, event,
}, cb) {
ejs.renderFile(
template,
{
renderType,
props,
data,
computed,
event,
},
cb,
);
Expand Down
11 changes: 11 additions & 0 deletions lib/tags/vue-event.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
exports.name = 'vue-event';

exports.options = {
canHaveType: true, // type of event-payload
canHaveName: true, // name of emitted event
onTagged(doclet, tag) {
doclet._isVueDoc = true;
doclet._vueEvent = doclet._vueEvent || [];
doclet._vueEvent.push(tag.value || {});
},
};
23 changes: 23 additions & 0 deletions lib/templates/default.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,27 @@
</table>
<% } %>
<% if(event.length > 0) { %>
<h3 class="subsection-title" style="margin-top: 1em" data-jsdoc-vuejs="section-event">Events</h3>
<table data-jsdoc-vuejs="table-event">
<thead>
<tr>
<th>Name</th>
<th>Payload-type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<% event.forEach(function(c) { %>
<tr>
<td><code><%- c.name %></code></td>
<td><%- renderType(c.type) %></td>
<td><%- typeof c.description === 'undefined' ? '-' : c.description %></td>
</tr>
<% }) %>
</tbody>
</table>
<% } %>
<div class="container-overview"><div><p> <%# Re-open JSDoc template tags %>
22 changes: 22 additions & 0 deletions lib/templates/docstrap.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,26 @@
</table>
<% } %>
<% if(event.length > 0) { %>
<h3 class="subsection-title" style="margin-top: 1em" data-jsdoc-vuejs="section-event">Events</h3>
<table data-jsdoc-vuejs="table-event">
<thead>
<tr>
<th>Name</th>
<th>Payload-type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<% event.forEach(function(c) { %>
<tr>
<td><code><%- c.name %></code></td>
<td><%- renderType(c.type) %></td>
<td><%- typeof c.description === 'undefined' ? '-' : c.description %></td>
</tr>
<% }) %>
</tbody>
</table>
<% } %>
<div class="container-overview"><div><p> <%# Re-open JSDoc template tags %>
22 changes: 22 additions & 0 deletions lib/templates/minami.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,26 @@
</table>
<% } %>
<% if(event.length > 0) { %>
<h3 class="subsection-title" style="margin-top: 1em" data-jsdoc-vuejs="section-event">Events</h3>
<table class="params" data-jsdoc-vuejs="table-event">
<thead>
<tr>
<th>Name</th>
<th>Payload-type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<% event.forEach(function(c) { %>
<tr>
<td><code><%- c.name %></code></td>
<td><%- renderType(c.type) %></td>
<td><%- typeof c.description === 'undefined' ? '-' : c.description %></td>
</tr>
<% }) %>
</tbody>
</table>
<% } %>
<div class="container-overview"><div><p> <%# Re-open JSDoc template tags %>
24 changes: 24 additions & 0 deletions lib/templates/tui.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,28 @@
</div>
<% } %>
<% if(computed.length > 0) { %>
<h3 class="subsection-title" style="margin-top: 1em" data-jsdoc-vuejs="section-event">Events</h3>
<div class="container-params">
<table class="params" data-jsdoc-vuejs="table-event">
<thead>
<tr>
<th>Name</th>
<th>Payload-type</th>
<th class="last">Description</th>
</tr>
</thead>
<tbody>
<% event.forEach(function(c) { %>
<tr>
<td class="name"><code><%- c.name %></code></td>
<td class="type"><span class="param-type"><%- renderType(c.type) %></span></td>
<td class="description last"><%- typeof c.description === 'undefined' ? '-' : c.description %></td>
</tr>
<% }) %>
</tbody>
</table>
</div>
<% } %>
<div class="container-overview"><div><p> <%# Re-open JSDoc template tags %>

0 comments on commit 9410183

Please sign in to comment.