6.0.0
I'm extremely stupid and didn't commit as I go. To anyone reading this I'm extremely sorry. A lot of these changes are very broad and I plan on releasing Babel 6.0.0 today live on stage at Ember Camp London so I'm afraid I couldn't wait. If you're ever in London I'll buy you a beer (or assorted beverage!) to make up for it, also I'll kiss your feet and give you a back massage, maybe.
This commit is contained in:
@@ -1,21 +1,14 @@
|
||||
/* @flow */
|
||||
|
||||
import escapeRegExp from "lodash/string/escapeRegExp";
|
||||
import startsWith from "lodash/string/startsWith";
|
||||
import cloneDeep from "lodash/lang/cloneDeep";
|
||||
import isBoolean from "lodash/lang/isBoolean";
|
||||
import * as messages from "babel-messages";
|
||||
import minimatch from "minimatch";
|
||||
import contains from "lodash/collection/contains";
|
||||
import traverse from "babel-traverse";
|
||||
import isString from "lodash/lang/isString";
|
||||
import isRegExp from "lodash/lang/isRegExp";
|
||||
import isEmpty from "lodash/lang/isEmpty";
|
||||
import parse from "./helpers/parse";
|
||||
import path from "path";
|
||||
import has from "lodash/object/has";
|
||||
import fs from "fs";
|
||||
import * as t from "babel-types";
|
||||
import slash from "slash";
|
||||
import pathExists from "path-exists";
|
||||
|
||||
export { inherits, inspect } from "util";
|
||||
|
||||
@@ -56,11 +49,15 @@ export function list(val?: string): Array<string> {
|
||||
*/
|
||||
|
||||
export function regexify(val: any): RegExp {
|
||||
if (!val) return new RegExp(/.^/);
|
||||
if (!val) {
|
||||
return new RegExp(/.^/);
|
||||
}
|
||||
|
||||
if (Array.isArray(val)) val = new RegExp(val.map(escapeRegExp).join("|"), "i");
|
||||
if (Array.isArray(val)) {
|
||||
val = new RegExp(val.map(escapeRegExp).join("|"), "i");
|
||||
}
|
||||
|
||||
if (isString(val)) {
|
||||
if (typeof val === "string") {
|
||||
// normalise path separators
|
||||
val = slash(val);
|
||||
|
||||
@@ -72,7 +69,9 @@ export function regexify(val: any): RegExp {
|
||||
return new RegExp(regex.source.slice(1, -1), "i");
|
||||
}
|
||||
|
||||
if (isRegExp(val)) return val;
|
||||
if (isRegExp(val)) {
|
||||
return val;
|
||||
}
|
||||
|
||||
throw new TypeError("illegal type for regexify");
|
||||
}
|
||||
@@ -99,8 +98,14 @@ export function arrayify(val: any, mapFn?: Function): Array<any> {
|
||||
*/
|
||||
|
||||
export function booleanify(val: any): boolean | any {
|
||||
if (val === "true") return true;
|
||||
if (val === "false") return false;
|
||||
if (val === "true" || val == 1) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (val === "false" || val == 0 || !val) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return val;
|
||||
}
|
||||
|
||||
@@ -141,106 +146,3 @@ function _shouldIgnore(pattern: Function | RegExp, filename: string) {
|
||||
return pattern.test(filename);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A visitor for Babel templates, replaces placeholder references.
|
||||
*/
|
||||
|
||||
let templateVisitor = {
|
||||
/**
|
||||
* 360 NoScope PWNd
|
||||
*/
|
||||
noScope: true,
|
||||
|
||||
enter(node: Object, parent: Object, scope, nodes: Array<Object>) {
|
||||
if (t.isExpressionStatement(node)) {
|
||||
node = node.expression;
|
||||
}
|
||||
|
||||
if (t.isIdentifier(node) && has(nodes, node.name)) {
|
||||
this.skip();
|
||||
this.replaceInline(nodes[node.name]);
|
||||
}
|
||||
},
|
||||
|
||||
exit(node: Object) {
|
||||
traverse.clearNode(node);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Create an instance of a template to use in a transformer.
|
||||
*/
|
||||
|
||||
export function template(name: string, nodes?: Array<Object>, keepExpression?: boolean): Object {
|
||||
let ast = exports.templates[name];
|
||||
if (!ast) throw new ReferenceError(`unknown template ${name}`);
|
||||
|
||||
if (nodes === true) {
|
||||
keepExpression = true;
|
||||
nodes = null;
|
||||
}
|
||||
|
||||
ast = cloneDeep(ast);
|
||||
|
||||
if (!isEmpty(nodes)) {
|
||||
traverse(ast, templateVisitor, null, nodes);
|
||||
}
|
||||
|
||||
if (ast.body.length > 1) return ast.body;
|
||||
|
||||
let node = ast.body[0];
|
||||
|
||||
if (!keepExpression && t.isExpressionStatement(node)) {
|
||||
return node.expression;
|
||||
} else {
|
||||
return node;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a template.
|
||||
*/
|
||||
|
||||
export function parseTemplate(loc: string, code: string): Object {
|
||||
try {
|
||||
let ast = parse(code, { filename: loc, looseModules: true }).program;
|
||||
ast = traverse.removeProperties(ast);
|
||||
return ast;
|
||||
} catch (err) {
|
||||
err.message = `${loc}: ${err.message}`;
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Load templates from transformation/templates directory.
|
||||
*/
|
||||
|
||||
function loadTemplates(): Object {
|
||||
let templates = {};
|
||||
|
||||
let templatesLoc = path.join(__dirname, "transformation/templates");
|
||||
if (!pathExists.sync(templatesLoc)) {
|
||||
throw new ReferenceError(messages.get("missingTemplatesDirectory"));
|
||||
}
|
||||
|
||||
for (let name of (fs.readdirSync(templatesLoc): Array)) {
|
||||
if (name[0] === ".") continue;
|
||||
|
||||
let key = path.basename(name, path.extname(name));
|
||||
let loc = path.join(templatesLoc, name);
|
||||
let code = fs.readFileSync(loc, "utf8");
|
||||
|
||||
templates[key] = parseTemplate(loc, code);
|
||||
}
|
||||
|
||||
return templates;
|
||||
}
|
||||
|
||||
try {
|
||||
exports.templates = require("../templates.json");
|
||||
} catch (err) {
|
||||
if (err.code !== "MODULE_NOT_FOUND") throw err;
|
||||
exports.templates = loadTemplates();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user