Skip to content

Commit

Permalink
Merge pull request #22 from hontas/master
Browse files Browse the repository at this point in the history
Enable getting type from comment for 'ExportNamedDeclaration'
  • Loading branch information
alexprey authored Dec 9, 2019
2 parents 2cc3d61 + 15003a3 commit 259534d
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 24 deletions.
18 changes: 4 additions & 14 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const getVisibility = (keywords, defaultVisibility) => {
return defaultVisibility;
};

const parseComment = (text, defaultVisibility = DEFAULT_VISIBILITY, features = ['description', 'keywords']) => {
const parseComment = (text, defaultVisibility = DEFAULT_VISIBILITY) => {
const result = {
keywords: [],
visibility: defaultVisibility,
Expand Down Expand Up @@ -47,29 +47,19 @@ const parseComment = (text, defaultVisibility = DEFAULT_VISIBILITY, features = [
index = matches.index;

const name = matches[1];
const description = features.includes('description')
? (matches[4] || '').trim()
: '';
const description = (matches[4] || '').trim();

result.keywords.push({ name, description });
}

if (features.includes('description')) {
result.description = parsedText.substring(0, indexDescription).trim();
}

result.description = parsedText.substring(0, indexDescription).trim();
result.visibility = getVisibility(result.keywords, result.visibility);

if (!features.includes('keywords')) {
result.keywords = [];
}

return result;
};

const getComment = (property, {
defaultVisibility = DEFAULT_VISIBILITY,
features,
useFirst = false,
useLeading = true,
useTrailing = true
Expand All @@ -91,7 +81,7 @@ const getComment = (property, {
}

if (lastComment) {
return parseComment(lastComment, defaultVisibility, features);
return parseComment(lastComment, defaultVisibility);
}

return {
Expand Down
3 changes: 2 additions & 1 deletion lib/v3/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const DEFAULT_OPTIONS = {};

const SUPPORTED_FEATURES = [
'name',
'keywords',
'data',
'computed',
'methods',
Expand Down Expand Up @@ -332,7 +333,7 @@ class Parser extends EventEmitter {
const declaration = node.declaration;

if (declaration) {
const exportNodeComment = utils.getComment(node, { defaultVisibility: 'public', features: this.features, useLeading: true, useTrailing: false });
const exportNodeComment = utils.getComment(node, { defaultVisibility: 'public', useLeading: true, useTrailing: false });

if (declaration.type === 'VariableDeclaration') {
const variables = this.parseVariableDeclaration(declaration);
Expand Down
13 changes: 7 additions & 6 deletions test/svelte3/integration/data/data.public.svelte
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<script>
export let variable = 'hello';
export let variable = "hello";
/**
* The property comment.
*/
export let propertyWithComment;
</script>
/**
* The property comment.
* @type {string}
*/
export let propertyWithComment;
</script>
6 changes: 3 additions & 3 deletions test/svelte3/integration/data/data.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ describe('SvelteDoc v3 - Props', () => {
parser.parse({
version: 3,
filename: path.resolve(__dirname, 'data.public.svelte'),
features: ['data', 'description'],
features: ['data'],
ignoredVisibilities: []
}).then((doc) => {
expect(doc, 'Document should be provided').to.exist;
expect(doc.data, 'Document events should be parsed').to.exist;
expect(doc.data, 'Document data should be parsed').to.exist;

expect(doc.data.length).to.equal(2);
const variableItem = doc.data.find(item => item.name === 'variable');
Expand All @@ -63,7 +63,7 @@ describe('SvelteDoc v3 - Props', () => {
expect(variableWithCommentItem.visibility).to.equal('public');
expect(variableWithCommentItem.static).to.be.false;
expect(variableWithCommentItem.description).to.be.equal('The property comment.');
expect(variableWithCommentItem.type).to.eql({ kind: 'type', type: 'any', text: 'any' });
expect(variableWithCommentItem.type).to.eql({ kind: 'type', type: 'string', text: 'string' });
expect(variableWithCommentItem.defaultValue).to.equal(undefined);

done();
Expand Down

0 comments on commit 259534d

Please sign in to comment.