@@ -5,6 +5,7 @@ use oxc_ast::{
55use oxc_diagnostics:: OxcDiagnostic ;
66use oxc_macros:: declare_oxc_lint;
77use oxc_span:: { GetSpan , Span } ;
8+ use schemars:: JsonSchema ;
89
910use crate :: { AstNode , context:: LintContext , rule:: Rule } ;
1011
@@ -14,10 +15,35 @@ fn no_commonjs_diagnostic(span: Span, name: &str, actual: &str) -> OxcDiagnostic
1415 . with_label ( span)
1516}
1617
17- #[ derive( Debug , Clone ) ]
18+ #[ derive( Debug , Clone , JsonSchema ) ]
19+ #[ serde( rename_all = "camelCase" , default ) ]
1820pub struct NoCommonjs {
21+ /// If `allowPrimitiveModules` option is set to true, the following is valid:
22+ ///
23+ /// ```js
24+ /// module.exports = "foo";
25+ /// module.exports = function rule(context) {
26+ /// return { /* ... */ };
27+ /// };
28+ /// ```
29+ ///
30+ /// but this is still reported:
31+ ///
32+ /// ```js
33+ /// module.exports = { x: "y" };
34+ /// exports.z = function bark() { /* ... */ };
35+ /// ```
1936 allow_primitive_modules : bool ,
37+ /// If set to `true`, `require` calls are valid:
38+ ///
39+ /// ```js
40+ /// var mod = require("./mod");
41+ /// ```
42+ ///
43+ /// but `module.exports` is reported as usual.
2044 allow_require : bool ,
45+ /// When set to `true`, allows conditional `require()` calls (e.g., inside `if` statements or try-catch blocks).
46+ /// This is useful for places where you need to conditionally load via commonjs requires if ESM imports are not supported.
2147 allow_conditional_require : bool ,
2248}
2349
@@ -69,41 +95,10 @@ declare_oxc_lint!(
6995 /// fs = require("fs");
7096 /// } catch (error) {}
7197 /// ```
72- ///
73- /// ### Allow require
74- ///
75- /// If `allowRequire` option is set to `true`, `require` calls are valid:
76- ///
77- /// ```js
78- /// var mod = require("./mod");
79- /// ```
80- ///
81- /// but `module.exports` is reported as usual.
82- ///
83- /// ### Allow conditional require
84- ///
85- /// By default, conditional requires are allowed, If the `allowConditionalRequire` option is set to `false`, they will be reported.
86- ///
87- /// ### Allow primitive modules
88- ///
89- /// If `allowPrimitiveModules` option is set to true, the following is valid:
90- ///
91- /// ```js
92- /// module.exports = "foo";
93- /// module.exports = function rule(context) {
94- /// return { /* ... */ };
95- /// };
96- /// ```
97- ///
98- /// but this is still reported:
99- ///
100- /// ```js
101- /// module.exports = { x: "y" };
102- /// exports.z = function bark() { /* ... */ };
103- /// ```
10498 NoCommonjs ,
10599 import,
106- restriction
100+ restriction,
101+ config = NoCommonjs ,
107102) ;
108103
109104fn is_conditional ( parent_node : & AstNode , ctx : & LintContext ) -> bool {
0 commit comments