Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

global #78

Merged
merged 1 commit into from
Aug 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions codemods/global/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import jscodeshift from 'jscodeshift';
import { removeImport } from '../shared.js';

/**
* @typedef {import('../../types.js').Codemod} Codemod
* @typedef {import('../../types.js').CodemodOptions} CodemodOptions
*/

/**
* @param {CodemodOptions} [options]
* @returns {Codemod}
*/
export default function (options) {
return {
name: 'global',
transform: ({ file }) => {
const j = jscodeshift;
const root = j(file.source);

const globalIdentifier = removeImport('global', root, j).identifier;
const documentIdentifier = removeImport(
'global/document',
root,
j,
).identifier;
const windowIdentifier = removeImport(
'global/window',
root,
j,
).identifier;

root
.find(j.Identifier, { name: globalIdentifier })
.replaceWith(j.identifier('globalThis'));
root
.find(j.Identifier, { name: documentIdentifier })
.replaceWith(j.identifier('document'));
root
.find(j.Identifier, { name: windowIdentifier })
.replaceWith(j.identifier('window'));

return root.toSource({ quote: 'single' });
},
};
}
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import functionBind from './codemods/function-bind/index.js';
import functionPrototypeName from './codemods/function.prototype.name/index.js';
import functionsHaveNames from './codemods/functions-have-names/index.js';
import getSymbolDescription from './codemods/get-symbol-description/index.js';
import global from './codemods/global/index.js';
import gopd from './codemods/gopd/index.js';
import has from './codemods/has/index.js';
import hasOwnProp from './codemods/has-own-prop/index.js';
Expand Down Expand Up @@ -208,6 +209,7 @@ export const codemods = {
"function.prototype.name": functionPrototypeName,
"functions-have-names": functionsHaveNames,
"get-symbol-description": getSymbolDescription,
"global": global,
"gopd": gopd,
"has": has,
"has-own-prop": hasOwnProp,
Expand Down
9 changes: 9 additions & 0 deletions test/fixtures/global/case-1/after.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var assert = require("assert");

a = 42;
assert.equal(a, 42);
assert.equal(globalThis.a, 42);

globalThis.b = 43;
assert.equal(b, 43);
assert.equal(globalThis.b, 43);
10 changes: 10 additions & 0 deletions test/fixtures/global/case-1/before.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
var global = require("global");
var assert = require("assert");

a = 42;
assert.equal(a, 42);
assert.equal(global.a, 42);

global.b = 43;
assert.equal(b, 43);
assert.equal(global.b, 43);
9 changes: 9 additions & 0 deletions test/fixtures/global/case-1/result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var assert = require("assert");

a = 42;
assert.equal(a, 42);
assert.equal(globalThis.a, 42);

globalThis.b = 43;
assert.equal(b, 43);
assert.equal(globalThis.b, 43);
1 change: 1 addition & 0 deletions test/fixtures/global/case-2/after.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log(document.head);
3 changes: 3 additions & 0 deletions test/fixtures/global/case-2/before.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var myDocument = require("global/document");

console.log(myDocument.head);
1 change: 1 addition & 0 deletions test/fixtures/global/case-2/result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log(document.head);
1 change: 1 addition & 0 deletions test/fixtures/global/case-3/after.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log(window.location.href);
3 changes: 3 additions & 0 deletions test/fixtures/global/case-3/before.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var window = require("global/window")

console.log(window.location.href);
1 change: 1 addition & 0 deletions test/fixtures/global/case-3/result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log(window.location.href);
Loading