From 4f028d89bce2fa76ee6b80236345be74593c595c Mon Sep 17 00:00:00 2001 From: Tom MacWright Date: Fri, 18 Aug 2017 11:05:55 -0700 Subject: [PATCH] fix: Allow array indexes in names (#193) --- lib/doctrine.js | 16 ++++++++-------- test/parse.js | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 8 deletions(-) diff --git a/lib/doctrine.js b/lib/doctrine.js index bd6d38c..263d7d6 100644 --- a/lib/doctrine.js +++ b/lib/doctrine.js @@ -266,7 +266,7 @@ function scanIdentifier(last) { var identifier; - if (!esutils.code.isIdentifierStartES5(source.charCodeAt(index))) { + if (!esutils.code.isIdentifierStartES5(source.charCodeAt(index)) && !source[index].match(/[0-9]/)) { return null; } identifier = advance(); @@ -294,13 +294,13 @@ return null; } - if (allowBrackets && source.charCodeAt(index) === 0x5B /* '[' */) { - useBrackets = true; - name = advance(); - } - - if (!esutils.code.isIdentifierStartES5(source.charCodeAt(index))) { - return null; + if (source.charCodeAt(index) === 0x5B /* '[' */) { + if (allowBrackets) { + useBrackets = true; + name = advance(); + } else { + return null; + } } name += scanIdentifier(last); diff --git a/test/parse.js b/test/parse.js index 18421dd..008acef 100644 --- a/test/parse.js +++ b/test/parse.js @@ -498,6 +498,41 @@ describe('parse', function () { }); }); + it('param with array indexes', function () { + var res = doctrine.parse( + [ + "/**", + " * @param {String} user.0", + "*/" + ].join('\n'), { unwrap: true }); + res.tags.should.have.length(1); + res.tags[0].should.have.property('title', 'param'); + res.tags[0].should.have.property('name', 'user.0'); + res.tags[0].should.have.property('type'); + res.tags[0].type.should.eql({ + type: 'NameExpression', + name: 'String' + }); + }); + + it('param with array indexes and descriptions', function () { + var res = doctrine.parse( + [ + "/**", + " * @param {String} user.0 The first element", + "*/" + ].join('\n'), { unwrap: true }); + res.tags.should.have.length(1); + res.tags[0].should.have.property('title', 'param'); + res.tags[0].should.have.property('name', 'user.0'); + res.tags[0].should.have.property('type'); + res.tags[0].should.have.property('description', 'The first element'); + res.tags[0].type.should.eql({ + type: 'NameExpression', + name: 'String' + }); + }); + it('arg with properties', function () { var res = doctrine.parse( [