Only transform this/use strict if a module.

This commit is contained in:
Logan Smyth
2017-09-14 17:36:25 -07:00
parent 8e6b5de042
commit 2b88e079ef
27 changed files with 46 additions and 58 deletions

View File

@@ -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() {

View File

@@ -1,5 +1,3 @@
"use strict";
var _values = values;
var _fieldName = fieldName;
value = _values[_fieldName];

View File

@@ -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,
});

View File

@@ -1,5 +1,3 @@
"use strict";
var Child =
/*#__PURE__*/
function (_Parent) {

View File

@@ -1,5 +1,3 @@
"use strict";
var Child =
/*#__PURE__*/
function (_Parent) {

View File

@@ -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; } }

View File

@@ -1,5 +1,3 @@
"use strict";
var A =
/*#__PURE__*/
function (_B) {

View File

@@ -1,5 +1,3 @@
"use strict";
var x = {
Foo:
/*#__PURE__*/

View File

@@ -1,5 +1,3 @@
"use strict";
var A = function A() {
babelHelpers.classCallCheck(this, A);
};

View File

@@ -1,5 +1,3 @@
"use strict";
var A =
/*#__PURE__*/
function () {

View File

@@ -1,5 +1,3 @@
"use strict";
var Example =
/*#__PURE__*/
function () {

View File

@@ -1,5 +1,3 @@
"use strict";
var Foo =
/*#__PURE__*/
function () {

View File

@@ -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; }

View File

@@ -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,

View File

@@ -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,

View File

@@ -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,

View File

@@ -1,5 +1,3 @@
"use strict";
var C =
/*#__PURE__*/
function () {

View File

@@ -1,5 +1,3 @@
"use strict";
function test(fn) {
return function _callee() {
var _args = arguments;

View File

@@ -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;

View File

@@ -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", () => {