Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit fc60629

Browse files
committedAug 2, 2024·
Add codemod for globalthis
1 parent 8ddb3f7 commit fc60629

File tree

18 files changed

+161
-9
lines changed

18 files changed

+161
-9
lines changed
 

‎codemods/globalthis/index.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import jscodeshift from 'jscodeshift';
2+
import { removeImport } from '../shared.js';
3+
4+
/**
5+
* @typedef {import('../../types.js').Codemod} Codemod
6+
* @typedef {import('../../types.js').CodemodOptions} CodemodOptions
7+
*/
8+
9+
/**
10+
* @param {CodemodOptions} [options]
11+
* @returns {Codemod}
12+
*/
13+
export default function (options) {
14+
return {
15+
name: 'globalthis',
16+
transform: ({ file }) => {
17+
const j = jscodeshift;
18+
const root = j(file.source);
19+
const identifier1 = removeImport('globalthis', root, j).identifier;
20+
const identifier2 = removeImport(
21+
'globalthis/polyfill',
22+
root,
23+
j,
24+
).identifier;
25+
const identifier3 = removeImport('globalthis/shim', root, j).identifier;
26+
const identifier = identifier1 || identifier2 || identifier3;
27+
28+
if (identifier) {
29+
root
30+
.find(j.Identifier, { name: identifier })
31+
.replaceWith(j.identifier('globalThis'));
32+
}
33+
34+
return root.toSource({ quote: 'single' });
35+
},
36+
};
37+
}

‎codemods/shared.js

+101-8
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,91 @@ export function removeImport(name, root, j) {
3333
},
3434
});
3535

36+
// Require statements with call expressions like `var globalThis = require('globalthis')()`
37+
const requireCallExpression = root.find(j.VariableDeclarator, {
38+
init: {
39+
callee: {
40+
type: 'CallExpression',
41+
callee: {
42+
name: 'require',
43+
},
44+
arguments: [
45+
{
46+
value: name,
47+
},
48+
],
49+
},
50+
},
51+
});
52+
53+
// Same as above without variable declaration like `require('globalthis')()`
54+
const sideEffectRequireCallExpression = root.find(j.ExpressionStatement, {
55+
expression: {
56+
callee: {
57+
type: 'CallExpression',
58+
callee: {
59+
name: 'require',
60+
},
61+
arguments: [
62+
{
63+
value: name,
64+
},
65+
],
66+
},
67+
},
68+
});
69+
70+
// Require chained call expressions like `var globalThis = require('globalthis').shim()`
71+
const requireMethodCallExpression = root.find(j.VariableDeclarator, {
72+
init: {
73+
type: 'CallExpression',
74+
callee: {
75+
type: 'MemberExpression',
76+
object: {
77+
type: 'CallExpression',
78+
callee: {
79+
name: 'require',
80+
},
81+
arguments: [
82+
{
83+
value: name,
84+
},
85+
],
86+
},
87+
property: {
88+
type: 'Identifier',
89+
},
90+
},
91+
},
92+
});
93+
94+
// Same as above without variable declaration like `require('globalthis').shim()`
95+
const sideEffectRequireMethodCallExpression = root.find(
96+
j.ExpressionStatement,
97+
{
98+
expression: {
99+
type: 'CallExpression',
100+
callee: {
101+
type: 'MemberExpression',
102+
object: {
103+
type: 'CallExpression',
104+
callee: {
105+
name: 'require',
106+
},
107+
arguments: [
108+
{
109+
value: name,
110+
},
111+
],
112+
},
113+
property: {
114+
type: 'Identifier',
115+
},
116+
},
117+
},
118+
},
119+
);
120+
36121
// Require statements without declarations like `Object.is = require("object-is");`
37122
const requireAssignment = root.find(j.AssignmentExpression, {
38123
operator: '=',
@@ -64,17 +149,25 @@ export function removeImport(name, root, j) {
64149

65150
// Return the identifier name, e.g. 'fn' in `import { fn } from 'is-boolean-object'`
66151
// or `var fn = require('is-boolean-object')`
67-
const identifier =
68-
importDeclaration.paths().length > 0
69-
? importDeclaration.get().node.specifiers[0].local.name
70-
: requireDeclaration.paths().length > 0
71-
? requireDeclaration.find(j.Identifier).get().node.name
72-
: requireAssignment.paths().length > 0
73-
? requireAssignment.find(j.Identifier).get().node.name
74-
: null;
152+
let identifier = null;
153+
if (importDeclaration.paths().length > 0) {
154+
identifier = importDeclaration.get().node.specifiers[0].local.name;
155+
} else if (requireDeclaration.paths().length > 0) {
156+
identifier = requireDeclaration.find(j.Identifier).get().node.name;
157+
} else if (requireCallExpression.paths().length > 0) {
158+
identifier = requireCallExpression.find(j.Identifier).get().node.name;
159+
} else if (requireMethodCallExpression.paths().length > 0) {
160+
identifier = requireMethodCallExpression.find(j.Identifier).get().node.name;
161+
} else if (requireAssignment.paths().length > 0) {
162+
identifier = requireAssignment.find(j.Identifier).get().node.name;
163+
}
75164

76165
importDeclaration.remove();
77166
requireDeclaration.remove();
167+
requireCallExpression.remove();
168+
sideEffectRequireCallExpression.remove();
169+
requireMethodCallExpression.remove();
170+
sideEffectRequireMethodCallExpression.remove();
78171
requireAssignment.remove();
79172
sideEffectRequireExpression.remove();
80173

‎index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ import functionBind from './codemods/function-bind/index.js';
5858
import functionPrototypeName from './codemods/function.prototype.name/index.js';
5959
import functionsHaveNames from './codemods/functions-have-names/index.js';
6060
import getSymbolDescription from './codemods/get-symbol-description/index.js';
61+
import globalthis from './codemods/globalthis/index.js';
6162
import gopd from './codemods/gopd/index.js';
6263
import has from './codemods/has/index.js';
6364
import hasOwnProp from './codemods/has-own-prop/index.js';
@@ -210,6 +211,7 @@ export const codemods = {
210211
"function.prototype.name": functionPrototypeName,
211212
"functions-have-names": functionsHaveNames,
212213
"get-symbol-description": getSymbolDescription,
214+
"globalthis": globalthis,
213215
"gopd": gopd,
214216
"has": has,
215217
"has-own-prop": hasOwnProp,
@@ -300,4 +302,4 @@ export const codemods = {
300302
"typed-array-length": typedArrayLength,
301303
"typedarray.prototype.slice": typedarrayPrototypeSlice,
302304
"xtend": xtend,
303-
};
305+
};
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
globalThis.a = 42;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
var globalThis = require('globalthis')();
2+
globalThis.a = 42;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
globalThis.a = 42;
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
globalThis.a = 42;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
var globalThis = require('globalthis/polyfill')();
2+
globalThis.a = 42;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
globalThis.a = 42;
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
globalThis.a = 42;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
var newGlobalThis = require('globalthis')();
2+
newGlobalThis.a = 42;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
globalThis.a = 42;
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
globalThis.a = 42;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
var shimmedGlobal = require('globalthis').shim();
2+
shimmedGlobal.a = 42;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
globalThis.a = 42;
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
globalThis.a = 42;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
var shimmedGlobal = require('globalthis/shim')();
2+
shimmedGlobal.a = 42;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
globalThis.a = 42;

0 commit comments

Comments
 (0)
Please sign in to comment.