Skip to content

Commit

Permalink
fix(@babel/traverse): Update to v7.0 stable release (DefinitelyTyped#…
Browse files Browse the repository at this point in the history
…47183)

* fix(@babel/traverse): Update to v7.0 stable release

* feat(@babel/traverse): Add top‑level `visitors` export
  • Loading branch information
ExE-Boss authored Sep 8, 2020
1 parent 751f0a5 commit a6e9b46
Show file tree
Hide file tree
Showing 3 changed files with 770 additions and 418 deletions.
93 changes: 82 additions & 11 deletions types/babel__traverse/babel__traverse-tests.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import traverse, { Visitor, NodePath, Hub } from '@babel/traverse';
import traverse, { Hub, NodePath, Visitor, visitors } from '@babel/traverse';
import * as t from '@babel/types';

// Examples from: https://github.com/thejameskyle/babel-handbook/blob/master/translations/en/plugin-handbook.md
const MyVisitor: Visitor = {
Identifier: {
enter(path) {
path.type; // $ExpectType "Identifier"
const x: NodePath<t.Identifier> = path;
console.log('Entered!');
},
exit(path) {
path.type; // $ExpectType "Identifier"
const x: NodePath<t.Identifier> = path;
console.log('Exited!');
},
Expand All @@ -17,6 +19,7 @@ const MyVisitor: Visitor = {

const MyVisitor2: Visitor = {
Identifier(path) {
path.type; // $ExpectType "Identifier"
console.log('Visiting: ' + path.node.name);
},
};
Expand All @@ -26,6 +29,10 @@ declare const ast: t.Node;

traverse(ast, {
enter(path) {
let actualType = path.type;
const expectedType: t.Node['type'] = actualType;
actualType = ast.type;

const node = path.node;
if (t.isIdentifier(node) && node.name === 'n') {
node.name = 'x';
Expand All @@ -37,27 +44,31 @@ traverse(ast, {

const v1: Visitor = {
BinaryExpression(path) {
path.type; // $ExpectType "BinaryExpression"

if (t.isIdentifier(path.node.left)) {
// ...
}
path.replaceWith(t.binaryExpression('**', path.node.left, t.numericLiteral(2))) as [
NodePath<t.BinaryExpression>,
];
// $ExpectType [NodePath<BinaryExpression>]
path.replaceWith(t.binaryExpression('**', path.node.left, t.numericLiteral(2)));
path.parentPath.replaceWith(
t.expressionStatement(t.stringLiteral("Anyway the wind blows, doesn't really matter to me, to me.")),
);
path.replaceInline(t.binaryExpression('**', path.node.left, t.numericLiteral(2))) as [
NodePath<t.BinaryExpression>,
];
// $ExpectType [NodePath<BinaryExpression>]
path.replaceInline(t.binaryExpression('**', path.node.left, t.numericLiteral(2)));
// $ExpectType [NodePath<Node>]
path.replaceWithSourceString('3 * 4') as [NodePath];
// $ExpectType [NodePath<BinaryExpression>, NodePath<ExpressionStatement>]
path.replaceInline([
t.binaryExpression('**', path.node.left, t.numericLiteral(2)),
t.expressionStatement(t.stringLiteral("Anyway the wind blows, doesn't really matter to me, to me.")),
]) as [NodePath<t.BinaryExpression>, NodePath<t.ExpressionStatement>];
] as const);
path.parentPath.remove();
},

Identifier(path) {
path.type; // $ExpectType "Identifier"

if (path.isReferencedIdentifier()) {
// ...
}
Expand All @@ -67,14 +78,19 @@ const v1: Visitor = {
},

ReturnStatement(path) {
path.type; // $ExpectType "ReturnStatement"

// $ExpectType [NodePath<ExpressionStatement>, NodePath<ExpressionStatement>, NodePath<ExpressionStatement>]
path.replaceWithMultiple([
t.expressionStatement(t.stringLiteral('Is this the real life?')),
t.expressionStatement(t.stringLiteral('Is this just fantasy?')),
t.expressionStatement(t.stringLiteral('(Enjoy singing the rest of the song in your head)')),
]) as [NodePath<t.ExpressionStatement>, NodePath<t.ExpressionStatement>, NodePath<t.ExpressionStatement>];
] as const);
},

FunctionDeclaration(path, state) {
path.type; // $ExpectType "FunctionDeclaration"

path.replaceWithSourceString(`function add(a, b) {
return a + b;
}`);
Expand Down Expand Up @@ -114,6 +130,8 @@ const v1: Visitor = {
path.unshiftContainer('returnType', t.stringLiteral('hello'));
},
ExportDefaultDeclaration(path) {
path.type; // $ExpectType "ExportDefaultDeclaration"

{
const [stringPath, booleanPath] = path.replaceWithMultiple([
t.stringLiteral('hello'),
Expand Down Expand Up @@ -146,6 +164,8 @@ const v1: Visitor = {
}
},
Program(path) {
path.type; // $ExpectType "Program"

{
const [newPath] = path.unshiftContainer('body', t.stringLiteral('hello'));
// $ExpectType NodePath<StringLiteral>
Expand Down Expand Up @@ -202,6 +222,8 @@ const v1: Visitor = {
// Binding.kind
const BindingKindTest: Visitor = {
Identifier(path) {
path.type; // $ExpectType "Identifier"

const kind = path.scope.getBinding('str')!.kind;
kind === 'module';
kind === 'const';
Expand All @@ -218,31 +240,45 @@ interface SomeVisitorState {

const VisitorStateTest: Visitor<SomeVisitorState> = {
enter(path, state) {
let actualType = path.type;
const expectedType: t.Node['type'] = actualType;
actualType = ast.type;

// $ExpectType SomeVisitorState
state;
// $ExpectType SomeVisitorState
this;
},
exit(path, state) {
let actualType = path.type;
const expectedType: t.Node['type'] = actualType;
actualType = ast.type;

// $ExpectType SomeVisitorState
state;
// $ExpectType SomeVisitorState
this;
},
Identifier(path, state) {
path.type; // $ExpectType "Identifier"

// $ExpectType SomeVisitorState
state;
// $ExpectType SomeVisitorState
this;
},
FunctionDeclaration: {
enter(path, state) {
path.type; // $ExpectType "FunctionDeclaration"

// $ExpectType SomeVisitorState
state;
// $ExpectType SomeVisitorState
this;
},
exit(path, state) {
path.type; // $ExpectType "FunctionDeclaration"

// $ExpectType SomeVisitorState
state;
// $ExpectType SomeVisitorState
Expand All @@ -258,8 +294,43 @@ const VisitorAliasTest: Visitor = {
Expression() {},
};

const hub = new Hub('file', { options: '' });
const hub = new Hub();
// $ExpectType string | undefined
hub.getCode();

traverse.visitors.merge([{ Expression(path) { } }, { Expression(path) { } }]);
declare const astExpression: t.Expression;

traverse.visitors; // $ExpectType typeof visitors

// $ExpectType Visitor<unknown>
visitors.merge([
{
Expression(path) {
let actualType = path.type;
const expectedType: t.Expression['type'] = actualType;
actualType = astExpression.type;
},
},
{
Expression(path) {
let actualType = path.type;
const expectedType: t.Expression['type'] = actualType;
actualType = astExpression.type;
},
},
]);

function testNullishPath(
optionalPath: NodePath<t.Node | null>,
nullPath: NodePath<null>,
undefinedPath: NodePath<undefined>,
unknownPath: NodePath<unknown>,
) {
nullPath.type; // $ExpectType undefined
undefinedPath.type; // $ExpectType undefined
unknownPath.type; // $ExpectType string | undefined

let actualType = optionalPath.type;
const expectedType: t.Node['type'] | undefined = actualType;
actualType = expectedType;
}
Loading

0 comments on commit a6e9b46

Please sign in to comment.