From 9dfc985164c505e4ae0c87935cefff3ce467814c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Zugmeyer?= Date: Fri, 18 Aug 2017 23:38:02 +0200 Subject: [PATCH] fix compatibility with babel 6.17.2 Since PR https://github.com/babel/babylon/pull/478 , some comments are now attached to the previous node. This is the case for example when the previous statement doesn't finish with a semi. Let's search for the comment in previous node trailing comments too. --- .../expected.js | 16 ++++++++++ .../input.js | 13 ++++++++ src/index.js | 31 ++++++++++++------- 3 files changed, 49 insertions(+), 11 deletions(-) create mode 100644 src/__tests__/fixtures/previous_statement_trailing_comments/expected.js create mode 100644 src/__tests__/fixtures/previous_statement_trailing_comments/input.js diff --git a/src/__tests__/fixtures/previous_statement_trailing_comments/expected.js b/src/__tests__/fixtures/previous_statement_trailing_comments/expected.js new file mode 100644 index 0000000..360906b --- /dev/null +++ b/src/__tests__/fixtures/previous_statement_trailing_comments/expected.js @@ -0,0 +1,16 @@ +'use strict'; + +// foo + +const __test__ = {}; +const Biz = { + bar: true + + // @test-export +}; +__test__.format = function format(rights) { + console.log('whatever'); +}; + +module.exports = {}; +module.exports.__test__ = __test__ diff --git a/src/__tests__/fixtures/previous_statement_trailing_comments/input.js b/src/__tests__/fixtures/previous_statement_trailing_comments/input.js new file mode 100644 index 0000000..3c9d212 --- /dev/null +++ b/src/__tests__/fixtures/previous_statement_trailing_comments/input.js @@ -0,0 +1,13 @@ +'use strict' + +// foo +const Biz = { + bar: true, +} + +// @test-export +function format(rights) { + console.log('whatever') +} + +module.exports = {} diff --git a/src/index.js b/src/index.js index 154208e..8031aa1 100644 --- a/src/index.js +++ b/src/index.js @@ -1,19 +1,27 @@ "use strict" -function hasExposeComment(node) { - if (!node || !node.leadingComments) return false - return node.leadingComments.some((comment) => ( - comment.value.includes("@test-export") - )) +function isExposeComment(comment) { + return comment.value.includes("@test-export") +} + +function hasExposeComment(path) { + if (!path.node) return false + if (path.node.leadingComments) { + return path.node.leadingComments.some(isExposeComment) + } + const previousPath = path.getPrevSibling() + if (previousPath.node && previousPath.node.trailingComments) { + return previousPath.node.trailingComments.some(isExposeComment) + } } function maybeDeclareExport(t, state, path) { if (state.get("exportDeclared")) return state.set("exportDeclared", true) - const statement = path.find((p) => p.parentPath.isProgram()) + const program = path.find((p) => p.isProgram()) - // statement.insertBefore( + // program.insertBefore( // t.exportNamedDeclaration( // t.variableDeclaration("const", [ // t.variableDeclarator( @@ -25,7 +33,8 @@ function maybeDeclareExport(t, state, path) { // ) // ) - statement.insertBefore( + program.unshiftContainer( + "body", t.variableDeclaration("const", [ t.variableDeclarator( t.identifier("__test__"), @@ -34,7 +43,7 @@ function maybeDeclareExport(t, state, path) { ]) ) - statement.parentPath.pushContainer( + program.pushContainer( "body", t.assignmentExpression( "=", @@ -95,7 +104,7 @@ module.exports = function ({ types: t }) { visitor: { VariableDeclaration(path, state) { - if (!hasExposeComment(path.node)) return + if (!hasExposeComment(path)) return maybeDeclareExport(t, state, path) @@ -112,7 +121,7 @@ module.exports = function ({ types: t }) { }, FunctionDeclaration(path, state) { - if (!hasExposeComment(path.node)) return + if (!hasExposeComment(path)) return maybeDeclareExport(t, state, path)