Skip to content

Commit 31daf79

Browse files
authored
docs(linter): Add configuration option docs for import/no-commonjs rule. (#15077)
Mostly just moving around the existing docs. Part of #14743. Generated docs: ```md ## Configuration This rule accepts a configuration object with the following properties: ### allowConditionalRequire type: `boolean` default: `true` When set to `true`, allows conditional `require()` calls (e.g., inside `if` statements or try-catch blocks). This is useful for places where you need to conditionally load via commonjs requires if ESM imports are not supported. ### allowPrimitiveModules type: `boolean` default: `false` If `allowPrimitiveModules` option is set to true, the following is valid: \```js module.exports = "foo"; module.exports = function rule(context) { return { /* ... */ }; }; \``` but this is still reported: \```js module.exports = { x: "y" }; exports.z = function bark() { /* ... */ }; \``` ### allowRequire type: `boolean` default: `false` If set to `true`, `require` calls are valid: \```js var mod = require("./mod"); \``` but `module.exports` is reported as usual. ```
1 parent 9bf8ebe commit 31daf79

File tree

1 file changed

+29
-34
lines changed

1 file changed

+29
-34
lines changed

crates/oxc_linter/src/rules/import/no_commonjs.rs

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use oxc_ast::{
55
use oxc_diagnostics::OxcDiagnostic;
66
use oxc_macros::declare_oxc_lint;
77
use oxc_span::{GetSpan, Span};
8+
use schemars::JsonSchema;
89

910
use 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)]
1820
pub 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

109104
fn is_conditional(parent_node: &AstNode, ctx: &LintContext) -> bool {

0 commit comments

Comments
 (0)