Skip to content

Commit

Permalink
Merge pull request #82 from ekhaled/events-in-conditions
Browse files Browse the repository at this point in the history
Parse event dispatchers inside if blocks
  • Loading branch information
alexprey authored Sep 8, 2021
2 parents 4e02b7d + 3947738 commit 4abe1ac
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
11 changes: 11 additions & 0 deletions lib/v3/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,17 @@ class ScriptParser extends EventEmitter {
return;
}

if (node.type === 'IfStatement') {
if (node.consequent) {
this.parseBodyRecursively(node.consequent, parseContext, level + 1);
}
if (node.alternate) {
this.parseBodyRecursively(node.alternate, parseContext, level + 1);
}

return;
}

if (node.type === 'VariableDeclaration' && parseContext.scopeType !== SCOPE_MARKUP) {
this.parseVariableDeclarations(node, parseContext, level, 'private');

Expand Down
13 changes: 12 additions & 1 deletion test/svelte3/integration/events/event.dispatcher.default.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@
import { createEventDispatcher } from 'svelte';
const dispatch = createEventDispatcher();
dispatch('notify', 'data');
function someFunc(cond){
dispatch('start', 'data');
if(cond){
dispatch('end', 'data');
}else if(!cond){
dispatch('running', 'data');
}else{
dispatch('failed', 'data')
}
}
</script>
24 changes: 22 additions & 2 deletions test/svelte3/integration/events/events.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ 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(1);
expect(doc.events.length).to.equal(5);
const event = doc.events[0];

expect(event, 'Event should be a valid entity').to.exist;
Expand All @@ -106,7 +106,27 @@ describe('SvelteDoc v3 - Events', () => {

const location = event.locations[0];

expect(location, 'Location should be correct identified').is.deep.equals({ start: 126, end: 134 });
expect(location, 'Location should be correct identified').is.deep.equals({ start: 122, end: 130 });

const event1 = doc.events[1];
expect(event1, 'Event should be a valid entity').to.exist;
expect(event1.name).to.equal('start');
expect(event1.visibility).to.equal('public');

const event2 = doc.events[2];
expect(event2, 'Event should be a valid entity').to.exist;
expect(event2.name).to.equal('end');
expect(event2.visibility).to.equal('public');

const event3= doc.events[3];
expect(event3, 'Event should be a valid entity').to.exist;
expect(event3.name).to.equal('running');
expect(event3.visibility).to.equal('public');

const event4 = doc.events[4];
expect(event4, 'Event should be a valid entity').to.exist;
expect(event4.name).to.equal('failed');
expect(event4.visibility).to.equal('public');

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

0 comments on commit 4abe1ac

Please sign in to comment.