Run prettier
This commit is contained in:
@@ -42,10 +42,15 @@ const buildExportAll = template(`
|
||||
});
|
||||
`);
|
||||
|
||||
const THIS_BREAK_KEYS = ["FunctionExpression", "FunctionDeclaration", "ClassProperty",
|
||||
"ClassMethod", "ObjectMethod"];
|
||||
const THIS_BREAK_KEYS = [
|
||||
"FunctionExpression",
|
||||
"FunctionDeclaration",
|
||||
"ClassProperty",
|
||||
"ClassMethod",
|
||||
"ObjectMethod",
|
||||
];
|
||||
|
||||
export default function () {
|
||||
export default function() {
|
||||
const REASSIGN_REMAP_SKIP = Symbol();
|
||||
|
||||
const reassignmentVisitor = {
|
||||
@@ -61,8 +66,12 @@ export default function () {
|
||||
path.replaceWith(t.sequenceExpression([t.numericLiteral(0), remap]));
|
||||
} else if (path.isJSXIdentifier() && t.isMemberExpression(remap)) {
|
||||
const { object, property } = remap;
|
||||
path.replaceWith(t.JSXMemberExpression(t.JSXIdentifier(object.name),
|
||||
t.JSXIdentifier(property.name)));
|
||||
path.replaceWith(
|
||||
t.JSXMemberExpression(
|
||||
t.JSXIdentifier(object.name),
|
||||
t.JSXIdentifier(property.name),
|
||||
),
|
||||
);
|
||||
} else {
|
||||
path.replaceWith(remap);
|
||||
}
|
||||
@@ -104,9 +113,17 @@ export default function () {
|
||||
// redeclared in this scope
|
||||
if (this.scope.getBinding(name) !== path.scope.getBinding(name)) return;
|
||||
|
||||
const node = t.assignmentExpression(path.node.operator[0] + "=", arg.node, t.numericLiteral(1));
|
||||
const node = t.assignmentExpression(
|
||||
path.node.operator[0] + "=",
|
||||
arg.node,
|
||||
t.numericLiteral(1),
|
||||
);
|
||||
|
||||
if ((path.parentPath.isExpressionStatement() && !path.isCompletionRecord()) || path.node.prefix) {
|
||||
if (
|
||||
(path.parentPath.isExpressionStatement() &&
|
||||
!path.isCompletionRecord()) ||
|
||||
path.node.prefix
|
||||
) {
|
||||
path.replaceWith(node);
|
||||
this.requeueInParent(path);
|
||||
return;
|
||||
@@ -118,7 +135,8 @@ export default function () {
|
||||
let operator;
|
||||
if (path.node.operator === "--") {
|
||||
operator = "+";
|
||||
} else { // "++"
|
||||
} else {
|
||||
// "++"
|
||||
operator = "-";
|
||||
}
|
||||
nodes.push(t.binaryExpression(operator, arg.node, t.numericLiteral(1)));
|
||||
@@ -139,7 +157,7 @@ export default function () {
|
||||
|
||||
if (
|
||||
state.opts.allowTopLevelThis !== true &&
|
||||
!path.findParent((path) => THIS_BREAK_KEYS.indexOf(path.type) >= 0)
|
||||
!path.findParent(path => THIS_BREAK_KEYS.indexOf(path.type) >= 0)
|
||||
) {
|
||||
path.replaceWith(t.identifier("undefined"));
|
||||
}
|
||||
@@ -177,12 +195,15 @@ export default function () {
|
||||
const cached = requires[source];
|
||||
if (cached) return cached;
|
||||
|
||||
const ref = path.scope.generateUidIdentifier(basename(source, extname(source)));
|
||||
const ref = path.scope.generateUidIdentifier(
|
||||
basename(source, extname(source)),
|
||||
);
|
||||
|
||||
const varDecl = t.variableDeclaration("var", [
|
||||
t.variableDeclarator(ref, buildRequire(
|
||||
t.stringLiteral(source)
|
||||
).expression),
|
||||
t.variableDeclarator(
|
||||
ref,
|
||||
buildRequire(t.stringLiteral(source)).expression,
|
||||
),
|
||||
]);
|
||||
|
||||
// Copy location from the original import statement for sourcemap
|
||||
@@ -197,7 +218,7 @@ export default function () {
|
||||
|
||||
topNodes.push(varDecl);
|
||||
|
||||
return requires[source] = ref;
|
||||
return (requires[source] = ref);
|
||||
}
|
||||
|
||||
function addTo(obj, key, arr) {
|
||||
@@ -209,11 +230,16 @@ export default function () {
|
||||
if (path.isExportDeclaration()) {
|
||||
hasExports = true;
|
||||
|
||||
const specifiers = [].concat(path.get("declaration"), path.get("specifiers"));
|
||||
const specifiers = [].concat(
|
||||
path.get("declaration"),
|
||||
path.get("specifiers"),
|
||||
);
|
||||
for (const specifier of specifiers) {
|
||||
const ids = specifier.getBindingIdentifiers();
|
||||
if (ids.__esModule) {
|
||||
throw specifier.buildCodeFrameError("Illegal export \"__esModule\"");
|
||||
throw specifier.buildCodeFrameError(
|
||||
'Illegal export "__esModule"',
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -233,7 +259,7 @@ export default function () {
|
||||
if (typeof path.node._blockHoist === "number") {
|
||||
importsEntry.maxBlockHoist = Math.max(
|
||||
path.node._blockHoist,
|
||||
importsEntry.maxBlockHoist
|
||||
importsEntry.maxBlockHoist,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -250,7 +276,12 @@ export default function () {
|
||||
topNodes.push(buildExportsAssignment(defNode, id));
|
||||
path.replaceWith(declaration.node);
|
||||
} else {
|
||||
topNodes.push(buildExportsAssignment(defNode, t.toExpression(declaration.node)));
|
||||
topNodes.push(
|
||||
buildExportsAssignment(
|
||||
defNode,
|
||||
t.toExpression(declaration.node),
|
||||
),
|
||||
);
|
||||
path.remove();
|
||||
}
|
||||
} else if (declaration.isClassDeclaration()) {
|
||||
@@ -263,7 +294,12 @@ export default function () {
|
||||
buildExportsAssignment(defNode, id),
|
||||
]);
|
||||
} else {
|
||||
path.replaceWith(buildExportsAssignment(defNode, t.toExpression(declaration.node)));
|
||||
path.replaceWith(
|
||||
buildExportsAssignment(
|
||||
defNode,
|
||||
t.toExpression(declaration.node),
|
||||
),
|
||||
);
|
||||
|
||||
// Manualy re-queue `export default class {}` expressions so that the ES3 transform
|
||||
// has an opportunity to convert them. Ideally this would happen automatically from the
|
||||
@@ -271,7 +307,12 @@ export default function () {
|
||||
path.parentPath.requeue(path.get("expression.left"));
|
||||
}
|
||||
} else {
|
||||
path.replaceWith(buildExportsAssignment(t.identifier("default"), declaration.node));
|
||||
path.replaceWith(
|
||||
buildExportsAssignment(
|
||||
t.identifier("default"),
|
||||
declaration.node,
|
||||
),
|
||||
);
|
||||
|
||||
// Manualy re-queue `export default foo;` expressions so that the ES3 transform
|
||||
// has an opportunity to convert them. Ideally this would happen automatically from the
|
||||
@@ -306,7 +347,9 @@ export default function () {
|
||||
|
||||
if (id.isIdentifier()) {
|
||||
addTo(exports, id.node.name, id.node);
|
||||
init.replaceWith(buildExportsAssignment(id.node, init.node).expression);
|
||||
init.replaceWith(
|
||||
buildExportsAssignment(id.node, init.node).expression,
|
||||
);
|
||||
nonHoistedExportNames[id.node.name] = true;
|
||||
} else if (id.isObjectPattern()) {
|
||||
for (let i = 0; i < id.node.properties.length; i++) {
|
||||
@@ -318,7 +361,9 @@ export default function () {
|
||||
propValue = prop.argument;
|
||||
}
|
||||
addTo(exports, propValue.name, propValue);
|
||||
exportsToInsert.push(buildExportsAssignment(propValue, propValue));
|
||||
exportsToInsert.push(
|
||||
buildExportsAssignment(propValue, propValue),
|
||||
);
|
||||
nonHoistedExportNames[propValue.name] = true;
|
||||
}
|
||||
} else if (id.isArrayPattern() && id.node.elements) {
|
||||
@@ -332,7 +377,9 @@ export default function () {
|
||||
}
|
||||
const name = elem.name;
|
||||
addTo(exports, name, elem);
|
||||
exportsToInsert.push(buildExportsAssignment(elem, elem));
|
||||
exportsToInsert.push(
|
||||
buildExportsAssignment(elem, elem),
|
||||
);
|
||||
nonHoistedExportNames[name] = true;
|
||||
}
|
||||
}
|
||||
@@ -356,15 +403,25 @@ export default function () {
|
||||
// todo
|
||||
} else if (specifier.isExportSpecifier()) {
|
||||
if (!noInterop && specifier.node.local.name === "default") {
|
||||
topNodes.push(buildExportsFrom(t.stringLiteral(specifier.node.exported.name),
|
||||
t.memberExpression(
|
||||
t.callExpression(this.addHelper("interopRequireDefault"), [ref]),
|
||||
specifier.node.local
|
||||
)
|
||||
));
|
||||
topNodes.push(
|
||||
buildExportsFrom(
|
||||
t.stringLiteral(specifier.node.exported.name),
|
||||
t.memberExpression(
|
||||
t.callExpression(
|
||||
this.addHelper("interopRequireDefault"),
|
||||
[ref],
|
||||
),
|
||||
specifier.node.local,
|
||||
),
|
||||
),
|
||||
);
|
||||
} else {
|
||||
topNodes.push(buildExportsFrom(t.stringLiteral(specifier.node.exported.name),
|
||||
t.memberExpression(ref, specifier.node.local)));
|
||||
topNodes.push(
|
||||
buildExportsFrom(
|
||||
t.stringLiteral(specifier.node.exported.name),
|
||||
t.memberExpression(ref, specifier.node.local),
|
||||
),
|
||||
);
|
||||
}
|
||||
nonHoistedExportNames[specifier.node.exported.name] = true;
|
||||
}
|
||||
@@ -372,16 +429,28 @@ export default function () {
|
||||
} else {
|
||||
for (const specifier of specifiers) {
|
||||
if (specifier.isExportSpecifier()) {
|
||||
addTo(exports, specifier.node.local.name, specifier.node.exported);
|
||||
addTo(
|
||||
exports,
|
||||
specifier.node.local.name,
|
||||
specifier.node.exported,
|
||||
);
|
||||
nonHoistedExportNames[specifier.node.exported.name] = true;
|
||||
nodes.push(buildExportsAssignment(specifier.node.exported, specifier.node.local));
|
||||
nodes.push(
|
||||
buildExportsAssignment(
|
||||
specifier.node.exported,
|
||||
specifier.node.local,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
path.replaceWithMultiple(nodes);
|
||||
} else if (path.isExportAllDeclaration()) {
|
||||
const exportNode = buildExportAll({
|
||||
OBJECT: addRequire(path.node.source.value, path.node._blockHoist),
|
||||
OBJECT: addRequire(
|
||||
path.node.source.value,
|
||||
path.node._blockHoist,
|
||||
),
|
||||
});
|
||||
exportNode.loc = path.node.loc;
|
||||
topNodes.push(exportNode);
|
||||
@@ -407,8 +476,8 @@ export default function () {
|
||||
specifier.local,
|
||||
t.callExpression(
|
||||
this.addHelper("interopRequireWildcard"),
|
||||
[uid]
|
||||
)
|
||||
[uid],
|
||||
),
|
||||
),
|
||||
]);
|
||||
|
||||
@@ -420,7 +489,10 @@ export default function () {
|
||||
}
|
||||
wildcard = specifier.local;
|
||||
} else if (t.isImportDefaultSpecifier(specifier)) {
|
||||
specifiers[i] = t.importSpecifier(specifier.local, t.identifier("default"));
|
||||
specifiers[i] = t.importSpecifier(
|
||||
specifier.local,
|
||||
t.identifier("default"),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -431,14 +503,16 @@ export default function () {
|
||||
if (wildcard) {
|
||||
target = wildcard;
|
||||
} else if (!noInterop) {
|
||||
target = wildcard = path.scope.generateUidIdentifier(uid.name);
|
||||
target = wildcard = path.scope.generateUidIdentifier(
|
||||
uid.name,
|
||||
);
|
||||
const varDecl = t.variableDeclaration("var", [
|
||||
t.variableDeclarator(
|
||||
target,
|
||||
t.callExpression(
|
||||
this.addHelper("interopRequireDefault"),
|
||||
[uid]
|
||||
)
|
||||
[uid],
|
||||
),
|
||||
),
|
||||
]);
|
||||
|
||||
@@ -449,8 +523,10 @@ export default function () {
|
||||
topNodes.push(varDecl);
|
||||
}
|
||||
}
|
||||
remaps[specifier.local.name] = t.memberExpression(target,
|
||||
t.cloneWithoutLoc(specifier.imported));
|
||||
remaps[specifier.local.name] = t.memberExpression(
|
||||
target,
|
||||
t.cloneWithoutLoc(specifier.imported),
|
||||
);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
@@ -462,25 +538,29 @@ export default function () {
|
||||
}
|
||||
|
||||
if (hasImports && Object.keys(nonHoistedExportNames).length) {
|
||||
|
||||
// avoid creating too long of export assignment to prevent stack overflow
|
||||
const maxHoistedExportsNodeAssignmentLength = 100;
|
||||
const nonHoistedExportNamesArr = Object.keys(nonHoistedExportNames);
|
||||
|
||||
for (
|
||||
let currentExportsNodeAssignmentLength = 0;
|
||||
currentExportsNodeAssignmentLength < nonHoistedExportNamesArr.length;
|
||||
currentExportsNodeAssignmentLength <
|
||||
nonHoistedExportNamesArr.length;
|
||||
currentExportsNodeAssignmentLength += maxHoistedExportsNodeAssignmentLength
|
||||
) {
|
||||
const nonHoistedExportNamesChunk = nonHoistedExportNamesArr.slice(
|
||||
currentExportsNodeAssignmentLength,
|
||||
currentExportsNodeAssignmentLength + maxHoistedExportsNodeAssignmentLength);
|
||||
currentExportsNodeAssignmentLength +
|
||||
maxHoistedExportsNodeAssignmentLength,
|
||||
);
|
||||
|
||||
let hoistedExportsNode = t.identifier("undefined");
|
||||
|
||||
nonHoistedExportNamesChunk.forEach(function (name) {
|
||||
hoistedExportsNode = buildExportsAssignment(t.identifier(name), hoistedExportsNode)
|
||||
.expression;
|
||||
nonHoistedExportNamesChunk.forEach(function(name) {
|
||||
hoistedExportsNode = buildExportsAssignment(
|
||||
t.identifier(name),
|
||||
hoistedExportsNode,
|
||||
).expression;
|
||||
});
|
||||
|
||||
const node = t.expressionStatement(hoistedExportsNode);
|
||||
@@ -493,7 +573,9 @@ export default function () {
|
||||
// add __esModule declaration if this file has any exports
|
||||
if (hasExports && !strict) {
|
||||
let buildTemplate = buildExportsModuleDeclaration;
|
||||
if (this.opts.loose) buildTemplate = buildLooseExportsModuleDeclaration;
|
||||
if (this.opts.loose) {
|
||||
buildTemplate = buildLooseExportsModuleDeclaration;
|
||||
}
|
||||
|
||||
const declar = buildTemplate();
|
||||
declar._blockHoist = 3;
|
||||
@@ -506,7 +588,7 @@ export default function () {
|
||||
remaps,
|
||||
scope,
|
||||
exports,
|
||||
requeueInParent: (newPath) => path.requeue(newPath),
|
||||
requeueInParent: newPath => path.requeue(newPath),
|
||||
});
|
||||
},
|
||||
},
|
||||
|
||||
@@ -2,8 +2,8 @@ const assert = require("assert");
|
||||
const babel = require("babel-core");
|
||||
const vm = require("vm");
|
||||
|
||||
test("Re-export doesn't overwrite __esModule flag", function () {
|
||||
let code = "export * from \"./dep\";";
|
||||
test("Re-export doesn't overwrite __esModule flag", function() {
|
||||
let code = 'export * from "./dep";';
|
||||
const depStub = {
|
||||
__esModule: false,
|
||||
};
|
||||
@@ -12,7 +12,7 @@ test("Re-export doesn't overwrite __esModule flag", function () {
|
||||
module: {
|
||||
exports: {},
|
||||
},
|
||||
require: function (id) {
|
||||
require: function(id) {
|
||||
if (id === "./dep") return depStub;
|
||||
return require(id);
|
||||
},
|
||||
@@ -20,10 +20,8 @@ test("Re-export doesn't overwrite __esModule flag", function () {
|
||||
context.exports = context.module.exports;
|
||||
|
||||
code = babel.transform(code, {
|
||||
"plugins": [
|
||||
[require("../"), { loose: true }],
|
||||
],
|
||||
"ast": false,
|
||||
plugins: [[require("../"), { loose: true }]],
|
||||
ast: false,
|
||||
}).code;
|
||||
|
||||
vm.runInNewContext(code, context);
|
||||
@@ -32,6 +30,6 @@ test("Re-export doesn't overwrite __esModule flag", function () {
|
||||
assert.equal(
|
||||
context.exports.__esModule,
|
||||
true,
|
||||
"Expected exports.__esModule === true"
|
||||
"Expected exports.__esModule === true",
|
||||
);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user