Skip to content

Commit e392e77

Browse files
authored
Merge branch 'main' into prettier-all
2 parents e54f523 + 8a981d2 commit e392e77

17 files changed

+152
-43
lines changed

Diff for: .github/workflows/ci.yml

+5-9
Original file line numberDiff line numberDiff line change
@@ -41,27 +41,23 @@ jobs:
4141
strategy:
4242
matrix:
4343
eslint: [8]
44-
node: [12.22.0, 12, 14.17.0, 14, 16.0.0, 16, 18, 20]
44+
node: [18.18.0, 20.9.0, 21.1.0, 22]
4545
os: [ubuntu-latest]
4646
include:
4747
# ESLint v9
4848
- eslint: 9
49-
node: 20
49+
node: 22
5050
os: ubuntu-latest
5151
# On other platforms
5252
- os: windows-latest
5353
eslint: 8
54-
node: 18
54+
node: 22
5555
- os: macos-latest
5656
eslint: 8
57-
node: 18
58-
# On old ESLint versions
59-
- eslint: 7
60-
node: 18
61-
os: ubuntu-latest
57+
node: 22
6258
# On the minimum supported ESLint/Node.js version
6359
- eslint: 7.0.0
64-
node: 12.22.0
60+
node: 18.18.0
6561
os: ubuntu-latest
6662
runs-on: ${{ matrix.os }}
6763
steps:

Diff for: .github/workflows/format.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ jobs:
1616

1717
steps:
1818
- name: ⬇️ Checkout repo
19-
uses: actions/checkout@v3
19+
uses: actions/checkout@v4
2020

2121
- name: ⎔ Setup node
22-
uses: actions/setup-node@v3
22+
uses: actions/setup-node@v4
2323
with:
2424
node-version: 18
2525

Diff for: README.md

+21-19
Original file line numberDiff line numberDiff line change
@@ -98,23 +98,23 @@ or start with the recommended rule set:
9898
✅ Set in the `recommended` configuration.\
9999
🔧 Automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/user-guide/command-line-interface#--fix).
100100

101-
| Name                      | Description | 💼 | ⚠️ | 🚫 | 🔧 |
102-
| :------------------------------------------------------------------- | :------------------------------------------------------------------------------------- | :-- | :-- | :-- | :-- |
103-
| [always-return](docs/rules/always-return.md) | Require returning inside each `then()` to create readable and reusable Promise chains. || | | |
104-
| [avoid-new](docs/rules/avoid-new.md) | Disallow creating `new` promises outside of utility libs (use [pify][] instead). | | || |
105-
| [catch-or-return](docs/rules/catch-or-return.md) | Enforce the use of `catch()` on un-returned promises. || | | |
106-
| [no-callback-in-promise](docs/rules/no-callback-in-promise.md) | Disallow calling `cb()` inside of a `then()` (use [nodeify][] instead). | || | |
107-
| [no-multiple-resolved](docs/rules/no-multiple-resolved.md) | Disallow creating new promises with paths that resolve multiple times. | | | | |
108-
| [no-native](docs/rules/no-native.md) | Require creating a `Promise` constructor before using it in an ES5 environment. | | || |
109-
| [no-nesting](docs/rules/no-nesting.md) | Disallow nested `then()` or `catch()` statements. | || | |
110-
| [no-new-statics](docs/rules/no-new-statics.md) | Disallow calling `new` on a Promise static method. || | | 🔧 |
111-
| [no-promise-in-callback](docs/rules/no-promise-in-callback.md) | Disallow using promises inside of callbacks. | || | |
112-
| [no-return-in-finally](docs/rules/no-return-in-finally.md) | Disallow return statements in `finally()`. | || | |
113-
| [no-return-wrap](docs/rules/no-return-wrap.md) | Disallow wrapping values in `Promise.resolve` or `Promise.reject` when not needed. || | | |
114-
| [param-names](docs/rules/param-names.md) | Enforce consistent param names and ordering when creating new promises. || | | |
115-
| [prefer-await-to-callbacks](docs/rules/prefer-await-to-callbacks.md) | Prefer async/await to the callback pattern. | | | | |
116-
| [prefer-await-to-then](docs/rules/prefer-await-to-then.md) | Prefer `await` to `then()`/`catch()`/`finally()` for reading Promise values. | | | | |
117-
| [valid-params](docs/rules/valid-params.md) | Enforces the proper number of arguments are passed to Promise functions. | || | |
101+
| Name                      | Description | 💼 | ⚠️ | 🚫 | 🔧 |
102+
| :------------------------------------------------------------------- | :----------------------------------------------------------------------------------------- | :-- | :-- | :-- | :-- |
103+
| [always-return](docs/rules/always-return.md) | Require returning inside each `then()` to create readable and reusable Promise chains. || | | |
104+
| [avoid-new](docs/rules/avoid-new.md) | Disallow creating `new` promises outside of utility libs (use [util.promisify][] instead). | | || |
105+
| [catch-or-return](docs/rules/catch-or-return.md) | Enforce the use of `catch()` on un-returned promises. || | | |
106+
| [no-callback-in-promise](docs/rules/no-callback-in-promise.md) | Disallow calling `cb()` inside of a `then()` (use [util.callbackify][] instead). | || | |
107+
| [no-multiple-resolved](docs/rules/no-multiple-resolved.md) | Disallow creating new promises with paths that resolve multiple times. | | | | |
108+
| [no-native](docs/rules/no-native.md) | Require creating a `Promise` constructor before using it in an ES5 environment. | | || |
109+
| [no-nesting](docs/rules/no-nesting.md) | Disallow nested `then()` or `catch()` statements. | || | |
110+
| [no-new-statics](docs/rules/no-new-statics.md) | Disallow calling `new` on a Promise static method. || | | 🔧 |
111+
| [no-promise-in-callback](docs/rules/no-promise-in-callback.md) | Disallow using promises inside of callbacks. | || | |
112+
| [no-return-in-finally](docs/rules/no-return-in-finally.md) | Disallow return statements in `finally()`. | || | |
113+
| [no-return-wrap](docs/rules/no-return-wrap.md) | Disallow wrapping values in `Promise.resolve` or `Promise.reject` when not needed. || | | |
114+
| [param-names](docs/rules/param-names.md) | Enforce consistent param names and ordering when creating new promises. || | | |
115+
| [prefer-await-to-callbacks](docs/rules/prefer-await-to-callbacks.md) | Prefer `async`/`await` to the callback pattern. | | | | |
116+
| [prefer-await-to-then](docs/rules/prefer-await-to-then.md) | Prefer `await` to `then()`/`catch()`/`finally()` for reading Promise values. | | | | |
117+
| [valid-params](docs/rules/valid-params.md) | Enforces the proper number of arguments are passed to Promise functions. | || | |
118118

119119
<!-- end auto-generated rules list -->
120120

@@ -129,8 +129,10 @@ or start with the recommended rule set:
129129
- (c) MMXV jden <mailto:[email protected]> - ISC license.
130130
- (c) 2016 Jamund Ferguson <mailto:[email protected]> - ISC license.
131131

132-
[nodeify]: https://www.npmjs.com/package/nodeify
133-
[pify]: https://www.npmjs.com/package/pify
132+
[util.callbackify]:
133+
https://nodejs.org/docs/latest/api/util.html#utilcallbackifyoriginal
134+
[util.promisify]:
135+
https://nodejs.org/dist/latest-v8.x/docs/api/util.html#util_util_promisify_original
134136
[@aaditmshah]: https://github.com/aaditmshah
135137
[@macklinu]: https://github.com/macklinu
136138
[@xjamundx]: https://github.com/xjamundx

Diff for: __tests__/always-return.js

+8
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,17 @@ ruleTester.run('always-return', rule, {
1717
'hey.then(x => { return; })',
1818
'hey.then(x => { return x ? x.id : null })',
1919
'hey.then(x => { return x * 10 })',
20+
'hey.then(x => { process.exit(0); })',
21+
'hey.then(x => { process.abort(); })',
2022
'hey.then(function() { return 42; })',
2123
'hey.then(function() { return new Promise(); })',
2224
'hey.then(function() { return "x"; }).then(doSomethingWicked)',
2325
'hey.then(x => x).then(function() { return "3" })',
2426
'hey.then(function() { throw new Error("msg"); })',
2527
'hey.then(function(x) { if (!x) { throw new Error("no x"); } return x; })',
2628
'hey.then(function(x) { if (x) { return x; } throw new Error("no x"); })',
29+
'hey.then(function(x) { if (x) { process.exit(0); } throw new Error("no x"); })',
30+
'hey.then(function(x) { if (x) { process.abort(); } throw new Error("no x"); })',
2731
'hey.then(x => { throw new Error("msg"); })',
2832
'hey.then(x => { if (!x) { throw new Error("no x"); } return x; })',
2933
'hey.then(x => { if (x) { return x; } throw new Error("no x"); })',
@@ -140,6 +144,10 @@ ruleTester.run('always-return', rule, {
140144
code: 'hey.then(function() { if (x) { } else { return x; }})',
141145
errors: [{ message }],
142146
},
147+
{
148+
code: 'hey.then(function() { if (x) { process.chdir(); } else { return x; }})',
149+
errors: [{ message }],
150+
},
143151
{
144152
code: 'hey.then(function() { if (x) { return you.then(function() { return x; }); } })',
145153
errors: [{ message }],

Diff for: __tests__/prefer-await-to-then.js

+38
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ ruleTester.run('prefer-await-to-then', rule, {
1515
'async function hi() { await thing() }',
1616
'async function hi() { await thing().then() }',
1717
'async function hi() { await thing().catch() }',
18+
'async function hi() { await thing().finally() }',
19+
'function * hi() { yield thing().then() }',
1820
'a = async () => (await something())',
1921
`a = async () => {
2022
try { await something() } catch (error) { somethingElse() }
@@ -54,5 +56,41 @@ ruleTester.run('prefer-await-to-then', rule, {
5456
code: 'function foo() { hey.finally(x => {}) }',
5557
errors: [{ message }],
5658
},
59+
{
60+
code: 'async function hi() { await thing().then() }',
61+
errors: [{ message }],
62+
options: [
63+
{
64+
strict: true,
65+
},
66+
],
67+
},
68+
{
69+
code: 'async function hi() { await thing().catch() }',
70+
errors: [{ message }],
71+
options: [
72+
{
73+
strict: true,
74+
},
75+
],
76+
},
77+
{
78+
code: 'async function hi() { await thing().finally() }',
79+
errors: [{ message }],
80+
options: [
81+
{
82+
strict: true,
83+
},
84+
],
85+
},
86+
{
87+
code: 'function * hi() { yield thing().then() }',
88+
errors: [{ message }],
89+
options: [
90+
{
91+
strict: true,
92+
},
93+
],
94+
},
5795
],
5896
})

Diff for: docs/rules/avoid-new.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
# Disallow creating `new` promises outside of utility libs (use [pify][] instead) (`promise/avoid-new`)
1+
# Disallow creating `new` promises outside of utility libs (use [util.promisify][] instead) (`promise/avoid-new`)
22

33
🚫 This rule is _disabled_ in the following configs: ✅ `flat/recommended`, ✅
44
`recommended`.
55

66
<!-- end auto-generated rule header -->
77

8-
[pify]: https://www.npmjs.com/package/pify
8+
[util.promisify]:
9+
https://nodejs.org/dist/latest-v8.x/docs/api/util.html#util_util_promisify_original

Diff for: docs/rules/no-callback-in-promise.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Disallow calling `cb()` inside of a `then()` (use [nodeify][] instead) (`promise/no-callback-in-promise`)
1+
# Disallow calling `cb()` inside of a `then()` (use [util.callbackify][] instead) (`promise/no-callback-in-promise`)
22

33
⚠️ This rule _warns_ in the following configs: ✅ `flat/recommended`, ✅
44
`recommended`.
@@ -80,4 +80,5 @@ callback code instead of combining the approaches.
8080

8181
String list of callback function names to exempt.
8282

83-
[nodeify]: https://www.npmjs.com/package/nodeify
83+
[util.callbackify]:
84+
https://nodejs.org/docs/latest/api/util.html#utilcallbackifyoriginal

Diff for: docs/rules/prefer-await-to-callbacks.md

+35-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,37 @@
1-
# Prefer async/await to the callback pattern (`promise/prefer-await-to-callbacks`)
1+
# Prefer `async`/`await` to the callback pattern (`promise/prefer-await-to-callbacks`)
22

33
<!-- end auto-generated rule header -->
4+
5+
`async`/`await` is a clearer pattern to follow than using callbacks.
6+
7+
## Rule details
8+
9+
ES2017's `async`/`await` makes it easier to deal with asynchronous code than the
10+
callback pattern.
11+
12+
Examples of **incorrect** code for this rule:
13+
14+
```js
15+
cb()
16+
callback()
17+
doSomething(arg, (err) => {})
18+
function doSomethingElse(cb) {}
19+
```
20+
21+
Examples of **correct** code for this rule:
22+
23+
```js
24+
await doSomething(arg)
25+
async function doSomethingElse() {}
26+
yield yieldValue(err => {})
27+
eventEmitter.on('error', err => {})
28+
```
29+
30+
## When Not To Use It
31+
32+
If you are not targeting an ES2017 or higher environment and cannot transpile
33+
`async`/`await`, you should disable this rule.
34+
35+
## Further Reading
36+
37+
- [Making asynchronous programming easier with async and await on MDN](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Promises)

Diff for: docs/rules/prefer-await-to-then.md

+15
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,18 @@ function exampleFour() {
4444
return myPromise.finally(cleanup)
4545
}
4646
```
47+
48+
## Options
49+
50+
### `strict`
51+
52+
Normally, this rule allows `then` or `catch` following an `await` (or `yield`)
53+
expression. Setting this option to `true` will err on such cases:
54+
55+
This will fail with the `strict` option:
56+
57+
```js
58+
async function hi() {
59+
await thing().then()
60+
}
61+
```

Diff for: index.js

-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ const pluginPromise = {
4343
}
4444
pluginPromise.configs = {
4545
recommended: {
46-
name: 'promise/recommended',
4746
plugins: ['promise'],
4847
rules: recommendedRules,
4948
},

Diff for: package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
"eslint": "^7.0.0 || ^8.0.0 || ^9.0.0"
8080
},
8181
"engines": {
82-
"node": "^12.22.0 || ^14.17.0 || >=16.0.0"
82+
"node": "^18.18.0 || ^20.9.0 || >=21.1.0"
8383
},
8484
"funding": "https://opencollective.com/eslint"
8585
}

Diff for: rules/always-return.js

+4
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,10 @@ module.exports = {
202202
return {
203203
'ReturnStatement:exit': markCurrentBranchAsGood,
204204
'ThrowStatement:exit': markCurrentBranchAsGood,
205+
'ExpressionStatement > CallExpression > MemberExpression[object.name="process"][property.name="exit"]:exit':
206+
markCurrentBranchAsGood,
207+
'ExpressionStatement > CallExpression > MemberExpression[object.name="process"][property.name="abort"]:exit':
208+
markCurrentBranchAsGood,
205209

206210
/**
207211
* @param {CodePathSegment} segment

Diff for: rules/avoid-new.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module.exports = {
1212
type: 'suggestion',
1313
docs: {
1414
description:
15-
'Disallow creating `new` promises outside of utility libs (use [pify][] instead).',
15+
'Disallow creating `new` promises outside of utility libs (use [util.promisify][] instead).',
1616
url: getDocsUrl('avoid-new'),
1717
},
1818
schema: [],

Diff for: rules/no-callback-in-promise.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ module.exports = {
1818
type: 'suggestion',
1919
docs: {
2020
description:
21-
'Disallow calling `cb()` inside of a `then()` (use [nodeify][] instead).',
21+
'Disallow calling `cb()` inside of a `then()` (use [util.callbackify][] instead).',
2222
url: getDocsUrl('no-callback-in-promise'),
2323
},
2424
messages: {

Diff for: rules/prefer-await-to-callbacks.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module.exports = {
77
meta: {
88
type: 'suggestion',
99
docs: {
10-
description: 'Prefer async/await to the callback pattern.',
10+
description: 'Prefer `async`/`await` to the callback pattern.',
1111
url: getDocsUrl('prefer-await-to-callbacks'),
1212
},
1313
messages: {

Diff for: rules/prefer-await-to-then.js

+13-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,16 @@ module.exports = {
1616
'Prefer `await` to `then()`/`catch()`/`finally()` for reading Promise values.',
1717
url: getDocsUrl('prefer-await-to-then'),
1818
},
19-
schema: [],
19+
schema: [
20+
{
21+
type: 'object',
22+
properties: {
23+
strict: {
24+
type: 'boolean',
25+
},
26+
},
27+
},
28+
],
2029
messages: {
2130
preferAwaitToCallback: 'Prefer await to then()/catch()/finally().',
2231
},
@@ -40,9 +49,11 @@ module.exports = {
4049
return getScope(context, node).block.type === 'Program'
4150
}
4251

52+
const { strict } = context.options[0] || {}
53+
4354
return {
4455
'CallExpression > MemberExpression.callee'(node) {
45-
if (isTopLevelScoped(node) || isInsideYieldOrAwait(node)) {
56+
if (isTopLevelScoped(node) || (!strict && isInsideYieldOrAwait(node))) {
4657
return
4758
}
4859

0 commit comments

Comments
 (0)