From 4833dad4e2827019b9f6cde15411d98b2ab4c2e3 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Fri, 17 Jul 2015 00:58:08 +0100 Subject: [PATCH] fix comments containg `@flow` being completely removed from output rather than just the specific directive - fixes #2022 --- CHANGELOG.md | 5 +++++ .../src/transformation/transformers/other/flow.js | 10 ++++++++-- .../transformation/flow/strip-directive/actual.js | 5 +++++ .../transformation/flow/strip-directive/expected.js | 5 +++++ 4 files changed, 23 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b751d9fca6..b966f2269a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,11 @@ _Note: Gaps between patch versions are faulty, broken or test releases._ See [CHANGELOG - 6to5](CHANGELOG-6to5.md) for the pre-4.0.0 version changelog. +## 5.7.4 + + * **Bug Fix** + * Fix comments containg `@flow` being completely removed from output rather than just the specific directive. + ## 5.7.3 * **Bug Fix** diff --git a/packages/babel/src/transformation/transformers/other/flow.js b/packages/babel/src/transformation/transformers/other/flow.js index 0cd45bbb54..dd4c84a0d0 100644 --- a/packages/babel/src/transformation/transformers/other/flow.js +++ b/packages/babel/src/transformation/transformers/other/flow.js @@ -4,6 +4,8 @@ export var metadata = { group: "builtin-trailing" }; +const FLOW_DIRECTIVE = "@flow"; + /** * [Please add a description.] */ @@ -16,8 +18,12 @@ export var visitor = { Program(node, parent, scope, file) { for (var comment of (file.ast.comments: Array)) { - if (comment.value.indexOf("@flow") >= 0) { - comment._displayed = true; + if (comment.value.indexOf(FLOW_DIRECTIVE) >= 0) { + // remove flow directive + comment.value = comment.value.replace(FLOW_DIRECTIVE, ""); + + // remove the comment completely if it only consists of whitespace and/or stars + if (!comment.value.replace(/\*/g, "").trim()) comment._displayed = true; } } }, diff --git a/packages/babel/test/fixtures/transformation/flow/strip-directive/actual.js b/packages/babel/test/fixtures/transformation/flow/strip-directive/actual.js index 575f234bbc..6fa81291fd 100644 --- a/packages/babel/test/fixtures/transformation/flow/strip-directive/actual.js +++ b/packages/babel/test/fixtures/transformation/flow/strip-directive/actual.js @@ -6,4 +6,9 @@ * @flow */ +/** + * @providesModule Foo + * @flow + */ + foo(); diff --git a/packages/babel/test/fixtures/transformation/flow/strip-directive/expected.js b/packages/babel/test/fixtures/transformation/flow/strip-directive/expected.js index a280f9a5cc..4bae7db107 100644 --- a/packages/babel/test/fixtures/transformation/flow/strip-directive/expected.js +++ b/packages/babel/test/fixtures/transformation/flow/strip-directive/expected.js @@ -1 +1,6 @@ +/** + * @providesModule Foo + * + */ + foo();