Skip to content

Commit b95dc12

Browse files
axetroyfisker
andauthored
prefer-global-this: Fix auto-fix for typeof window (#2501)
Co-authored-by: fisker Cheung <[email protected]>
1 parent ea89962 commit b95dc12

File tree

4 files changed

+140
-3
lines changed

4 files changed

+140
-3
lines changed

rules/prefer-global-this.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const MESSAGE_ID_ERROR = 'prefer-global-this/error';
22
const messages = {
3-
[MESSAGE_ID_ERROR]: 'Prefer `globalThis` over `{{value}}`.',
3+
[MESSAGE_ID_ERROR]: 'Prefer `{{replacement}}` over `{{value}}`.',
44
};
55

66
const globalIdentifier = new Set([
@@ -188,11 +188,16 @@ const create = context => ({
188188
continue;
189189
}
190190

191+
// Skip the fix for `typeof window` and `typeof self`
192+
const isTypeofLegacyGlobal = identifier.parent.type === 'UnaryExpression' && identifier.parent.operator === 'typeof' && identifier.parent.argument === identifier;
193+
194+
const replacement = isTypeofLegacyGlobal ? 'globalThis.' + identifier.name : 'globalThis';
195+
191196
yield {
192197
node: identifier,
193198
messageId: MESSAGE_ID_ERROR,
194-
data: {value: identifier.name},
195-
fix: fixer => fixer.replaceText(identifier, 'globalThis'),
199+
data: {replacement, value: identifier.name},
200+
fix: fixer => fixer.replaceText(identifier, replacement),
196201
};
197202
}
198203
},

test/prefer-global-this.js

+6
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,12 @@ test.snapshot({
168168
'[window.foo] = []',
169169
'foo[window]',
170170
'foo[window.foo]',
171+
'typeof window !== "undefined"',
172+
'typeof self !== "undefined"',
173+
'typeof global !== "undefined"',
174+
'typeof window.something === "function"',
175+
'typeof self.something === "function"',
176+
'typeof global.something === "function"',
171177
],
172178
});
173179

test/snapshots/prefer-global-this.js.md

+126
Original file line numberDiff line numberDiff line change
@@ -1172,6 +1172,132 @@ Generated by [AVA](https://avajs.dev).
11721172
| ^^^^^^ Prefer \`globalThis\` over \`window\`.␊
11731173
`
11741174

1175+
## invalid(51): typeof window !== "undefined"
1176+
1177+
> Input
1178+
1179+
`␊
1180+
1 | typeof window !== "undefined"␊
1181+
`
1182+
1183+
> Output
1184+
1185+
`␊
1186+
1 | typeof globalThis.window !== "undefined"␊
1187+
`
1188+
1189+
> Error 1/1
1190+
1191+
`␊
1192+
> 1 | typeof window !== "undefined"␊
1193+
| ^^^^^^ Prefer \`globalThis.window\` over \`window\`.␊
1194+
`
1195+
1196+
## invalid(52): typeof self !== "undefined"
1197+
1198+
> Input
1199+
1200+
`␊
1201+
1 | typeof self !== "undefined"␊
1202+
`
1203+
1204+
> Output
1205+
1206+
`␊
1207+
1 | typeof globalThis.self !== "undefined"␊
1208+
`
1209+
1210+
> Error 1/1
1211+
1212+
`␊
1213+
> 1 | typeof self !== "undefined"␊
1214+
| ^^^^ Prefer \`globalThis.self\` over \`self\`.␊
1215+
`
1216+
1217+
## invalid(53): typeof global !== "undefined"
1218+
1219+
> Input
1220+
1221+
`␊
1222+
1 | typeof global !== "undefined"␊
1223+
`
1224+
1225+
> Output
1226+
1227+
`␊
1228+
1 | typeof globalThis.global !== "undefined"␊
1229+
`
1230+
1231+
> Error 1/1
1232+
1233+
`␊
1234+
> 1 | typeof global !== "undefined"␊
1235+
| ^^^^^^ Prefer \`globalThis.global\` over \`global\`.␊
1236+
`
1237+
1238+
## invalid(54): typeof window.something === "function"
1239+
1240+
> Input
1241+
1242+
`␊
1243+
1 | typeof window.something === "function"␊
1244+
`
1245+
1246+
> Output
1247+
1248+
`␊
1249+
1 | typeof globalThis.something === "function"␊
1250+
`
1251+
1252+
> Error 1/1
1253+
1254+
`␊
1255+
> 1 | typeof window.something === "function"␊
1256+
| ^^^^^^ Prefer \`globalThis\` over \`window\`.␊
1257+
`
1258+
1259+
## invalid(55): typeof self.something === "function"
1260+
1261+
> Input
1262+
1263+
`␊
1264+
1 | typeof self.something === "function"␊
1265+
`
1266+
1267+
> Output
1268+
1269+
`␊
1270+
1 | typeof globalThis.something === "function"␊
1271+
`
1272+
1273+
> Error 1/1
1274+
1275+
`␊
1276+
> 1 | typeof self.something === "function"␊
1277+
| ^^^^ Prefer \`globalThis\` over \`self\`.␊
1278+
`
1279+
1280+
## invalid(56): typeof global.something === "function"
1281+
1282+
> Input
1283+
1284+
`␊
1285+
1 | typeof global.something === "function"␊
1286+
`
1287+
1288+
> Output
1289+
1290+
`␊
1291+
1 | typeof globalThis.something === "function"␊
1292+
`
1293+
1294+
> Error 1/1
1295+
1296+
`␊
1297+
> 1 | typeof global.something === "function"␊
1298+
| ^^^^^^ Prefer \`globalThis\` over \`global\`.␊
1299+
`
1300+
11751301
## invalid(1): global.global_did_not_declare_in_language_options
11761302

11771303
> Input
222 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)