Skip to content

Commit 3236c8c

Browse files
committed
feat: disableReporting and markVariablesAsUsed options
1 parent 4eaf868 commit 3236c8c

File tree

4 files changed

+90
-14
lines changed

4 files changed

+90
-14
lines changed

.README/rules/no-undefined-types.md

+9-2
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,18 @@ array's items will be considered as defined for the purposes of that tag.
4747

4848
## Options
4949

50-
An option object may have the following key:
50+
An option object may have the following keys:
5151

5252
- `definedTypes` - This array can be populated to indicate other types which
5353
are automatically considered as defined (in addition to globals, etc.).
5454
Defaults to an empty array.
55+
- `markVariablesAsUsed` - Whether to mark variables as used for the purposes
56+
of the `no-unused-vars` rule when they are not found to be undefined.
57+
Defaults to `true`. May be set to `false` to enforce a practice of not
58+
importing types unless used in code.
59+
- `disableReporting` - Whether to disable reporting of errors. Defaults to
60+
`false`. This may be set to `true` in order to take advantage of marking
61+
variables as used.
5562

5663
## Context and settings
5764

@@ -62,7 +69,7 @@ An option object may have the following key:
6269
|Aliases|`constructor`, `const`, `extends`, `var`, `arg`, `argument`, `prop`, `return`, `exception`, `yield`|
6370
|Closure-only|`package`, `private`, `protected`, `public`, `static`|
6471
|Recommended|true|
65-
|Options|`definedTypes`|
72+
|Options|`definedTypes`, `markVariablesAsUsed`, `disableReporting`|
6673
|Settings|`preferredTypes`, `mode`, `structuredTags`|
6774

6875

docs/rules/no-undefined-types.md

+21-3
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,18 @@ array's items will be considered as defined for the purposes of that tag.
5555
<a name="no-undefined-types-options"></a>
5656
## Options
5757

58-
An option object may have the following key:
58+
An option object may have the following keys:
5959

6060
- `definedTypes` - This array can be populated to indicate other types which
6161
are automatically considered as defined (in addition to globals, etc.).
6262
Defaults to an empty array.
63+
- `markVariablesAsUsed` - Whether to mark variables as used for the purposes
64+
of the `no-unused-vars` rule when they are not found to be undefined.
65+
Defaults to `true`. May be set to `false` to enforce a practice of not
66+
importing types unless used in code.
67+
- `disableReporting` - Whether to disable reporting of errors. Defaults to
68+
`false`. This may be set to `true` in order to take advantage of marking
69+
variables as used.
6370

6471
<a name="user-content-no-undefined-types-context-and-settings"></a>
6572
<a name="no-undefined-types-context-and-settings"></a>
@@ -72,7 +79,7 @@ An option object may have the following key:
7279
|Aliases|`constructor`, `const`, `extends`, `var`, `arg`, `argument`, `prop`, `return`, `exception`, `yield`|
7380
|Closure-only|`package`, `private`, `protected`, `public`, `static`|
7481
|Recommended|true|
75-
|Options|`definedTypes`|
82+
|Options|`definedTypes`, `markVariablesAsUsed`, `disableReporting`|
7683
|Settings|`preferredTypes`, `mode`, `structuredTags`|
7784

7885

@@ -354,7 +361,7 @@ import {MyType} from 'my-library';
354361
* @param {object<string, number>} foo
355362
* @param {Array<string>} baz
356363
*/
357-
function quux(foo, bar, baz) {
364+
function quux(foo, bar, baz) {
358365

359366
}
360367

@@ -727,5 +734,16 @@ class Foo {
727734
}
728735
}
729736
// Settings: {"jsdoc":{"mode":"typescript"}}
737+
738+
import {MyType} from 'my-library';
739+
740+
/**
741+
* @param {MyType} foo - Bar.
742+
* @param {AnUndefinedType} bar
743+
*/
744+
function quux(foo, bar) {
745+
746+
}
747+
// "jsdoc/no-undefined-types": ["error"|"warn", {"disableReporting":true}]
730748
````
731749

src/rules/noUndefinedTypes.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ export default iterateJsdoc(({
6464

6565
const {
6666
definedTypes = [],
67+
disableReporting = false,
68+
markVariablesAsUsed = true,
6769
} = context.options[0] || {};
6870

6971
let definedPreferredTypes = [];
@@ -209,8 +211,10 @@ export default iterateJsdoc(({
209211
if (!allDefinedTypes.has(value) &&
210212
(!Array.isArray(structuredTypes) || !structuredTypes.includes(value))
211213
) {
212-
report(`The type '${value}' is undefined.`, null, tag);
213-
} else if (!extraTypes.includes(value)) {
214+
if (!disableReporting) {
215+
report(`The type '${value}' is undefined.`, null, tag);
216+
}
217+
} else if (markVariablesAsUsed && !extraTypes.includes(value)) {
214218
context.markVariableAsUsed(value);
215219
}
216220
}
@@ -233,6 +237,12 @@ export default iterateJsdoc(({
233237
},
234238
type: 'array',
235239
},
240+
disableReporting: {
241+
type: 'boolean',
242+
},
243+
markVariablesAsUsed: {
244+
type: 'boolean',
245+
},
236246
},
237247
type: 'object',
238248
},

test/rules/assertions/noUndefinedTypes.js

+48-7
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,6 @@ export default {
6565
message: 'The type \'strnig\' is undefined.',
6666
},
6767
],
68-
rules: {
69-
'no-undef': 'error',
70-
},
7168
},
7269
{
7370
code: `
@@ -248,9 +245,6 @@ export default {
248245
message: 'The type \'strnig\' is undefined.',
249246
},
250247
],
251-
rules: {
252-
'no-undef': 'error',
253-
},
254248
},
255249
{
256250
code: `
@@ -585,7 +579,7 @@ export default {
585579
* @param {object<string, number>} foo
586580
* @param {Array<string>} baz
587581
*/
588-
function quux(foo, bar, baz) {
582+
function quux(foo, bar, baz) {
589583
590584
}
591585
`,
@@ -1283,5 +1277,52 @@ export default {
12831277
},
12841278
},
12851279
},
1280+
{
1281+
code: `
1282+
// THIS SHOULD CAUSE ERRORS, BUT DUE TO ESLINT TESTER
1283+
// LIMITATIONS, WE CAN'T TEST THE \`no-unused-vars\` RULE;
1284+
// WE PUT IT HERE FOR COVERAGE
1285+
1286+
import {MyInterface} from 'xyz';
1287+
/**
1288+
* @type {MyInterface}
1289+
*/
1290+
function quux(foo) {
1291+
console.log(foo);
1292+
}
1293+
1294+
quux(0);
1295+
`,
1296+
ignoreReadme: true,
1297+
options: [
1298+
{
1299+
markVariablesAsUsed: false,
1300+
},
1301+
],
1302+
parserOptions: {
1303+
sourceType: 'module',
1304+
},
1305+
},
1306+
{
1307+
code: `
1308+
import {MyType} from 'my-library';
1309+
1310+
/**
1311+
* @param {MyType} foo - Bar.
1312+
* @param {AnUndefinedType} bar
1313+
*/
1314+
function quux(foo, bar) {
1315+
1316+
}
1317+
`,
1318+
options: [
1319+
{
1320+
disableReporting: true,
1321+
},
1322+
],
1323+
parserOptions: {
1324+
sourceType: 'module',
1325+
},
1326+
},
12861327
],
12871328
};

0 commit comments

Comments
 (0)