mirror of
https://github.com/FlipsideCrypto/DefinitelyTyped.git
synced 2026-02-06 10:56:53 +00:00
fix(@babel/traverse): Update to v7.0 stable release (#47183)
* fix(@babel/traverse): Update to v7.0 stable release * feat(@babel/traverse): Add top‑level `visitors` export
This commit is contained in:
parent
751f0a5544
commit
a6e9b46ee3
@ -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!');
|
||||
},
|
||||
@ -17,6 +19,7 @@ const MyVisitor: Visitor = {
|
||||
|
||||
const MyVisitor2: Visitor = {
|
||||
Identifier(path) {
|
||||
path.type; // $ExpectType "Identifier"
|
||||
console.log('Visiting: ' + path.node.name);
|
||||
},
|
||||
};
|
||||
@ -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';
|
||||
@ -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()) {
|
||||
// ...
|
||||
}
|
||||
@ -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;
|
||||
}`);
|
||||
@ -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'),
|
||||
@ -146,6 +164,8 @@ const v1: Visitor = {
|
||||
}
|
||||
},
|
||||
Program(path) {
|
||||
path.type; // $ExpectType "Program"
|
||||
|
||||
{
|
||||
const [newPath] = path.unshiftContainer('body', t.stringLiteral('hello'));
|
||||
// $ExpectType NodePath<StringLiteral>
|
||||
@ -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';
|
||||
@ -218,18 +240,28 @@ 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
|
||||
@ -237,12 +269,16 @@ const VisitorStateTest: Visitor<SomeVisitorState> = {
|
||||
},
|
||||
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
|
||||
@ -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;
|
||||
}
|
||||
|
||||
1088
types/babel__traverse/index.d.ts
vendored
1088
types/babel__traverse/index.d.ts
vendored
File diff suppressed because it is too large
Load Diff
@ -1,6 +1 @@
|
||||
{
|
||||
"extends": "dtslint/dt.json",
|
||||
"rules": {
|
||||
"no-unnecessary-type-assertion": false
|
||||
}
|
||||
}
|
||||
{ "extends": "dtslint/dt.json" }
|
||||
|
||||
Loading…
Reference in New Issue
Block a user