Skip to content

Commit cfe34cc

Browse files
authored
Merge pull request #46 from babs20/feature/setprototypeof
feat: add `setprototypeof`
2 parents 7229350 + 50f57ba commit cfe34cc

File tree

7 files changed

+191
-0
lines changed

7 files changed

+191
-0
lines changed

codemods/setprototypeof/index.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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: 'setprototypeof',
16+
transform: ({ file }) => {
17+
const j = jscodeshift;
18+
const root = j(file.source);
19+
let dirtyFlag = false;
20+
21+
const { identifier } = removeImport('setprototypeof', root, j);
22+
23+
if (identifier) {
24+
const callExpressions = root.find(j.CallExpression, {
25+
callee: { name: identifier },
26+
});
27+
28+
for (const path of callExpressions.paths()) {
29+
j(path).replaceWith(
30+
j.callExpression(
31+
j.memberExpression(
32+
j.identifier('Object'),
33+
j.identifier('setPrototypeOf'),
34+
),
35+
path.node.arguments,
36+
),
37+
);
38+
dirtyFlag = true;
39+
}
40+
}
41+
42+
return dirtyFlag ? root.toSource(options) : file.source;
43+
},
44+
};
45+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
var assert = require('assert');
2+
3+
function Foo() { }
4+
function Bar() { }
5+
6+
Object.setPrototypeOf(Foo.prototype, Bar.prototype);
7+
8+
const obj = {};
9+
const proto = {};
10+
Object.setPrototypeOf(obj, proto);
11+
12+
function testFunction() {
13+
const innerObj = {};
14+
const innerProto = {};
15+
Object.setPrototypeOf(innerObj, innerProto);
16+
}
17+
18+
const chainExample = Object.setPrototypeOf({}, {}).prop;
19+
20+
Object.setPrototypeOf();
21+
Object.setPrototypeOf({}, {}, 'extra');
22+
23+
assert.strictEqual(Object.getPrototypeOf(Foo.prototype), Bar.prototype);
24+
assert.strictEqual(Object.getPrototypeOf(obj), proto);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
var setPrototypeOf = require('setprototypeof');
2+
var assert = require('assert');
3+
4+
function Foo() { }
5+
function Bar() { }
6+
7+
setPrototypeOf(Foo.prototype, Bar.prototype);
8+
9+
const obj = {};
10+
const proto = {};
11+
setPrototypeOf(obj, proto);
12+
13+
function testFunction() {
14+
const innerObj = {};
15+
const innerProto = {};
16+
setPrototypeOf(innerObj, innerProto);
17+
}
18+
19+
const chainExample = setPrototypeOf({}, {}).prop;
20+
21+
setPrototypeOf();
22+
setPrototypeOf({}, {}, 'extra');
23+
24+
assert.strictEqual(Object.getPrototypeOf(Foo.prototype), Bar.prototype);
25+
assert.strictEqual(Object.getPrototypeOf(obj), proto);
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
var assert = require('assert');
2+
3+
function Foo() { }
4+
function Bar() { }
5+
6+
Object.setPrototypeOf(Foo.prototype, Bar.prototype);
7+
8+
const obj = {};
9+
const proto = {};
10+
Object.setPrototypeOf(obj, proto);
11+
12+
function testFunction() {
13+
const innerObj = {};
14+
const innerProto = {};
15+
Object.setPrototypeOf(innerObj, innerProto);
16+
}
17+
18+
const chainExample = Object.setPrototypeOf({}, {}).prop;
19+
20+
Object.setPrototypeOf();
21+
Object.setPrototypeOf({}, {}, 'extra');
22+
23+
assert.strictEqual(Object.getPrototypeOf(Foo.prototype), Bar.prototype);
24+
assert.strictEqual(Object.getPrototypeOf(obj), proto);
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import assert from 'assert';
2+
3+
function Foo() { }
4+
function Bar() { }
5+
6+
Object.setPrototypeOf(Foo.prototype, Bar.prototype);
7+
8+
const obj = {};
9+
const proto = {};
10+
Object.setPrototypeOf(obj, proto);
11+
12+
function testFunction() {
13+
const innerObj = {};
14+
const innerProto = {};
15+
Object.setPrototypeOf(innerObj, innerProto);
16+
}
17+
18+
const chainExample = Object.setPrototypeOf({}, {}).prop;
19+
20+
Object.setPrototypeOf();
21+
Object.setPrototypeOf({}, {}, 'extra');
22+
23+
assert.strictEqual(Object.getPrototypeOf(Foo.prototype), Bar.prototype);
24+
assert.strictEqual(Object.getPrototypeOf(obj), proto);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import setprototypeof from 'setprototypeof';
2+
import assert from 'assert';
3+
4+
function Foo() { }
5+
function Bar() { }
6+
7+
setprototypeof(Foo.prototype, Bar.prototype);
8+
9+
const obj = {};
10+
const proto = {};
11+
setprototypeof(obj, proto);
12+
13+
function testFunction() {
14+
const innerObj = {};
15+
const innerProto = {};
16+
setprototypeof(innerObj, innerProto);
17+
}
18+
19+
const chainExample = setprototypeof({}, {}).prop;
20+
21+
setprototypeof();
22+
setprototypeof({}, {}, 'extra');
23+
24+
assert.strictEqual(Object.getPrototypeOf(Foo.prototype), Bar.prototype);
25+
assert.strictEqual(Object.getPrototypeOf(obj), proto);
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import assert from 'assert';
2+
3+
function Foo() { }
4+
function Bar() { }
5+
6+
Object.setPrototypeOf(Foo.prototype, Bar.prototype);
7+
8+
const obj = {};
9+
const proto = {};
10+
Object.setPrototypeOf(obj, proto);
11+
12+
function testFunction() {
13+
const innerObj = {};
14+
const innerProto = {};
15+
Object.setPrototypeOf(innerObj, innerProto);
16+
}
17+
18+
const chainExample = Object.setPrototypeOf({}, {}).prop;
19+
20+
Object.setPrototypeOf();
21+
Object.setPrototypeOf({}, {}, 'extra');
22+
23+
assert.strictEqual(Object.getPrototypeOf(Foo.prototype), Bar.prototype);
24+
assert.strictEqual(Object.getPrototypeOf(obj), proto);

0 commit comments

Comments
 (0)