Skip to content

Commit d91cc9c

Browse files
authored
Merge pull request #222 from brettz9/no-undefined-types-node
Ensure `no-undefined-types` catches modular scope for Node/commonjs env's
2 parents 40a7fc0 + 7307d35 commit d91cc9c

File tree

3 files changed

+64
-6
lines changed

3 files changed

+64
-6
lines changed

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1371,6 +1371,13 @@ function quux(foo) {
13711371

13721372
}
13731373

1374+
/**
1375+
* @param {Promise} foo - Bar.
1376+
*/
1377+
function quux(foo) {
1378+
1379+
}
1380+
13741381
class MyClass {}
13751382

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

13921399
}
13931400

1401+
const MyType = require('my-library').MyType;
1402+
1403+
/**
1404+
* @param {MyType} foo - Bar.
1405+
*/
1406+
function quux(foo) {
1407+
1408+
}
1409+
13941410
import {MyType} from 'my-library';
13951411

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

14031419
}
14041420

1405-
/*global MyType*/
1421+
/*globals MyType*/
14061422

14071423
/**
14081424
* @param {MyType} foo - Bar.

src/rules/noUndefinedTypes.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,20 @@ export default iterateJsdoc(({
5151
})
5252

5353
// If the file is a module, concat the variables from the module scope.
54-
.concat(scopeManager.isModule() ? globalScope.childScopes[0].variables.map((variable) => {
55-
return variable.name;
56-
}) : [])
54+
.concat(
55+
56+
// This covers `commonjs` as well as `node`
57+
scopeManager.__options.nodejsScope ||
58+
scopeManager.isModule() ?
59+
globalScope.childScopes.reduce((arr, {variables}) => {
60+
// Flatten
61+
arr.push(...variables);
62+
63+
return arr;
64+
}, []).map(({name}) => {
65+
return name;
66+
}) : []
67+
)
5768
.concat(extraTypes)
5869
.concat(typedefDeclarations);
5970

test/rules/assertions/noUndefinedTypes.js

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,19 @@ export default {
3131
}
3232
`
3333
},
34+
{
35+
code: `
36+
/**
37+
* @param {Promise} foo - Bar.
38+
*/
39+
function quux(foo) {
40+
41+
}
42+
`,
43+
env: {
44+
es6: true
45+
}
46+
},
3447
{
3548
code: `
3649
class MyClass {}
@@ -58,7 +71,25 @@ export default {
5871
function quux(foo) {
5972
6073
}
61-
`
74+
`,
75+
env: {
76+
node: true
77+
}
78+
},
79+
{
80+
code: `
81+
const MyType = require('my-library').MyType;
82+
83+
/**
84+
* @param {MyType} foo - Bar.
85+
*/
86+
function quux(foo) {
87+
88+
}
89+
`,
90+
env: {
91+
node: false
92+
}
6293
},
6394
{
6495
code: `
@@ -79,7 +110,7 @@ export default {
79110
},
80111
{
81112
code: `
82-
/*global MyType*/
113+
/*globals MyType*/
83114
84115
/**
85116
* @param {MyType} foo - Bar.

0 commit comments

Comments
 (0)