From 507ad63140a410b960752ab896372954acfaadb7 Mon Sep 17 00:00:00 2001 From: Siddhant N Trivedi Date: Mon, 22 Feb 2021 02:29:53 +0530 Subject: [PATCH] [babel 8] Remove the `Noop` node type (#12361) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Nicolò Ribaudo --- .../babel-generator/src/generators/base.ts | 2 - .../babel-generator/src/generators/flow.ts | 2 +- packages/babel-generator/src/printer.ts | 5 ++ packages/babel-types/src/definitions/core.ts | 51 ++++++++++++------- .../src/definitions/experimental.ts | 8 ++- packages/babel-types/src/definitions/misc.ts | 8 +-- .../babel-types/src/definitions/typescript.ts | 8 ++- scripts/integration-tests/e2e-jest.sh | 10 ++-- 8 files changed, 63 insertions(+), 31 deletions(-) diff --git a/packages/babel-generator/src/generators/base.ts b/packages/babel-generator/src/generators/base.ts index 3d876062501f..247dbd4a602e 100644 --- a/packages/babel-generator/src/generators/base.ts +++ b/packages/babel-generator/src/generators/base.ts @@ -46,8 +46,6 @@ export function BlockStatement(this: Printer, node: t.BlockStatement) { } } -export function Noop(this: Printer) {} - export function Directive(this: Printer, node: t.Directive) { this.print(node.value, node); this.semicolon(); diff --git a/packages/babel-generator/src/generators/flow.ts b/packages/babel-generator/src/generators/flow.ts index 6e68f3424e45..c59b2518389f 100644 --- a/packages/babel-generator/src/generators/flow.ts +++ b/packages/babel-generator/src/generators/flow.ts @@ -56,7 +56,7 @@ export function DeclareFunction( this.word("function"); this.space(); this.print(node.id, node); - // @ts-expect-error todo(flow->ts) typeAnnotation does not exist on Noop + // @ts-ignore TODO(Babel 8) Remove this comment, since we'll remove the Noop node this.print(node.id.typeAnnotation.typeAnnotation, node); if (node.predicate) { diff --git a/packages/babel-generator/src/printer.ts b/packages/babel-generator/src/printer.ts index f25a88e45384..176870cc2a6a 100644 --- a/packages/babel-generator/src/printer.ts +++ b/packages/babel-generator/src/printer.ts @@ -680,6 +680,11 @@ class Printer { // Expose the node type functions and helpers on the prototype for easy usage. Object.assign(Printer.prototype, generatorFunctions); +if (!process.env.BABEL_8_BREAKING) { + // @ts-ignore + Printer.prototype.Noop = function Noop(this: Printer) {}; +} + type GeneratorFunctions = typeof generatorFunctions; interface Printer extends GeneratorFunctions {} export default Printer; diff --git a/packages/babel-types/src/definitions/core.ts b/packages/babel-types/src/definitions/core.ts index fdede9c2e625..a14636cd0fed 100644 --- a/packages/babel-types/src/definitions/core.ts +++ b/packages/babel-types/src/definitions/core.ts @@ -373,15 +373,19 @@ export const functionCommon = { export const functionTypeAnnotationCommon = { returnType: { - validate: assertNodeType("TypeAnnotation", "TSTypeAnnotation", "Noop"), + validate: process.env.BABEL_8_BREAKING + ? assertNodeType("TypeAnnotation", "TSTypeAnnotation") + : assertNodeType("TypeAnnotation", "TSTypeAnnotation", "Noop"), optional: true, }, typeParameters: { - validate: assertNodeType( - "TypeParameterDeclaration", - "TSTypeParameterDeclaration", - "Noop", - ), + validate: process.env.BABEL_8_BREAKING + ? assertNodeType("TypeParameterDeclaration", "TSTypeParameterDeclaration") + : assertNodeType( + "TypeParameterDeclaration", + "TSTypeParameterDeclaration", + "Noop", + ), optional: true, }, }; @@ -455,8 +459,9 @@ defineType("FunctionExpression", { export const patternLikeCommon = { typeAnnotation: { - // TODO: @babel/plugin-transform-flow-comments puts a Noop here, is there a better way? - validate: assertNodeType("TypeAnnotation", "TSTypeAnnotation", "Noop"), + validate: process.env.BABEL_8_BREAKING + ? assertNodeType("TypeAnnotation", "TSTypeAnnotation") + : assertNodeType("TypeAnnotation", "TSTypeAnnotation", "Noop"), optional: true, }, decorators: { @@ -1272,11 +1277,16 @@ defineType("ClassExpression", { optional: true, }, typeParameters: { - validate: assertNodeType( - "TypeParameterDeclaration", - "TSTypeParameterDeclaration", - "Noop", - ), + validate: process.env.BABEL_8_BREAKING + ? assertNodeType( + "TypeParameterDeclaration", + "TSTypeParameterDeclaration", + ) + : assertNodeType( + "TypeParameterDeclaration", + "TSTypeParameterDeclaration", + "Noop", + ), optional: true, }, body: { @@ -1324,11 +1334,16 @@ defineType("ClassDeclaration", { validate: assertNodeType("Identifier"), }, typeParameters: { - validate: assertNodeType( - "TypeParameterDeclaration", - "TSTypeParameterDeclaration", - "Noop", - ), + validate: process.env.BABEL_8_BREAKING + ? assertNodeType( + "TypeParameterDeclaration", + "TSTypeParameterDeclaration", + ) + : assertNodeType( + "TypeParameterDeclaration", + "TSTypeParameterDeclaration", + "Noop", + ), optional: true, }, body: { diff --git a/packages/babel-types/src/definitions/experimental.ts b/packages/babel-types/src/definitions/experimental.ts index 4dd41acd134e..d463bef6c03a 100644 --- a/packages/babel-types/src/definitions/experimental.ts +++ b/packages/babel-types/src/definitions/experimental.ts @@ -60,7 +60,9 @@ defineType("ClassProperty", { optional: true, }, typeAnnotation: { - validate: assertNodeType("TypeAnnotation", "TSTypeAnnotation", "Noop"), + validate: process.env.BABEL_8_BREAKING + ? assertNodeType("TypeAnnotation", "TSTypeAnnotation") + : assertNodeType("TypeAnnotation", "TSTypeAnnotation", "Noop"), optional: true, }, decorators: { @@ -118,7 +120,9 @@ defineType("ClassPrivateProperty", { optional: true, }, typeAnnotation: { - validate: assertNodeType("TypeAnnotation", "TSTypeAnnotation", "Noop"), + validate: process.env.BABEL_8_BREAKING + ? assertNodeType("TypeAnnotation", "TSTypeAnnotation") + : assertNodeType("TypeAnnotation", "TSTypeAnnotation", "Noop"), optional: true, }, decorators: { diff --git a/packages/babel-types/src/definitions/misc.ts b/packages/babel-types/src/definitions/misc.ts index c33f18c73e27..7624706cd4e0 100644 --- a/packages/babel-types/src/definitions/misc.ts +++ b/packages/babel-types/src/definitions/misc.ts @@ -5,9 +5,11 @@ import defineType, { } from "./utils"; import { PLACEHOLDERS } from "./placeholders"; -defineType("Noop", { - visitor: [], -}); +if (!process.env.BABEL_8_BREAKING) { + defineType("Noop", { + visitor: [], + }); +} defineType("Placeholder", { visitor: [], diff --git a/packages/babel-types/src/definitions/typescript.ts b/packages/babel-types/src/definitions/typescript.ts index 7e55d71ac9e8..7a0df86891b6 100644 --- a/packages/babel-types/src/definitions/typescript.ts +++ b/packages/babel-types/src/definitions/typescript.ts @@ -20,11 +20,15 @@ const bool = assertValueType("boolean"); const tSFunctionTypeAnnotationCommon = { returnType: { - validate: assertNodeType("TSTypeAnnotation", "Noop"), + validate: process.env.BABEL_8_BREAKING + ? assertNodeType("TSTypeAnnotation") + : assertNodeType("TSTypeAnnotation", "Noop"), optional: true, }, typeParameters: { - validate: assertNodeType("TSTypeParameterDeclaration", "Noop"), + validate: process.env.BABEL_8_BREAKING + ? assertNodeType("TSTypeParameterDeclaration") + : assertNodeType("TSTypeParameterDeclaration", "Noop"), optional: true, }, }; diff --git a/scripts/integration-tests/e2e-jest.sh b/scripts/integration-tests/e2e-jest.sh index a44d5129a8f2..f00030838e41 100755 --- a/scripts/integration-tests/e2e-jest.sh +++ b/scripts/integration-tests/e2e-jest.sh @@ -37,14 +37,18 @@ python --version # TEST # #==============================================================================# +startLocalRegistry "$root"/verdaccio-config.yml +yarn install +yarn dedupe '@babel/*' + if [ "$BABEL_8_BREAKING" = true ] ; then # This option is removed in Babel 8 sed -i 's/allowDeclareFields: true,\?/\/* allowDeclareFields: true *\//g' babel.config.js + + # Jest depends on @types/babel__traverse for Babel 7, and they contain the removed Noop node + sed -i 's/t.Noop/any/g' node_modules/@types/babel__traverse/index.d.ts fi -startLocalRegistry "$root"/verdaccio-config.yml -yarn install -yarn dedupe '@babel/*' yarn build # The full test suite takes about 20mins on CircleCI. We run only a few of them