Skip to content

Commit

Permalink
finish work
Browse files Browse the repository at this point in the history
  • Loading branch information
andrico1234 committed Jul 30, 2024
1 parent ca92fd5 commit 4a26a2d
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 48 deletions.
61 changes: 40 additions & 21 deletions codemods/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,41 +89,49 @@ export function removeImport(name, root, j) {
*/
export function insertAfterImports(code, root, j) {
const importDeclarations = root.find(j.ImportDeclaration);

if (importDeclarations.length) {
const lastItem = importDeclarations.at(-1).get();
j(lastItem).insertAfter(code);
return;
}

const requireDeclarations = root.find(j.VariableDeclarator, {
init: {
callee: {
name: 'require',
},
}
});

if (requireDeclarations.length) {
const lastItem = requireDeclarations.at(-1).get();
j(lastItem).insertAfter(code);
return;
}

const requireAssignments = root.find(j.AssignmentExpression, {
operator: '=',
right: {
callee: {
name: 'require',
},
},
}).length;
});

// Side effect requires statements like `require("error-cause/auto");`
const sideEffectRequireExpression = root.find(j.ExpressionStatement, {
expression: {
callee: {
name: 'require',
}
},
});

if (requireAssignments) {
const lastItem = requireAssignments.at(-1).get();
j(lastItem).insertAfter(code);
const allNodes = [
...importDeclarations.nodes(),
...requireDeclarations.nodes(),
...requireAssignments.nodes(),
...sideEffectRequireExpression.nodes(),
];

if (allNodes.length === 0) {
return;
}

const sortedNodes = allNodes.sort((a, b) => {
return a.loc.start.line - b.loc.start.line;

Check failure on line 129 in codemods/shared.js

View workflow job for this annotation

GitHub Actions / typecheck (20.x)

'a.loc' is possibly 'null' or 'undefined'.

Check failure on line 129 in codemods/shared.js

View workflow job for this annotation

GitHub Actions / typecheck (20.x)

'b.loc' is possibly 'null' or 'undefined'.
});

const bottomMostNode = sortedNodes[sortedNodes.length - 1];
const node = getAncestorOnLine(bottomMostNode.loc.end.line, root, j);

Check failure on line 133 in codemods/shared.js

View workflow job for this annotation

GitHub Actions / typecheck (20.x)

'bottomMostNode.loc' is possibly 'null' or 'undefined'.
j(node).insertAfter(code);
}

export const DEFAULT_IMPORT = Symbol('DEFAULT_IMPORT');
Expand Down Expand Up @@ -509,14 +517,25 @@ export function replaceRequireMemberExpression(importName, value, root, j) {
* @param {import("jscodeshift").Collection} root
* @param {import("jscodeshift").JSCodeshift} j
*/
export function insertCommentAboveNode(comment, startLine, root, j) {
const node = root.find(j.Node, {
export function getAncestorOnLine(line, root, j) {

Check failure on line 520 in codemods/shared.js

View workflow job for this annotation

GitHub Actions / typecheck (20.x)

Parameter 'line' implicitly has an 'any' type.
return root.find(j.Node, {
loc: {
start: {
line: startLine,
line,
}
}
}).at(0).get()
}

/**
*
* @param {import("jscodeshift").CommentBlock} comment
* @param {number} startLine
* @param {import("jscodeshift").Collection} root
* @param {import("jscodeshift").JSCodeshift} j
*/
export function insertCommentAboveNode(comment, startLine, root, j) {
const node = getAncestorOnLine(startLine, root, j);

node.value.comments = node.value.comments || [];
node.value.comments.push(comment);
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/define-properties/case-2/after.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const assert = require('assert');


const $define = function (object, map) {
let propKeys = Object.keys(map);
propKeys = propKeys.concat(Object.getOwnPropertySymbols(map));
Expand Down Expand Up @@ -51,10 +52,9 @@ assert.deepEqual(Object.getOwnPropertyDescriptor(res1, 'c'), {
writable: false
});


const object2 = { a: 1, b: 2 };

const res2 = $define(object2, {
$define(object2, {
c: 30
})

Expand Down
49 changes: 25 additions & 24 deletions test/fixtures/define-properties/case-2/result.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
const assert = require('assert'),

const $define = function (object, map) {
let propKeys = Object.keys(map);
propKeys = propKeys.concat(Object.getOwnPropertySymbols(map));

for (var i = 0; i < propKeys.length; i += 1) {
const propKey = propKeys[i];
const value = map[propKey];

if (propKey in object) {
continue;
}

Object.defineProperty(object, propKey, {
value,
configurable: true,
enumerable: false,
writable: false,
})
}

return object;
};;
const assert = require('assert');


const $define = function (object, map) {
let propKeys = Object.keys(map);
propKeys = propKeys.concat(Object.getOwnPropertySymbols(map));

for (var i = 0; i < propKeys.length; i += 1) {
const propKey = propKeys[i];
const value = map[propKey];

if (propKey in object) {
continue;
}

Object.defineProperty(object, propKey, {
value,
configurable: true,
enumerable: false,
writable: false,
})
}

return object;
};

const object1 = { a: 1, b: 2 };

Expand Down
28 changes: 27 additions & 1 deletion test/fixtures/define-properties/case-4/after.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,33 @@
const define = require('define-properties');
const assert = require('assert');

define({ a: 1, b: 2 }, {
require("fake/auto");
import fake from "fake";

const $define = function (object, map) {
let propKeys = Object.keys(map);
propKeys = propKeys.concat(Object.getOwnPropertySymbols(map));

for (var i = 0; i < propKeys.length; i += 1) {
const propKey = propKeys[i];
const value = map[propKey];

if (propKey in object) {
continue;
}

Object.defineProperty(object, propKey, {
value,
configurable: true,
enumerable: false,
writable: false,
})
}

return object;
};

$define({ a: 1, b: 2 }, {
a: 10,
b: 20,
c: 30,
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/define-properties/case-4/before.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
const define = require('define-properties');
const assert = require('assert');

require("fake/auto");
import fake from "fake";

const $define = function (object, map) {
let propKeys = Object.keys(map);
propKeys = propKeys.concat(Object.getOwnPropertySymbols(map));
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/define-properties/case-4/result.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
const define = require('define-properties');
const assert = require('assert');

require("fake/auto");
import fake from "fake";

const $define = function (object, map) {
let propKeys = Object.keys(map);
propKeys = propKeys.concat(Object.getOwnPropertySymbols(map));
Expand Down

0 comments on commit 4a26a2d

Please sign in to comment.