Skip to content

Commit 12ae7c6

Browse files
authored
Merge pull request #308 from interfaced/feature/add-more-type-checks-to-valid-types
feat(`valid-types`): add more type checks
2 parents 112f244 + 902fac0 commit 12ae7c6

File tree

7 files changed

+381
-130
lines changed

7 files changed

+381
-130
lines changed

README.md

Lines changed: 75 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7362,39 +7362,39 @@ function quux() {
73627362
function quux() {
73637363

73647364
}
7365-
// Message: Syntax error in type: module:namespace.SomeClass<~
7365+
// Message: Syntax error in namepath: module:namespace.SomeClass<~
73667366

73677367
/**
73687368
* @memberof module:namespace.SomeClass~<
73697369
*/
73707370
function quux() {
73717371

73727372
}
7373-
// Message: Syntax error in type: module:namespace.SomeClass~<
7373+
// Message: Syntax error in namepath: module:namespace.SomeClass~<
73747374

73757375
/**
73767376
* @borrows foo% as bar
73777377
*/
73787378
function quux() {
73797379

73807380
}
7381-
// Message: Syntax error in type: foo%
7381+
// Message: Syntax error in namepath: foo%
73827382

73837383
/**
73847384
* @borrows #foo as bar
73857385
*/
73867386
function quux() {
73877387

73887388
}
7389-
// Message: Syntax error in type: #foo
7389+
// Message: Syntax error in namepath: #foo
73907390

73917391
/**
73927392
* @borrows foo as bar%
73937393
*/
73947394
function quux() {
73957395

73967396
}
7397-
// Message: Syntax error in type: bar%
7397+
// Message: Syntax error in namepath: bar%
73987398

73997399
/**
74007400
* @borrows foo
@@ -7411,7 +7411,7 @@ function quux() {
74117411

74127412
}
74137413
// Options: [{"checkSeesForNamepaths":true}]
7414-
// Message: Syntax error in type: foo%
7414+
// Message: Syntax error in namepath: foo%
74157415

74167416
/** */
74177417
function foo() {}
@@ -7424,15 +7424,15 @@ function foo() {}
74247424
function quux() {
74257425

74267426
}
7427-
// Message: Syntax error in type: module:abc#event:foo-bar
7427+
// Message: Syntax error in namepath: module:abc#event:foo-bar
74287428

74297429
/**
74307430
* @mixes module:namespace.SomeClass~
74317431
*/
74327432
function quux() {
74337433

74347434
}
7435-
// Message: Syntax error in type: module:namespace.SomeClass~
7435+
// Message: Syntax error in namepath: module:namespace.SomeClass~
74367436

74377437
/**
74387438
* @callback
@@ -7441,7 +7441,35 @@ function quux() {
74417441

74427442
}
74437443
// Options: [{"allowEmptyNamepaths":false}]
7444-
// Message: Syntax error in type:
7444+
// Message: Tag @callback must have a namepath
7445+
7446+
/**
7447+
* @constant {str%ng}
7448+
*/
7449+
const FOO = 'foo';
7450+
// Message: Syntax error in type: str%ng
7451+
7452+
/**
7453+
* @typedef {str%ng} UserString
7454+
*/
7455+
// Message: Syntax error in type: str%ng
7456+
7457+
/**
7458+
* @typedef {string} UserStr%ng
7459+
*/
7460+
// Message: Syntax error in namepath: UserStr%ng
7461+
7462+
/**
7463+
* @extends
7464+
*/
7465+
class Bar {};
7466+
// Message: Tag @extends must have either a type or namepath
7467+
7468+
/**
7469+
* @type
7470+
*/
7471+
let foo;
7472+
// Message: Tag @type must have a type
74457473
````
74467474

74477475
The following patterns are not considered problems:
@@ -7496,12 +7524,20 @@ function quux() {
74967524

74977525
}
74987526

7527+
/**
7528+
* @callback foo
7529+
*/
7530+
function quux() {
7531+
7532+
}
7533+
74997534
/**
75007535
* @callback
75017536
*/
75027537
function quux() {
75037538

75047539
}
7540+
// Options: [{"allowEmptyNamepaths":true}]
75057541

75067542
/**
75077543
* @class
@@ -7516,6 +7552,7 @@ function quux() {
75167552
function quux() {
75177553

75187554
}
7555+
// Options: [{"checkSeesForNamepaths":true}]
75197556

75207557
/**
75217558
*
@@ -7545,6 +7582,35 @@ function quux() {
75457582
function quux() {
75467583

75477584
}
7585+
7586+
/**
7587+
* @constant {string}
7588+
*/
7589+
const FOO = 'foo';
7590+
7591+
/**
7592+
* @constant {string} FOO
7593+
*/
7594+
const FOO = 'foo';
7595+
7596+
/**
7597+
* @extends Foo
7598+
*/
7599+
class Bar {};
7600+
7601+
/**
7602+
* @extends {Foo<String>}
7603+
*/
7604+
class Bar {};
7605+
7606+
/**
7607+
* @typedef {number|string} UserDefinedType
7608+
*/
7609+
7610+
/**
7611+
* @typedef {number|string}
7612+
*/
7613+
let UserDefinedGCCType;
75487614
````
75497615

75507616

src/iterateJsdoc.js

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,31 @@ import getJSDocComment from './eslint/getJSDocComment';
1111
* @returns {object}
1212
*/
1313
const parseComment = (commentNode, indent, trim = true) => {
14+
const skipSeeLink = (parser) => {
15+
return (str, data) => {
16+
if (data.tag === 'see' && str.match(/{@link.+?}/)) {
17+
return null;
18+
}
19+
20+
return parser(str, data);
21+
};
22+
};
23+
1424
// Preserve JSDoc block start/end indentation.
1525
return commentParser(`${indent}/*${commentNode.value}${indent}*/`, {
1626
// @see https://github.com/yavorskiy/comment-parser/issues/21
1727
parsers: [
1828
commentParser.PARSERS.parse_tag,
19-
commentParser.PARSERS.parse_type,
20-
(str, data) => {
21-
if (['example', 'return', 'returns', 'throws', 'exception'].includes(data.tag)) {
22-
return null;
23-
}
29+
skipSeeLink(commentParser.PARSERS.parse_type),
30+
skipSeeLink(
31+
(str, data) => {
32+
if (['example', 'return', 'returns', 'throws', 'exception'].includes(data.tag)) {
33+
return null;
34+
}
2435

25-
return commentParser.PARSERS.parse_name(str, data);
26-
},
36+
return commentParser.PARSERS.parse_name(str, data);
37+
},
38+
),
2739
trim ?
2840
commentParser.PARSERS.parse_description :
2941

@@ -181,20 +193,32 @@ const getUtils = (
181193
return false;
182194
};
183195

184-
utils.isNamepathDefiningTag = (tagName) => {
185-
return jsdocUtils.isNamepathDefiningTag(tagName);
196+
utils.tagMustHaveEitherTypeOrNamepath = (tagName) => {
197+
return jsdocUtils.tagMustHaveEitherTypeOrNamepath(tagName);
198+
};
199+
200+
utils.tagMightHaveEitherTypeOrNamepath = (tagName) => {
201+
return jsdocUtils.tagMightHaveEitherTypeOrNamepath(tagName);
186202
};
187-
utils.isNamepathTag = (tagName, checkSeesForNamepaths) => {
188-
return jsdocUtils.isNamepathTag(tagName, checkSeesForNamepaths);
203+
204+
utils.tagMustHaveNamepath = (tagName) => {
205+
return jsdocUtils.tagMustHaveNamepath(tagName);
189206
};
190207

191-
utils.isTagWithType = (tagName) => {
192-
return jsdocUtils.isTagWithType(tagName);
208+
utils.tagMightHaveNamepath = (tagName) => {
209+
return jsdocUtils.tagMightHaveNamepath(tagName);
193210
};
194211

195-
utils.passesEmptyNamepathCheck = (tag, allowEmptyNamepaths) => {
196-
return !tag.name && allowEmptyNamepaths &&
197-
jsdocUtils.isPotentiallyEmptyNamepathTag(tag.tag);
212+
utils.tagMustHaveType = (tagName) => {
213+
return jsdocUtils.tagMustHaveType(tagName);
214+
};
215+
216+
utils.tagMightHaveType = (tagName) => {
217+
return jsdocUtils.tagMightHaveType(tagName);
218+
};
219+
220+
utils.isNamepathDefiningTag = (tagName) => {
221+
return jsdocUtils.isNamepathDefiningTag(tagName);
198222
};
199223

200224
utils.hasDefinedTypeReturnTag = (tag) => {

0 commit comments

Comments
 (0)