Delete Logger class.

This commit is contained in:
Logan Smyth
2017-03-13 14:35:54 -07:00
parent 188fe105c2
commit c1be6a36c4
5 changed files with 32 additions and 84 deletions

View File

@@ -10,7 +10,6 @@ import sourceMap from "source-map";
import generate from "babel-generator";
import codeFrame from "babel-code-frame";
import traverse from "babel-traverse";
import Logger from "./logger";
import Store from "../../store";
import { parse } from "babylon";
import * as util from "../../util";
@@ -43,9 +42,7 @@ export default class File extends Store {
constructor(opts: Object = {}) {
super();
this.log = new Logger(this, opts.filename || "unknown");
opts = this.log.wrap(() => new OptionManager().init(opts));
opts = new OptionManager().init(opts);
let passes = [];
if (opts.plugins) passes.push(opts.plugins);
@@ -101,7 +98,6 @@ export default class File extends Store {
pluginPasses: Array<Array<[Plugin, Object]>>;
parserOpts: BabelParserOptions;
log: Logger;
opts: Object;
dynamicImportTypes: Object;
dynamicImportIds: Object;
@@ -368,9 +364,9 @@ export default class File extends Store {
}
}
this.log.debug("Parse start");
util.debug(this.opts, "Parse start");
const ast = parseCode(code, parserOpts || this.parserOpts);
this.log.debug("Parse stop");
util.debug(this.opts, "Parse stop");
return ast;
}
@@ -388,9 +384,9 @@ export default class File extends Store {
}
addAst(ast) {
this.log.debug("Start set AST");
util.debug(this.opts, "Start set AST");
this._addAst(ast);
this.log.debug("End set AST");
util.debug(this.opts, "End set AST");
}
transform(): BabelFileResult {
@@ -405,13 +401,13 @@ export default class File extends Store {
}
this.call("pre", passes);
this.log.debug("Start transform traverse");
util.debug(this.opts, "Start transform traverse");
// merge all plugin visitors into a single visitor
const visitor = traverse.visitors.merge(visitors, passes, this.opts.wrapPluginVisitorMethod);
traverse(this.ast, visitor, this.scope);
this.log.debug("End transform traverse");
util.debug(this.opts, "End transform traverse");
this.call("post", passes);
}
@@ -552,14 +548,14 @@ export default class File extends Store {
}
}
this.log.debug("Generation start");
util.debug(this.opts, "Generation start");
const _result = gen(ast, opts.generatorOpts ? Object.assign(opts, opts.generatorOpts) : opts,
this.code);
result.code = _result.code;
result.map = _result.map;
this.log.debug("Generation end");
util.debug(this.opts, "Generation end");
if (this.shebang) {
// add back shebang

View File

@@ -1,66 +0,0 @@
import type File from "./index";
import buildDebug from "debug";
const verboseDebug = buildDebug("babel:verbose");
const generalDebug = buildDebug("babel");
const seenDeprecatedMessages = [];
export default class Logger {
constructor(file: File, filename: string) {
this.filename = filename;
this.file = file;
}
filename: string;
file: File;
_buildMessage(msg: string): string {
let parts = `[BABEL] ${this.filename}`;
if (msg) parts += `: ${msg}`;
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));
}
error(msg: string, Constructor: typeof Error = Error): Error {
throw new Constructor(this._buildMessage(msg));
}
deprecate(msg: string) {
if (this.file.opts && this.file.opts.suppressDeprecationMessages) return;
msg = this._buildMessage(msg);
// already seen this message
if (seenDeprecatedMessages.indexOf(msg) >= 0) return;
// make sure we don't see it again
seenDeprecatedMessages.push(msg);
console.error(msg);
}
verbose(msg: string) {
if (verboseDebug.enabled) verboseDebug(this._buildMessage(msg));
}
debug(msg: string) {
if (generalDebug.enabled) generalDebug(this._buildMessage(msg));
}
deopt(node: Object, msg: string) {
this.debug(msg);
}
}

View File

@@ -373,8 +373,13 @@ export default class OptionManager {
}
init(opts: Object = {}): Object {
for (const config of buildConfigChain(opts)) {
this.mergeOptions(config);
try {
for (const config of buildConfigChain(opts)) {
this.mergeOptions(config);
}
} catch (e) {
e.message = util.message(opts, e.message);
throw e;
}
opts = this.options;

View File

@@ -5,9 +5,24 @@ import includes from "lodash/includes";
import isRegExp from "lodash/isRegExp";
import path from "path";
import slash from "slash";
import buildDebug from "debug";
export { inherits, inspect } from "util";
const debugBabel = buildDebug("babel");
export function debug(opts: Object, msg: string) {
debugBabel(message(opts, msg));
}
export function message(opts: Object, msg: string) {
// There are a few case where throws errors will try to annotate themselves multiple times, so
// to keep things simple we just bail out if re-wrapping the message.
if (/^\[BABEL\]/.test(msg)) return msg;
return `[BABEL] ${opts.filename || "unknown"}: ${msg}`;
}
/**
* Test if a filename ends with a compilable extension.
*/

View File

@@ -45,9 +45,7 @@ export default function ({ types: t }) {
}
if (!state.fileNameIdentifier) {
const fileName = state.file.log.filename !== "unknown"
? state.file.log.filename
: null;
const fileName = state.file.opts.filename;
const fileNameIdentifier = path.scope.generateUidIdentifier(FILE_NAME_VAR);
path.hub.file.scope.push({ id: fileNameIdentifier, init: t.stringLiteral(fileName) });