Skip to content

Commit bedf526

Browse files
Exterskybrettz9
authored andcommitted
browserEnv fixes
* window variables were not checked for exports * window variable was populated incorrectly
1 parent 5f59670 commit bedf526

File tree

4 files changed

+157
-9
lines changed

4 files changed

+157
-9
lines changed

.README/rules/require-jsdoc.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ be checked by the rule.
2222
- `ancestorsOnly` - Only check node ancestors to check if node is exported
2323
- `exports` - ESM exports are checked for JSDoc comments (Defaults to `true`)
2424
- `modules` - CommonJS exports are checked for JSDoc comments (Defaults to `true`)
25-
- `browserEnv` - Populates window variable
25+
- `browserEnv` - Window global exports are checked for JSDoc comments
2626

2727
- `require` - An object with the following optional boolean keys which all
2828
default to `false` except as noted:

README.md

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3436,7 +3436,7 @@ be checked by the rule.
34363436
- `ancestorsOnly` - Only check node ancestors to check if node is exported
34373437
- `exports` - ESM exports are checked for JSDoc comments (Defaults to `true`)
34383438
- `modules` - CommonJS exports are checked for JSDoc comments (Defaults to `true`)
3439-
- `browserEnv` - Populates window variable
3439+
- `browserEnv` - Window global exports are checked for JSDoc comments
34403440

34413441
- `require` - An object with the following optional boolean keys which all
34423442
default to `false` except as noted:
@@ -3695,6 +3695,24 @@ export default class A {
36953695
}
36963696
// Options: [{"publicOnly":{"ancestorsOnly":true},"require":{"ClassDeclaration":true}}]
36973697
// Message: Missing JSDoc comment.
3698+
3699+
var test = function () {
3700+
3701+
}
3702+
// Options: [{"publicOnly":{"browserEnv":true},"require":{"FunctionExpression":true}}]
3703+
// Message: Missing JSDoc comment.
3704+
3705+
window.test = function () {
3706+
3707+
}
3708+
// Options: [{"publicOnly":{"browserEnv":true},"require":{"FunctionExpression":true}}]
3709+
// Message: Missing JSDoc comment.
3710+
3711+
function test () {
3712+
3713+
}
3714+
// Options: [{"publicOnly":{"browserEnv":true}}]
3715+
// Message: Missing JSDoc comment.
36983716
````
36993717

37003718
The following patterns are not considered problems:
@@ -4111,6 +4129,19 @@ export default class A {
41114129

41124130
}
41134131
// Options: [{"publicOnly":{"ancestorsOnly":true},"require":{"ClassDeclaration":true}}]
4132+
4133+
/**
4134+
*
4135+
*/
4136+
var test = function () {
4137+
4138+
}
4139+
// Options: [{"publicOnly":{"browserEnv":true},"require":{"FunctionExpression":true}}]
4140+
4141+
let test = function () {
4142+
4143+
}
4144+
// Options: [{"publicOnly":{"browserEnv":true},"require":{"FunctionExpression":true}}]
41144145
````
41154146

41164147

src/exportParser.js

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,14 @@ const getSymbol = function (node, globals, scope, opt) {
111111
return null;
112112
};
113113

114-
createSymbol = function (node, globals, value, scope) {
114+
const createBlockSymbol = function(block, name, value, globals, isGlobal) {
115+
block.props[name] = value;
116+
if (isGlobal && globals.props.window && globals.props.window.special) {
117+
globals.props.window.props[name] = value;
118+
}
119+
};
120+
121+
createSymbol = function (node, globals, value, scope, isGlobal) {
115122
const block = scope || globals;
116123
let symbol;
117124
switch (node.type) {
@@ -124,13 +131,13 @@ createSymbol = function (node, globals, value, scope) {
124131
if (value) {
125132
const valueSymbol = getSymbol(value, globals, block);
126133
if (valueSymbol) {
127-
block.props[node.name] = valueSymbol;
134+
createBlockSymbol(block, node.name, valueSymbol, globals, isGlobal);
128135

129136
return block.props[node.name];
130137
}
131138
debug('Identifier: Missing value symbol for %s', node.name);
132139
} else {
133-
block.props[node.name] = createNode();
140+
createBlockSymbol(block, node.name, createNode(), globals, isGlobal);
134141

135142
return block.props[node.name];
136143
}
@@ -141,7 +148,7 @@ createSymbol = function (node, globals, value, scope) {
141148
const propertySymbol = getSymbol(node.property, globals, block, {simpleIdentifier: !node.computed});
142149
const propertyValue = getSymbolValue(propertySymbol);
143150
if (symbol && propertyValue) {
144-
symbol.props[propertyValue] = getSymbol(value, globals, block);
151+
createBlockSymbol(symbol, propertyValue, getSymbol(value, globals, block), globals, isGlobal);
145152

146153
return symbol.props[propertyValue];
147154
}
@@ -204,12 +211,13 @@ const mapVariables = function (node, globals, opt) {
204211
break;
205212
} case 'VariableDeclaration': {
206213
node.declarations.forEach((declaration) => {
207-
createSymbol(declaration.id, globals, declaration.init);
214+
const isGlobal = opts.initWindow && node.kind === 'var' && globals.props.window;
215+
createSymbol(declaration.id, globals, declaration.init, globals, isGlobal);
208216
});
209217
break;
210218
} case 'FunctionDeclaration': {
211219
if (node.id.type === 'Identifier') {
212-
createSymbol(node.id, globals, node, globals);
220+
createSymbol(node.id, globals, node, globals, true);
213221
}
214222
break;
215223
} case 'ExportDefaultDeclaration': {
@@ -302,6 +310,12 @@ const isNodeExported = function (node, globals, opt) {
302310
}
303311
}
304312

313+
if (opt.initWindow && globals.props.window) {
314+
if (findNode(node, globals.props.window)) {
315+
return true;
316+
}
317+
}
318+
305319
if (opt.exports && findExportedNode(globals, node)) {
306320
return true;
307321
}
@@ -335,7 +349,8 @@ const parse = function (ast, node, opt) {
335349
globalVars.props.exports = globalVars.props.module.props.exports;
336350
}
337351
if (opts.initWindow) {
338-
globalVars.props.window = globalVars;
352+
globalVars.props.window = createNode();
353+
globalVars.props.window.special = true;
339354
}
340355

341356
if (opts.ancestorsOnly) {

test/rules/assertions/requireJsdoc.js

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,69 @@ export default {
665665
parserOptions: {
666666
sourceType: 'module'
667667
}
668+
},
669+
{
670+
code: `
671+
var test = function () {
672+
673+
}
674+
`,
675+
errors: [{
676+
message: 'Missing JSDoc comment.',
677+
type: 'FunctionExpression'
678+
}],
679+
options: [{
680+
publicOnly: {
681+
browserEnv: true
682+
},
683+
require: {
684+
FunctionExpression: true
685+
}
686+
}],
687+
parserOptions: {
688+
sourceType: 'module'
689+
}
690+
},
691+
{
692+
code: `
693+
window.test = function () {
694+
695+
}
696+
`,
697+
errors: [{
698+
message: 'Missing JSDoc comment.',
699+
type: 'FunctionExpression'
700+
}],
701+
options: [{
702+
publicOnly: {
703+
browserEnv: true
704+
},
705+
require: {
706+
FunctionExpression: true
707+
}
708+
}],
709+
parserOptions: {
710+
sourceType: 'module'
711+
}
712+
},
713+
{
714+
code: `
715+
function test () {
716+
717+
}
718+
`,
719+
errors: [{
720+
message: 'Missing JSDoc comment.',
721+
type: 'FunctionDeclaration'
722+
}],
723+
options: [{
724+
publicOnly: {
725+
browserEnv: true
726+
}
727+
}],
728+
parserOptions: {
729+
sourceType: 'module'
730+
}
668731
}
669732
],
670733
valid: [{
@@ -1452,5 +1515,44 @@ export default {
14521515
parserOptions: {
14531516
sourceType: 'module'
14541517
}
1518+
},
1519+
{
1520+
code: `
1521+
/**
1522+
*
1523+
*/
1524+
var test = function () {
1525+
1526+
}
1527+
`,
1528+
options: [{
1529+
publicOnly: {
1530+
browserEnv: true
1531+
},
1532+
require: {
1533+
FunctionExpression: true
1534+
}
1535+
}],
1536+
parserOptions: {
1537+
sourceType: 'module'
1538+
}
1539+
},
1540+
{
1541+
code: `
1542+
let test = function () {
1543+
1544+
}
1545+
`,
1546+
options: [{
1547+
publicOnly: {
1548+
browserEnv: true
1549+
},
1550+
require: {
1551+
FunctionExpression: true
1552+
}
1553+
}],
1554+
parserOptions: {
1555+
sourceType: 'module'
1556+
}
14551557
}]
14561558
};

0 commit comments

Comments
 (0)