diff --git a/lib/compile.js b/lib/compile.js index f1c39eb..cdea3d3 100644 --- a/lib/compile.js +++ b/lib/compile.js @@ -219,25 +219,26 @@ module.exports = function(file, fileName) { } }); + lines.unshift("import Immutable from 'seamless-immutable';"); - // f + // de-dent module methods if (state.hasModule) { lines = lines.map(function(line) { - if (line.match('RS_MODULE_LINE')) { - return "import Immutable from 'seamless-immutable'"; - } - else { - // if first two lines are blank de-dent module internals - if (line[0] && !line[1].trim() && line[1] && !line[1].trim()) { - return line.slice(2); - } else { - return line; - } + var firstCharEmpty = line[0] && !line[0].trim(); + var secondCharEmpty = line[1] && !line[1].trim(); + + if (firstCharEmpty && secondCharEmpty) { + return line.slice(2); + } else { + return line; } - return line; }); } + lines = lines.filter(function(line) { + return ! line.match('RS_REMOVE_LINE'); + }) + // return transformed lines return lines.join('\n'); }; diff --git a/lib/transform.js b/lib/transform.js index 0985a21..2458755 100644 --- a/lib/transform.js +++ b/lib/transform.js @@ -268,7 +268,7 @@ module.exports = { state.hasModule = true; var lline; - var newLine = line.replace(/^\s*defmodule\s+([\w$]+)\s+do\s*$/, 'RS_MODULE_LINE'); + var newLine = line.replace(/^\s*defmodule\s+([\w$]+)\s+do\s*$/, 'RS_REMOVE_LINE'); // ** side effect, removes last 'end' for (var i=1, n=lines.length; i < n; i++) { diff --git a/test/examples/aliases.js b/test/examples/aliases.js index 53fa919..44ad104 100644 --- a/test/examples/aliases.js +++ b/test/examples/aliases.js @@ -1,3 +1,4 @@ +import Immutable from 'seamless-immutable'; // this is a single line comment //# diff --git a/test/examples/functions.js b/test/examples/functions.js index b699f9a..b92fd42 100644 --- a/test/examples/functions.js +++ b/test/examples/functions.js @@ -1,3 +1,4 @@ +import Immutable from 'seamless-immutable'; // compiles to eq. JavaScript, this example does not // include the module syntax: `defmodule` as that is // a separate transform (see test/examples/module.rs) diff --git a/test/examples/immutable.js b/test/examples/immutable.js index dd0b985..b380ef6 100644 --- a/test/examples/immutable.js +++ b/test/examples/immutable.js @@ -1,3 +1,4 @@ +import Immutable from 'seamless-immutable'; // const arr = Immutable([1, 2, 3]); const arr = Immutable([1, 2, 3]) const arr = Immutable([1, {foo: 1}, [2,[3]] , 3]) diff --git a/test/examples/module.js b/test/examples/module.js index 0e4d426..8ec3349 100644 --- a/test/examples/module.js +++ b/test/examples/module.js @@ -1,4 +1,4 @@ -import Immutable from 'seamless-immutable' +import Immutable from 'seamless-immutable'; export function add(a, b) { return a + b } diff --git a/test/examples/pipe_operator.js b/test/examples/pipe_operator.js index ce31c4a..2c738e6 100644 --- a/test/examples/pipe_operator.js +++ b/test/examples/pipe_operator.js @@ -1,3 +1,4 @@ +import Immutable from 'seamless-immutable'; // single line pipes not finished const bar = _.chain(Immutable([1, 2, 3])).pipesCall(Enum.uppcase).value(); diff --git a/test/examples/sample.js b/test/examples/sample.js index a5d04ad..21c1d35 100644 --- a/test/examples/sample.js +++ b/test/examples/sample.js @@ -1,3 +1,4 @@ +import Immutable from 'seamless-immutable'; // auto declared constants const bar = 2