From f8a292cdd3f277e4b00691505df8ea725f93a079 Mon Sep 17 00:00:00 2001 From: Alexey Mulyukin Date: Wed, 19 Aug 2020 18:25:59 +0300 Subject: [PATCH] Fix issue #27 (Events is not exposed from exported functions and arrow functions) --- .vscode/launch.json | 14 ++++++++-- CHANGELOG.md | 2 ++ lib/v3/parser.js | 28 +++++++++++++++++-- .../event.dispatcher.insideMethod.svelte | 8 ++++++ .../svelte3/integration/events/events.spec.js | 21 ++++++++++---- 5 files changed, 63 insertions(+), 10 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index b048bd4..9a58ec6 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -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": {} } ] } \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index 57f9eb8..323610a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/lib/v3/parser.js b/lib/v3/parser.js index 3b25d80..3bb4c95 100644 --- a/lib/v3/parser.js +++ b/lib/v3/parser.js @@ -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; @@ -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; @@ -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; } } @@ -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); } diff --git a/test/svelte3/integration/events/event.dispatcher.insideMethod.svelte b/test/svelte3/integration/events/event.dispatcher.insideMethod.svelte index d18bde0..8b5ea9a 100644 --- a/test/svelte3/integration/events/event.dispatcher.insideMethod.svelte +++ b/test/svelte3/integration/events/event.dispatcher.insideMethod.svelte @@ -10,4 +10,12 @@ const anotherHandler = () => { dispatch('notify2'); }; + + export function publicMethodHandler() { + dispatch('notify3'); + } + + export const publicArrow = () => { + dispatch('notify4'); + }; \ No newline at end of file diff --git a/test/svelte3/integration/events/events.spec.js b/test/svelte3/integration/events/events.spec.js index bdeabda..503c10f 100644 --- a/test/svelte3/integration/events/events.spec.js +++ b/test/svelte3/integration/events/events.spec.js @@ -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);