-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: get ast-types scope basically working
- Loading branch information
1 parent
f3f363f
commit 04103c9
Showing
3 changed files
with
69 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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], | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |