From 4fccaa038eee7bb2d2a16eb6f4df79614d95e0fd Mon Sep 17 00:00:00 2001 From: Jan Potoms <2109932+Janpot@users.noreply.github.com> Date: Thu, 20 Feb 2020 19:25:41 +0100 Subject: [PATCH] Add missing return types for babel__traverse unshiftContainer and replaceWithMultiple (#42407) * Add missing return types * try this * Fix test * babel core depends on babel traverse * increase ts version on plugin-macros * this should be the last one depending on babel/traverse * try a rerun * trigger a rerun --- types/babel-plugin-macros/index.d.ts | 3 +- types/babel__core/index.d.ts | 2 +- types/babel__standalone/index.d.ts | 2 +- .../babel__traverse/babel__traverse-tests.ts | 49 +++++++++++++++++++ types/babel__traverse/index.d.ts | 8 +-- 5 files changed, 58 insertions(+), 6 deletions(-) diff --git a/types/babel-plugin-macros/index.d.ts b/types/babel-plugin-macros/index.d.ts index 2db0ea45b8..4060dca09f 100644 --- a/types/babel-plugin-macros/index.d.ts +++ b/types/babel-plugin-macros/index.d.ts @@ -3,7 +3,8 @@ // Definitions by: Billy Kwok // Jake Runzer // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// TypeScript Version: 2.9 +// Minimum TypeScript Version: 3.4 + import * as Babel from '@babel/core'; export interface References { diff --git a/types/babel__core/index.d.ts b/types/babel__core/index.d.ts index 57f2d3175f..4288b74982 100644 --- a/types/babel__core/index.d.ts +++ b/types/babel__core/index.d.ts @@ -5,7 +5,7 @@ // Melvin Groenhoff // Jessica Franco // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// TypeScript Version: 2.9 +// Minimum TypeScript Version: 3.4 import { GeneratorOptions } from "@babel/generator"; import traverse, { Visitor, NodePath } from "@babel/traverse"; diff --git a/types/babel__standalone/index.d.ts b/types/babel__standalone/index.d.ts index dc2716d230..ae77350ff6 100644 --- a/types/babel__standalone/index.d.ts +++ b/types/babel__standalone/index.d.ts @@ -2,7 +2,7 @@ // Project: https://github.com/babel/babel/tree/master/packages/babel-standalone // Definitions by: Matheus Goncalves da Silva // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// TypeScript Version: 2.9 +// Minimum TypeScript Version: 3.4 import { TransformOptions, types, FileResultCallback } from '@babel/core'; diff --git a/types/babel__traverse/babel__traverse-tests.ts b/types/babel__traverse/babel__traverse-tests.ts index fc02a672bc..9bc410d0ae 100644 --- a/types/babel__traverse/babel__traverse-tests.ts +++ b/types/babel__traverse/babel__traverse-tests.ts @@ -99,6 +99,55 @@ const v1: Visitor = { path.scope.rename("n", "x"); path.scope.rename("n"); + }, + ExportDefaultDeclaration(path) { + { + const [stringPath, booleanPath] = path.replaceWithMultiple([ + t.stringLiteral('hello'), + t.booleanLiteral(false) + ]); + // $ExpectType NodePath + stringPath; + // $ExpectType NodePath + booleanPath; + } + { + const [stringPath, booleanPath] = path.replaceWithMultiple<[t.StringLiteral, t.BooleanLiteral]>([ + t.stringLiteral('hello'), + t.booleanLiteral(false) + ]); + // $ExpectType NodePath + stringPath; + // $ExpectType NodePath + booleanPath; + } + }, + Program(path) { + { + const [newPath] = path.unshiftContainer('body', t.stringLiteral('hello')); + // $ExpectType NodePath + newPath; + } + { + const [stringPath, booleanPath] = path.unshiftContainer('body', [ + t.stringLiteral('hello'), + t.booleanLiteral(false) + ]); + // $ExpectType NodePath + stringPath; + // $ExpectType NodePath + booleanPath; + } + { + const [stringPath, booleanPath] = path.unshiftContainer<[t.StringLiteral, t.BooleanLiteral]>('body', [ + t.stringLiteral('hello'), + t.booleanLiteral(false) + ]); + // $ExpectType NodePath + stringPath; + // $ExpectType NodePath + booleanPath; + } } }; diff --git a/types/babel__traverse/index.d.ts b/types/babel__traverse/index.d.ts index ae1af9966b..a3537b9e3f 100644 --- a/types/babel__traverse/index.d.ts +++ b/types/babel__traverse/index.d.ts @@ -6,7 +6,7 @@ // Melvin Groenhoff // Dean L. // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// TypeScript Version: 2.9 +// Minimum TypeScript Version: 3.4 import * as t from "@babel/types"; @@ -171,6 +171,8 @@ export interface VisitNodeObject { exit?: VisitNodeFunction; } +export type NodePaths = T extends Node[] ? { [K in keyof T]: NodePath } : [NodePath]; + export class NodePath { constructor(hub: Hub, parent: Node); parent: Node; @@ -272,7 +274,7 @@ export class NodePath { * - Insert the provided nodes after the current node. * - Remove the current node. */ - replaceWithMultiple(nodes: Node[]): void; + replaceWithMultiple(nodes: Nodes): NodePaths; /** * Parse a string as an expression and replace the current node with the result. @@ -433,7 +435,7 @@ export class NodePath { * @param listKey - The key at which the child nodes are stored (usually body). * @param nodes - the nodes to insert. */ - unshiftContainer(listKey: string, nodes: Node | Node[]): void; + unshiftContainer(listKey: string, nodes: Nodes): NodePaths; /** * Insert child nodes at the end of the current node.