From 78544417fcca3380569b111a1b439167c7268a97 Mon Sep 17 00:00:00 2001 From: Justin Ridgewell Date: Thu, 20 Jul 2017 11:36:13 -0400 Subject: [PATCH] Remove noop (#5970) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It’s ugly, but it gets the job done. And it unblocks my babel-type changes. --- .../src/index.js | 103 ++++++++++++++---- .../class-prop-values-2/expected.js | 3 +- .../class-prop-values/expected.js | 3 +- .../def-site-variance/expected.js | 3 +- .../flow-comments/import-export/actual.js | 1 + .../flow-comments/import-export/expected.js | 4 +- .../flow-comments/import-type-alias/actual.js | 5 +- .../import-type-alias/expected.js | 6 +- .../flow-comments/type-alias/expected.js | 3 +- 9 files changed, 94 insertions(+), 37 deletions(-) diff --git a/packages/babel-plugin-transform-flow-comments/src/index.js b/packages/babel-plugin-transform-flow-comments/src/index.js index 21e383af66..3528072f3b 100644 --- a/packages/babel-plugin-transform-flow-comments/src/index.js +++ b/packages/babel-plugin-transform-flow-comments/src/index.js @@ -2,8 +2,14 @@ import syntaxFlow from "babel-plugin-syntax-flow"; export default function({ types: t }) { function wrapInFlowComment(path, parent) { - path.addComment("trailing", generateComment(path, parent)); - path.replaceWith(t.noop()); + let attach = path.getPrevSibling(); + let where = "trailing"; + if (!attach.node) { + attach = path.parentPath; + where = "inner"; + } + attach.addComment(where, generateComment(path, parent)); + path.remove(); } function generateComment(path, parent) { @@ -30,40 +36,79 @@ export default function({ types: t }) { // support function a(b?) {} Identifier(path) { - const { node } = path; - if (!node.optional || node.typeAnnotation) { + if (path.parentPath.isFlow()) { return; } - path.addComment("trailing", ":: ?"); + + const { node } = path; + if (node.typeAnnotation) { + const typeAnnotation = path.get("typeAnnotation"); + path.addComment("trailing", generateComment(typeAnnotation, node)); + typeAnnotation.remove(); + if (node.optional) { + node.optional = false; + } + } else if (node.optional) { + path.addComment("trailing", ":: ?"); + node.optional = false; + } }, AssignmentPattern: { exit({ node }) { - node.left.optional = false; + const { left } = node; + if (left.optional) { + left.optional = false; + } }, }, // strip optional property from function params - facebook/fbjs#17 - Function: { - exit({ node }) { - node.params.forEach(param => (param.optional = false)); - }, + Function(path) { + if (path.isDeclareFunction()) return; + const { node } = path; + if (node.returnType) { + const returnType = path.get("returnType"); + const typeAnnotation = returnType.get("typeAnnotation"); + const block = path.get("body"); + block.addComment( + "leading", + generateComment(returnType, typeAnnotation.node), + ); + returnType.remove(); + } + if (node.typeParameters) { + const typeParameters = path.get("typeParameters"); + const id = path.get("id"); + id.addComment( + "trailing", + generateComment(typeParameters, typeParameters.node), + ); + typeParameters.remove(); + } }, // support for `class X { foo: string }` - #4622 ClassProperty(path) { const { node, parent } = path; - if (!node.value) wrapInFlowComment(path, parent); + if (!node.value) { + wrapInFlowComment(path, parent); + } else if (node.typeAnnotation) { + const typeAnnotation = path.get("typeAnnotation"); + path + .get("key") + .addComment( + "trailing", + generateComment(typeAnnotation, typeAnnotation.node), + ); + typeAnnotation.remove(); + } }, // support `export type a = {}` - #8 Error: You passed path.replaceWith() a falsy node - "ExportNamedDeclaration|Flow"(path) { + ExportNamedDeclaration(path) { const { node, parent } = path; - if ( - t.isExportNamedDeclaration(node) && - node.exportKind !== "type" && - !t.isFlow(node.declaration) - ) { + if (node.exportKind !== "type" && !t.isFlow(node.declaration)) { return; } wrapInFlowComment(path, parent); @@ -72,15 +117,29 @@ export default function({ types: t }) { // support `import type A` and `import typeof A` #10 ImportDeclaration(path) { const { node, parent } = path; - if ( - t.isImportDeclaration(node) && - node.importKind !== "type" && - node.importKind !== "typeof" - ) { + if (node.importKind !== "type" && node.importKind !== "typeof") { return; } wrapInFlowComment(path, parent); }, + + Flow(path) { + const { parent } = path; + wrapInFlowComment(path, parent); + }, + + Class(path) { + const { node } = path; + if (node.typeParameters) { + const typeParameters = path.get("typeParameters"); + const block = path.get("body"); + block.addComment( + "leading", + generateComment(typeParameters, typeParameters.node), + ); + typeParameters.remove(); + } + }, }, }; } diff --git a/packages/babel-plugin-transform-flow-comments/test/fixtures/flow-comments/class-prop-values-2/expected.js b/packages/babel-plugin-transform-flow-comments/test/fixtures/flow-comments/class-prop-values-2/expected.js index 0c849b1a8f..6144dc648a 100644 --- a/packages/babel-plugin-transform-flow-comments/test/fixtures/flow-comments/class-prop-values-2/expected.js +++ b/packages/babel-plugin-transform-flow-comments/test/fixtures/flow-comments/class-prop-values-2/expected.js @@ -2,5 +2,4 @@ class X { /*:: a: number*/ /*:: b: ?string*/ - -} \ No newline at end of file +} diff --git a/packages/babel-plugin-transform-flow-comments/test/fixtures/flow-comments/class-prop-values/expected.js b/packages/babel-plugin-transform-flow-comments/test/fixtures/flow-comments/class-prop-values/expected.js index 72b72666e4..9ea0973c87 100644 --- a/packages/babel-plugin-transform-flow-comments/test/fixtures/flow-comments/class-prop-values/expected.js +++ b/packages/babel-plugin-transform-flow-comments/test/fixtures/flow-comments/class-prop-values/expected.js @@ -3,7 +3,6 @@ class X { bar /*: number*/ = 3; - /*:: baz: ?string*/ -} \ No newline at end of file +} diff --git a/packages/babel-plugin-transform-flow-comments/test/fixtures/flow-comments/def-site-variance/expected.js b/packages/babel-plugin-transform-flow-comments/test/fixtures/flow-comments/def-site-variance/expected.js index f40a4c02e4..2eff60881f 100644 --- a/packages/babel-plugin-transform-flow-comments/test/fixtures/flow-comments/def-site-variance/expected.js +++ b/packages/babel-plugin-transform-flow-comments/test/fixtures/flow-comments/def-site-variance/expected.js @@ -5,5 +5,4 @@ class C function f /*:: <+T, -U>*/ () {} - -/*:: type T<+T, -U> = {};*/ \ No newline at end of file +/*:: type T<+T, -U> = {};*/ diff --git a/packages/babel-plugin-transform-flow-comments/test/fixtures/flow-comments/import-export/actual.js b/packages/babel-plugin-transform-flow-comments/test/fixtures/flow-comments/import-export/actual.js index 8da713ff61..990b2b8b8c 100644 --- a/packages/babel-plugin-transform-flow-comments/test/fixtures/flow-comments/import-export/actual.js +++ b/packages/babel-plugin-transform-flow-comments/test/fixtures/flow-comments/import-export/actual.js @@ -1,3 +1,4 @@ +export type { A } from './types'; import lib from 'library'; export { foo } from 'foo'; export type { B, C } from './types'; diff --git a/packages/babel-plugin-transform-flow-comments/test/fixtures/flow-comments/import-export/expected.js b/packages/babel-plugin-transform-flow-comments/test/fixtures/flow-comments/import-export/expected.js index a42ac1b8f8..e8aabc9cec 100644 --- a/packages/babel-plugin-transform-flow-comments/test/fixtures/flow-comments/import-export/expected.js +++ b/packages/babel-plugin-transform-flow-comments/test/fixtures/flow-comments/import-export/expected.js @@ -1,4 +1,4 @@ +/*:: export type { A } from './types';*/ import lib from 'library'; export { foo } from 'foo'; - -/*:: export type { B, C } from './types';*/ \ No newline at end of file +/*:: export type { B, C } from './types';*/ diff --git a/packages/babel-plugin-transform-flow-comments/test/fixtures/flow-comments/import-type-alias/actual.js b/packages/babel-plugin-transform-flow-comments/test/fixtures/flow-comments/import-type-alias/actual.js index 11934d8c50..2084611789 100644 --- a/packages/babel-plugin-transform-flow-comments/test/fixtures/flow-comments/import-type-alias/actual.js +++ b/packages/babel-plugin-transform-flow-comments/test/fixtures/flow-comments/import-type-alias/actual.js @@ -1,3 +1,4 @@ -import lib from 'library'; import type A, { B, C } from './types'; -import typeof D, { E, F } from './types'; +import lib from 'library'; +import type D from './types'; +import typeof E, { F, G } from './types'; diff --git a/packages/babel-plugin-transform-flow-comments/test/fixtures/flow-comments/import-type-alias/expected.js b/packages/babel-plugin-transform-flow-comments/test/fixtures/flow-comments/import-type-alias/expected.js index 1d17f3867d..e665cdfa62 100644 --- a/packages/babel-plugin-transform-flow-comments/test/fixtures/flow-comments/import-type-alias/expected.js +++ b/packages/babel-plugin-transform-flow-comments/test/fixtures/flow-comments/import-type-alias/expected.js @@ -1,5 +1,5 @@ -import lib from 'library'; - /*:: import type A, { B, C } from './types';*/ +import lib from 'library'; +/*:: import type D from './types';*/ -/*:: import typeof D, { E, F } from './types';*/ \ No newline at end of file +/*:: import typeof E, { F, G } from './types';*/ diff --git a/packages/babel-plugin-transform-flow-comments/test/fixtures/flow-comments/type-alias/expected.js b/packages/babel-plugin-transform-flow-comments/test/fixtures/flow-comments/type-alias/expected.js index 91daa4e7eb..5939e876d2 100644 --- a/packages/babel-plugin-transform-flow-comments/test/fixtures/flow-comments/type-alias/expected.js +++ b/packages/babel-plugin-transform-flow-comments/test/fixtures/flow-comments/type-alias/expected.js @@ -1,5 +1,4 @@ function a() {} - /*:: type A = number;*/ /*:: type B = { @@ -14,4 +13,4 @@ function a() {} /*:: type overloads = & ((x: string) => number) & ((x: number) => string) -;*/ \ No newline at end of file +;*/