Skip to content

Commit 1c4a4c7

Browse files
authored
fix: Allow array indexes in names (eslint#193)
1 parent 9aed54d commit 1c4a4c7

File tree

2 files changed

+43
-8
lines changed

2 files changed

+43
-8
lines changed

lib/doctrine.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@
266266

267267
function scanIdentifier(last) {
268268
var identifier;
269-
if (!esutils.code.isIdentifierStartES5(source.charCodeAt(index))) {
269+
if (!esutils.code.isIdentifierStartES5(source.charCodeAt(index)) && !source[index].match(/[0-9]/)) {
270270
return null;
271271
}
272272
identifier = advance();
@@ -294,13 +294,13 @@
294294
return null;
295295
}
296296

297-
if (allowBrackets && source.charCodeAt(index) === 0x5B /* '[' */) {
298-
useBrackets = true;
299-
name = advance();
300-
}
301-
302-
if (!esutils.code.isIdentifierStartES5(source.charCodeAt(index))) {
303-
return null;
297+
if (source.charCodeAt(index) === 0x5B /* '[' */) {
298+
if (allowBrackets) {
299+
useBrackets = true;
300+
name = advance();
301+
} else {
302+
return null;
303+
}
304304
}
305305

306306
name += scanIdentifier(last);

test/parse.js

+35
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,41 @@ describe('parse', function () {
498498
});
499499
});
500500

501+
it('param with array indexes', function () {
502+
var res = doctrine.parse(
503+
[
504+
"/**",
505+
" * @param {String} user.0",
506+
"*/"
507+
].join('\n'), { unwrap: true });
508+
res.tags.should.have.length(1);
509+
res.tags[0].should.have.property('title', 'param');
510+
res.tags[0].should.have.property('name', 'user.0');
511+
res.tags[0].should.have.property('type');
512+
res.tags[0].type.should.eql({
513+
type: 'NameExpression',
514+
name: 'String'
515+
});
516+
});
517+
518+
it('param with array indexes and descriptions', function () {
519+
var res = doctrine.parse(
520+
[
521+
"/**",
522+
" * @param {String} user.0 The first element",
523+
"*/"
524+
].join('\n'), { unwrap: true });
525+
res.tags.should.have.length(1);
526+
res.tags[0].should.have.property('title', 'param');
527+
res.tags[0].should.have.property('name', 'user.0');
528+
res.tags[0].should.have.property('type');
529+
res.tags[0].should.have.property('description', 'The first element');
530+
res.tags[0].type.should.eql({
531+
type: 'NameExpression',
532+
name: 'String'
533+
});
534+
});
535+
501536
it('arg with properties', function () {
502537
var res = doctrine.parse(
503538
[

0 commit comments

Comments
 (0)