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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421import { describe, expect, test, vi } from 'vitest'import { babelParse, isCallOf, isDeclarationType, isExpressionType, isFunctionType, isIdentifierOf, isInDestructureAssignment, isInNewExpression, isLiteralType, isReferenced, isReferencedIdentifier, isStaticProperty, isStaticPropertyKey, isTaggedFunctionCallOf, isTypeOf, walkIdentifiers,} from '../src'import { parse as _parse } from './_utils'import type * as t from '@babel/types'
describe('utils', () => { test('isTypeOf', () => { expect(isTypeOf(null, 'NullLiteral')).toBe(false) expect(isTypeOf({ type: 'NullLiteral' }, 'NullLiteral')).toBe(true) expect( isTypeOf({ type: 'NullLiteral' }, ['Literal', 'ObjectExpression']), ).toBe(true) expect( isTypeOf({ type: 'ClassMethod' } as t.ClassMethod, ['Function']), ).toBe(true) expect(isTypeOf({ type: 'AnyTypeAnnotation' }, ['NullLiteral'])).toBe(false)
expect( isTypeOf({ type: 'ArrayExpression' } as t.Expression, 'Expression'), ).toBe(true) expect(isTypeOf({ type: 'NullLiteral' }, 'Expression')).toBe(true) expect(isTypeOf({ type: 'JSXElement' } as t.Expression, 'Expression')).toBe( true, ) })
test('isLiteralType', () => { expect(isLiteralType({ type: 'NullLiteral' })).toBe(true) expect(isLiteralType({ type: 'AnyTypeAnnotation' })).toBe(false) })
test('isFunctionType', () => { expect( isFunctionType({ type: 'FunctionDeclaration' } as t.FunctionDeclaration), ).toBe(true) expect( isFunctionType({ type: 'FunctionExpression' } as t.FunctionExpression), ).toBe(true) expect( isFunctionType({ type: 'TSDeclareMethod' } as t.TSDeclareMethod), ).toBe(false) })
test('isDeclarationType', () => { expect( isDeclarationType({ type: 'FunctionDeclaration', } as t.FunctionDeclaration), ).toBe(true) expect( isDeclarationType({ type: 'Placeholder', expectedNode: 'Identifier', } as t.Placeholder), ).toBe(false) expect( isDeclarationType({ type: 'Placeholder', expectedNode: 'Declaration', } as t.Placeholder), ).toBe(true) expect( isDeclarationType({ type: 'FunctionExpression' } as t.FunctionExpression), ).toBe(false) expect(isDeclarationType(null)).toBe(false) })
test('isExpressionType', () => { expect(isExpressionType({ type: 'ArrayExpression' } as t.Expression)).toBe( true, ) expect(isExpressionType({ type: 'BooleanLiteral' } as t.Expression)).toBe( true, ) expect( isExpressionType({ type: 'Identifier', name: 'a' } as t.Expression), ).toBe(true)
expect( isExpressionType({ type: 'FunctionDeclaration', } as t.FunctionDeclaration), ).toBe(false) expect( isExpressionType({ type: 'FunctionExpression' } as t.FunctionExpression), ).toBe(true) })
test('isIdentifierOf', () => { expect(isIdentifierOf({ type: 'Identifier', name: 'foo' }, 'foo')).toBe( true, ) expect( isIdentifierOf({ type: 'Identifier', name: 'bar' }, ['foo', 'bar']), ).toBe(true) })
test('isCallOf', () => { expect(isCallOf(null, 'foo')).toBe(false) expect(isCallOf({ type: 'ThisExpression' }, 'foo')).toBe(false) expect( isCallOf( { type: 'CallExpression', callee: { type: 'ThisExpression' }, arguments: [], }, 'foo', ), ).toBe(false) expect( isCallOf( { type: 'CallExpression', callee: { type: 'Identifier', name: 'foo' }, arguments: [], }, 'foo', ), ).toBe(true) expect( isCallOf( { type: 'CallExpression', callee: { type: 'Identifier', name: 'bar' }, arguments: [], }, ['foo', 'bar'], ), ).toBe(true) expect( isCallOf( { type: 'CallExpression', callee: { type: 'Identifier', name: 'bar' }, arguments: [], }, (n) => n.startsWith('b'), ), ).toBe(true) })
test('isTaggedFunctionCallOf', () => { expect(isTaggedFunctionCallOf(null, 'foo')).toBe(false) expect(isTaggedFunctionCallOf({ type: 'ThisExpression' }, 'foo')).toBe( false, ) expect( isTaggedFunctionCallOf( { type: 'TaggedTemplateExpression', tag: { type: 'Identifier', name: 'foo' }, quasi: { type: 'TemplateLiteral', quasis: [], expressions: [] }, }, 'foo', ), ).toBe(true) })
describe('isReferenced', () => { test('member', () => { const node = _parse<t.MemberExpression>('foo.bar', true) expect(isReferenced(node.object, node)).toBe(true) expect(isReferenced(node.property, node)).toBe(false)
const node2 = _parse<t.MemberExpression>('foo[bar]', true) expect(isReferenced(node2.property, node2)).toBe(true) })
test('class', () => { const node = _parse('class Foo {}').body[0] as t.ClassDeclaration expect(isReferenced(node.id!, node)).toBe(false)
const node2 = _parse<t.ClassExpression>('class extends Foo {}', true) expect(isReferenced(node2.superClass!, node2)) //.toBe(true) }) })
describe('isReferencedIdentifier', () => { test('no parent', () => { const identifier = _parse<t.Identifier>('foo', true) expect(isReferencedIdentifier(identifier, undefined, [])).toBe(true) })
test('arguments', () => { const ast = babelParse('function foo() { arguments }') const funcBody = (ast.body[0] as t.FunctionDeclaration).body const parent = funcBody.body[0] as t.ExpressionStatement const identifier = parent.expression as t.Identifier expect(isReferencedIdentifier(identifier, parent, [])).toBe(false) })
test('identifier is referenced in a variable declaration', () => { expect.assertions(1) const ast = babelParse(`const a = b`, 'ts') walkIdentifiers(ast.body[0], (node, parent, parentStack) => { if (node.type === 'Identifier' && node.name === 'b') { expect(isReferencedIdentifier(node, parent, parentStack)).toBe(true) } }) })
test('identifier is referenced in a function call', () => { expect.assertions(1) const ast = babelParse(`foo(bar)`, 'ts') walkIdentifiers(ast.body[0], (node, parent, parentStack) => { if (node.type === 'Identifier' && node.name === 'bar') { expect(isReferencedIdentifier(node, parent, parentStack)).toBe(true) } }) })
test('identifier is referenced in a member expression', () => { expect.assertions(1) const ast = babelParse(`obj.prop`, 'ts') walkIdentifiers(ast.body[0], (node, parent, parentStack) => { if (node.type === 'Identifier' && node.name === 'obj') { expect(isReferencedIdentifier(node, parent, parentStack)).toBe(true) } }) })
test('identifier is referenced in an assignment expression', () => { expect.assertions(1) const ast = babelParse(`a = b`, 'ts') walkIdentifiers(ast.body[0], (node, parent, parentStack) => { if (node.type === 'Identifier' && node.name === 'b') { expect(isReferencedIdentifier(node, parent, parentStack)).toBe(true) } }) })
test('identifier is referenced in a return statement', () => { expect.assertions(1) const ast = babelParse(`function foo() { return bar }`, 'ts') walkIdentifiers(ast.body[0], (node, parent, parentStack) => { if (node.type === 'Identifier' && node.name === 'bar') { expect(isReferencedIdentifier(node, parent, parentStack)).toBe(true) } }) })
test('identifier is referenced in a conditional expression', () => { expect.assertions(2) const ast = babelParse(`a ? b : c`, 'ts') walkIdentifiers(ast.body[0], (node, parent, parentStack) => { if ( node.type === 'Identifier' && (node.name === 'b' || node.name === 'c') ) { expect(isReferencedIdentifier(node, parent, parentStack)).toBe(true) } }) })
test('identifiers in function parameters should not be inferred as references', () => { expect.assertions(4) const ast = babelParse(`(({ title }) => [])`) walkIdentifiers( ast.body[0], (node, parent, parentStack, isReference) => { expect(isReference).toBe(false) expect(isReferencedIdentifier(node, parent, parentStack)).toBe(false) }, true, ) })
test('JSXNamespacedName should not be inferred as references', () => { const ast = babelParse(`const Comp = <svg:circle foo:bar="" />`, 'tsx') const onIdentifier = vi.fn() walkIdentifiers(ast.body[0], onIdentifier) expect(onIdentifier).toHaveBeenCalledTimes(0) }) })
describe('isInNewExpression', () => { test('identifier in NewExpression', () => { expect.assertions(1) const ast = babelParse(`new Foo()`, 'ts') walkIdentifiers(ast.body[0], (node, parent, parentStack) => { if (node.type === 'Identifier' && node.name === 'Foo') { expect(isInNewExpression(parentStack)).toBe(true) } }) })
test('identifier not in NewExpression', () => { expect.assertions(1) const ast = babelParse(`Foo()`, 'ts') walkIdentifiers(ast.body[0], (node, parent, parentStack) => { if (node.type === 'Identifier' && node.name === 'Foo') { expect(isInNewExpression(parentStack)).toBe(false) } }) })
test('identifier in nested MemberExpression within NewExpression', () => { expect.assertions(1) const ast = babelParse(`new Foo.Bar()`, 'ts') walkIdentifiers(ast.body[0], (node, parent, parentStack) => { if (node.type === 'Identifier' && node.name === 'Foo') { expect(isInNewExpression(parentStack)).toBe(true) } }) })
test('identifier in deeply nested MemberExpression within NewExpression', () => { expect.assertions(1) const ast = babelParse(`new Foo.Bar.Baz()`, 'ts') walkIdentifiers(ast.body[0], (node, parent, parentStack) => { if (node.type === 'Identifier' && node.name === 'Foo') { expect(isInNewExpression(parentStack)).toBe(true) } }) })
test('identifier in MemberExpression not in NewExpression', () => { expect.assertions(1) const ast = babelParse(`Foo.Bar()`, 'ts') walkIdentifiers(ast.body[0], (node, parent, parentStack) => { if (node.type === 'Identifier' && node.name === 'Foo') { expect(isInNewExpression(parentStack)).toBe(false) } }) }) })
describe('isInDestructureAssignment', () => { test('identifier in object destructure assignment', () => { expect.assertions(1) const ast = babelParse(`({ a: c } = obj)`, 'ts') walkIdentifiers(ast.body[0], (node, parent, parentStack) => { if (node.type === 'Identifier' && node.name === 'c') { expect(isInDestructureAssignment(parent!, parentStack)).toBe(true) } }) })
test('identifier in array destructure assignment', () => { expect.assertions(1) const ast = babelParse(`[a, b] = arr`, 'ts') walkIdentifiers(ast.body[0], (node, parent, parentStack) => { if (node.type === 'Identifier' && node.name === 'a') { expect(isInDestructureAssignment(parent!, parentStack)).toBe(true) } }) })
test('identifier in nested object destructure assignment', () => { expect.assertions(1) const ast = babelParse(`({ a: { b: c } } = obj)`, 'ts') walkIdentifiers(ast.body[0], (node, parent, parentStack) => { if (node.type === 'Identifier' && node.name === 'c') { expect(isInDestructureAssignment(parent!, parentStack)).toBe(true) } }) })
test('identifier in nested array destructure assignment', () => { expect.assertions(1) const ast = babelParse(`[[a], b] = arr`, 'ts') walkIdentifiers(ast.body[0], (node, parent, parentStack) => { if (node.type === 'Identifier' && node.name === 'a') { expect(isInDestructureAssignment(parent!, parentStack)).toBe(true) } }) })
test('identifier in mixed destructure assignment', () => { expect.assertions(1) const ast = babelParse(`({ a: [b, c] } = obj)`, 'ts') walkIdentifiers(ast.body[0], (node, parent, parentStack) => { if (node.type === 'Identifier' && node.name === 'b') { expect(isInDestructureAssignment(parent!, parentStack)).toBe(true) } }) })
test('identifier in destructure assignment with default value', () => { expect.assertions(1) const ast = babelParse(`({ a = 1 } = obj)`, 'ts') walkIdentifiers(ast.body[0], (node, parent, parentStack) => { if (node.type === 'Identifier' && node.name === 'a') { expect(isInDestructureAssignment(parent!, parentStack)).toBe(false) } }) }) })
test('isStaticProperty', () => { const ast = babelParse(`const obj = { a: 1, b() {} }`) const objDecl = ast.body[0] as t.VariableDeclaration const objInit = objDecl.declarations[0].init as t.ObjectExpression
objInit.properties.forEach((prop) => { expect(isStaticProperty(prop)).toBe(true) expect(isStaticPropertyKey((prop as t.ObjectProperty).key, prop)).toBe( true, ) }) })})