Skip to content

Commit 69eef5b

Browse files
committed
add "window" as default
1 parent f237054 commit 69eef5b

File tree

3 files changed

+33
-16
lines changed

3 files changed

+33
-16
lines changed

__tests__/always-return.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,19 @@ ruleTester.run('always-return', rule, {
105105
.finally(() => console.error('end'))`,
106106
options: [{ ignoreLastCallback: true }],
107107
},
108-
{
109-
code: `hey.then(x => { window.a = x })`,
110-
options: [{ ignoreAssignmentVariable: ['window'] }],
111-
},
108+
`hey.then(x => { window.a = x })`,
112109
{
113110
code: `hey.then(x => { window.a.x = x })`,
114111
options: [{ ignoreAssignmentVariable: ['window.a'] }],
115112
},
113+
{
114+
code: `hey.then(x => { globalThis.a = x })`,
115+
options: [{ ignoreAssignmentVariable: ['globalThis'] }],
116+
},
117+
{
118+
code: `hey.then(x => { globalThis.a.x = x })`,
119+
options: [{ ignoreAssignmentVariable: ['globalThis.a'] }],
120+
},
116121
],
117122

118123
invalid: [

docs/rules/always-return.md

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -98,23 +98,33 @@ function foo() {
9898
You can pass an `{ ignoreAssignmentVariable: [] }` as an option to this rule
9999
with a list of variable names so that the last `then()` callback in a promise
100100
chain does not warn if it does an assignment to a global variable. Default is
101-
`[]`.
101+
`["window"]`.
102102

103103
```js
104-
/* eslint promise/always-return: ["error", { ignoreAssignmentVariable: ["window"] }] */
104+
/* eslint promise/always-return: ["error", { ignoreAssignmentVariable: ["globalThis", "globalThis.modules"] }] */
105105

106106
// OK
107107
promise.then((x) => {
108108
window.x = x
109109
})
110110

111-
promise
112-
// NG
113-
.then((x) => {
114-
window.x.y = x
115-
})
116-
// NG
117-
.then((x) => {
118-
let b = x
119-
})
111+
// OK
112+
promise.then((x) => {
113+
globalThis.x = x
114+
})
115+
116+
// OK
117+
promise.then((x) => {
118+
globalThis.modules.x = x
119+
})
120+
121+
// NG
122+
promise.then((x) => {
123+
window.x.y = x
124+
})
125+
126+
// NG
127+
promise.then((x) => {
128+
let a = x
129+
})
120130
```

rules/always-return.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,9 @@ module.exports = {
185185
create(context) {
186186
const options = context.options[0] || {}
187187
const ignoreLastCallback = !!options.ignoreLastCallback
188-
const ignoreAssignmentVariable = options.ignoreAssignmentVariable || []
188+
const ignoreAssignmentVariable = options.ignoreAssignmentVariable || [
189+
'window',
190+
]
189191
/**
190192
* @typedef {object} FuncInfo
191193
* @property {string[]} branchIDStack This is a stack representing the currently

0 commit comments

Comments
 (0)