Skip to content

Commit

Permalink
fix compatibility with babel 6.17.2
Browse files Browse the repository at this point in the history
Since PR babel/babylon#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.
  • Loading branch information
BenoitZugmeyer committed Aug 18, 2017
1 parent 55eb9fa commit 9dfc985
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -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__
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict'

// foo
const Biz = {
bar: true,
}

// @test-export
function format(rights) {
console.log('whatever')
}

module.exports = {}
31 changes: 20 additions & 11 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -25,7 +33,8 @@ function maybeDeclareExport(t, state, path) {
// )
// )

statement.insertBefore(
program.unshiftContainer(
"body",
t.variableDeclaration("const", [
t.variableDeclarator(
t.identifier("__test__"),
Expand All @@ -34,7 +43,7 @@ function maybeDeclareExport(t, state, path) {
])
)

statement.parentPath.pushContainer(
program.pushContainer(
"body",
t.assignmentExpression(
"=",
Expand Down Expand Up @@ -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)

Expand All @@ -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)

Expand Down

0 comments on commit 9dfc985

Please sign in to comment.