Merge pull request #6280 from loganfsmyth/only-transform-modules
Only transform 'this'->'undefined' and inject 'use strict' if module statements are present
This commit is contained in:
@@ -285,16 +285,7 @@ describe("api", function() {
|
||||
|
||||
assert.equal(aliasBaseType, "NumberTypeAnnotation");
|
||||
|
||||
assert.deepEqual(
|
||||
[
|
||||
'"use strict";',
|
||||
"",
|
||||
"var x = function x(y) {",
|
||||
" return y;",
|
||||
"};",
|
||||
].join("\n"),
|
||||
result.code,
|
||||
);
|
||||
assert.deepEqual(result.code, "var x = function x(y) {\n return y;\n};");
|
||||
|
||||
// 2. passPerPreset: false
|
||||
|
||||
@@ -304,16 +295,7 @@ describe("api", function() {
|
||||
|
||||
assert.equal(aliasBaseType, null);
|
||||
|
||||
assert.deepEqual(
|
||||
[
|
||||
'"use strict";',
|
||||
"",
|
||||
"var x = function x(y) {",
|
||||
" return y;",
|
||||
"};",
|
||||
].join("\n"),
|
||||
result.code,
|
||||
);
|
||||
assert.deepEqual(result.code, "var x = function x(y) {\n return y;\n};");
|
||||
});
|
||||
|
||||
it("complex plugin and preset ordering", function() {
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
"use strict";
|
||||
|
||||
var _values = values;
|
||||
var _fieldName = fieldName;
|
||||
value = _values[_fieldName];
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import assert from "assert";
|
||||
import cloneDeep from "lodash/cloneDeep";
|
||||
import trimEnd from "lodash/trimEnd";
|
||||
import resolve from "try-resolve";
|
||||
@@ -83,24 +84,46 @@ export default function get(entryLoc): Array<Suite> {
|
||||
}
|
||||
|
||||
function push(taskName, taskDir) {
|
||||
const actualLocAlias = suiteName + "/" + taskName + "/actual.js";
|
||||
let actualLocAlias = suiteName + "/" + taskName + "/actual.js";
|
||||
let expectLocAlias = suiteName + "/" + taskName + "/expected.js";
|
||||
const execLocAlias = suiteName + "/" + taskName + "/exec.js";
|
||||
let execLocAlias = suiteName + "/" + taskName + "/exec.js";
|
||||
|
||||
const actualLoc = taskDir + "/actual.js";
|
||||
let actualLoc = taskDir + "/actual.js";
|
||||
let expectLoc = taskDir + "/expected.js";
|
||||
let execLoc = taskDir + "/exec.js";
|
||||
|
||||
if (fs.statSync(taskDir).isFile()) {
|
||||
const ext = path.extname(taskDir);
|
||||
if (ext !== ".js" && ext !== ".module.js") return;
|
||||
const hasExecJS = fs.existsSync(execLoc);
|
||||
const hasExecMJS = fs.existsSync(asMJS(execLoc));
|
||||
if (hasExecMJS) {
|
||||
assert(!hasExecJS, `${asMJS(execLoc)}: Found conflicting .js`);
|
||||
|
||||
execLoc = taskDir;
|
||||
execLoc = asMJS(execLoc);
|
||||
execLocAlias = asMJS(execLocAlias);
|
||||
}
|
||||
|
||||
if (resolve.relative(expectLoc + "on")) {
|
||||
expectLoc += "on";
|
||||
expectLocAlias += "on";
|
||||
const hasExpectJS = fs.existsSync(expectLoc);
|
||||
const hasExpectMJS = fs.existsSync(asMJS(expectLoc));
|
||||
if (hasExpectMJS) {
|
||||
assert(!hasExpectJS, `${asMJS(expectLoc)}: Found conflicting .js`);
|
||||
|
||||
expectLoc = asMJS(expectLoc);
|
||||
expectLocAlias = asMJS(expectLocAlias);
|
||||
}
|
||||
|
||||
const hasActualJS = fs.existsSync(actualLoc);
|
||||
const hasActualMJS = fs.existsSync(asMJS(actualLoc));
|
||||
if (hasActualMJS) {
|
||||
assert(!hasActualJS, `${asMJS(actualLoc)}: Found conflicting .js`);
|
||||
|
||||
actualLoc = asMJS(actualLoc);
|
||||
actualLocAlias = asMJS(actualLocAlias);
|
||||
}
|
||||
|
||||
if (fs.statSync(taskDir).isFile()) {
|
||||
const ext = path.extname(taskDir);
|
||||
if (ext !== ".js" && ext !== ".mjs") return;
|
||||
|
||||
execLoc = taskDir;
|
||||
}
|
||||
|
||||
const taskOpts = cloneDeep(suite.options);
|
||||
@@ -186,6 +209,10 @@ export function multiple(entryLoc, ignore?: Array<string>) {
|
||||
return categories;
|
||||
}
|
||||
|
||||
function asMJS(filepath) {
|
||||
return filepath.replace(/\.js$/, ".mjs");
|
||||
}
|
||||
|
||||
export function readFile(filename) {
|
||||
if (fs.existsSync(filename)) {
|
||||
let file = trimEnd(fs.readFileSync(filename, "utf8"));
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import assert from "assert";
|
||||
import * as t from "babel-types";
|
||||
import template from "babel-template";
|
||||
import chunk from "lodash/chunk";
|
||||
@@ -11,6 +12,31 @@ import normalizeAndLoadModuleMetadata, {
|
||||
|
||||
export { hasExports, isSideEffectImport };
|
||||
|
||||
export function isModule(path: NodePath, requireUnambiguous: boolean = false) {
|
||||
const { sourceType } = path.node;
|
||||
if (sourceType !== "module" && sourceType !== "script") {
|
||||
throw path.buildCodeFrameError(
|
||||
`Unknown sourceType "${sourceType}", cannot transform.`,
|
||||
);
|
||||
}
|
||||
|
||||
const filename = path.hub.file.opts.filename;
|
||||
if (/\.mjs$/.test(filename)) {
|
||||
requireUnambiguous = false;
|
||||
}
|
||||
|
||||
return (
|
||||
path.node.sourceType === "module" &&
|
||||
(!requireUnambiguous || isUnambiguousModule(path))
|
||||
);
|
||||
}
|
||||
|
||||
// This approach is not ideal. It is here to preserve compatibility for now,
|
||||
// but really this should just return true or be deleted.
|
||||
function isUnambiguousModule(path) {
|
||||
return path.get("body").some(p => p.isModuleDeclaration());
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform all of the generic ES6 module rewriting needed to handle initial
|
||||
* module processing. This function will rewrite the majority of the given
|
||||
@@ -21,6 +47,9 @@ export function rewriteModuleStatementsAndPrepareHeader(
|
||||
path: NodePath,
|
||||
{ exportName, strict, allowTopLevelThis, strictMode, loose, noInterop },
|
||||
) {
|
||||
assert(isModule(path), "Cannot process module statements in a script");
|
||||
path.node.sourceType = "script";
|
||||
|
||||
const meta = normalizeAndLoadModuleMetadata(path, exportName, {
|
||||
noInterop,
|
||||
});
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
"use strict";
|
||||
|
||||
var Child =
|
||||
/*#__PURE__*/
|
||||
function (_Parent) {
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
"use strict";
|
||||
|
||||
var Child =
|
||||
/*#__PURE__*/
|
||||
function (_Parent) {
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
"use strict";
|
||||
|
||||
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
|
||||
|
||||
function _instanceof(left, right) { if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) { return right[Symbol.hasInstance](left); } else { return left instanceof right; } }
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
"use strict";
|
||||
|
||||
var A =
|
||||
/*#__PURE__*/
|
||||
function (_B) {
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
"use strict";
|
||||
|
||||
var x = {
|
||||
Foo:
|
||||
/*#__PURE__*/
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
"use strict";
|
||||
|
||||
var A = function A() {
|
||||
babelHelpers.classCallCheck(this, A);
|
||||
};
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
"use strict";
|
||||
|
||||
var A =
|
||||
/*#__PURE__*/
|
||||
function () {
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
"use strict";
|
||||
|
||||
var Example =
|
||||
/*#__PURE__*/
|
||||
function () {
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
"use strict";
|
||||
|
||||
var Foo =
|
||||
/*#__PURE__*/
|
||||
function () {
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
"use strict";
|
||||
|
||||
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
|
||||
|
||||
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import template from "babel-template";
|
||||
import {
|
||||
isModule,
|
||||
rewriteModuleStatementsAndPrepareHeader,
|
||||
hasExports,
|
||||
isSideEffectImport,
|
||||
@@ -18,6 +19,8 @@ export default function({ types: t }) {
|
||||
visitor: {
|
||||
Program: {
|
||||
exit(path, state) {
|
||||
if (!isModule(path)) return;
|
||||
|
||||
const {
|
||||
loose,
|
||||
allowTopLevelThis,
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import {
|
||||
isModule,
|
||||
rewriteModuleStatementsAndPrepareHeader,
|
||||
isSideEffectImport,
|
||||
buildNamespaceInitStatements,
|
||||
@@ -11,6 +12,10 @@ export default function({ types: t }) {
|
||||
visitor: {
|
||||
Program: {
|
||||
exit(path, state) {
|
||||
// For now this requires unambiguous rather that just sourceType
|
||||
// because Babel currently parses all files as sourceType:module.
|
||||
if (!isModule(path, true /* requireUnambiguous */)) return;
|
||||
|
||||
const {
|
||||
loose,
|
||||
allowTopLevelThis,
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { basename, extname } from "path";
|
||||
import template from "babel-template";
|
||||
import {
|
||||
isModule,
|
||||
rewriteModuleStatementsAndPrepareHeader,
|
||||
hasExports,
|
||||
isSideEffectImport,
|
||||
@@ -107,6 +108,8 @@ export default function({ types: t }) {
|
||||
visitor: {
|
||||
Program: {
|
||||
exit(path, state) {
|
||||
if (!isModule(path)) return;
|
||||
|
||||
const {
|
||||
globals,
|
||||
exactGlobals,
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
"use strict";
|
||||
|
||||
// @flow
|
||||
var C =
|
||||
/*#__PURE__*/
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
"use strict";
|
||||
|
||||
// @flow
|
||||
var C =
|
||||
/*#__PURE__*/
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
"use strict";
|
||||
|
||||
var C =
|
||||
/*#__PURE__*/
|
||||
function () {
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
"use strict";
|
||||
|
||||
var C =
|
||||
/*#__PURE__*/
|
||||
function () {
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
"use strict";
|
||||
|
||||
function test(fn) {
|
||||
return function _callee() {
|
||||
var _args = arguments;
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
{
|
||||
"plugins": ["transform-strict-mode", "transform-es2015-modules-commonjs"]
|
||||
"plugins": ["transform-strict-mode"]
|
||||
}
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
"use strict";
|
||||
|
||||
var _this = void 0;
|
||||
var _this = this;
|
||||
|
||||
"1".concat(a);
|
||||
(function () {
|
||||
babelHelpers.newArrowCheck(this, _this);
|
||||
}).bind(void 0);
|
||||
}).bind(this);
|
||||
|
||||
function a() {
|
||||
var _this2 = this;
|
||||
|
||||
@@ -104,7 +104,7 @@ function buildRuntimeRewritePlugin(relativePath, helperName) {
|
||||
}
|
||||
|
||||
function buildHelper(helperName, modules, useBuiltIns) {
|
||||
const tree = t.program(helpers.get(helperName).nodes);
|
||||
const tree = t.program(helpers.get(helperName).nodes, [], "module");
|
||||
|
||||
const transformOpts = makeTransformOpts(modules, useBuiltIns);
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@ describe("babel-standalone", () => {
|
||||
const output = Babel.transform("class A {}", {
|
||||
presets: ["es2015-loose"],
|
||||
}).code;
|
||||
assert.equal(output, '"use strict";\n\nvar A = function A() {};');
|
||||
assert.equal(output, "var A = function A() {};");
|
||||
});
|
||||
it("handles the typescript preset", () => {
|
||||
const output = Babel.transform("var a: string;", {
|
||||
@@ -56,7 +56,7 @@ describe("babel-standalone", () => {
|
||||
};
|
||||
const output = Babel.transformFromAst(ast, "42", { presets: ["es2015"] })
|
||||
.code;
|
||||
assert.equal(output, '"use strict";\n' + "\n" + "42;");
|
||||
assert.equal(output, "42;");
|
||||
});
|
||||
|
||||
it("handles the react preset", () => {
|
||||
|
||||
@@ -513,13 +513,14 @@ defineType("NewExpression", { inherits: "CallExpression" });
|
||||
|
||||
defineType("Program", {
|
||||
visitor: ["directives", "body"],
|
||||
builder: ["body", "directives"],
|
||||
builder: ["body", "directives", "sourceType"],
|
||||
fields: {
|
||||
sourceFile: {
|
||||
validate: assertValueType("string"),
|
||||
},
|
||||
sourceType: {
|
||||
validate: assertOneOf("script", "module"),
|
||||
default: "script",
|
||||
},
|
||||
directives: {
|
||||
validate: chain(
|
||||
|
||||
Reference in New Issue
Block a user