Skip to content

Commit 00c20e6

Browse files
aduh95targos
authored andcommitted
tools,doc: forbid CJS globals in ESM code snippets
PR-URL: #38889 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Danielle Adams <adamzdanielle@gmail.com> Reviewed-By: Zeyu Yang <himself65@outlook.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Darshan Sen <raisinten@gmail.com>
1 parent 17a9846 commit 00c20e6

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

.eslintrc.js

+23
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,29 @@ module.exports = {
7676
'doc/api/packages.md/*.js',
7777
],
7878
parserOptions: { sourceType: 'module' },
79+
rules: { 'no-restricted-globals': [
80+
'error',
81+
{
82+
name: '__filename',
83+
message: 'Use import.meta.url instead',
84+
},
85+
{
86+
name: '__dirname',
87+
message: 'Not available in ESM',
88+
},
89+
{
90+
name: 'exports',
91+
message: 'Not available in ESM',
92+
},
93+
{
94+
name: 'module',
95+
message: 'Not available in ESM',
96+
},
97+
{
98+
name: 'require',
99+
message: 'Use import instead',
100+
},
101+
] },
79102
},
80103
],
81104
rules: {

doc/api/esm.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ analysis process.
386386
387387
For example, consider a CommonJS module written:
388388
389-
```js
389+
```cjs
390390
// cjs.cjs
391391
exports.name = 'exported';
392392
```

doc/api/packages.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,7 @@ import { another } from 'a-package/m.mjs';
704704
Self-referencing is also available when using `require`, both in an ES module,
705705
and in a CommonJS one. For example, this code will also work:
706706

707-
```js
707+
```cjs
708708
// ./a-module.js
709709
const { something } = require('a-package/foo'); // Loads from ./foo.js.
710710
```
@@ -807,7 +807,7 @@ to be treated as ES modules, just as `"type": "commonjs"` would cause them
807807
to be treated as CommonJS.
808808
See [Enabling](#esm_enabling).
809809

810-
```js
810+
```cjs
811811
// ./node_modules/pkg/index.cjs
812812
exports.name = 'value';
813813
```
@@ -920,7 +920,7 @@ CommonJS and ES module instances of the package:
920920
CommonJS and ES module versions of the package. For example, if the CommonJS
921921
and ES module entry points are `index.cjs` and `index.mjs`, respectively:
922922

923-
```js
923+
```cjs
924924
// ./node_modules/pkg/index.cjs
925925
const state = require('./state.cjs');
926926
module.exports.state = state;
@@ -1034,7 +1034,7 @@ The `"main"` field defines the script that is used when the [package directory
10341034
is loaded via `require()`](modules.md#modules_folders_as_modules). Its value
10351035
is a path.
10361036

1037-
```js
1037+
```cjs
10381038
require('./path/to/directory'); // This resolves to ./path/to/directory/main.js.
10391039
```
10401040

0 commit comments

Comments
 (0)