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)