Skip to content

Commit

Permalink
Add prefer-module rule (#1141)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
fisker and sindresorhus committed Apr 21, 2021
1 parent 65378cb commit a7e393c
Show file tree
Hide file tree
Showing 10 changed files with 2,073 additions and 11 deletions.
117 changes: 117 additions & 0 deletions docs/rules/prefer-module.md
@@ -0,0 +1,117 @@
# Prefer JavaScript modules (ESM) over CommonJS

Prefer using the [JavaScript module](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules) format over the legacy CommonJS module format.

1. Forbids `'use strict'` directive.

JavaScript modules use “Strict Mode” by default.

1. Forbids “Global Return”.

This is a CommonJS-only feature.

1. Forbids the global variables `__dirname` and `__filename`.

They are [not available in JavaScript modules](https://nodejs.org/api/esm.html#esm_no_filename_or_dirname).

Replacements:

```js
import {fileURLToPath} from 'url';
import path from 'path';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(fileURLToPath(import.meta.url));
```

However, in most cases, this is better:

```js
import {fileURLToPath} from 'url';

const foo = fileURLToPath(new URL('foo.js', import.meta.url));
```

And many Node.js APIs accept `URL` directly, so you can just do this:

```js
const foo = new URL('foo.js', import.meta.url);
```

1. Forbids `require(…)`.

`require(…)` can be replaced by `import …` or `import(…)`.

1. Forbids `exports` and `module.exports`.

`export …` should be used in JavaScript modules.

## Fail

```js
'use strict';

//
```

```js
if (foo) {
return;
}

//
```

```js
const file = path.join(__dirname, 'foo.js');
```

```js
const content = fs.readFileSync(__filename, 'utf8');
```

```js
const {fromPairs} = require('lodash');
```

```js
module.exports = foo;
```

```js
exports.foo = foo;
```

## Pass

```js
function run() {
if (foo) {
return;
}

//
}

run();
```

```js
const file = fileURLToPath(new URL('foo.js', import.meta.url));
```
```js
import {fromPairs} from 'lodash-es';
```
```js
export default foo;
```
```js
export {foo};
```
## Resources
- [Get Ready For ESM](https://blog.sindresorhus.com/get-ready-for-esm-aa53530b3f77) by @sindresorhus
1 change: 1 addition & 0 deletions index.js
Expand Up @@ -99,6 +99,7 @@ module.exports = {
'unicorn/prefer-keyboard-event-key': 'error',
'unicorn/prefer-math-trunc': 'error',
'unicorn/prefer-modern-dom-apis': 'error',
'unicorn/prefer-module': 'error',
'unicorn/prefer-negative-index': 'error',
'unicorn/prefer-number-properties': 'error',
'unicorn/prefer-optional-catch-binding': 'error',
Expand Down
2 changes: 2 additions & 0 deletions readme.md
Expand Up @@ -90,6 +90,7 @@ Configure it in `package.json`.
"unicorn/prefer-keyboard-event-key": "error",
"unicorn/prefer-math-trunc": "error",
"unicorn/prefer-modern-dom-apis": "error",
"unicorn/prefer-module": "error",
"unicorn/prefer-negative-index": "error",
"unicorn/prefer-number-properties": "error",
"unicorn/prefer-optional-catch-binding": "error",
Expand Down Expand Up @@ -181,6 +182,7 @@ Each rule has emojis denoting:
| [prefer-keyboard-event-key](docs/rules/prefer-keyboard-event-key.md) | Prefer `KeyboardEvent#key` over `KeyboardEvent#keyCode`. || 🔧 |
| [prefer-math-trunc](docs/rules/prefer-math-trunc.md) | Enforce the use of `Math.trunc` instead of bitwise operators. || 🔧 |
| [prefer-modern-dom-apis](docs/rules/prefer-modern-dom-apis.md) | Prefer `.before()` over `.insertBefore()`, `.replaceWith()` over `.replaceChild()`, prefer one of `.before()`, `.after()`, `.append()` or `.prepend()` over `insertAdjacentText()` and `insertAdjacentElement()`. || 🔧 |
| [prefer-module](docs/rules/prefer-module.md) | Prefer JavaScript modules (ESM) over CommonJS. || 🔧 |
| [prefer-negative-index](docs/rules/prefer-negative-index.md) | Prefer negative index over `.length - index` for `{String,Array,TypedArray}#slice()` and `Array#splice()`. || 🔧 |
| [prefer-number-properties](docs/rules/prefer-number-properties.md) | Prefer `Number` static properties over global ones. || 🔧 |
| [prefer-optional-catch-binding](docs/rules/prefer-optional-catch-binding.md) | Prefer omitting the `catch` binding parameter. || 🔧 |
Expand Down

0 comments on commit a7e393c

Please sign in to comment.