Skip to content

Commit

Permalink
Implement refs parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
alexprey committed Aug 9, 2019
1 parent a7dafce commit f29e6b1
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 5 deletions.
32 changes: 27 additions & 5 deletions lib/v3/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,11 @@ const SUPPORTED_FEATURES = [
'data',
'computed',
'methods',
// 'actions',
// 'helpers',
'components',
// 'description',
'description',
'events',
'slots',
// 'transitions',
// 'store'
'refs'
];

const SCOPE_DEFAULT = 'default';
Expand Down Expand Up @@ -155,6 +152,14 @@ class Parser extends EventEmitter {
this.emit('event', item);
}

emitRefItem(ref) {
const item = Object.assign({}, ref, {
visibility: 'private'
});

this.emit('ref', item);
}

emitImportedComponentItem(importNode, name, path) {
const item = Object.assign({}, utils.getComment(importNode, 'private'), {
name: name,
Expand Down Expand Up @@ -564,6 +569,23 @@ class Parser extends EventEmitter {
lastComment = null;
}

if (this.features.includes('refs')) {
if (attrs.hasOwnProperty('bind:this') && attrs['bind:this']) {
const value = attrs['bind:this'];
if (value.length > 2 && value.charAt(0) === '{' && value.charAt(value.length - 1) === '}') {
const bindedVariableName = value.substr(1, value.length - 2);

this.emitRefItem({
name: bindedVariableName,
parent: tagName,
loc: this.includeSourceLocations && lastAttributeLocations.hasOwnProperty('bind:this')
? lastAttributeLocations[name]
: null
});
}
}
}

// Parse event handlers
Object.keys(attrs)
.filter(name => name.length > 3 && name.indexOf('on:') === 0 && attrs[name])
Expand Down
5 changes: 5 additions & 0 deletions test/svelte3/integration/refs/ref.component.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<ShopingCart bind:this={cart} />

<script>
import ShopingCart from './ShopingCart.svelte';
</script>
7 changes: 7 additions & 0 deletions test/svelte3/integration/refs/ref.declared.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<div bind:this={header}>
Header
</div>

<script>
let header;
</script>
3 changes: 3 additions & 0 deletions test/svelte3/integration/refs/ref.simple.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div bind:this={header}>
The header
</div>
79 changes: 79 additions & 0 deletions test/svelte3/integration/refs/refs.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
const path = require('path');
const chai = require('chai');
const expect = chai.expect;

const parser = require('../../../../index');

describe('SvelteDoc v3 - Refs', () => {
it('Ref to non declared variable', (done) => {
parser.parse({
version: 3,
filename: path.resolve(__dirname, 'ref.simple.svelte'),
features: ['refs'],
ignoredVisibilities: []
}).then((doc) => {
expect(doc, 'Document should be provided').to.exist;
expect(doc.refs, 'Document refs should be parsed').to.exist;

expect(doc.refs.length).to.equal(1);
const ref = doc.refs[0];

expect(ref, 'Ref should be a valid entity').to.exist;
expect(ref.name).to.equal('header');
expect(ref.parent).to.equal('div');
expect(ref.visibility).to.equal('private');

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

it('Ref to declared variable', (done) => {
parser.parse({
version: 3,
filename: path.resolve(__dirname, 'ref.declared.svelte'),
features: ['refs'],
ignoredVisibilities: []
}).then((doc) => {
expect(doc, 'Document should be provided').to.exist;
expect(doc.refs, 'Document refs should be parsed').to.exist;

expect(doc.refs.length).to.equal(1);
const ref = doc.refs[0];

expect(ref, 'Ref should be a valid entity').to.exist;
expect(ref.name).to.equal('header');
expect(ref.parent).to.equal('div');
expect(ref.visibility).to.equal('private');

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

it('Ref to component', (done) => {
parser.parse({
version: 3,
filename: path.resolve(__dirname, 'ref.component.svelte'),
features: ['refs'],
ignoredVisibilities: []
}).then((doc) => {
expect(doc, 'Document should be provided').to.exist;
expect(doc.refs, 'Document refs should be parsed').to.exist;

expect(doc.refs.length).to.equal(1);
const ref = doc.refs[0];

expect(ref, 'Ref should be a valid entity').to.exist;
expect(ref.name).to.equal('cart');
expect(ref.parent).to.equal('ShopingCart');
expect(ref.visibility).to.equal('private');

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

0 comments on commit f29e6b1

Please sign in to comment.