Skip to content

Commit a771ca7

Browse files
committed
Merge pull request eslint#144 from tschaub/this-name
New: support name expression for @this tag (fixes eslint#143)
2 parents 0155d47 + 479a778 commit a771ca7

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

lib/doctrine.js

+18-1
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,23 @@
582582
return true;
583583
};
584584

585+
TagParser.prototype.parseThis = function parseAccess() {
586+
// this name may be a name expression (e.g. {foo.bar})
587+
// or a name path (e.g. foo.bar)
588+
var value = trim(sliceSource(source, index, this._last));
589+
if (value && value.charAt(0) === '{') {
590+
var gotType = this.parseType();
591+
if (gotType && this._tag.type.type === 'NameExpression') {
592+
this._tag.name = this._tag.type.name;
593+
return true;
594+
} else {
595+
return this.addError('Invalid name for this');
596+
}
597+
} else {
598+
return this.parseNamePath();
599+
}
600+
};
601+
585602
TagParser.prototype.parseVariation = function parseVariation() {
586603
var variation, text;
587604
text = trim(sliceSource(source, index, this._last));
@@ -688,7 +705,7 @@
688705
// http://usejsdoc.org/tags-summary.html
689706
'summary': ['parseDescription'],
690707
// http://usejsdoc.org/tags-this.html
691-
'this': ['parseNamePath', 'ensureEnd'],
708+
'this': ['parseThis', 'ensureEnd'],
692709
// http://usejsdoc.org/tags-todo.html
693710
'todo': ['parseDescription'],
694711
// http://usejsdoc.org/tags-typedef.html

test/parse.js

+26
Original file line numberDiff line numberDiff line change
@@ -1117,6 +1117,32 @@ describe('parse', function () {
11171117
res.tags[0].should.have.property('name', 'thingName.name');
11181118
});
11191119

1120+
it('this with name expression', function () {
1121+
var res = doctrine.parse(
1122+
[
1123+
"/**",
1124+
" * @this {thingName.name}",
1125+
"*/"
1126+
].join('\n'), { unwrap: true });
1127+
res.tags.should.have.length(1);
1128+
res.tags[0].should.have.property('title', 'this');
1129+
res.tags[0].should.have.property('name', 'thingName.name');
1130+
});
1131+
1132+
it('this error with type application', function () {
1133+
var res = doctrine.parse(
1134+
[
1135+
"/**",
1136+
" * @this {Array<string>}",
1137+
"*/"
1138+
].join('\n'), { unwrap: true, recoverable: true });
1139+
res.tags.should.have.length(1);
1140+
res.tags[0].should.have.property('title', 'this');
1141+
res.tags[0].should.have.property('errors');
1142+
res.tags[0].errors.should.have.length(1);
1143+
res.tags[0].errors[0].should.equal('Invalid name for this');
1144+
});
1145+
11201146
it('this error', function () {
11211147
var res = doctrine.parse(
11221148
[

0 commit comments

Comments
 (0)