Only set options in cli if different from default (#4507)

* CLI: Only set options if different from default 

Currently default values (like comments: true) will be set always for the transform.
This behaviour dos not allow for setting this options from babelrc as the default would always have precedence.

* Add new tests for comments
Also ensure that the babelrc in the fixture folder is copied over to the
working directory during tests
This commit is contained in:
Daniel Tschinder 2016-09-13 14:25:08 +02:00 committed by Henry Zhu
parent 993f887240
commit ff78fb19f7
12 changed files with 76 additions and 17 deletions

View File

@ -107,7 +107,7 @@ if (errors.length) {
let opts = exports.opts = {}; let opts = exports.opts = {};
each(options, function (opt, key) { each(options, function (opt, key) {
if (commander[key] !== undefined) { if (commander[key] !== undefined && commander[key] !== opt.default) {
opts[key] = commander[key]; opts[key] = commander[key];
} }
}); });

View File

@ -0,0 +1,7 @@
/*
Test comment
*/
arr.map(x => x * MULTIPLIER);
// END OF FILE

View File

@ -0,0 +1,3 @@
{
"args": ["script.js", "--out-file", "script2.js", "--no-comments"]
}

View File

@ -0,0 +1,5 @@
"use strict";
arr.map(function (x) {
return x * MULTIPLIER;
});

View File

@ -0,0 +1,3 @@
{
"comments": false
}

View File

@ -0,0 +1,7 @@
/*
Test comment
*/
arr.map(x => x * MULTIPLIER);
// END OF FILE

View File

@ -0,0 +1,3 @@
{
"args": ["script.js", "--out-file", "script2.js"]
}

View File

@ -0,0 +1,5 @@
"use strict";
arr.map(function (x) {
return x * MULTIPLIER;
});

View File

@ -0,0 +1,7 @@
/*
Test comment
*/
arr.map(x => x * MULTIPLIER);
// END OF FILE

View File

@ -0,0 +1,3 @@
{
"args": ["script.js", "--out-file", "script2.js"]
}

View File

@ -0,0 +1,11 @@
"use strict";
/*
Test comment
*/
arr.map(function (x) {
return x * MULTIPLIER;
});
// END OF FILE

View File

@ -12,25 +12,24 @@ var fs = require("fs");
var pathExists = require("path-exists"); var pathExists = require("path-exists");
var _ = require("lodash"); var _ = require("lodash");
var fixtureLoc = __dirname + "/fixtures"; var fixtureLoc = path.join(__dirname, "fixtures");
var tmpLoc = __dirname + "/tmp"; var tmpLoc = path.join(__dirname, "tmp");
var presetLocs = [ var presetLocs = [
__dirname + "/../../babel-preset-es2015", path.join(__dirname, "../../babel-preset-es2015"),
__dirname + "/../../babel-preset-react" path.join(__dirname, "../../babel-preset-react")
].join(","); ].join(",");
var pluginLocs = [ var pluginLocs = [
__dirname + "/../../babel-plugin-transform-strict-mode", path.join(__dirname, "/../../babel-plugin-transform-strict-mode"),
__dirname + "/../../babel-plugin-transform-es2015-modules-commonjs", path.join(__dirname, "/../../babel-plugin-transform-es2015-modules-commonjs"),
].join(","); ].join(",");
var readDir = function (loc) { var readDir = function (loc) {
var files = {}; var files = {};
if (pathExists.sync(loc)) { if (pathExists.sync(loc)) {
_.each(readdir(loc), function (filename) { _.each(readdir(loc), function (filename) {
var contents = helper.readFile(loc + "/" + filename); files[filename] = helper.readFile(path.join(loc, filename));
files[filename] = contents;
}); });
} }
return files; return files;
@ -77,7 +76,7 @@ var assertTest = function (stdout, stderr, opts) {
}; };
var buildTest = function (binName, testName, opts) { var buildTest = function (binName, testName, opts) {
var binLoc = path.normalize(__dirname + "/../../babel-cli/lib/" + binName); var binLoc = path.join(__dirname, "../lib", binName);
return function (callback) { return function (callback) {
this.timeout(5000); this.timeout(5000);
@ -142,22 +141,22 @@ var clear = function () {
_.each(fs.readdirSync(fixtureLoc), function (binName) { _.each(fs.readdirSync(fixtureLoc), function (binName) {
if (binName[0] === ".") return; if (binName[0] === ".") return;
var suiteLoc = fixtureLoc + "/" + binName; var suiteLoc = path.join(fixtureLoc, binName);
suite("bin/" + binName, function () { suite("bin/" + binName, function () {
_.each(fs.readdirSync(fixtureLoc + "/" + binName), function (testName) { _.each(fs.readdirSync(suiteLoc), function (testName) {
if (testName[0] === ".") return; if (testName[0] === ".") return;
var testLoc = suiteLoc + "/" + testName; var testLoc = path.join(suiteLoc, testName);
var opts = { var opts = {
args: [] args: []
}; };
var optionsLoc = testLoc + "/options.json" var optionsLoc = path.join(testLoc, "options.json");
if (pathExists.sync(optionsLoc)) _.merge(opts, require(optionsLoc)); if (pathExists.sync(optionsLoc)) _.merge(opts, require(optionsLoc));
_.each(["stdout", "stdin", "stderr"], function (key) { _.each(["stdout", "stdin", "stderr"], function (key) {
var loc = testLoc + "/" + key + ".txt"; var loc = path.join(testLoc, key + ".txt");
if (pathExists.sync(loc)) { if (pathExists.sync(loc)) {
opts[key] = helper.readFile(loc); opts[key] = helper.readFile(loc);
} else { } else {
@ -165,8 +164,14 @@ _.each(fs.readdirSync(fixtureLoc), function (binName) {
} }
}); });
opts.outFiles = readDir(testLoc + "/out-files"); opts.outFiles = readDir(path.join(testLoc, "out-files"));
opts.inFiles = readDir(testLoc + "/in-files"); opts.inFiles = readDir(path.join(testLoc, "in-files"));
var babelrcLoc = path.join(testLoc, ".babelrc");
if (pathExists.sync(babelrcLoc)) {
// copy .babelrc file to tmp directory
opts.inFiles['.babelrc'] = helper.readFile(babelrcLoc);
}
test(testName, buildTest(binName, testName, opts)); test(testName, buildTest(binName, testName, opts));
}); });