Skip to content

Commit d24be36

Browse files
feat!: add support for Node.js ESM resolution (eslint-community#4)
* add support for Node.js ESM resolution * Update lib/rules/no-hide-core-modules.js Co-authored-by: 唯然 <weiran.zsd@outlook.com> * Update lib/util/import-target.js * invalidate ESM imports that were valid in Node 10 * Update scripts/update.js Co-authored-by: 唯然 <weiran.zsd@outlook.com>
1 parent 4deaca7 commit d24be36

38 files changed

+3325
-212
lines changed

.eslintignore

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
/coverage
55
/docs
66
/node_modules
7+
/lib/converted-esm

.nycrc

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
],
55
"exclude": [
66
"lib/rules/no-hide-core-modules.js",
7-
"lib/rules/no-unsupported-features.js"
7+
"lib/rules/no-unsupported-features.js",
8+
"lib/converted-esm/*.js"
89
],
910
"reporter": [
1011
"lcov",

README.md

+37-44
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@
22

33
> forked from [eslint-plugin-node](https://www.npmjs.com/package/eslint-plugin-node) v11.1.0. as the original repository seems [no longer maintained](https://github.com/mysticatea/eslint-plugin-node/issues/300).
44
5-
[![npm version](https://img.shields.io/npm/v/eslint-plugin-n.svg)](https://www.npmjs.com/package/eslint-plugin-n)
6-
[![Downloads/month](https://img.shields.io/npm/dm/eslint-plugin-n.svg)](http://www.npmtrends.com/eslint-plugin-n)
7-
[![Build Status](https://github.com/weiran-zsd/eslint-plugin-node/workflows/CI/badge.svg)](https://github.com/weiran-zsd/eslint-plugin-node/actions)
8-
[![Coverage Status](https://codecov.io/gh/weiran-zsd/eslint-plugin-node/branch/master/graph/badge.svg)](https://codecov.io/gh/weiran-zsd/eslint-plugin-node)
9-
[![Dependency Status](https://david-dm.org/weiran-zsd/eslint-plugin-node.svg)](https://david-dm.org/weiran-zsd/eslint-plugin-node)
5+
[![npm version](https://img.shields.io/npm/v/eslint-plugin-n.svg)](https://www.npmjs.com/package/eslint-plugin-n) [![Downloads/month](https://img.shields.io/npm/dm/eslint-plugin-n.svg)](http://www.npmtrends.com/eslint-plugin-n) [![Build Status](https://github.com/weiran-zsd/eslint-plugin-node/workflows/CI/badge.svg)](https://github.com/weiran-zsd/eslint-plugin-node/actions) [![Coverage Status](https://codecov.io/gh/weiran-zsd/eslint-plugin-node/branch/master/graph/badge.svg)](https://codecov.io/gh/weiran-zsd/eslint-plugin-node) [![Dependency Status](https://david-dm.org/weiran-zsd/eslint-plugin-node.svg)](https://david-dm.org/weiran-zsd/eslint-plugin-node)
106

117
Additional ESLint's rules for Node.js
128

@@ -16,19 +12,16 @@ Additional ESLint's rules for Node.js
1612
$ npm install --save-dev eslint eslint-plugin-n
1713
```
1814

19-
- Requires Node.js `>=12.22.0`
20-
- Requires ESLint `>=7.0.0`
15+
- Requires Node.js `>=12.22.0`
16+
- Requires ESLint `>=7.0.0`
2117

2218
**Note:** It recommends a use of [the "engines" field of package.json](https://docs.npmjs.com/files/package.json#engines). The "engines" field is used by `n/no-unsupported-features/*` rules.
2319

2420
**.eslintrc.json** (An example)
2521

2622
```jsonc
2723
{
28-
"extends": [
29-
"eslint:recommended",
30-
"plugin:n/recommended"
31-
],
24+
"extends": ["eslint:recommended", "plugin:n/recommended"],
3225
"parserOptions": {
3326
// Only ESLint 6.2.0 and later support ES2020.
3427
"ecmaVersion": 2020
@@ -62,8 +55,8 @@ $ npm install --save-dev eslint eslint-plugin-n
6255

6356
## 📖 Rules
6457

65-
- ⭐️ - the mark of recommended rules.
66-
- ✒️ - the mark of fixable rules.
58+
- ⭐️ - the mark of recommended rules.
59+
- ✒️ - the mark of fixable rules.
6760

6861
<!--RULES_TABLE_START-->
6962
### Possible Errors
@@ -133,48 +126,48 @@ These rules have been deprecated in accordance with the [deprecation policy](htt
133126

134127
This plugin provides three configs:
135128

136-
- `plugin:n/recommended` considers both CommonJS and ES Modules. If [`"type":"module"` field](https://medium.com/@nodejs/announcing-a-new-experimental-modules-1be8d2d6c2ff#b023) existed in package.json then it considers files as ES Modules. Otherwise it considers files as CommonJS. In addition, it considers `*.mjs` files as ES Modules and `*.cjs` files as CommonJS.
137-
- `plugin:n/recommended-module` considers all files as ES Modules.
138-
- `plugin:n/recommended-script` considers all files as CommonJS.
129+
- `plugin:n/recommended` considers both CommonJS and ES Modules. If [`"type":"module"` field](https://medium.com/@nodejs/announcing-a-new-experimental-modules-1be8d2d6c2ff#b023) existed in package.json then it considers files as ES Modules. Otherwise it considers files as CommonJS. In addition, it considers `*.mjs` files as ES Modules and `*.cjs` files as CommonJS.
130+
- `plugin:n/recommended-module` considers all files as ES Modules.
131+
- `plugin:n/recommended-script` considers all files as CommonJS.
139132

140133
Those preset config:
141134

142-
- enable [no-process-exit](http://eslint.org/docs/rules/no-process-exit) rule because [the official document](https://nodejs.org/api/process.html#process_process_exit_code) does not recommend a use of `process.exit()`.
143-
- enable plugin rules which are given :star: in the above table.
144-
- add `{ecmaVersion: 2019}` and etc into `parserOptions`.
145-
- add proper globals into `globals`.
146-
- add this plugin into `plugins`.
135+
- enable [no-process-exit](http://eslint.org/docs/rules/no-process-exit) rule because [the official document](https://nodejs.org/api/process.html#process_process_exit_code) does not recommend a use of `process.exit()`.
136+
- enable plugin rules which are given :star: in the above table.
137+
- add `{ecmaVersion: 2019}` and etc into `parserOptions`.
138+
- add proper globals into `globals`.
139+
- add this plugin into `plugins`.
147140

148141
## 👫 FAQ
149142

150-
- Q: The `no-missing-import` / `no-missing-require` rules don't work with nested folders in SublimeLinter-eslint
151-
- A: See [context.getFilename() in rule returns relative path](https://github.com/roadhump/SublimeLinter-eslint#contextgetfilename-in-rule-returns-relative-path) in the SublimeLinter-eslint FAQ.
143+
- Q: The `no-missing-import` / `no-missing-require` rules don't work with nested folders in SublimeLinter-eslint
144+
- A: See [context.getFilename() in rule returns relative path](https://github.com/roadhump/SublimeLinter-eslint#contextgetfilename-in-rule-returns-relative-path) in the SublimeLinter-eslint FAQ.
152145

153146
## 🚥 Semantic Versioning Policy
154147

155148
`eslint-plugin-n` follows [semantic versioning](http://semver.org/) and [ESLint's Semantic Versioning Policy](https://github.com/eslint/eslint#semantic-versioning-policy).
156149

157-
- Patch release (intended to not break your lint build)
158-
- A bug fix in a rule that results in it reporting fewer errors.
159-
- Improvements to documentation.
160-
- Non-user-facing changes such as refactoring code, adding, deleting, or modifying tests, and increasing test coverage.
161-
- Re-releasing after a failed release (i.e., publishing a release that doesn't work for anyone).
162-
- Minor release (might break your lint build)
163-
- A bug fix in a rule that results in it reporting more errors.
164-
- A new rule is created.
165-
- A new option to an existing rule is created.
166-
- An existing rule is deprecated.
167-
- Major release (likely to break your lint build)
168-
- A support for old Node version is dropped.
169-
- A support for old ESLint version is dropped.
170-
- An existing rule is changed in it reporting more errors.
171-
- An existing rule is removed.
172-
- An existing option of a rule is removed.
173-
- An existing config is updated.
150+
- Patch release (intended to not break your lint build)
151+
- A bug fix in a rule that results in it reporting fewer errors.
152+
- Improvements to documentation.
153+
- Non-user-facing changes such as refactoring code, adding, deleting, or modifying tests, and increasing test coverage.
154+
- Re-releasing after a failed release (i.e., publishing a release that doesn't work for anyone).
155+
- Minor release (might break your lint build)
156+
- A bug fix in a rule that results in it reporting more errors.
157+
- A new rule is created.
158+
- A new option to an existing rule is created.
159+
- An existing rule is deprecated.
160+
- Major release (likely to break your lint build)
161+
- A support for old Node version is dropped.
162+
- A support for old ESLint version is dropped.
163+
- An existing rule is changed in it reporting more errors.
164+
- An existing rule is removed.
165+
- An existing option of a rule is removed.
166+
- An existing config is updated.
174167

175168
## 📰 Changelog
176169

177-
- [GitHub Releases](https://github.com/weiran-zsd/eslint-plugin-n/releases)
170+
- [GitHub Releases](https://github.com/weiran-zsd/eslint-plugin-n/releases)
178171

179172
## ❤️ Contributing
180173

@@ -184,6 +177,6 @@ Please use GitHub's Issues/PRs.
184177

185178
### Development Tools
186179

187-
- `npm test` runs tests and measures coverage.
188-
- `npm run coverage` shows the coverage result of `npm test` command.
189-
- `npm run clean` removes the coverage result of `npm test` command.
180+
- `npm test` runs tests and measures coverage.
181+
- `npm run coverage` shows the coverage result of `npm test` command.
182+
- `npm run clean` removes the coverage result of `npm test` command.

docs/rules/file-extension-in-import.md

-23
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ This rule has a string option and an object option.
2727
"error",
2828
"always" or "never",
2929
{
30-
"tryExtensions": [".js", ".json", ".node"],
3130
".xxx": "always" or "never",
3231
}
3332
]
@@ -36,7 +35,6 @@ This rule has a string option and an object option.
3635

3736
- `"always"` (default) requires file extensions in `import`/`export` declarations.
3837
- `"never"` disallows file extensions in `import`/`export` declarations.
39-
- `tryExtensions` is the file extensions to resolve import paths. Default is `[".js", ".json", ".node"]`.
4038
- `.xxx` is the overriding setting for specific file extensions. You can use arbitrary property names which start with `.`.
4139

4240
#### always
@@ -90,27 +88,6 @@ import styles from "./styles.css"
9088
import logo from "./logo.png"
9189
```
9290

93-
### Shared Settings
94-
95-
The following options can be set by [shared settings](http://eslint.org/docs/user-guide/configuring.html#adding-shared-settings).
96-
Several rules have the same option, but we can set this option at once.
97-
98-
- `tryExtensions`
99-
100-
```js
101-
// .eslintrc.js
102-
module.exports = {
103-
"settings": {
104-
"node": {
105-
"tryExtensions": [".js", ".json", ".node"]
106-
}
107-
},
108-
"rules": {
109-
"n/file-extension-in-import": "error"
110-
}
111-
}
112-
```
113-
11491
## 🔎 Implementation
11592

11693
- [Rule source](../../lib/rules/file-extension-in-import.js)

docs/rules/no-extraneous-import.md

+1-11
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ This rule warns `import` declarations of extraneous modules.
1616
"rules": {
1717
"n/no-extraneous-import": ["error", {
1818
"allowModules": [],
19-
"resolvePaths": [],
20-
"tryExtensions": []
19+
"resolvePaths": []
2120
}]
2221
}
2322
}
@@ -48,21 +47,13 @@ If a path is relative, it will be resolved from CWD.
4847

4948
Default is `[]`
5049

51-
#### tryExtensions
52-
53-
When an import path does not exist, this rule checks whether or not any of `path.js`, `path.json`, and `path.node` exists.
54-
`tryExtensions` option is the extension list this rule uses at the time.
55-
56-
Default is `[".js", ".json", ".node"]`.
57-
5850
### Shared Settings
5951

6052
The following options can be set by [shared settings](http://eslint.org/docs/user-guide/configuring.html#adding-shared-settings).
6153
Several rules have the same option, but we can set this option at once.
6254

6355
- `allowModules`
6456
- `resolvePaths`
65-
- `tryExtensions`
6657

6758
```js
6859
// .eslintrc.js
@@ -71,7 +62,6 @@ module.exports = {
7162
"node": {
7263
"allowModules": ["electron"],
7364
"resolvePaths": [__dirname],
74-
"tryExtensions": [".js", ".json", ".node"]
7565
}
7666
},
7767
"rules": {

docs/rules/no-missing-import.md

+1-11
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ import existingModule from "existing-module";
3636
"rules": {
3737
"n/no-missing-import": ["error", {
3838
"allowModules": [],
39-
"resolvePaths": ["/path/to/a/modules/directory"],
40-
"tryExtensions": [".js", ".json", ".node"]
39+
"resolvePaths": ["/path/to/a/modules/directory"]
4140
}]
4241
}
4342
}
@@ -68,21 +67,13 @@ If a path is relative, it will be resolved from CWD.
6867

6968
Default is `[]`
7069

71-
#### tryExtensions
72-
73-
When an import path does not exist, this rule checks whether or not any of `path.js`, `path.json`, and `path.node` exists.
74-
`tryExtensions` option is the extension list this rule uses at the time.
75-
76-
Default is `[".js", ".json", ".node"]`.
77-
7870
### Shared Settings
7971

8072
The following options can be set by [shared settings](http://eslint.org/docs/user-guide/configuring.html#adding-shared-settings).
8173
Several rules have the same option, but we can set this option at once.
8274

8375
- `allowModules`
8476
- `resolvePaths`
85-
- `tryExtensions`
8677

8778
```js
8879
// .eslintrc.js
@@ -91,7 +82,6 @@ module.exports = {
9182
"node": {
9283
"allowModules": ["electron"],
9384
"resolvePaths": [__dirname],
94-
"tryExtensions": [".js", ".json", ".node"]
9585
}
9686
},
9787
"rules": {

docs/rules/no-unpublished-import.md

+3-14
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ Then this rule warns `import` declarations in \*published\* files if the `import
2525
"rules": {
2626
"n/no-unpublished-import": ["error", {
2727
"allowModules": [],
28-
"convertPath": null,
29-
"tryExtensions": [".js", ".json", ".node"]
28+
"convertPath": null
3029
}]
3130
}
3231
}
@@ -63,8 +62,7 @@ For example:
6362
"n/no-unpublished-import": ["error", {
6463
"convertPath": {
6564
"src/**/*.jsx": ["^src/(.+?)\\.jsx$", "lib/$1.js"]
66-
},
67-
"tryExtensions": [".js", ".jsx", ".json"]
65+
}
6866
}]
6967
}
7068
}
@@ -104,21 +102,13 @@ For example:
104102
In this style, this option has the following shape as the same expression as above: `{include: [<targetFiles>], replace: [<fromRegExp>, <toString>]}`.
105103
In addition, we can specify glob patterns to exclude files.
106104

107-
#### tryExtensions
108-
109-
When an import path does not exist, this rule checks whether or not any of `path.js`, `path.json`, and `path.node` exists.
110-
`tryExtensions` option is the extension list this rule uses at the time.
111-
112-
Default is `[".js", ".json", ".node"]`.
113-
114105
### Shared Settings
115106

116107
The following options can be set by [shared settings](http://eslint.org/docs/user-guide/configuring.html#adding-shared-settings).
117108
Several rules have the same option, but we can set this option at once.
118109

119110
- `allowModules`
120111
- `convertPath`
121-
- `tryExtensions`
122112

123113
For Example:
124114

@@ -129,8 +119,7 @@ For Example:
129119
"allowModules": ["electron"],
130120
"convertPath": {
131121
"src/**/*.jsx": ["^src/(.+?)\\.jsx$", "lib/$1.js"]
132-
},
133-
"tryExtensions": [".js", ".jsx", ".json"]
122+
}
134123
}
135124
},
136125
"rules": {

0 commit comments

Comments
 (0)