|
1 | 1 | import './ts3.6/babel-types-tests'; |
| 2 | +import traverse from "babel-traverse"; |
2 | 3 | import * as t from 'babel-types'; |
3 | 4 |
|
| 5 | +// Examples from https://github.com/thejameskyle/babel-handbook/blob/master/translations/en/plugin-handbook.md#babel-types |
| 6 | +declare const ast: t.Node; |
| 7 | + |
| 8 | +traverse(ast, { |
| 9 | + enter(path) { |
| 10 | + const node = path.node; |
| 11 | + if (t.isIdentifier(node, { name: "n" })) { |
| 12 | + node.name = "x"; |
| 13 | + } |
| 14 | + if (t.isFunctionExpression(node)) { |
| 15 | + node.params = [t.identifier('param')]; |
| 16 | + } |
| 17 | + } |
| 18 | +}); |
| 19 | + |
| 20 | +if (t.isBinaryExpression(ast)) { |
| 21 | + ast.left; |
| 22 | + ast.right; |
| 23 | + ast.operator; |
| 24 | +} |
| 25 | + |
| 26 | +t.assertBinaryExpression(ast); |
| 27 | +t.assertBinaryExpression(ast, { operator: "*" }); |
| 28 | + |
| 29 | +const exp: t.Expression = t.nullLiteral(); |
| 30 | + |
| 31 | +// React examples: |
| 32 | +// https://github.com/babel/babel/blob/4e50b2d9d9c376cee7a2cbf56553fe5b982ea53c/packages/babel-plugin-transform-react-inline-elements/src/index.js#L61 |
| 33 | +traverse(ast, { |
| 34 | + JSXElement(path, file) { |
| 35 | + const { node } = path; |
| 36 | + const open = node.openingElement; |
| 37 | + |
| 38 | + // init |
| 39 | + const type = open.name; |
| 40 | + |
| 41 | + let newType: t.StringLiteral; |
| 42 | + if (t.isJSXIdentifier(type) && t.react.isCompatTag(type.name)) { |
| 43 | + newType = t.stringLiteral(type.name); |
| 44 | + } |
| 45 | + |
| 46 | + const args: any[] = []; |
| 47 | + if (node.children.length) { |
| 48 | + const children = t.react.buildChildren(node); |
| 49 | + args.push( |
| 50 | + t.unaryExpression("void", t.numericLiteral(0), true), |
| 51 | + ...children, |
| 52 | + ); |
| 53 | + } |
| 54 | + } |
| 55 | +}); |
| 56 | + |
| 57 | +// Node type checks |
| 58 | +t.isIdentifier(t.identifier("id")); |
| 59 | +t.isIdentifier(exp); |
| 60 | +t.isIdentifier(null); |
| 61 | +t.isIdentifier(undefined); |
| 62 | + |
| 63 | +// TypeScript Types |
| 64 | +// TODO: Test all variants of these functions' signatures |
| 65 | + |
| 66 | +const id = t.identifier("id"); |
| 67 | +const tparam = t.typeParameterDeclaration([id]); |
| 68 | + |
| 69 | +const any = t.TSAnyKeyword(); |
| 70 | +t.TSArrayType(any); |
| 71 | +t.TSAsExpression(exp, any); |
| 72 | +t.TSBooleanKeyword(); |
| 73 | +t.TSCallSignatureDeclaration(tparam); |
| 74 | +t.TSConstructSignatureDeclaration(); |
| 75 | +t.TSConstructorType(); |
| 76 | +t.TSDeclareFunction(id, t.noop(), [id], t.noop()); |
| 77 | +t.TSDeclareMethod([t.decorator(exp)], exp, t.noop(), [id]); |
| 78 | +t.TSEnumDeclaration(id, [t.TSEnumMember(id)]); |
| 79 | +t.TSEnumMember(id); |
| 80 | +const expo = t.TSExportAssignment(exp); |
| 81 | +t.TSExpressionWithTypeArguments(id); |
| 82 | +const ext = t.TSExternalModuleReference(t.stringLiteral()); |
| 83 | +t.TSFunctionType(); |
| 84 | +t.TSImportEqualsDeclaration(id, ext); |
| 85 | +const sig = t.TSIndexSignature([id]); |
| 86 | +t.TSIndexedAccessType(any, any); |
| 87 | +t.TSInterfaceBody([sig]); |
| 88 | +t.TSInterfaceDeclaration(id, null, null, t.TSInterfaceBody([sig])); |
| 89 | +t.TSIntersectionType([any]); |
| 90 | +t.TSLiteralType(t.stringLiteral("a")); |
| 91 | +t.TSMappedType(t.typeParameter()); |
| 92 | +t.TSMethodSignature(id); |
| 93 | +const block = t.TSModuleBlock([expo]); |
| 94 | +t.TSModuleDeclaration(id, block); |
| 95 | +t.TSNamespaceExportDeclaration(id); |
| 96 | +t.TSNeverKeyword(); |
| 97 | +t.TSNonNullExpression(exp); |
| 98 | +t.TSNullKeyword(); |
| 99 | +t.TSNumberKeyword(); |
| 100 | +t.TSObjectKeyword(); |
| 101 | +t.TSParameterProperty(id); |
| 102 | +t.TSParenthesizedType(any); |
| 103 | +t.TSPropertySignature(id); |
| 104 | +t.TSQualifiedName(id, id); |
| 105 | +t.TSStringKeyword(); |
| 106 | +t.TSSymbolKeyword(); |
| 107 | +t.TSThisType(); |
| 108 | +t.TSTupleType([any, any]); |
| 109 | +t.TSTypeAliasDeclaration(id, tparam, any); |
| 110 | +t.TSTypeAnnotation(any); |
| 111 | +t.TSTypeAssertion(any, exp); |
| 112 | +t.TSTypeLiteral([sig]); |
| 113 | +t.TSTypeOperator(any); |
| 114 | +const param = t.TSTypeParameter(); |
| 115 | +t.TSTypeParameterDeclaration([param]); |
| 116 | +t.TSTypeParameterInstantiation([any]); |
| 117 | +t.TSTypePredicate(id, t.TSTypeAnnotation(any)); |
| 118 | +t.TSTypeQuery(id); |
| 119 | +t.TSTypeReference(id, t.TSTypeParameterInstantiation([any])); |
| 120 | +t.TSUndefinedKeyword(); |
| 121 | +t.TSUnionType([any]); |
| 122 | +t.TSVoidKeyword(); |
| 123 | + |
4 | 124 | function testArrayExpression(node: t.Node) { |
5 | 125 | t.assertArrayExpression(node); |
6 | 126 | node; // $ExpectType ArrayExpression |
|
0 commit comments