Skip to content

feat: disableReporting and markVariablesAsUsed options #1065

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions .README/rules/no-undefined-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,18 @@ array's items will be considered as defined for the purposes of that tag.

## Options

An option object may have the following key:
An option object may have the following keys:

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

## Context and settings

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


Expand Down
24 changes: 21 additions & 3 deletions docs/rules/no-undefined-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,18 @@ array's items will be considered as defined for the purposes of that tag.
<a name="no-undefined-types-options"></a>
## Options

An option object may have the following key:
An option object may have the following keys:

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

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


Expand Down Expand Up @@ -354,7 +361,7 @@ import {MyType} from 'my-library';
* @param {object<string, number>} foo
* @param {Array<string>} baz
*/
function quux(foo, bar, baz) {
function quux(foo, bar, baz) {

}

Expand Down Expand Up @@ -727,5 +734,16 @@ class Foo {
}
}
// Settings: {"jsdoc":{"mode":"typescript"}}

import {MyType} from 'my-library';

/**
* @param {MyType} foo - Bar.
* @param {AnUndefinedType} bar
*/
function quux(foo, bar) {

}
// "jsdoc/no-undefined-types": ["error"|"warn", {"disableReporting":true}]
````

14 changes: 12 additions & 2 deletions src/rules/noUndefinedTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ export default iterateJsdoc(({

const {
definedTypes = [],
disableReporting = false,
markVariablesAsUsed = true,
} = context.options[0] || {};

let definedPreferredTypes = [];
Expand Down Expand Up @@ -209,8 +211,10 @@ export default iterateJsdoc(({
if (!allDefinedTypes.has(value) &&
(!Array.isArray(structuredTypes) || !structuredTypes.includes(value))
) {
report(`The type '${value}' is undefined.`, null, tag);
} else if (!extraTypes.includes(value)) {
if (!disableReporting) {
report(`The type '${value}' is undefined.`, null, tag);
}
} else if (markVariablesAsUsed && !extraTypes.includes(value)) {
context.markVariableAsUsed(value);
}
}
Expand All @@ -233,6 +237,12 @@ export default iterateJsdoc(({
},
type: 'array',
},
disableReporting: {
type: 'boolean',
},
markVariablesAsUsed: {
type: 'boolean',
},
},
type: 'object',
},
Expand Down
55 changes: 48 additions & 7 deletions test/rules/assertions/noUndefinedTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,6 @@ export default {
message: 'The type \'strnig\' is undefined.',
},
],
rules: {
'no-undef': 'error',
},
},
{
code: `
Expand Down Expand Up @@ -248,9 +245,6 @@ export default {
message: 'The type \'strnig\' is undefined.',
},
],
rules: {
'no-undef': 'error',
},
},
{
code: `
Expand Down Expand Up @@ -585,7 +579,7 @@ export default {
* @param {object<string, number>} foo
* @param {Array<string>} baz
*/
function quux(foo, bar, baz) {
function quux(foo, bar, baz) {

}
`,
Expand Down Expand Up @@ -1283,5 +1277,52 @@ export default {
},
},
},
{
code: `
// THIS SHOULD CAUSE ERRORS, BUT DUE TO ESLINT TESTER
// LIMITATIONS, WE CAN'T TEST THE \`no-unused-vars\` RULE;
// WE PUT IT HERE FOR COVERAGE

import {MyInterface} from 'xyz';
/**
* @type {MyInterface}
*/
function quux(foo) {
console.log(foo);
}

quux(0);
`,
ignoreReadme: true,
options: [
{
markVariablesAsUsed: false,
},
],
parserOptions: {
sourceType: 'module',
},
},
{
code: `
import {MyType} from 'my-library';

/**
* @param {MyType} foo - Bar.
* @param {AnUndefinedType} bar
*/
function quux(foo, bar) {

}
`,
options: [
{
disableReporting: true,
},
],
parserOptions: {
sourceType: 'module',
},
},
],
};