Skip to content

Commit 4bb0bd0

Browse files
aduh95richardlau
authored andcommitted
tools,doc: forbid CJS globals in ESM code snippets
PR-URL: #38889 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Danielle Adams <[email protected]> Reviewed-By: Zeyu Yang <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Darshan Sen <[email protected]>
1 parent ff7cc8f commit 4bb0bd0

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

.eslintrc.js

Lines changed: 23 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ analysis process.
373373
374374
For example, consider a CommonJS module written:
375375
376-
```js
376+
```cjs
377377
// cjs.cjs
378378
exports.name = 'exported';
379379
```

doc/api/packages.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@ import { another } from 'a-package/m.mjs';
659659
Self-referencing is also available when using `require`, both in an ES module,
660660
and in a CommonJS one. For example, this code will also work:
661661

662-
```js
662+
```cjs
663663
// ./a-module.js
664664
const { something } = require('a-package/foo'); // Loads from ./foo.js.
665665
```
@@ -762,7 +762,7 @@ to be treated as ES modules, just as `"type": "commonjs"` would cause them
762762
to be treated as CommonJS.
763763
See [Enabling](#esm_enabling).
764764

765-
```js
765+
```cjs
766766
// ./node_modules/pkg/index.cjs
767767
exports.name = 'value';
768768
```
@@ -875,7 +875,7 @@ CommonJS and ES module instances of the package:
875875
CommonJS and ES module versions of the package. For example, if the CommonJS
876876
and ES module entry points are `index.cjs` and `index.mjs`, respectively:
877877

878-
```js
878+
```cjs
879879
// ./node_modules/pkg/index.cjs
880880
const state = require('./state.cjs');
881881
module.exports.state = state;
@@ -989,7 +989,7 @@ The `"main"` field defines the script that is used when the [package directory
989989
is loaded via `require()`](modules.md#modules_folders_as_modules). Its value
990990
is a path.
991991

992-
```js
992+
```cjs
993993
require('./path/to/directory'); // This resolves to ./path/to/directory/main.js.
994994
```
995995

0 commit comments

Comments
 (0)