File tree Expand file tree Collapse file tree 3 files changed +69
-3
lines changed Expand file tree Collapse file tree 3 files changed +69
-3
lines changed Original file line number Diff line number Diff line change @@ -5,7 +5,6 @@ import * as defaultTypes from '@babel/types'
5
5
import { memoize , omit , mapValues } from 'lodash'
6
6
import fork from 'ast-types/fork'
7
7
import { Fork } from 'ast-types/types'
8
- import nodePathPlugin from 'ast-types/lib/node-path'
9
8
10
9
const babelAstTypes : ( t ?: typeof defaultTypes ) => ReturnType < typeof fork > =
11
10
memoize ( ( t : typeof defaultTypes = defaultTypes ) : ReturnType < typeof fork > => {
@@ -14,8 +13,6 @@ const babelAstTypes: (t?: typeof defaultTypes) => ReturnType<typeof fork> =
14
13
const { builtInTypes, Type } = types
15
14
const { def, or } = Type
16
15
17
- fork . use ( nodePathPlugin )
18
-
19
16
def ( 'Node' ) . field ( 'type' , builtInTypes . string )
20
17
def ( 'Comment' )
21
18
. field ( 'type' , builtInTypes . string )
@@ -118,6 +115,7 @@ const babelAstTypes: (t?: typeof defaultTypes) => ReturnType<typeof fork> =
118
115
)
119
116
}
120
117
}
118
+ def ( 'Identifier' ) . bases ( 'Pattern' )
121
119
}
122
120
123
121
return fork ( [ babel ] )
Original file line number Diff line number Diff line change
1
+ import { TransformOptions } from '../../src'
2
+ import { astxTestcase } from '../astxTestcase'
3
+ import { NodePath as AstTypesNodePath } from 'ast-types/lib/node-path'
4
+ import dedent from 'dedent-js'
5
+
6
+ astxTestcase ( {
7
+ file : __filename ,
8
+ input : dedent `
9
+ const foo = 1;
10
+ ` ,
11
+ parsers : [ 'babel' , 'babel/tsx' ] ,
12
+ astx : ( { astx, report } : TransformOptions ) : void => {
13
+ const path = astx . find `const foo = 1;` . paths [ 0 ]
14
+ report ( ( path as AstTypesNodePath ) . scope !== null )
15
+ } ,
16
+ expectedReports : [ true ] ,
17
+ } )
Original file line number Diff line number Diff line change
1
+ import { NodePath as AstTypesNodePath } from 'ast-types/lib/node-path'
2
+ import { Astx } from '../../src'
3
+ import { astxTestcase } from '../astxTestcase'
4
+ import dedent from 'dedent-js'
5
+
6
+ astxTestcase ( {
7
+ file : __filename ,
8
+ input : dedent `
9
+ const x = 1, y = 2;
10
+ function foo() {
11
+ const y = 3;
12
+ const target = x + y;
13
+ }
14
+ ` ,
15
+ expected : dedent `
16
+ const x = 1, y = 2;
17
+ function foo() {
18
+ const y = 3;
19
+ const target = 1 + 3;
20
+ }
21
+ ` ,
22
+ parsers : [ 'babel' , 'babel/tsx' ] ,
23
+ astx : ( { astx } ) => {
24
+ for ( const { $a, $b } of astx . find `const target = $a + $b` ) {
25
+ inlineNaively ( $a )
26
+ inlineNaively ( $b )
27
+ }
28
+ } ,
29
+ } )
30
+
31
+ /** kludge to gain access to scope */
32
+ type AstxWithScope = Astx & {
33
+ path : AstTypesNodePath
34
+ }
35
+
36
+ // inspired by jscodeshift
37
+ function getDeclarator ( a : AstxWithScope ) {
38
+ const name = a . path . value . name
39
+ const bindings = new Astx (
40
+ a . context ,
41
+ a . path . scope ?. lookup ( name ) ?. getBindings ( ) [ name ]
42
+ )
43
+ return bindings
44
+ . closest ( ( astx ) => astx . node . type === 'VariableDeclarator' )
45
+ . at ( 0 )
46
+ }
47
+
48
+ function inlineNaively ( a : Astx ) {
49
+ const val = getDeclarator ( a as AstxWithScope ) . path . get ( 'init' ) . value
50
+ a . replace ( val )
51
+ }
You can’t perform that action at this time.
0 commit comments