File tree Expand file tree Collapse file tree 4 files changed +72
-0
lines changed
codemods/get-symbol-description
test/fixtures/get-symbol-description/case-1 Expand file tree Collapse file tree 4 files changed +72
-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 : 'get-symbol-description' ,
16+ transform : ( { file } ) => {
17+ const j = jscodeshift ;
18+ const root = j ( file . source ) ;
19+ let dirtyFlag = false ;
20+
21+ const { identifier } = removeImport ( 'get-symbol-description' , root , j ) ;
22+
23+ root
24+ . find ( j . CallExpression , {
25+ callee : {
26+ type : 'Identifier' ,
27+ name : identifier ,
28+ } ,
29+ } )
30+ . forEach ( ( path ) => {
31+ const args = path . value . arguments ;
32+ if ( args . length === 1 ) {
33+ const newExpression = j . memberExpression (
34+ //@ts -ignore
35+ args [ 0 ] ,
36+ j . identifier ( 'description' ) ,
37+ ) ;
38+
39+ j ( path ) . replaceWith ( newExpression ) ;
40+ dirtyFlag = true ;
41+ }
42+ } ) ;
43+
44+ return dirtyFlag ? root . toSource ( options ) : file . source ;
45+ } ,
46+ } ;
47+ }
Original file line number Diff line number Diff line change 1+ var assert = require ( "assert" ) ;
2+
3+ var testSymbol = symbol ( "foo" ) ;
4+
5+ assert . equal (
6+ testSymbol . description ,
7+ "foo" ,
8+ ) ;
Original file line number Diff line number Diff line change 1+ var symbolDescription = require ( "get-symbol-description" ) ;
2+ var assert = require ( "assert" ) ;
3+
4+ var testSymbol = symbol ( "foo" ) ;
5+
6+ assert . equal (
7+ symbolDescription ( testSymbol ) ,
8+ "foo" ,
9+ ) ;
Original file line number Diff line number Diff line change 1+ var assert = require ( "assert" ) ;
2+
3+ var testSymbol = symbol ( "foo" ) ;
4+
5+ assert . equal (
6+ testSymbol . description ,
7+ "foo" ,
8+ ) ;
You can’t perform that action at this time.
0 commit comments