Skip to content

Commit 54d8bec

Browse files
committed
fix(valid-types): allow more namepath-defining tags by default
Some namepath-defining tags allow the defining of a name elsewhere in the block, so don't automatically enforce rule against these tags
1 parent 78766fd commit 54d8bec

File tree

4 files changed

+70
-20
lines changed

4 files changed

+70
-20
lines changed

.README/rules/valid-types.md

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,25 @@ Requires all types to be valid JSDoc or Closure compiler types without syntax er
44

55
Also impacts behaviors on namepath (or event)-defining and pointing tags:
66

7-
1. Name(path)-defining tags: `@class`, `@constructor`, `@constant`, `@const`, `@external`, `@host`, `@function`, `@func`, `@method`, `@interface`, `@member`, `@var`, `@mixin`, `@name`, `@namespace`, `@typedef`
8-
1. Name(path)-pointing tags: `@alias`, `@augments`, `@extends`, `@lends`, `@memberof`, `@memberof!`, `@mixes`, `@this`
9-
1. Name(path)-defining tags (which may have value without namepath): `@callback`, `@event`
10-
1. Name(path)-pointing tags (which may have value without namepath): `@listens`, `@fires`, `@emits`
7+
1. Name(path)-defining tags requiring namepath: `@external`, `@host`, `@name`, `@typedef`
8+
1. Name(path)-defining tags (which may have value without namepath or their
9+
namepath can be expressed elsewhere on the block): `@event`, `@callback`,
10+
`@class`, `@constructor`, `@constant`, `@const`,
11+
`@function`, `@func`, `@method`, `@interface`, `@member`, `@var`,
12+
`@mixin`, `@namespace`
13+
1. Name(path)-pointing tags requiring namepath: `@alias`, `@augments`, `@extends`, `@lends`, `@memberof`, `@memberof!`, `@mixes`, `@this`
14+
1. Name(path)-pointing tags (which may have value without namepath or their
15+
namepath can be expressed elsewhere on the block): `@listens`, `@fires`,
16+
`@emits`
1117
1. Name(path)-pointing tags (multiple names in one): `@borrows`
1218

1319
...with the following applying to the above sets:
1420

1521
- Expect tags in set 1-4 to have a valid namepath if present
16-
- Prevent sets 3-4 from being empty by setting `allowEmptyNamepaths` to `false` as these tags might have some indicative value without a path (but sets 1-2 will always fail if empty)
22+
- Prevent sets 2 and 4 from being empty by setting `allowEmptyNamepaths` to
23+
`false` as these tags might have some indicative value without a path
24+
or may allow a name expressed elsewhere on the block (but sets 1 and 3 will
25+
always fail if empty)
1726
- For the special case of set 5, i.e., `@borrows <that namepath> as <this namepath>`, check that both namepaths are present and valid and ensure there is an `as ` between them.
1827

1928
|||

src/iterateJsdoc.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,8 @@ const curryUtils = (
158158
};
159159

160160
utils.passesEmptyNamepathCheck = (tag) => {
161-
return !tag.name && allowEmptyNamepaths && [
162-
// These may serve some minor purpose when empty
163-
'callback', 'event', 'listens', 'fires', 'emits'
164-
].includes(tag.tag);
161+
return !tag.name && allowEmptyNamepaths &&
162+
jsdocUtils.isPotentiallyEmptyNamepathTag(tag.tag);
165163
};
166164

167165
utils.hasDefinedTypeReturnTag = (tag) => {

src/jsdocUtils.js

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -119,30 +119,32 @@ const hasDefinedTypeReturnTag = (tag) => {
119119

120120
const namepathDefiningTags = [
121121
// NOT USEFUL WITHOUT NAMEPATH
122-
'class', 'constructor',
123-
'constant', 'const',
124122
'external', 'host',
125-
'function', 'func', 'method',
126-
'interface',
127-
'member', 'var',
128-
'mixin',
129123
'name',
130-
'namespace',
131124
'typedef',
132125

133126
// MAY BE USEFUL WITHOUT NAMEPATH
127+
'event',
128+
129+
// MAY BE USEFUL WITHOUT NAMEPATH (OR
130+
// BLOCK CAN USE NAMEPATH FROM ELSEWHERE)
131+
'class', 'constructor',
132+
'constant', 'const',
134133
'callback',
135-
'event'
134+
'function', 'func', 'method',
135+
'interface',
136+
'member', 'var',
137+
'mixin',
138+
'namespace'
136139
];
137140

138141
const namepathPointingTags = [
139142
// NOT USEFUL WITHOUT NAMEPATH
140143
'alias',
141-
'augments',
144+
'augments', 'extends',
142145

143146
// `borrows` has a different format, however, so needs special parsing
144147
'borrows',
145-
'extends',
146148
'lends',
147149
'memberof',
148150
'memberof!',
@@ -169,6 +171,25 @@ const isNamepathTag = (tagName, checkSeesForNamepaths) => {
169171
isNamepathPointingTag(tagName, checkSeesForNamepaths);
170172
};
171173

174+
const potentiallyEmptyNamepathTags = [
175+
// These may serve some minor purpose when empty or
176+
// their namepath can be expressed elsewhere on the block
177+
'event',
178+
'callback',
179+
'class', 'constructor',
180+
'constant', 'const',
181+
'function', 'func', 'method',
182+
'interface',
183+
'member', 'var',
184+
'mixin',
185+
'namespace',
186+
'listens', 'fires', 'emits'
187+
];
188+
189+
const isPotentiallyEmptyNamepathTag = (tag) => {
190+
return potentiallyEmptyNamepathTags.includes(tag);
191+
};
192+
172193
let tagsWithTypes = [
173194
'class',
174195
'constant',
@@ -442,6 +463,7 @@ export default {
442463
hasTag,
443464
isNamepathDefiningTag,
444465
isNamepathTag,
466+
isPotentiallyEmptyNamepathTag,
445467
isTagWithType,
446468
isValidTag
447469
};

test/rules/assertions/validTypes.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ export default {
165165
{
166166
code: `
167167
/**
168-
* @alias module:svgcanvas.SvgCanvas#event:ext_langReady
168+
* @alias module:namespace.SomeClass#event:ext_anevent
169169
*/
170170
function quux() {
171171
@@ -182,13 +182,34 @@ export default {
182182
}
183183
`
184184
},
185+
{
186+
code: `
187+
/**
188+
* @class
189+
*/
190+
function quux() {
191+
192+
}
193+
`
194+
},
185195
{
186196
code: `
187197
/**
188198
* @see {@link foo}
189199
*/
190200
function quux() {
191201
202+
}
203+
`
204+
},
205+
{
206+
code: `
207+
/**
208+
*
209+
* @fires {module:namespace.SomeClass#event:ext_anevent}
210+
*/
211+
function quux() {
212+
192213
}
193214
`
194215
}

0 commit comments

Comments
 (0)