Remove Logger usage from options processing.
This commit is contained in:
@@ -132,7 +132,7 @@ export default class File extends Store {
|
||||
}
|
||||
|
||||
initOptions(opts) {
|
||||
opts = new OptionManager(this.log).init(opts);
|
||||
opts = this.log.wrap(() => new OptionManager().init(opts));
|
||||
|
||||
if (opts.inputSourceMap) {
|
||||
opts.sourceMaps = true;
|
||||
|
||||
@@ -21,6 +21,15 @@ export default class Logger {
|
||||
return parts;
|
||||
}
|
||||
|
||||
wrap<T>(callback: () => T): T {
|
||||
try {
|
||||
return callback();
|
||||
} catch (e) {
|
||||
e.message = this._buildMessage(e.message);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
warn(msg: string) {
|
||||
console.warn(this._buildMessage(msg));
|
||||
}
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import type Logger from "../logger";
|
||||
import resolve from "../../../helpers/resolve";
|
||||
import json5 from "json5";
|
||||
import path from "path";
|
||||
@@ -21,9 +20,9 @@ function exists(filename) {
|
||||
}
|
||||
}
|
||||
|
||||
export default function buildConfigChain(opts: Object = {}, log?: Logger) {
|
||||
export default function buildConfigChain(opts: Object = {}) {
|
||||
const filename = opts.filename;
|
||||
const builder = new ConfigChainBuilder(log);
|
||||
const builder = new ConfigChainBuilder();
|
||||
|
||||
// resolve all .babelrc files
|
||||
if (opts.babelrc !== false) {
|
||||
@@ -40,10 +39,9 @@ export default function buildConfigChain(opts: Object = {}, log?: Logger) {
|
||||
}
|
||||
|
||||
class ConfigChainBuilder {
|
||||
constructor(log?: Logger) {
|
||||
constructor() {
|
||||
this.resolvedConfigs = [];
|
||||
this.configs = [];
|
||||
this.log = log;
|
||||
}
|
||||
|
||||
errorMultipleConfigs(loc1: string, loc2: string) {
|
||||
@@ -183,7 +181,7 @@ class ConfigChainBuilder {
|
||||
if (extendsLoc) {
|
||||
this.addConfig(extendsLoc);
|
||||
} else {
|
||||
if (this.log) this.log.error(`Couldn't resolve extends clause of ${options.extends} in ${alias}`);
|
||||
throw new Error(`Couldn't resolve extends clause of ${options.extends} in ${alias}`);
|
||||
}
|
||||
delete options.extends;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import * as context from "../../../index";
|
||||
import type Logger from "../logger";
|
||||
import Plugin from "../../plugin";
|
||||
import * as messages from "babel-messages";
|
||||
import { normaliseOptions } from "./index";
|
||||
@@ -35,15 +34,13 @@ type MergeOptions = {
|
||||
};
|
||||
|
||||
export default class OptionManager {
|
||||
constructor(log?: Logger) {
|
||||
constructor() {
|
||||
this.resolvedConfigs = [];
|
||||
this.options = OptionManager.createBareOptions();
|
||||
this.log = log;
|
||||
}
|
||||
|
||||
resolvedConfigs: Array<string>;
|
||||
options: Object;
|
||||
log: ?Logger;
|
||||
|
||||
static memoisedPlugins: Array<{
|
||||
container: Function;
|
||||
@@ -158,7 +155,7 @@ export default class OptionManager {
|
||||
|
||||
//
|
||||
if (typeof rawOpts !== "object" || Array.isArray(rawOpts)) {
|
||||
this.log.error(`Invalid options type for ${alias}`, TypeError);
|
||||
throw new TypeError(`Invalid options type for ${alias}`);
|
||||
}
|
||||
|
||||
//
|
||||
@@ -176,15 +173,14 @@ export default class OptionManager {
|
||||
const option = config[key];
|
||||
|
||||
// check for an unknown option
|
||||
if (!option && this.log) {
|
||||
if (!option) {
|
||||
if (removed[key]) {
|
||||
this.log.error(`Using removed Babel 5 option: ${alias}.${key} - ${removed[key].message}`,
|
||||
ReferenceError);
|
||||
throw new ReferenceError(`Using removed Babel 5 option: ${alias}.${key} - ${removed[key].message}`);
|
||||
} else {
|
||||
// eslint-disable-next-line max-len
|
||||
const unknownOptErr = `Unknown option: ${alias}.${key}. Check out http://babeljs.io/docs/usage/options/ for more information about options.`;
|
||||
|
||||
this.log.error(unknownOptErr, ReferenceError);
|
||||
throw new ReferenceError(unknownOptErr);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -333,7 +329,7 @@ export default class OptionManager {
|
||||
}
|
||||
|
||||
init(opts: Object = {}): Object {
|
||||
for (const config of buildConfigChain(opts, this.log)) {
|
||||
for (const config of buildConfigChain(opts)) {
|
||||
this.mergeOptions(config);
|
||||
}
|
||||
|
||||
|
||||
@@ -141,7 +141,7 @@ describe("api", function () {
|
||||
plugins: [__dirname + "/../../babel-plugin-syntax-jsx", false],
|
||||
});
|
||||
},
|
||||
/TypeError: Falsy value found in plugins/
|
||||
/TypeError: \[BABEL\] unknown: Falsy value found in plugins/
|
||||
);
|
||||
});
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import assert from "assert";
|
||||
import OptionManager from "../lib/transformation/file/options/option-manager";
|
||||
import Logger from "../lib/transformation/file/logger";
|
||||
import path from "path";
|
||||
|
||||
describe("option-manager", () => {
|
||||
@@ -17,7 +16,7 @@ describe("option-manager", () => {
|
||||
it("throws for removed babel 5 options", () => {
|
||||
return assert.throws(
|
||||
() => {
|
||||
const opt = new OptionManager(new Logger(null, "unknown"));
|
||||
const opt = new OptionManager();
|
||||
opt.init({
|
||||
"randomOption": true,
|
||||
});
|
||||
@@ -29,7 +28,7 @@ describe("option-manager", () => {
|
||||
it("throws for removed babel 5 options", () => {
|
||||
return assert.throws(
|
||||
() => {
|
||||
const opt = new OptionManager(new Logger(null, "unknown"));
|
||||
const opt = new OptionManager();
|
||||
opt.init({
|
||||
"auxiliaryComment": true,
|
||||
"blacklist": true,
|
||||
@@ -43,7 +42,7 @@ describe("option-manager", () => {
|
||||
it("throws for resolved but erroring preset", () => {
|
||||
return assert.throws(
|
||||
() => {
|
||||
const opt = new OptionManager(new Logger(null, "unknown"));
|
||||
const opt = new OptionManager();
|
||||
opt.init({
|
||||
"presets": [path.join(__dirname, "fixtures/option-manager/not-a-preset")],
|
||||
});
|
||||
@@ -56,7 +55,7 @@ describe("option-manager", () => {
|
||||
describe("presets", function () {
|
||||
function presetTest(name) {
|
||||
it(name, function () {
|
||||
const opt = new OptionManager(new Logger(null, "unknown"));
|
||||
const opt = new OptionManager();
|
||||
const options = opt.init({
|
||||
"presets": [path.join(__dirname, "fixtures/option-manager/presets", name)],
|
||||
});
|
||||
@@ -68,7 +67,7 @@ describe("option-manager", () => {
|
||||
|
||||
function presetThrowsTest(name, msg) {
|
||||
it(name, function () {
|
||||
const opt = new OptionManager(new Logger(null, "unknown"));
|
||||
const opt = new OptionManager();
|
||||
assert.throws(() => opt.init({
|
||||
"presets": [path.join(__dirname, "fixtures/option-manager/presets", name)],
|
||||
}), msg);
|
||||
|
||||
Reference in New Issue
Block a user