Skip to content

Commit

Permalink
fix: get ast-types scope basically working
Browse files Browse the repository at this point in the history
  • Loading branch information
jedwards1211 committed Apr 18, 2024
1 parent f3f363f commit 04103c9
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 3 deletions.
4 changes: 1 addition & 3 deletions src/babel/babelAstTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import * as defaultTypes from '@babel/types'
import { memoize, omit, mapValues } from 'lodash'
import fork from 'ast-types/fork'
import { Fork } from 'ast-types/types'
import nodePathPlugin from 'ast-types/lib/node-path'

const babelAstTypes: (t?: typeof defaultTypes) => ReturnType<typeof fork> =
memoize((t: typeof defaultTypes = defaultTypes): ReturnType<typeof fork> => {
Expand All @@ -14,8 +13,6 @@ const babelAstTypes: (t?: typeof defaultTypes) => ReturnType<typeof fork> =
const { builtInTypes, Type } = types
const { def, or } = Type

fork.use(nodePathPlugin)

def('Node').field('type', builtInTypes.string)
def('Comment')
.field('type', builtInTypes.string)
Expand Down Expand Up @@ -118,6 +115,7 @@ const babelAstTypes: (t?: typeof defaultTypes) => ReturnType<typeof fork> =
)
}
}
def('Identifier').bases('Pattern')
}

return fork([babel])
Expand Down
17 changes: 17 additions & 0 deletions test/astx/bugs_scope.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { TransformOptions } from '../../src'
import { astxTestcase } from '../astxTestcase'
import { NodePath as AstTypesNodePath } from 'ast-types/lib/node-path'
import dedent from 'dedent-js'

astxTestcase({
file: __filename,
input: dedent`
const foo = 1;
`,
parsers: ['babel', 'babel/tsx'],
astx: ({ astx, report }: TransformOptions): void => {
const path = astx.find`const foo = 1;`.paths[0]
report((path as AstTypesNodePath).scope !== null)
},
expectedReports: [true],
})
51 changes: 51 additions & 0 deletions test/astx/scope-testcase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { NodePath as AstTypesNodePath } from 'ast-types/lib/node-path'
import { Astx } from '../../src'
import { astxTestcase } from '../astxTestcase'
import dedent from 'dedent-js'

astxTestcase({
file: __filename,
input: dedent`
const x = 1, y = 2;
function foo() {
const y = 3;
const target = x + y;
}
`,
expected: dedent`
const x = 1, y = 2;
function foo() {
const y = 3;
const target = 1 + 3;
}
`,
parsers: ['babel', 'babel/tsx'],
astx: ({ astx }) => {
for (const { $a, $b } of astx.find`const target = $a + $b`) {
inlineNaively($a)
inlineNaively($b)
}
},
})

/** kludge to gain access to scope */
type AstxWithScope = Astx & {
path: AstTypesNodePath
}

// inspired by jscodeshift
function getDeclarator(a: AstxWithScope) {
const name = a.path.value.name
const bindings = new Astx(
a.context,
a.path.scope?.lookup(name)?.getBindings()[name]
)
return bindings
.closest((astx) => astx.node.type === 'VariableDeclarator')
.at(0)
}

function inlineNaively(a: Astx) {
const val = getDeclarator(a as AstxWithScope).path.get('init').value
a.replace(val)
}

0 comments on commit 04103c9

Please sign in to comment.