Skip to content
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

Ensure no-undefined-types catches modular scope for Node/commonjs env's #222

Merged
merged 4 commits into from
May 16, 2019
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
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1371,6 +1371,13 @@ function quux(foo) {

}

/**
* @param {Promise} foo - Bar.
*/
function quux(foo) {

}

class MyClass {}

/**
Expand All @@ -1391,6 +1398,15 @@ const MyType = require('my-library').MyType;

}

const MyType = require('my-library').MyType;

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

}

import {MyType} from 'my-library';

/**
Expand All @@ -1402,7 +1418,7 @@ import {MyType} from 'my-library';

}

/*global MyType*/
/*globals MyType*/

/**
* @param {MyType} foo - Bar.
Expand Down
17 changes: 14 additions & 3 deletions src/rules/noUndefinedTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,20 @@ export default iterateJsdoc(({
})

// If the file is a module, concat the variables from the module scope.
.concat(scopeManager.isModule() ? globalScope.childScopes[0].variables.map((variable) => {
return variable.name;
}) : [])
.concat(

// This covers `commonjs` as well as `node`
scopeManager.__options.nodejsScope ||
scopeManager.isModule() ?
globalScope.childScopes.reduce((arr, {variables}) => {
// Flatten
arr.push(...variables);

return arr;
}, []).map(({name}) => {
return name;
}) : []
)
.concat(extraTypes)
.concat(typedefDeclarations);

Expand Down
35 changes: 33 additions & 2 deletions test/rules/assertions/noUndefinedTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@ export default {
}
`
},
{
code: `
/**
* @param {Promise} foo - Bar.
*/
function quux(foo) {

}
`,
env: {
es6: true
}
},
{
code: `
class MyClass {}
Expand Down Expand Up @@ -58,7 +71,25 @@ export default {
function quux(foo) {

}
`
`,
env: {
node: true
}
},
{
code: `
const MyType = require('my-library').MyType;

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

}
`,
env: {
node: false
}
},
{
code: `
Expand All @@ -79,7 +110,7 @@ export default {
},
{
code: `
/*global MyType*/
/*globals MyType*/

/**
* @param {MyType} foo - Bar.
Expand Down