Skip to content

Commit 071cf77

Browse files
committed
INCOMPLETE
1. Add tests (for desired features added to docs) -> implement 2. (apparent problem in eslint itself with our use of Program:exit): test(`no-undefined-types`): issues gajus#507
1 parent 55d70a5 commit 071cf77

File tree

5 files changed

+150
-2
lines changed

5 files changed

+150
-2
lines changed

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,38 @@ reporting on use of that namepath elsewhere) and/or that a tag's `type` is
4343

4444
#### Options
4545

46-
An option object may have the following key:
46+
An option object may have the following keys, helping indicate types or
47+
file sources of types:
4748

4849
- `definedTypes` - This array can be populated to indicate other types which
4950
are automatically considered as defined (in addition to globals, etc.).
5051
Defaults to an empty array.
5152

53+
- `entryFiles` - Array of entry files objects indicating JavaScript or HTML
54+
files whose `import` or `require` statements should be resolved recursively
55+
and be analyzed for `@typedef`'s or globals to treat as "defined" for the
56+
purposes of this rule. Each object should have a `file` array and with an
57+
optional `node` boolean property to indicate whether to use the Node
58+
Resolution Algorithm (e.g., for Node.js) and/or a `cjs` boolean property (if
59+
following `require`) properties. Set one of the `file` items to `<main>`,
60+
`<exports>`, `<exports.imports>`, or `<exports.require>` to use the file
61+
referenced in the correpsonding property in `package.json`.
62+
63+
- `jsdocConfig` - Object with:
64+
- `file` string pointing to a path for a
65+
[jsdoc config file](https://jsdoc.app/about-configuring-jsdoc.html)
66+
which will be parsed for [input files](https://jsdoc.app/about-configuring-jsdoc.html#specifying-input-files),
67+
including `include`, `exclude`, `includePattern`, and `excludePattern`
68+
properties within the file as well as `opts.recurse`. See `entryFiles`
69+
on how the (JavaScript) files will be treated (with
70+
`sourceType: 'module'` in the jsdoc config file causing "cjs" to be
71+
set to `false`).
72+
73+
- `typeSources` - Array with `globals`, `exports`, and/or `locals` indicating
74+
the source types that will be treated as valid types when found in the
75+
current file or any entry files (`locals` will only apply to the
76+
current file).
77+
5278
|||
5379
|---|---|
5480
|Context|everywhere|

README.md

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6848,12 +6848,38 @@ reporting on use of that namepath elsewhere) and/or that a tag's `type` is
68486848
<a name="eslint-plugin-jsdoc-rules-no-undefined-types-options-16"></a>
68496849
#### Options
68506850
6851-
An option object may have the following key:
6851+
An option object may have the following keys, helping indicate types or
6852+
file sources of types:
68526853
68536854
- `definedTypes` - This array can be populated to indicate other types which
68546855
are automatically considered as defined (in addition to globals, etc.).
68556856
Defaults to an empty array.
68566857
6858+
- `entryFiles` - Array of entry files objects indicating JavaScript or HTML
6859+
files whose `import` or `require` statements should be resolved recursively
6860+
and be analyzed for `@typedef`'s or globals to treat as "defined" for the
6861+
purposes of this rule. Each object should have a `file` array and with an
6862+
optional `node` property to indicate whether to use the Node Resolution
6863+
Algorithm (e.g., for Node.js) and/or `cjs` (if following `require`)
6864+
properties. Set one of the `file` items to `<main>`, `<exports>`,
6865+
`<exports.imports>`, or `<exports.require>` to use the file referenced in
6866+
the correpsonding property in `package.json`.
6867+
6868+
- `jsdocConfig` - Object with:
6869+
- `file` string pointing to a path for a
6870+
[jsdoc config file](https://jsdoc.app/about-configuring-jsdoc.html)
6871+
which will be parsed for [input files](https://jsdoc.app/about-configuring-jsdoc.html#specifying-input-files),
6872+
including `include`, `exclude`, `includePattern`, and `excludePattern`
6873+
properties within the file as well as `opts.recurse`. See `entryFiles`
6874+
on how the (JavaScript) files will be treated (with
6875+
`sourceType: 'module'` in the jsdoc config file causing "cjs" to be
6876+
set to `false`).
6877+
6878+
- `typeSources` - Array with `globals`, `exports`, and/or `locals` indicating
6879+
the source types that will be treated as valid types when found in the
6880+
current file or any entry files (`locals` will only apply to the
6881+
current file).
6882+
68576883
|||
68586884
|---|---|
68596885
|Context|everywhere|
@@ -7345,6 +7371,30 @@ function quux () {}
73457371
* @type {SomeType}
73467372
*/
73477373
// Settings: {"jsdoc":{"structuredTags":{"namepathDefiner":{"name":"namepath-defining"}}}}
7374+
7375+
import {myTypesA} from '../internal/file.js'; // ERROR
7376+
import {myTypesB} from '../internal/file.js'; // NO ERROR
7377+
7378+
/**
7379+
* @typedef newType
7380+
* @property {myTypesA.someType} someProp - Some prop.
7381+
*/
7382+
7383+
/**
7384+
* @param {newType} arg - Arg.
7385+
*/
7386+
function myFunctionA(arg) {
7387+
return arg;
7388+
}
7389+
7390+
/**
7391+
* @param {myTypesB.someType} arg - Arg.
7392+
*/
7393+
function myFunctionB(arg) {
7394+
return arg;
7395+
}
7396+
7397+
export {myFunctionA, myFunctionB};
73487398
````
73497399
73507400

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"dependencies": {
88
"comment-parser": "^0.7.6",
99
"debug": "^4.1.1",
10+
"es-file-traverse": "^0.1.2",
1011
"jsdoctypeparser": "^9.0.0",
1112
"lodash": "^4.17.20",
1213
"regextras": "^0.7.1",

src/rules/noUndefinedTypes.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,44 @@ export default iterateJsdoc(({
179179
},
180180
type: 'array',
181181
},
182+
entryFiles: {
183+
items: {
184+
properties: {
185+
cjs: {
186+
type: 'boolean',
187+
},
188+
file: {
189+
items: {
190+
type: 'string',
191+
},
192+
type: 'array',
193+
},
194+
node: {
195+
type: 'boolean',
196+
},
197+
},
198+
require: ['file'],
199+
type: 'object',
200+
},
201+
type: 'array',
202+
},
203+
jsdocConfig: {
204+
properties: {
205+
file: {
206+
type: 'string',
207+
},
208+
},
209+
type: 'object',
210+
},
211+
typeSources: {
212+
items: {
213+
enum: [
214+
'globals', 'exports', 'locals',
215+
],
216+
type: 'string',
217+
},
218+
type: 'array',
219+
},
182220
},
183221
type: 'object',
184222
},

test/rules/assertions/noUndefinedTypes.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -897,5 +897,38 @@ export default {
897897
},
898898
},
899899
},
900+
{
901+
code: `
902+
import {myTypesA} from '../internal/file.js'; // ERROR
903+
import {myTypesB} from '../internal/file.js'; // NO ERROR
904+
905+
/**
906+
* @typedef newType
907+
* @property {myTypesA.someType} someProp - Some prop.
908+
*/
909+
910+
/**
911+
* @param {newType} arg - Arg.
912+
*/
913+
function myFunctionA(arg) {
914+
return arg;
915+
}
916+
917+
/**
918+
* @param {myTypesB.someType} arg - Arg.
919+
*/
920+
function myFunctionB(arg) {
921+
return arg;
922+
}
923+
924+
export {myFunctionA, myFunctionB};
925+
`,
926+
parserOptions: {
927+
sourceType: 'module',
928+
},
929+
rules: {
930+
'no-unused-vars': ['error'],
931+
},
932+
},
900933
],
901934
};

0 commit comments

Comments
 (0)