Skip to content

Commit 654dc10

Browse files
author
Xiaoji Chen
committed
unbreak try catch use case
1 parent 147b74c commit 654dc10

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

src/rules/no-commonjs.js

+14-5
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,18 @@ function validateScope(scope) {
3030
return false
3131
}
3232

33+
// https://github.com/estree/estree/blob/master/es5.md
34+
function isConditional(node) {
35+
if (
36+
node.type === 'IfStatement'
37+
|| node.type === 'TryStatement'
38+
|| node.type === 'LogicalExpression'
39+
|| node.type === 'ConditionalExpression'
40+
) return true
41+
if (node.parent) return isConditional(node.parent)
42+
return false
43+
}
44+
3345
//------------------------------------------------------------------------------
3446
// Rule Definition
3547
//------------------------------------------------------------------------------
@@ -93,11 +105,6 @@ module.exports = {
93105
},
94106
'CallExpression': function (call) {
95107
if (!validateScope(context.getScope())) return
96-
if (
97-
call.parent.type !== 'ExpressionStatement'
98-
&& call.parent.type !== 'VariableDeclarator'
99-
&& call.parent.type !== 'AssignmentExpression'
100-
) return
101108

102109
if (call.callee.type !== 'Identifier') return
103110
if (call.callee.name !== 'require') return
@@ -110,6 +117,8 @@ module.exports = {
110117

111118
if (allowRequire(call, options)) return
112119

120+
if (isConditional(call.parent)) return
121+
113122
// keeping it simple: all 1-string-arg `require` calls are reported
114123
context.report({
115124
node: call.callee,

tests/src/rules/no-commonjs.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,11 @@ ruleTester.run('no-commonjs', require('rules/no-commonjs'), {
5858
{ code: 'module.exports = "foo"', options: [{ allowPrimitiveModules: true }] },
5959

6060
{ code: 'if (typeof window !== "undefined") require("x")', options: [{ allowRequire: true }] },
61+
{ code: 'if (typeof window !== "undefined") require("x")', options: [{ allowRequire: false }] },
6162
{ code: 'if (typeof window !== "undefined") { require("x") }', options: [{ allowRequire: true }] },
63+
{ code: 'if (typeof window !== "undefined") { require("x") }', options: [{ allowRequire: false }] },
64+
65+
{ code: 'try { require("x") } catch (error) {}' },
6266
],
6367

6468
invalid: [
@@ -68,12 +72,6 @@ ruleTester.run('no-commonjs', require('rules/no-commonjs'), {
6872
{ code: 'var x = require("x")', errors: [ { message: IMPORT_MESSAGE }] },
6973
{ code: 'x = require("x")', errors: [ { message: IMPORT_MESSAGE }] },
7074
{ code: 'require("x")', errors: [ { message: IMPORT_MESSAGE }] },
71-
{ code: 'if (typeof window !== "undefined") require("x")',
72-
errors: [ { message: IMPORT_MESSAGE }],
73-
},
74-
{ code: 'if (typeof window !== "undefined") { require("x") }',
75-
errors: [ { message: IMPORT_MESSAGE }],
76-
},
7775
]),
7876

7977
// exports

0 commit comments

Comments
 (0)