Skip to content

Commit

Permalink
Fix issue #27 (Events is not exposed from exported functions and arro…
Browse files Browse the repository at this point in the history
…w functions)
  • Loading branch information
alexprey committed Aug 19, 2020
1 parent c5c966f commit f8a292c
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 10 deletions.
14 changes: 11 additions & 3 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,18 @@
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceFolder}\\index.js"
"name": "Debug Mocha Test",
"type": "node",
"program": "${workspaceRoot}/node_modules/mocha/bin/_mocha",
"args": [
"./test/**/*.spec.js",
"--colors",
"--no-timeouts"
],
"cwd": "${workspaceRoot}",
"runtimeExecutable": null,
"env": {}
}
]
}
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how

## UNRELEASED

- 🛠 [Fixed] Fix issue #27 (Events is not exposed from exported functions and arrow functions)

## [3.0.1] 17.08.2020

- [Fixed] Solve issue #26, support `export { variables as var }` statement.
Expand Down
28 changes: 26 additions & 2 deletions lib/v3/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,12 @@ class Parser extends EventEmitter {
this.emitGlobalComment(firstComment);
}

if (node.type === 'BlockStatement') {
this.parseBodyRecursively(node.body, parseContext, level);

return;
}

if (node.type === 'ExpressionStatement') {
const expressionNode = node.expression;

Expand Down Expand Up @@ -341,6 +347,22 @@ class Parser extends EventEmitter {

variables.forEach(variable => {
this.emitDataItem(variable, parseContext, 'public', exportNodeComment);

if (variable.declarator.init) {
const initNode = variable.declarator.init;

if (initNode.type === 'CallExpression') {
const callee = initNode.callee;

if (callee.type === 'Identifier' && this.dispatcherConstructorNames.includes(callee.name)) {
this.dispatcherNames.push(variable.name);
}
} else if (initNode.type === 'ArrowFunctionExpression') {
if (initNode.body) {
this.parseBodyRecursively(initNode.body, parseContext, level + 1);
}
}
}
});

return;
Expand All @@ -351,6 +373,10 @@ class Parser extends EventEmitter {

this.emitMethodItem(func, parseContext, 'public', exportNodeComment);

if (declaration.body) {
this.parseBodyRecursively(declaration.body, parseContext, level + 1);
}

return;
}
}
Expand Down Expand Up @@ -486,8 +512,6 @@ class Parser extends EventEmitter {
}
}

// console.log(util.inspect(node, false, null, true));

if (node.body) {
this.parseBodyRecursively(node.body, parseContext, level + 1);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,12 @@
const anotherHandler = () => {
dispatch('notify2');
};
export function publicMethodHandler() {
dispatch('notify3');
}
export const publicArrow = () => {
dispatch('notify4');
};
</script>
21 changes: 16 additions & 5 deletions test/svelte3/integration/events/events.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,19 +146,30 @@ describe('SvelteDoc v3 - Events', () => {
expect(doc, 'Document should be provided').to.exist;
expect(doc.events, 'Document events should be parsed').to.exist;

expect(doc.events.length).to.equal(2);
const event1 = doc.events[0];
const event1 = doc.events.find(e => e.name === 'notify');

expect(event1, 'Event should be a valid entity').to.exist;
expect(event1, 'Event from regular function must be parsed').to.exist;
expect(event1.name).to.equal('notify');
expect(event1.visibility).to.equal('public');

const event2 = doc.events[1];
const event2 = doc.events.find(e => e.name === 'notify2');

expect(event2, 'Event should be a valid entity').to.exist;
expect(event2, 'Event from arrow function must be parsed').to.exist;
expect(event2.name).to.equal('notify2');
expect(event2.visibility).to.equal('public');

const event3 = doc.events.find(e => e.name === 'notify3');

expect(event3, 'Event from exported function must be parsed').to.exist;
expect(event3.name).to.equal('notify3');
expect(event3.visibility).to.equal('public');

const event4 = doc.events.find(e => e.name === 'notify4');

expect(event4, 'Event from exported arrow function must be parsed').to.exist;
expect(event4.name).to.equal('notify4');
expect(event4.visibility).to.equal('public');

done();
}).catch(e => {
done(e);
Expand Down

0 comments on commit f8a292c

Please sign in to comment.