Skip to content

Commit

Permalink
Parsing component imports
Browse files Browse the repository at this point in the history
  • Loading branch information
ArdenIvanov committed Jun 3, 2019
1 parent 0b25055 commit ec5de3d
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 3 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ module.exports.parse = (options) => new Promise((resolve, reject) => {

const structure = loadFileStructureFromOptions(options);

const version = options.version || SvelteVersionDetector.detectVersionFromStructure(structure);
const version = options.version || SvelteVersionDetector.detectVersionFromStructure(structure, options.defaultVersion);

const parser = buildSvelteParser(structure, options, version);

Expand Down
38 changes: 36 additions & 2 deletions lib/v3/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const SUPPORTED_FEATURES = [
// 'methods',
// 'actions',
// 'helpers',
// 'components',
'components',
// 'description',
'events',
'slots',
Expand Down Expand Up @@ -126,6 +126,15 @@ class Parser extends EventEmitter {
this.emit('computed', item);
}

emitImportedComponentItem(importNode, name, path) {
const item = Object.assign({}, utils.getComment(importNode, 'private'), {
name: name,
value: path
});

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

parseScriptBlock(scriptBlock) {
const ast = espree.parse(scriptBlock.content, {
attachComment: true,
Expand Down Expand Up @@ -183,7 +192,32 @@ class Parser extends EventEmitter {
}
}

console.log(util.inspect(node, false, null, true));
if (node.type === 'ImportDeclaration') {
const specifier = node.specifiers[0];

if (specifier && specifier.type === 'ImportDefaultSpecifier') {
const source = node.source;

if (source && source.type === 'Literal') {
const importEntry = {
identifier: specifier.local.name,
sourceFilename: source.value
};

if (!this.imports.hasOwnProperty(importEntry.identifier)) {
this.imports[importEntry.identifier] = importEntry;

if (importEntry.identifier && importEntry.identifier[0] === importEntry.identifier[0].toUpperCase()) {
this.emitImportedComponentItem(node, importEntry.identifier, importEntry.sourceFilename);

return;
}
}
}
}
}

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

Expand Down
4 changes: 4 additions & 0 deletions test/svelte3/integration/components/components.import.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<script>
/** The nested component. */
import Nested from './components.nested.svelte';
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<script>
import * as Imported from './components.importable.js';
</script>
2 changes: 2 additions & 0 deletions test/svelte3/integration/components/components.importable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export let X = 1;
export let y = 2;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<script>
import nested from './components.nested.svelte';
</script>
4 changes: 4 additions & 0 deletions test/svelte3/integration/components/components.nested.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<script>
let width = 1;
let height = 2;
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<script>
import { X } from './components.importable.js';
</script>
81 changes: 81 additions & 0 deletions test/svelte3/integration/components/components.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
const path = require('path');
const chai = require('chai');
const expect = chai.expect;

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

describe('SvelteDoc v3 - Components', () => {
it('Import with upper case default should be parsed as component', (done) => {
parser.parse({
version: 3,
filename: path.resolve(__dirname, 'components.import.svelte'),
features: ['components'],
ignoredVisibilities: []
}).then((doc) => {
expect(doc, 'Document should be provided').to.exist;
expect(doc.components, 'Document components should be parsed').to.exist;

expect(doc.components.length).to.equal(1);
const component = doc.components[0];
expect(component.name).to.equal('Nested');
expect(component.value).to.equal('./components.nested.svelte');
expect(component.visibility).to.equal('private');

expect(component.description).to.equal('The nested component.');
done();
}).catch(e => {
done(e);
});
});

it('Import with upper case not default should not be parsed as component', (done) => {
parser.parse({
version: 3,
filename: path.resolve(__dirname, 'components.notdefault.svelte'),
features: ['components'],
ignoredVisibilities: []
}).then((doc) => {
expect(doc, 'Document should be provided').to.exist;
expect(doc.components, 'Document components should be parsed').to.exist;

expect(doc.components.length).to.equal(0);
done();
}).catch(e => {
done(e);
});
});

it('Import with lowercase case default should not be parsed as component', (done) => {
parser.parse({
version: 3,
filename: path.resolve(__dirname, 'components.lowercase.svelte'),
features: ['components'],
ignoredVisibilities: []
}).then((doc) => {
expect(doc, 'Document should be provided').to.exist;
expect(doc.components, 'Document components should be parsed').to.exist;

expect(doc.components.length).to.equal(0);
done();
}).catch(e => {
done(e);
});
});

it('Import with start and alias should not be parsed as component', (done) => {
parser.parse({
version: 3,
filename: path.resolve(__dirname, 'components.importStar.svelte'),
features: ['components'],
ignoredVisibilities: []
}).then((doc) => {
expect(doc, 'Document should be provided').to.exist;
expect(doc.components, 'Document components should be parsed').to.exist;

expect(doc.components.length).to.equal(0);
done();
}).catch(e => {
done(e);
});
});
});

0 comments on commit ec5de3d

Please sign in to comment.