Skip to content

Commit

Permalink
cleanup test-transform sourceType logic
Browse files Browse the repository at this point in the history
  • Loading branch information
boopathi committed May 1, 2018
1 parent 6063ec6 commit 3497044
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions utils/test-transform/src/test-transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@ const babel = require("@babel/core");
const unpad = require("unpad");

function _transform(source, options) {
// reset defaults to older babel
// babel-7 current beta defaults to sourceType module
options.sourceType = defaults(options, "sourceType", "script");

// don't use config file - babel.config.js to apply transformations
// this is almost never required as babel.config.js represents the
// project's configuration and not the config for test environment
options.configFile = false;

return babel.transformSync(unpad(source), options).code.trim();
}

Expand All @@ -24,7 +33,7 @@ function makeTester(

const { stack } = new Error();
const options = Object.assign(
{ plugins, sourceType: "script", configFile: false },
{ plugins, sourceType: "script" },
opts,
babelOpts
);
Expand Down Expand Up @@ -56,11 +65,6 @@ function makeTester(
{
test,
transform(source, options) {
options.configFile = false;
options.sourceType = hop(options, "sourceType")
? options.sourceType
: "script";

return unpad(source)
.split("\n")
.map(line => _transform(line, options))
Expand Down Expand Up @@ -103,6 +107,10 @@ exports.snapshot = (plugins, opts) =>
}
});

function defaults(o, key, def) {
return hop(o, key) ? o[key] : def;
}

function hop(o, key) {
return Object.prototype.hasOwnProperty.call(o, key);
}

0 comments on commit 3497044

Please sign in to comment.