Skip to content

Commit

Permalink
Some fixes for param parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
alexprey committed Dec 7, 2018
1 parent 8f32405 commit e3227d7
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 11 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ All notable changes to the "svelte-intellisense" extension will be documented in

Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how to structure this file.

## [1.1.4] 07.12.2018
## [1.1.5] 07.12.2018

- [Fixed] Fix and refactor param keyword parsing to handle some wrong cases (fix and close issue #7)

Expand Down
11 changes: 6 additions & 5 deletions lib/jsdoc.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
const PARAM_NAME = '[a-z0-9$\\.\\[\\]_]+';
const TYPE = '\\s*\\(?\\s*(?:(?:\\*|[a-z_]+[a-z0-9_<>\\[\\]]*|\\\'[^\\\']*\\\')\\s*\\|?\\s*)+\\s*\\)?\\s*';

const TYPE_RE = new RegExp(`^\\s*\\{(${TYPE})\\}`, 'i');
const PARAM_NAME = '[a-z0-9$\\.\\[\\]_]+';

const PARAM_RE = new RegExp(`^\\s*(?:\\{((?:\\.\\.\\.)?${TYPE}=?)\\}\\s+)?(?:\\[\\s*(${PARAM_NAME})\\s*(?:=\s*([^\\]]+))?\\]|(${PARAM_NAME}))(?:\\s+(?:\\-\\s+)?(.*))?`, 'i');
const TYPE = '\\{([^\\}]*)\\}';
const PARAM_TYPE = '\\{((?:\\.\\.\\.)?[^\\}]*)=?\\}';

const RETURN_RE = new RegExp(`^\\s*(\\{\\(?(${TYPE})\\)?\\}\\s+)?-?(.*)`, 'i');
const TYPE_RE = new RegExp(`^\\s*${TYPE}`, 'i');
const PARAM_RE = new RegExp(`^\\s*(?:${PARAM_TYPE}\\s+)?(?:\\[\\s*(${PARAM_NAME})\\s*(?:=\s*([^\\]]+))?\\]|(${PARAM_NAME}))(?:\\s+(?:\\-\\s+)?(.*))?`, 'i');
const RETURN_RE = new RegExp(`^\\s*(${TYPE}\\s+)?-?(.*)`, 'i');

const DEFAULT_TYPE = 'any';

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sveltedoc-parser",
"version": "1.1.4",
"version": "1.1.5",
"description": "Generate a JSON documentation for a Svelte file",
"main": "index.js",
"scripts": {
Expand Down
27 changes: 23 additions & 4 deletions test/unit/jsdoc/jsdoc.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,15 @@ describe('JSDoc parser module tests', () => {
});

it('Union of string constants', () => {
const type = jsdoc.parseTypeKeyword('{\'plain\'|\'primary<alert>\'|\'{secondary}\'|\'plain-inverse\'}');
const type = jsdoc.parseTypeKeyword('{\'plain\'|\'primary<alert>\'|\'secondary\'|\'plain-inverse\'}');

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

expect(type.type.some(t => t.kind === 'const' && t.value === 'plain'), 'should have \'plain\' const').to.be.true;
expect(type.type.some(t => t.kind === 'const' && t.value === 'primary<alert>'), 'should have \'primary<alert>\' const').to.be.true;
expect(type.type.some(t => t.kind === 'const' && t.value === '{secondary}'), 'should have \'{secondary}\' const').to.be.true;
expect(type.type.some(t => t.kind === 'const' && t.value === 'secondary'), 'should have \'secondary\' const').to.be.true;
expect(type.type.some(t => t.kind === 'const' && t.value === 'plain-inverse'), 'should have \'plain-inverse\' const').to.be.true;
});
});
Expand Down Expand Up @@ -303,16 +303,35 @@ describe('JSDoc parser module tests', () => {
});

it('Union of string constants', () => {
const param = jsdoc.parseParamKeyword('{\'plain\'|\'primary<alert>\'|\'{secondary}\'|\'plain-inverse\'} parameter');
const param = jsdoc.parseParamKeyword('{\'plain\'|\'primary<alert>\'|\'secondary\'|\'plain-inverse\'} parameter');

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

expect(param.type.type.some(t => t.kind === 'const' && t.value === 'plain'), 'should have \'plain\' const').to.be.true;
expect(param.type.type.some(t => t.kind === 'const' && t.value === 'primary<alert>'), 'should have \'primary<alert>\' const').to.be.true;
expect(param.type.type.some(t => t.kind === 'const' && t.value === '{secondary}'), 'should have \'{secondary}\' const').to.be.true;
expect(param.type.type.some(t => t.kind === 'const' && t.value === 'secondary'), 'should have \'secondary\' const').to.be.true;
expect(param.type.type.some(t => t.kind === 'const' && t.value === 'plain-inverse'), 'should have \'plain-inverse\' const').to.be.true;
});

it('Union of string constants with missing quote', () => {
const param = jsdoc.parseParamKeyword('{\'plain\'|\'primary<alert>\'|secondary\'|\'plain-inverse\'} parameter');

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

expect(param.type.type.some(t => t.kind === 'const' && t.value === 'plain'), 'should have \'plain\' const').to.be.true;
expect(param.type.type.some(t => t.kind === 'const' && t.value === 'primary<alert>'), 'should have \'primary<alert>\' const').to.be.true;
expect(param.type.type.some(t => t.kind === 'type' && t.type === 'secondary\''), 'should have \'secondary\' as a not constant').to.be.true;
expect(param.type.type.some(t => t.kind === 'const' && t.value === 'plain-inverse'), 'should have \'plain-inverse\' const').to.be.true;
});

it('Union of string constants with figure bracet', () => {
const param = jsdoc.parseParamKeyword('{\'plain\'|\'primary<alert>\'|\'seco{ndary}\'|\'plain-inverse\'} parameter');

expect(param.type).is.exist;
});
});
});

0 comments on commit e3227d7

Please sign in to comment.