Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/sxzz/ast-kit. A toolkit for easy Babel AST generation and manipulation. jsr.io/@sxzz/ast-kit
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081import { describe, expect, test } from 'vitest'import { babelParse, extractIdentifiers } from '../src/index.ts'import type * as t from '@babel/types'
describe('extract', () => { test('extractIdentifiers', () => { const ast = babelParse(` const one = 1 const { propA, propB: aliasPropB, propC = 33, propD: aliasPropD = 44, ...objRest } = { propA: 1, propB: 2, propC: 3, propD: 4, propE: 5, propF: 6, } const [elOne, elTwo = 22, ...elRest] = [1, 2, 3, 4] memberExpressionObj.a `)
const identifiers: t.Identifier[] = []
for (const b of ast.body) { if (b.type === 'VariableDeclaration') { for (const d of b.declarations) { extractIdentifiers(d.id, identifiers) } }
if (b.type === 'ExpressionStatement') { extractIdentifiers(b.expression, identifiers) } }
expect( identifiers.map((id) => ({ name: id.name, })), ).toMatchInlineSnapshot(` [ { "name": "one", }, { "name": "propA", }, { "name": "aliasPropB", }, { "name": "propC", }, { "name": "aliasPropD", }, { "name": "objRest", }, { "name": "elOne", }, { "name": "elTwo", }, { "name": "elRest", }, { "name": "memberExpressionObj", }, ] `) })})