File tree Expand file tree Collapse file tree 7 files changed +191
-0
lines changed
test/fixtures/setprototypeof Expand file tree Collapse file tree 7 files changed +191
-0
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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 ) ;
Original file line number Diff line number Diff line change 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 ) ;
Original file line number Diff line number Diff line change 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 ) ;
Original file line number Diff line number Diff line change 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 ) ;
Original file line number Diff line number Diff line change 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 ) ;
Original file line number Diff line number Diff line change 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 ) ;
You can’t perform that action at this time.
0 commit comments