Skip to content

Commit

Permalink
Merge pull request #36 from soft-decay/master
Browse files Browse the repository at this point in the history
fix(jsdoc): Object literals not supported in type annotation (#35)
  • Loading branch information
alexprey authored Nov 28, 2020
2 parents 66e6b89 + 3078d9a commit bda24da
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
19 changes: 19 additions & 0 deletions examples/Button.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,25 @@
]
},
"defaultValue": "normal"
},
{
"keywords": [
{
"name": "type",
"description": "{{ rtl: boolean, lang: string }}"
}
],
"visibility": "public",
"description": "Additional options for the button.",
"name": "options",
"kind": "let",
"static": false,
"readonly": false,
"type": {
"kind": "type",
"text": "{ rtl: boolean, lang: string }",
"type": "{ rtl: boolean, lang: string }"
}
}
],
"computed": [],
Expand Down
6 changes: 6 additions & 0 deletions examples/Button.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@
* @type {'small'|'normal'|'big'}
*/
export let size = 'normal';
/**
* Additional options for the button.
* @type {{ rtl: boolean, lang: string }}
*/
export let options = { rtl: false, lang: 'en-us' };
</script>

<button type="button" on:click>
Expand Down
2 changes: 1 addition & 1 deletion lib/jsdoc.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

const PARAM_NAME = '[a-z0-9$\\.\\[\\]_]+';

const TYPE = '\\{([^\\}]*)\\}';
const TYPE = '\\{(.*)\\}';
const PARAM_TYPE = '\\{((?:\\.\\.\\.)?[^\\}]*)=?\\}';

const TYPE_RE = new RegExp(`^\\s*${TYPE}`, 'i');
Expand Down
27 changes: 27 additions & 0 deletions test/unit/jsdoc/jsdoc.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,22 @@ describe('JSDoc parser module tests', () => {
expect(type.type).is.equal('string');
});

it('Object literal', () => {
const type = jsdoc.parseTypeKeyword('{{ prop: string }}');

expect(type).is.exist;
expect(type.kind).is.equal('type');
expect(type.type).is.equal('{ prop: string }');
});

it('Nested object literals', () => {
const type = jsdoc.parseTypeKeyword('{{ prop: { internal: string } }}');

expect(type).is.exist;
expect(type.kind).is.equal('type');
expect(type.type).is.equal('{ prop: { internal: string } }');
});

it('Array with generic declaration', () => {
const type = jsdoc.parseTypeKeyword('{Array<string>}');

Expand Down Expand Up @@ -64,6 +80,17 @@ describe('JSDoc parser module tests', () => {
expect(type.type.some(t => t.type === 'MouseEvent')).to.be.true;
});

it('Union of object literals', () => {
const type = jsdoc.parseTypeKeyword('{{ some: string }|{ other: boolean }}');

expect(type).is.exist;
expect(type.kind).is.equal('union');
expect(type.type.length).is.equal(2);

expect(type.type.some(t => t.type === '{ some: string }')).to.be.true;
expect(type.type.some(t => t.type === '{ other: boolean }')).to.be.true;
});

it('Union of classes with spacings', () => {
const type = jsdoc.parseTypeKeyword('{KeyboardEvent | MouseEvent}');

Expand Down

0 comments on commit bda24da

Please sign in to comment.