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 "babel-polyfill";
import register from "babel-register"; import register from "babel-register";
import pkg from "../package.json";
const program = new commander.Command("babel-node"); const program = new commander.Command("babel-node");
program.option("-e, --eval [script]", "Evaluate script"); 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("-w, --plugins [string]", "", util.list);
program.option("-b, --presets [string]", "", util.list); program.option("-b, --presets [string]", "", util.list);
const pkg = require("../package.json");
program.version(pkg.version); program.version(pkg.version);
program.usage("[options] [ -e script | script.js ] [arguments]"); program.usage("[options] [ -e script | script.js ] [arguments]");
program.parse(process.argv); program.parse(process.argv);

View File

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

View File

@ -1,10 +1,11 @@
const outputFileSync = require("output-file-sync"); import outputFileSync from "output-file-sync";
const slash = require("slash"); import slash from "slash";
const path = require("path"); import path from "path";
const util = require("./util"); import fs from "fs";
const fs = require("fs");
module.exports = function (commander, filenames) { import * as util from "./util";
export default function (commander, filenames) {
function write(src, relative) { function write(src, relative) {
// remove extension and then append back on .js // remove extension and then append back on .js
relative = relative.replace(/\.(\w*?)$/, "") + ".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"); import convertSourceMap from "convert-source-map";
const sourceMap = require("source-map"); import sourceMap from "source-map";
const slash = require("slash"); import slash from "slash";
const path = require("path"); import path from "path";
const util = require("./util"); import fs from "fs";
const fs = require("fs");
module.exports = function (commander, filenames, opts) { import * as util from "./util";
export default function (commander, filenames, opts) {
if (commander.sourceMaps === "inline") { if (commander.sourceMaps === "inline") {
opts.sourceMaps = true; opts.sourceMaps = true;
} }
@ -176,4 +177,4 @@ module.exports = function (commander, filenames, opts) {
} else { } else {
stdin(); stdin();
} }
}; }

View File

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

View File

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

View File

@ -1,6 +1,6 @@
/* eslint max-len: "off" */ /* eslint max-len: "off" */
module.exports = { export default {
filename: { filename: {
type: "filename", type: "filename",
description: "filename to use when reading from stdin - this will be used in source-maps, errors etc", 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" */ /* eslint max-len: "off" */
module.exports = { export default {
"auxiliaryComment": { "auxiliaryComment": {
"message": "Use `auxiliaryCommentBefore` or `auxiliaryCommentAfter`" "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(); this.semicolon();
} }
function variableDeclarationIdent() { function variableDeclarationIndent() {
// "let " or "var " indentation. // "let " or "var " indentation.
this.token(","); this.token(",");
this.newline(); this.newline();
if (this.endsWith("\n")) for (let i = 0; i < 4; i++) this.space(true); if (this.endsWith("\n")) for (let i = 0; i < 4; i++) this.space(true);
} }
function constDeclarationIdent() { function constDeclarationIndent() {
// "const " indentation. // "const " indentation.
this.token(","); this.token(",");
this.newline(); this.newline();
@ -262,7 +262,7 @@ export function VariableDeclaration(node: Object, parent: Object) {
let separator; let separator;
if (hasInits) { 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 parens from "./parentheses";
import * as t from "babel-types"; import * as t from "babel-types";

View File

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

View File

@ -7,6 +7,8 @@ import * as n from "./node";
import Whitespace from "./whitespace"; import Whitespace from "./whitespace";
import * as t from "babel-types"; import * as t from "babel-types";
import * as generatorFunctions from "./generators";
const SCIENTIFIC_NOTATION = /e/i; const SCIENTIFIC_NOTATION = /e/i;
const ZERO_DECIMAL_INTEGER = /\.0+$/; const ZERO_DECIMAL_INTEGER = /\.0+$/;
const NON_DECIMAL_LITERAL = /^0[box]/; 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() { function commaSeparator() {
this.token(","); this.token(",");
this.space(); 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 () { export default function () {
return { 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 remapAsyncToGenerator from "babel-helper-remap-async-to-generator";
import syntaxAsyncGenerators from "babel-plugin-syntax-async-generators";
export default function ({ types: t }) { export default function ({ types: t }) {
const yieldStarVisitor = { const yieldStarVisitor = {
@ -17,7 +18,7 @@ export default function ({ types: t }) {
}; };
return { return {
inherits: require("babel-plugin-syntax-async-generators"), inherits: syntaxAsyncGenerators,
visitor: { visitor: {
Function(path, state) { Function(path, state) {
if (!path.node.async || !path.node.generator) return; if (!path.node.async || !path.node.generator) return;

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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