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

Added "@vue-event" tag for "$emit" events #168

Closed
wants to merge 6 commits into from
Closed
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
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
39 changes: 36 additions & 3 deletions cypress/integration/templates/default.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,28 +144,61 @@ 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(2);

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');

cy.get('#decrement')
.contains('decrement()')
.next('.description')
.next('.details')
.contains('a[href="better-components_BetterCounter.vue.html#line53"]', 'line 53');
.contains('a[href="better-components_BetterCounter.vue.html#line56"]', 'line 56');

cy.get('#increment')
.contains('increment()')
.next('.description')
.next('.details')
.contains('a[href="better-components_BetterCounter.vue.html#line46"]', 'line 46');
.contains('a[href="better-components_BetterCounter.vue.html#line48"]', 'line 48');

cy.get('#showDialog')
.contains('showDialog(counter)')
.next('.description')
.next('h5')
.next('.params')
.next('.details')
.contains('a[href="better-components_BetterCounter.vue.html#line61"]', 'line 61');
.contains('a[href="better-components_BetterCounter.vue.html#line65"]', 'line 65');

cy.contains('created()').should('not.exist');
});
Expand Down
39 changes: 36 additions & 3 deletions cypress/integration/templates/docstrap.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,28 +144,61 @@ describe('Template: docstrap', () => {
});
});

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(2);

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');
cy.get('#decrement')
.contains('decrement()')
.parent()
.next('dd')
.find('.details')
.contains('a[href="better-components_BetterCounter.vue.html#sunlight-1-line-53"]', 'line 53');
.contains('a[href="better-components_BetterCounter.vue.html#sunlight-1-line-56"]', 'line 56');

cy.get('#increment')
.contains('increment()')
.parent()
.next('dd')
.find('.details')
.contains('a[href="better-components_BetterCounter.vue.html#sunlight-1-line-46"]', 'line 46');
.contains('a[href="better-components_BetterCounter.vue.html#sunlight-1-line-48"]', 'line 48');

cy.get('#showDialog')
.contains('showDialog(counter)')
.parent()
.next('dd')
.find('.details')
.contains('a[href="better-components_BetterCounter.vue.html#sunlight-1-line-61"]', 'line 61');
.contains('a[href="better-components_BetterCounter.vue.html#sunlight-1-line-65"]', 'line 65');

cy.contains('created()').should('not.exist');
});
Expand Down
39 changes: 36 additions & 3 deletions cypress/integration/templates/minami.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,25 +146,58 @@ describe('Template: minami', () => {
});
});

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(2);

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');
cy.get('#decrement')
.contains('decrement()')
.next('.description')
.next('.details')
.contains('a[href="better-components_BetterCounter.vue.html#line53"]', 'line 53');
.contains('a[href="better-components_BetterCounter.vue.html#line56"]', 'line 56');

cy.get('#increment')
.contains('increment()')
.next('.description')
.next('.details')
.contains('a[href="better-components_BetterCounter.vue.html#line46"]', 'line 46');
.contains('a[href="better-components_BetterCounter.vue.html#line48"]', 'line 48');

cy.get('#showDialog')
.contains('showDialog(counter)')
.next('.description')
.next('.details')
.contains('a[href="better-components_BetterCounter.vue.html#line61"]', 'line 61');
.contains('a[href="better-components_BetterCounter.vue.html#line65"]', 'line 65');

cy.contains('created()').should('not.exist');
});
Expand Down
39 changes: 36 additions & 3 deletions cypress/integration/templates/tui.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,19 +148,52 @@ describe('Template: tui', () => {
});
});

it('should renders event correctly', () => {
const events = [
{ name: '<code>increment</code>', type: '<span class="param-type">Number</span>', description: 'Emit value of counter after increment' },
{ name: '<code>decrement</code>', type: '<span class="param-type">Number</span>', 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(2);

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');
cy.get('#decrement')
.contains('decrement()')
.contains('a[href="better-components_BetterCounter.vue.html#line53"]', 'line 53');
.contains('a[href="better-components_BetterCounter.vue.html#line56"]', 'line 56');

cy.get('#increment')
.contains('increment()')
.contains('a[href="better-components_BetterCounter.vue.html#line46"]', 'line 46');
.contains('a[href="better-components_BetterCounter.vue.html#line48"]', 'line 48');

cy.get('#showDialog')
.contains('showDialog(counter)')
.contains('a[href="better-components_BetterCounter.vue.html#line61"]', 'line 61');
.contains('a[href="better-components_BetterCounter.vue.html#line65"]', 'line 65');

cy.contains('created()').should('not.exist');
});
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 || {});
},
};
Loading