Avoid usage of exports/module.exports/require().

This commit is contained in:
Logan Smyth 2017-02-20 00:52:24 -08:00
parent d9f01cbce6
commit 4ee385e96c
32 changed files with 141 additions and 105 deletions

View File

@ -9,6 +9,8 @@ import vm from "vm";
import "babel-polyfill";
import register from "babel-register";
import pkg from "../package.json";
const program = new commander.Command("babel-node");
program.option("-e, --eval [script]", "Evaluate script");
@ -19,7 +21,6 @@ program.option("-x, --extensions [extensions]", "List of extensions to hook into
program.option("-w, --plugins [string]", "", util.list);
program.option("-b, --presets [string]", "", util.list);
const pkg = require("../package.json");
program.version(pkg.version);
program.usage("[options] [ -e script | script.js ] [arguments]");
program.parse(process.argv);

View File

@ -3,8 +3,8 @@
* when found, before invoking the "real" _babel-node(1) executable.
*/
const getV8Flags = require("v8flags");
const path = require("path");
import getV8Flags from "v8flags";
import path from "path";
let args = [path.join(__dirname, "_babel-node")];

View File

@ -1,10 +1,11 @@
const outputFileSync = require("output-file-sync");
const slash = require("slash");
const path = require("path");
const util = require("./util");
const fs = require("fs");
import outputFileSync from "output-file-sync";
import slash from "slash";
import path from "path";
import fs from "fs";
module.exports = function (commander, filenames) {
import * as util from "./util";
export default function (commander, filenames) {
function write(src, relative) {
// remove extension and then append back on .js
relative = relative.replace(/\.(\w*?)$/, "") + ".js";
@ -88,4 +89,4 @@ module.exports = function (commander, filenames) {
});
});
}
};
}

View File

@ -1,11 +1,12 @@
const convertSourceMap = require("convert-source-map");
const sourceMap = require("source-map");
const slash = require("slash");
const path = require("path");
const util = require("./util");
const fs = require("fs");
import convertSourceMap from "convert-source-map";
import sourceMap from "source-map";
import slash from "slash";
import path from "path";
import fs from "fs";
module.exports = function (commander, filenames, opts) {
import * as util from "./util";
export default function (commander, filenames, opts) {
if (commander.sourceMaps === "inline") {
opts.sourceMaps = true;
}
@ -176,4 +177,4 @@ module.exports = function (commander, filenames, opts) {
} else {
stdin();
}
};
}

View File

@ -1,12 +1,16 @@
#!/usr/bin/env node
const fs = require("fs");
const commander = require("commander");
const kebabCase = require("lodash/kebabCase");
const options = require("babel-core").options;
const util = require("babel-core").util;
const uniq = require("lodash/uniq");
const glob = require("glob");
import fs from "fs";
import commander from "commander";
import kebabCase from "lodash/kebabCase";
import { options, util, version } from "babel-core";
import uniq from "lodash/uniq";
import glob from "glob";
import dirCommand from "./dir";
import fileCommand from "./file";
import pkg from "../../package.json";
Object.keys(options).forEach(function (key) {
const option = options[key];
@ -45,8 +49,7 @@ commander.option("-D, --copy-files", "When compiling a directory copy over non-c
commander.option("-q, --quiet", "Don't log anything");
/* eslint-enable max-len */
const pkg = require("../../package.json");
commander.version(pkg.version + " (babel-core " + require("babel-core").version + ")");
commander.version(pkg.version + " (babel-core " + version + ")");
commander.usage("[options] <files ...>");
commander.parse(process.argv);
@ -103,7 +106,7 @@ if (errors.length) {
//
const opts = exports.opts = {};
export const opts = {};
Object.keys(options).forEach(function (key) {
const opt = options[key];
@ -118,12 +121,5 @@ if (opts.only) {
opts.only = util.arrayify(opts.only, util.regexify);
}
let fn;
if (commander.outDir) {
fn = require("./dir");
} else {
fn = require("./file");
}
fn(commander, filenames, exports.opts);
const fn = commander.outDir ? dirCommand : fileCommand;
fn(commander, filenames, opts);

View File

@ -1,11 +1,11 @@
const commander = require("commander");
const defaults = require("lodash/defaults");
const readdir = require("fs-readdir-recursive");
const index = require("./index");
const babel = require("babel-core");
const util = require("babel-core").util;
const path = require("path");
const fs = require("fs");
import commander from "commander";
import defaults from "lodash/defaults";
import readdir from "fs-readdir-recursive";
import * as babel from "babel-core";
import path from "path";
import fs from "fs";
import * as index from "./index";
export function chmod(src, dest) {
fs.chmodSync(dest, fs.statSync(src).mode);
@ -13,16 +13,16 @@ export function chmod(src, dest) {
export function readdirFilter(filename) {
return readdir(filename).filter(function (filename) {
return util.canCompile(filename);
return babel.util.canCompile(filename);
});
}
export { readdir };
export const canCompile = util.canCompile;
export const canCompile = babel.util.canCompile;
export function shouldIgnore(loc) {
return util.shouldIgnore(loc, index.opts.ignore, index.opts.only);
return babel.util.shouldIgnore(loc, index.opts.ignore, index.opts.only);
}
export function addSourceMappingUrl(code, loc) {

View File

@ -1,6 +1,6 @@
/* eslint max-len: "off" */
module.exports = {
export default {
filename: {
type: "filename",
description: "filename to use when reading from stdin - this will be used in source-maps, errors etc",

View File

@ -1,6 +1,6 @@
/* eslint max-len: "off" */
module.exports = {
export default {
"auxiliaryComment": {
"message": "Use `auxiliaryCommentBefore` or `auxiliaryCommentAfter`"
},

View File

@ -0,0 +1,10 @@
export * from "./template-literals";
export * from "./expressions";
export * from "./statements";
export * from "./classes";
export * from "./methods";
export * from "./modules";
export * from "./types";
export * from "./flow";
export * from "./base";
export * from "./jsx";

View File

@ -219,14 +219,14 @@ export function DebuggerStatement() {
this.semicolon();
}
function variableDeclarationIdent() {
function variableDeclarationIndent() {
// "let " or "var " indentation.
this.token(",");
this.newline();
if (this.endsWith("\n")) for (let i = 0; i < 4; i++) this.space(true);
}
function constDeclarationIdent() {
function constDeclarationIndent() {
// "const " indentation.
this.token(",");
this.newline();
@ -262,7 +262,7 @@ export function VariableDeclaration(node: Object, parent: Object) {
let separator;
if (hasInits) {
separator = node.kind === "const" ? constDeclarationIdent : variableDeclarationIdent;
separator = node.kind === "const" ? constDeclarationIndent : variableDeclarationIndent;
}
//

View File

@ -1,4 +1,4 @@
import whitespace from "./whitespace";
import * as whitespace from "./whitespace";
import * as parens from "./parentheses";
import * as t from "babel-types";

View File

@ -60,7 +60,7 @@ function isType(node) {
* Tests for node types that need whitespace.
*/
exports.nodes = {
export const nodes = {
/**
* Test if AssignmentExpression needs whitespace.
@ -164,10 +164,10 @@ exports.nodes = {
* Test if Property or SpreadProperty needs whitespace.
*/
exports.nodes.ObjectProperty =
exports.nodes.ObjectTypeProperty =
exports.nodes.ObjectMethod =
exports.nodes.SpreadProperty = function (node: Object, parent): ?WhitespaceObject {
nodes.ObjectProperty =
nodes.ObjectTypeProperty =
nodes.ObjectMethod =
nodes.SpreadProperty = function (node: Object, parent): ?WhitespaceObject {
if (parent.properties[0] === node) {
return {
before: true
@ -179,7 +179,7 @@ exports.nodes.SpreadProperty = function (node: Object, parent): ?WhitespaceObjec
* Returns lists from node types that need whitespace.
*/
exports.list = {
export const list = {
/**
* Return VariableDeclaration declarations init properties.
@ -222,7 +222,7 @@ exports.list = {
amounts = { after: amounts, before: amounts };
}
[type].concat(t.FLIPPED_ALIAS_KEYS[type] || []).forEach(function (type) {
exports.nodes[type] = function () {
nodes[type] = function () {
return amounts;
};
});

View File

@ -7,6 +7,8 @@ import * as n from "./node";
import Whitespace from "./whitespace";
import * as t from "babel-types";
import * as generatorFunctions from "./generators";
const SCIENTIFIC_NOTATION = /e/i;
const ZERO_DECIMAL_INTEGER = /\.0+$/;
const NON_DECIMAL_LITERAL = /^0[box]/;
@ -572,22 +574,10 @@ export default class Printer {
}
}
// Expose the node type functions and helpers on the prototype for easy usage.
Object.assign(Printer.prototype, generatorFunctions);
function commaSeparator() {
this.token(",");
this.space();
}
for (const generator of [
require("./generators/template-literals"),
require("./generators/expressions"),
require("./generators/statements"),
require("./generators/classes"),
require("./generators/methods"),
require("./generators/modules"),
require("./generators/types"),
require("./generators/flow"),
require("./generators/base"),
require("./generators/jsx")
]) {
Object.assign(Printer.prototype, generator);
}

View File

@ -1,5 +1,7 @@
import asyncSyntaxPlugin from "babel-plugin-syntax-async-functions";
export default function () {
return {
inherits: require("babel-plugin-syntax-async-functions")
inherits: asyncSyntaxPlugin,
};
}

View File

@ -1,4 +1,5 @@
import remapAsyncToGenerator from "babel-helper-remap-async-to-generator";
import syntaxAsyncGenerators from "babel-plugin-syntax-async-generators";
export default function ({ types: t }) {
const yieldStarVisitor = {
@ -17,7 +18,7 @@ export default function ({ types: t }) {
};
return {
inherits: require("babel-plugin-syntax-async-generators"),
inherits: syntaxAsyncGenerators,
visitor: {
Function(path, state) {
if (!path.node.async || !path.node.generator) return;

View File

@ -1,8 +1,9 @@
import remapAsyncToGenerator from "babel-helper-remap-async-to-generator";
import syntaxAsyncFunctions from "babel-plugin-syntax-async-functions";
export default function () {
return {
inherits: require("babel-plugin-syntax-async-functions"),
inherits: syntaxAsyncFunctions,
visitor: {
Function(path, state) {

View File

@ -1,8 +1,9 @@
import remapAsyncToGenerator from "babel-helper-remap-async-to-generator";
import syntaxAsyncFunctions from "babel-plugin-syntax-async-functions";
export default function () {
return {
inherits: require("babel-plugin-syntax-async-functions"),
inherits: syntaxAsyncFunctions,
visitor: {
Function(path, state) {

View File

@ -1,5 +1,6 @@
import nameFunction from "babel-helper-function-name";
import template from "babel-template";
import syntaxClassProperties from "babel-plugin-syntax-class-properties";
export default function ({ types: t }) {
const findBareSupers = {
@ -39,7 +40,7 @@ export default function ({ types: t }) {
);
return {
inherits: require("babel-plugin-syntax-class-properties"),
inherits: syntaxClassProperties,
visitor: {
Class(path, state) {

View File

@ -1,6 +1,7 @@
// Fork of https://github.com/loganfsmyth/babel-plugin-transform-decorators-legacy
import template from "babel-template";
import syntaxDecorators from "babel-plugin-syntax-decorators";
const buildClassDecorator = template(`
DECORATOR(CLASS_REF = INNER) || CLASS_REF;
@ -281,7 +282,7 @@ export default function({ types: t }) {
}
return {
inherits: require("babel-plugin-syntax-decorators"),
inherits: syntaxDecorators,
visitor: {
ExportDefaultDeclaration(path) {

View File

@ -1,6 +1,8 @@
import syntaxDoExpressions from "babel-plugin-syntax-do-expressions";
export default function () {
return {
inherits: require("babel-plugin-syntax-do-expressions"),
inherits: syntaxDoExpressions,
visitor: {
DoExpression(path) {

View File

@ -1,4 +1,5 @@
import template from "babel-template";
import transformCommonjs from "babel-plugin-transform-es2015-modules-commonjs";
const buildDefine = template(`
define(MODULE_NAME, [SOURCES], FACTORY);
@ -58,7 +59,7 @@ export default function ({ types: t }) {
};
return {
inherits: require("babel-plugin-transform-es2015-modules-commonjs"),
inherits: transformCommonjs,
pre() {
// source strings

View File

@ -1,6 +1,7 @@
import { basename, extname } from "path";
import template from "babel-template";
import * as t from "babel-types";
import transformStrictMode from "babel-plugin-transform-strict-mode";
const buildRequire = template(`
require($0);
@ -127,7 +128,7 @@ export default function () {
};
return {
inherits: require("babel-plugin-transform-strict-mode"),
inherits: transformStrictMode,
visitor: {
ThisExpression(path, state) {

View File

@ -1,5 +1,6 @@
import { basename, extname } from "path";
import template from "babel-template";
import transformAMD from "babel-plugin-transform-es2015-modules-amd";
const buildPrerequisiteAssignment = template(`
GLOBAL_REFERENCE = GLOBAL_REFERENCE || {}
@ -42,7 +43,7 @@ export default function ({ types: t }) {
}
return {
inherits: require("babel-plugin-transform-es2015-modules-amd"),
inherits: transformAMD,
visitor: {
Program: {

View File

@ -1,8 +1,9 @@
import build from "babel-helper-builder-binary-assignment-operator-visitor";
import syntaxExponentiationOperator from "babel-plugin-syntax-exponentiation-operator";
export default function ({ types: t }) {
return {
inherits: require("babel-plugin-syntax-exponentiation-operator"),
inherits: syntaxExponentiationOperator,
visitor: build({
operator: "**",

View File

@ -1,3 +1,5 @@
import syntaxExportExtensions from "babel-plugin-syntax-export-extensions";
export default function ({ types: t }) {
function build(node, nodes, scope) {
const first = node.specifiers[0];
@ -20,7 +22,7 @@ export default function ({ types: t }) {
}
return {
inherits: require("babel-plugin-syntax-export-extensions"),
inherits: syntaxExportExtensions,
visitor: {
ExportNamedDeclaration(path) {

View File

@ -1,3 +1,5 @@
import syntaxFlow from "babel-plugin-syntax-flow";
export default function ({ types: t }) {
function wrapInFlowComment(path, parent) {
path.addComment("trailing", generateComment(path, parent));
@ -12,7 +14,7 @@ export default function ({ types: t }) {
}
return {
inherits: require("babel-plugin-syntax-flow"),
inherits: syntaxFlow,
visitor: {
TypeCastExpression(path) {

View File

@ -1,8 +1,10 @@
import syntaxFlow from "babel-plugin-syntax-flow";
export default function ({ types: t }) {
const FLOW_DIRECTIVE = "@flow";
return {
inherits: require("babel-plugin-syntax-flow"),
inherits: syntaxFlow,
visitor: {
Program(path, { file: { ast: { comments } } }) {

View File

@ -1,3 +1,5 @@
import syntaxFunctionBind from "babel-plugin-syntax-function-bind";
export default function ({ types: t }) {
function getTempId(scope) {
let id = scope.path.getData("functionBind");
@ -29,7 +31,7 @@ export default function ({ types: t }) {
}
return {
inherits: require("babel-plugin-syntax-function-bind"),
inherits: syntaxFunctionBind,
visitor: {
CallExpression({ node, scope }) {

View File

@ -1,3 +1,5 @@
import syntaxObjectRestSpread from "babel-plugin-syntax-object-rest-spread";
export default function ({ types: t }) {
function hasRestProperty(path) {
let foundRestProperty = false;
@ -63,7 +65,7 @@ export default function ({ types: t }) {
}
return {
inherits: require("babel-plugin-syntax-object-rest-spread"),
inherits: syntaxObjectRestSpread,
visitor: {
// taken from transform-es2015-parameters/src/destructuring.js

View File

@ -1 +1 @@
module.exports = require("regenerator-transform");
export { default } from "regenerator-transform";

View File

@ -1,4 +1,4 @@
module.exports = {
export default {
builtins: {
Symbol: "symbol",
Promise: "promise",

View File

@ -9,6 +9,19 @@ import Scope from "../scope";
import * as t from "babel-types";
import { path as pathCache } from "../cache";
// NodePath is split across many files.
import * as NodePath_ancestry from "./ancestry";
import * as NodePath_inference from "./inference";
import * as NodePath_replacement from "./replacement";
import * as NodePath_evaluation from "./evaluation";
import * as NodePath_conversion from "./conversion";
import * as NodePath_introspection from "./introspection";
import * as NodePath_context from "./context";
import * as NodePath_removal from "./removal";
import * as NodePath_modification from "./modification";
import * as NodePath_family from "./family";
import * as NodePath_comments from "./comments";
const debug = buildDebug("babel");
export default class NodePath {
@ -151,17 +164,18 @@ export default class NodePath {
}
}
assign(NodePath.prototype, require("./ancestry"));
assign(NodePath.prototype, require("./inference"));
assign(NodePath.prototype, require("./replacement"));
assign(NodePath.prototype, require("./evaluation"));
assign(NodePath.prototype, require("./conversion"));
assign(NodePath.prototype, require("./introspection"));
assign(NodePath.prototype, require("./context"));
assign(NodePath.prototype, require("./removal"));
assign(NodePath.prototype, require("./modification"));
assign(NodePath.prototype, require("./family"));
assign(NodePath.prototype, require("./comments"));
assign(NodePath.prototype,
NodePath_ancestry,
NodePath_inference,
NodePath_replacement,
NodePath_evaluation,
NodePath_conversion,
NodePath_introspection,
NodePath_context,
NodePath_removal,
NodePath_modification,
NodePath_family,
NodePath_comments);
for (const type of (t.TYPES: Array<string>)) {
const typeKey = `is${type}`;