Skip to content

Commit

Permalink
INCOMPLETE
Browse files Browse the repository at this point in the history
	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
  • Loading branch information
brettz9 committed Sep 29, 2020
1 parent 3cca564 commit f82de63
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 3 deletions.
28 changes: 27 additions & 1 deletion .README/rules/no-undefined-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,38 @@ reporting on use of that namepath elsewhere) and/or that a tag's `type` is

#### Options

An option object may have the following key:
An option object may have the following keys, helping indicate types or
file sources of types:

- `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.

- `entryFiles` - Array of entry files objects indicating JavaScript or HTML
files whose `import` or `require` statements should be resolved recursively
and be analyzed for `@typedef`'s, globals, etc. (see `typeSources`) to treat
as "defined" for the purposes of this rule. Each object should have a
`file` array and with an optional `node` boolean property to indicate whether
to use the Node Resolution Algorithm (e.g., for Node.js) and/or a `cjs`
boolean property (if following `require`) properties. Set one of the `file`
items to `<main>`, `<exports>`, `<exports.imports>`, or `<exports.require>`
to use the file referenced in the correpsonding property in `package.json`.

- `jsdocConfig` - Object with:
- `file` string pointing to a path for a
[jsdoc config file](https://jsdoc.app/about-configuring-jsdoc.html)
which will be parsed for [input files](https://jsdoc.app/about-configuring-jsdoc.html#specifying-input-files),
including `include`, `exclude`, `includePattern`, and `excludePattern`
properties within the file as well as `opts.recurse`. See `entryFiles`
on how the (JavaScript) files will be treated (with
`sourceType: 'module'` in the jsdoc config file causing "cjs" to be
set to `false`).

- `typeSources` - Array with `globals`, `exports`, and/or `locals` indicating
the source types that will be treated as valid types when found in the
current file or any entry files (`locals` will only apply to the
current file). Defaults to `['typedefs', 'globals', 'exports', 'locals']`.

|||
|---|---|
|Context|everywhere|
Expand Down
52 changes: 51 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6848,12 +6848,38 @@ reporting on use of that namepath elsewhere) and/or that a tag's `type` is
<a name="eslint-plugin-jsdoc-rules-no-undefined-types-options-16"></a>
#### Options
An option object may have the following key:
An option object may have the following keys, helping indicate types or
file sources of types:
- `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.
- `entryFiles` - Array of entry files objects indicating JavaScript or HTML
files whose `import` or `require` statements should be resolved recursively
and be analyzed for `@typedef`'s or globals to treat as "defined" for the
purposes of this rule. Each object should have a `file` array and with an
optional `node` property to indicate whether to use the Node Resolution
Algorithm (e.g., for Node.js) and/or `cjs` (if following `require`)
properties. Set one of the `file` items to `<main>`, `<exports>`,
`<exports.imports>`, or `<exports.require>` to use the file referenced in
the correpsonding property in `package.json`.
- `jsdocConfig` - Object with:
- `file` string pointing to a path for a
[jsdoc config file](https://jsdoc.app/about-configuring-jsdoc.html)
which will be parsed for [input files](https://jsdoc.app/about-configuring-jsdoc.html#specifying-input-files),
including `include`, `exclude`, `includePattern`, and `excludePattern`
properties within the file as well as `opts.recurse`. See `entryFiles`
on how the (JavaScript) files will be treated (with
`sourceType: 'module'` in the jsdoc config file causing "cjs" to be
set to `false`).
- `typeSources` - Array with `globals`, `exports`, and/or `locals` indicating
the source types that will be treated as valid types when found in the
current file or any entry files (`locals` will only apply to the
current file).
|||
|---|---|
|Context|everywhere|
Expand Down Expand Up @@ -7345,6 +7371,30 @@ function quux () {}
* @type {SomeType}
*/
// Settings: {"jsdoc":{"structuredTags":{"namepathDefiner":{"name":"namepath-defining"}}}}
import {myTypesA} from '../internal/file.js'; // ERROR
import {myTypesB} from '../internal/file.js'; // NO ERROR
/**
* @typedef newType
* @property {myTypesA.someType} someProp - Some prop.
*/
/**
* @param {newType} arg - Arg.
*/
function myFunctionA(arg) {
return arg;
}
/**
* @param {myTypesB.someType} arg - Arg.
*/
function myFunctionB(arg) {
return arg;
}
export {myFunctionA, myFunctionB};
````
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"dependencies": {
"comment-parser": "^0.7.6",
"debug": "^4.1.1",
"es-file-traverse": "^0.1.2",
"jsdoctypeparser": "^9.0.0",
"lodash": "^4.17.20",
"regextras": "^0.7.1",
Expand Down
50 changes: 49 additions & 1 deletion src/rules/noUndefinedTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,17 @@ export default iterateJsdoc(({
const {scopeManager} = sourceCode;
const {globalScope} = scopeManager;

const {definedTypes = []} = context.options[0] || {};
const {
definedTypes = [],
entryFiles = [
// {file, cjs, node}
],
jsdocConfig: {file: jsdocConfigFile},
typeSources = ['typedefs', 'globals', 'exports', 'locals'],
} = context.options[0] || {};

// eslint-disable-next-line no-console
console.log('entryFiles', entryFiles, jsdocConfigFile, typeSources);

let definedPreferredTypes = [];
const {preferredTypes, mode} = settings;
Expand Down Expand Up @@ -179,6 +189,44 @@ export default iterateJsdoc(({
},
type: 'array',
},
entryFiles: {
items: {
properties: {
cjs: {
type: 'boolean',
},
file: {
items: {
type: 'string',
},
type: 'array',
},
node: {
type: 'boolean',
},
},
require: ['file'],
type: 'object',
},
type: 'array',
},
jsdocConfig: {
properties: {
file: {
type: 'string',
},
},
type: 'object',
},
typeSources: {
items: {
enum: [
'globals', 'exports', 'locals',
],
type: 'string',
},
type: 'array',
},
},
type: 'object',
},
Expand Down
38 changes: 38 additions & 0 deletions test/rules/assertions/noUndefinedTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -897,5 +897,43 @@ export default {
},
},
},
{
code: `
import {myTypesA} from '../internal/file.js'; // ERROR
import {myTypesB} from '../internal/file.js'; // NO ERROR
/**
* @typedef newType
* @property {myTypesA.someType} someProp - Some prop.
*/
/**
* @param {newType} arg - Arg.
*/
function myFunctionA(arg) {
return arg;
}
/**
* @param {myTypesB.someType} arg - Arg.
*/
function myFunctionB(arg) {
return arg;
}
export {myFunctionA, myFunctionB};
`,
options: [
{
entryFiles: [],
},
],
parserOptions: {
sourceType: 'module',
},
rules: {
'no-unused-vars': ['error'],
},
},
],
};

0 comments on commit f82de63

Please sign in to comment.