Upgrade Babel to self-host with beta.46 (#7784)

This commit is contained in:
Logan Smyth 2018-04-27 15:04:37 -07:00 committed by GitHub
parent 4f312f5739
commit acf509bab5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 656 additions and 589 deletions

View File

@ -1,5 +0,0 @@
# Ensure babel-register won't compile fixtures, or try to recompile compiled code.
packages/*/test/fixtures
packages/*/lib
packages/babel-standalone/babel.js
packages/babel-preset-env-standalone/babel-preset-env.js

View File

@ -99,8 +99,6 @@ function buildRollup(packages) {
plugins: [ plugins: [
rollupBabel({ rollupBabel({
envName: "babylon", envName: "babylon",
babelrc: false,
extends: "./.babelrc.js",
}), }),
rollupNodeResolve(), rollupNodeResolve(),
], ],

View File

@ -12,12 +12,14 @@ module.exports = function(api) {
}; };
let convertESM = true; let convertESM = true;
let ignoreLib = true;
switch (env) { switch (env) {
// Configs used during bundling builds. // Configs used during bundling builds.
case "babylon": case "babylon":
case "standalone": case "standalone":
convertESM = false; convertESM = false;
ignoreLib = false;
break; break;
case "production": case "production":
// Config during builds before publish. // Config during builds before publish.
@ -40,6 +42,15 @@ module.exports = function(api) {
const config = { const config = {
comments: false, comments: false,
ignore: [
// These may not be strictly necessary with the newly-limited scope of
// babelrc searching, but including them for now because we had them
// in our .babelignore before.
"packages/*/test/fixtures",
ignoreLib ? "packages/*/lib" : null,
"packages/babel-standalone/babel.js",
"packages/babel-preset-env-standalone/babel-preset-env.js",
].filter(Boolean),
presets: [["@babel/env", envOpts]], presets: [["@babel/env", envOpts]],
plugins: [ plugins: [
// TODO: Use @babel/preset-flow when // TODO: Use @babel/preset-flow when

View File

@ -10,19 +10,19 @@
"test": "make test" "test": "make test"
}, },
"devDependencies": { "devDependencies": {
"@babel/cli": "7.0.0-beta.44", "@babel/cli": "7.0.0-beta.46",
"@babel/core": "7.0.0-beta.44", "@babel/core": "7.0.0-beta.46",
"@babel/plugin-transform-modules-commonjs": "7.0.0-beta.44", "@babel/plugin-transform-modules-commonjs": "7.0.0-beta.46",
"@babel/preset-env": "7.0.0-beta.44", "@babel/preset-env": "7.0.0-beta.46",
"@babel/preset-flow": "7.0.0-beta.44", "@babel/preset-flow": "7.0.0-beta.46",
"@babel/preset-stage-0": "7.0.0-beta.44", "@babel/preset-stage-0": "7.0.0-beta.46",
"@babel/register": "7.0.0-beta.44", "@babel/register": "7.0.0-beta.46",
"babel-core": "^7.0.0-0", "babel-core": "^7.0.0-0",
"babel-eslint": "^8.0.1", "babel-eslint": "^8.0.1",
"babel-jest": "^22.4.1", "babel-jest": "^22.4.1",
"babel-loader": "8.0.0-beta.0", "babel-loader": "8.0.0-beta.0",
"babel-plugin-transform-charcodes": "^0.1.0", "babel-plugin-transform-charcodes": "^0.1.0",
"babylon": "7.0.0-beta.44", "babylon": "7.0.0-beta.46",
"browserify": "^13.1.1", "browserify": "^13.1.1",
"bundle-collapser": "^1.2.1", "bundle-collapser": "^1.2.1",
"chalk": "^2.3.2", "chalk": "^2.3.2",

View File

@ -12,11 +12,35 @@ function assertNotIgnored(result) {
expect(result.ignored).toBeFalsy(); expect(result.ignored).toBeFalsy();
} }
function transform(code, opts) {
return babel.transform(code, {
cwd: __dirname,
...opts,
});
}
function transformFile(filename, opts, cb) {
return babel.transformFile(
filename,
{
cwd: __dirname,
...opts,
},
cb,
);
}
function transformFileSync(filename, opts) {
return babel.transformFileSync(filename, {
cwd: __dirname,
...opts,
});
}
// shim // shim
function transformAsync(code, opts) { function transformAsync(code, opts) {
return { return {
then: function(resolve) { then: function(resolve) {
resolve(babel.transform(code, opts)); resolve(transform(code, opts));
}, },
}; };
} }
@ -32,7 +56,7 @@ describe("parser and generator options", function() {
}; };
function newTransform(string) { function newTransform(string) {
return babel.transform(string, { return transform(string, {
ast: true, ast: true,
parserOpts: { parserOpts: {
parser: recast.parse, parser: recast.parse,
@ -48,7 +72,7 @@ describe("parser and generator options", function() {
it("options", function() { it("options", function() {
const string = "original;"; const string = "original;";
expect(newTransform(string).ast).toEqual( expect(newTransform(string).ast).toEqual(
babel.transform(string, { ast: true }).ast, transform(string, { ast: true }).ast,
); );
expect(newTransform(string).code).toBe(string); expect(newTransform(string).code).toBe(string);
}); });
@ -57,7 +81,7 @@ describe("parser and generator options", function() {
const experimental = "var a: number = 1;"; const experimental = "var a: number = 1;";
expect(newTransform(experimental).ast).toEqual( expect(newTransform(experimental).ast).toEqual(
babel.transform(experimental, { transform(experimental, {
ast: true, ast: true,
parserOpts: { parserOpts: {
plugins: ["flow"], plugins: ["flow"],
@ -67,7 +91,7 @@ describe("parser and generator options", function() {
expect(newTransform(experimental).code).toBe(experimental); expect(newTransform(experimental).code).toBe(experimental);
function newTransformWithPlugins(string) { function newTransformWithPlugins(string) {
return babel.transform(string, { return transform(string, {
ast: true, ast: true,
plugins: [__dirname + "/../../babel-plugin-syntax-flow"], plugins: [__dirname + "/../../babel-plugin-syntax-flow"],
parserOpts: { parserOpts: {
@ -80,7 +104,7 @@ describe("parser and generator options", function() {
} }
expect(newTransformWithPlugins(experimental).ast).toEqual( expect(newTransformWithPlugins(experimental).ast).toEqual(
babel.transform(experimental, { transform(experimental, {
ast: true, ast: true,
parserOpts: { parserOpts: {
plugins: ["flow"], plugins: ["flow"],
@ -94,7 +118,7 @@ describe("parser and generator options", function() {
const experimental = "if (true) {\n import a from 'a';\n}"; const experimental = "if (true) {\n import a from 'a';\n}";
expect(newTransform(experimental).ast).not.toBe( expect(newTransform(experimental).ast).not.toBe(
babel.transform(experimental, { transform(experimental, {
ast: true, ast: true,
parserOpts: { parserOpts: {
allowImportExportEverywhere: true, allowImportExportEverywhere: true,
@ -123,7 +147,7 @@ describe("api", function() {
babelrc: false, babelrc: false,
}; };
Object.freeze(options); Object.freeze(options);
babel.transformFile(__dirname + "/fixtures/api/file.js", options, function( transformFile(__dirname + "/fixtures/api/file.js", options, function(
err, err,
res, res,
) { ) {
@ -141,15 +165,14 @@ describe("api", function() {
}; };
Object.freeze(options); Object.freeze(options);
expect( expect(
babel.transformFileSync(__dirname + "/fixtures/api/file.js", options) transformFileSync(__dirname + "/fixtures/api/file.js", options).code,
.code,
).toBe("foo();"); ).toBe("foo();");
expect(options).toEqual({ babelrc: false }); expect(options).toEqual({ babelrc: false });
}); });
it("options throw on falsy true", function() { it("options throw on falsy true", function() {
return expect(function() { return expect(function() {
babel.transform("", { transform("", {
plugins: [__dirname + "/../../babel-plugin-syntax-jsx", false], plugins: [__dirname + "/../../babel-plugin-syntax-jsx", false],
}); });
}).toThrow(/.plugins\[1\] must be a string, object, function/); }).toThrow(/.plugins\[1\] must be a string, object, function/);
@ -170,7 +193,7 @@ describe("api", function() {
let calledRaw = 0; let calledRaw = 0;
let calledIntercept = 0; let calledIntercept = 0;
babel.transform("function foo() { bar(foobar); }", { transform("function foo() { bar(foobar); }", {
wrapPluginVisitorMethod: function(pluginAlias, visitorType, callback) { wrapPluginVisitorMethod: function(pluginAlias, visitorType, callback) {
if (pluginAlias !== "foobar") { if (pluginAlias !== "foobar") {
return callback; return callback;
@ -204,7 +227,7 @@ describe("api", function() {
let aliasBaseType = null; let aliasBaseType = null;
function execTest(passPerPreset) { function execTest(passPerPreset) {
return babel.transform("type Foo = number; let x = (y): Foo => y;", { return transform("type Foo = number; let x = (y): Foo => y;", {
sourceType: "script", sourceType: "script",
passPerPreset: passPerPreset, passPerPreset: passPerPreset,
presets: [ presets: [
@ -293,7 +316,7 @@ describe("api", function() {
const oldEnv = process.env.BABEL_ENV; const oldEnv = process.env.BABEL_ENV;
process.env.BABEL_ENV = "development"; process.env.BABEL_ENV = "development";
const result = babel.transform("", { const result = transform("", {
cwd: path.join(__dirname, "fixtures", "config", "complex-plugin-config"), cwd: path.join(__dirname, "fixtures", "config", "complex-plugin-config"),
filename: path.join( filename: path.join(
__dirname, __dirname,
@ -348,7 +371,7 @@ describe("api", function() {
}); });
it("source map merging", function() { it("source map merging", function() {
const result = babel.transform( const result = transform(
[ [
/* eslint-disable max-len */ /* eslint-disable max-len */
'function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }', 'function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }',
@ -555,7 +578,7 @@ describe("api", function() {
}); });
it("default", function() { it("default", function() {
const result = babel.transform("foo;", { const result = transform("foo;", {
env: { env: {
development: { comments: false }, development: { comments: false },
}, },
@ -566,7 +589,7 @@ describe("api", function() {
it("BABEL_ENV", function() { it("BABEL_ENV", function() {
process.env.BABEL_ENV = "foo"; process.env.BABEL_ENV = "foo";
const result = babel.transform("foo;", { const result = transform("foo;", {
env: { env: {
foo: { comments: false }, foo: { comments: false },
}, },
@ -576,7 +599,7 @@ describe("api", function() {
it("NODE_ENV", function() { it("NODE_ENV", function() {
process.env.NODE_ENV = "foo"; process.env.NODE_ENV = "foo";
const result = babel.transform("foo;", { const result = transform("foo;", {
env: { env: {
foo: { comments: false }, foo: { comments: false },
}, },
@ -634,7 +657,7 @@ describe("api", function() {
}; };
it("only syntax plugin available", function(done) { it("only syntax plugin available", function(done) {
babel.transformFile( transformFile(
__dirname + "/fixtures/api/parsing-errors/only-syntax/file.js", __dirname + "/fixtures/api/parsing-errors/only-syntax/file.js",
options, options,
function(err) { function(err) {
@ -651,7 +674,7 @@ describe("api", function() {
}); });
it("both syntax and transform plugin available", function(done) { it("both syntax and transform plugin available", function(done) {
babel.transformFile( transformFile(
__dirname + "/fixtures/api/parsing-errors/syntax-and-transform/file.js", __dirname + "/fixtures/api/parsing-errors/syntax-and-transform/file.js",
options, options,
function(err) { function(err) {

View File

@ -1,11 +1,18 @@
import fs from "fs"; import fs from "fs";
import path from "path"; import path from "path";
import { loadOptions } from "../lib"; import { loadOptions as loadOptionsOrig } from "../lib";
function fixture(...args) { function fixture(...args) {
return path.join(__dirname, "fixtures", "config", ...args); return path.join(__dirname, "fixtures", "config", ...args);
} }
function loadOptions(opts) {
return loadOptionsOrig({
cwd: __dirname,
...opts,
});
}
describe("buildConfigChain", function() { describe("buildConfigChain", function() {
describe("test", () => { describe("test", () => {
describe("single", () => { describe("single", () => {

View File

@ -1,6 +1,13 @@
import { loadOptions } from "../lib"; import { loadOptions as loadOptionsOrig } from "../lib";
import path from "path"; import path from "path";
function loadOptions(opts) {
return loadOptionsOrig({
cwd: __dirname,
...opts,
});
}
describe("option-manager", () => { describe("option-manager", () => {
it("throws for babel 5 plugin", () => { it("throws for babel 5 plugin", () => {
return expect(() => { return expect(() => {

View File

@ -22,7 +22,10 @@ describe("parse", function() {
const input = fs.readFileSync(fixture("input.js"), "utf8"); const input = fs.readFileSync(fixture("input.js"), "utf8");
const output = require(fixture("output.json")); const output = require(fixture("output.json"));
const result = parse(input, { parserOpts: { plugins: ["decorators"] } }); const result = parse(input, {
parserOpts: { plugins: ["decorators"] },
cwd: fixture(),
});
expect(JSON.parse(JSON.stringify(result))).toEqual(output); expect(JSON.parse(JSON.stringify(result))).toEqual(output);
}); });
}); });

View File

@ -6,6 +6,7 @@ describe("traversal path", function() {
const expectCode = "function foo() {}"; const expectCode = "function foo() {}";
const actualCode = transform(expectCode, { const actualCode = transform(expectCode, {
cwd: __dirname,
plugins: [ plugins: [
new Plugin({ new Plugin({
visitor: { visitor: {
@ -24,6 +25,7 @@ describe("traversal path", function() {
const expectCode = "var fn = () => true;"; const expectCode = "var fn = () => true;";
const actualCode = transform(expectCode, { const actualCode = transform(expectCode, {
cwd: __dirname,
plugins: [ plugins: [
new Plugin({ new Plugin({
visitor: { visitor: {
@ -53,6 +55,7 @@ describe("traversal path", function() {
const expectCode = "var fn = () => { return true; }"; const expectCode = "var fn = () => { return true; }";
const actualCode = transform(expectCode, { const actualCode = transform(expectCode, {
cwd: __dirname,
plugins: [ plugins: [
new Plugin({ new Plugin({
visitor: { visitor: {
@ -74,6 +77,7 @@ describe("traversal path", function() {
const expectCode = "for (KEY in right);"; const expectCode = "for (KEY in right);";
const actualCode = transform(expectCode, { const actualCode = transform(expectCode, {
cwd: __dirname,
plugins: [ plugins: [
new Plugin({ new Plugin({
visitor: { visitor: {
@ -104,6 +108,7 @@ describe("traversal path", function() {
const expectCode = "for (var KEY in right);"; const expectCode = "for (var KEY in right);";
const actualCode = transform(expectCode, { const actualCode = transform(expectCode, {
cwd: __dirname,
plugins: [ plugins: [
new Plugin({ new Plugin({
visitor: { visitor: {
@ -125,6 +130,7 @@ describe("traversal path", function() {
const expectCode = "for (KEY;;);"; const expectCode = "for (KEY;;);";
const actualCode = transform(expectCode, { const actualCode = transform(expectCode, {
cwd: __dirname,
plugins: [ plugins: [
new Plugin({ new Plugin({
visitor: { visitor: {
@ -155,6 +161,7 @@ describe("traversal path", function() {
const expectCode = "for (var KEY;;);"; const expectCode = "for (var KEY;;);";
const actualCode = transform(expectCode, { const actualCode = transform(expectCode, {
cwd: __dirname,
plugins: [ plugins: [
new Plugin({ new Plugin({
visitor: { visitor: {

View File

@ -10,6 +10,7 @@ function test(sourceType, opts, initializer, expectedCode) {
} }
const result = babel.transform("", { const result = babel.transform("", {
cwd: __dirname,
sourceType, sourceType,
filename: "example" + (sourceType === "module" ? ".mjs" : ".js"), filename: "example" + (sourceType === "module" ? ".mjs" : ".js"),
babelrc: false, babelrc: false,

View File

@ -88,6 +88,10 @@ export function runCodeInTestContext(
exports: {}, exports: {},
}; };
const oldCwd = process.cwd();
try {
if (opts.filename) process.chdir(path.dirname(opts.filename));
// Expose the test options as "opts", but otherwise run the test in a CommonJS-like environment. // Expose the test options as "opts", but otherwise run the test in a CommonJS-like environment.
// Note: This isn't doing .call(module.exports, ...) because some of our tests currently // Note: This isn't doing .call(module.exports, ...) because some of our tests currently
// rely on 'this === global'. // rely on 'this === global'.
@ -96,6 +100,9 @@ export function runCodeInTestContext(
filename, filename,
displayErrors: true, displayErrors: true,
})(module.exports, req, module, filename, dirname, opts); })(module.exports, req, module, filename, dirname, opts);
} finally {
process.chdir(oldCwd);
}
} }
function wrapPackagesArray(type, names, optionsDir) { function wrapPackagesArray(type, names, optionsDir) {
@ -320,6 +327,7 @@ function run(task) {
function getOpts(self) { function getOpts(self) {
const newOpts = merge( const newOpts = merge(
{ {
cwd: path.dirname(self.filename),
filename: self.loc, filename: self.loc,
filenameRelative: self.filename, filenameRelative: self.filename,
sourceFileName: self.filename, sourceFileName: self.filename,

View File

@ -4,6 +4,7 @@ test("Doesn't use the same object for two different nodes in the AST", function(
const code = 'import Foo from "bar"; Foo; Foo;'; const code = 'import Foo from "bar"; Foo; Foo;';
const ast = babel.transform(code, { const ast = babel.transform(code, {
cwd: __dirname,
ast: true, ast: true,
plugins: [[require("../"), { loose: true }]], plugins: [[require("../"), { loose: true }]],
}).ast; }).ast;

View File

@ -19,6 +19,7 @@ test("Re-export doesn't overwrite __esModule flag", function() {
context.exports = context.module.exports; context.exports = context.module.exports;
code = babel.transform(code, { code = babel.transform(code, {
cwd: __dirname,
plugins: [[require("../"), { loose: true }]], plugins: [[require("../"), { loose: true }]],
ast: false, ast: false,
}).code; }).code;

View File

@ -1,6 +1,13 @@
import * as babel from "@babel/core"; import * as babel from "@babel/core";
import es2015 from "../lib"; import es2015 from "../lib";
function transform(code, opts) {
return babel.transform(code, {
cwd: __dirname,
...opts,
});
}
describe("es2015 preset", function() { describe("es2015 preset", function() {
it("does throw clear error when no options passed for Babel 6", () => { it("does throw clear error when no options passed for Babel 6", () => {
expect(function() { expect(function() {
@ -12,7 +19,7 @@ describe("es2015 preset", function() {
describe("loose", function() { describe("loose", function() {
it("throws on non-boolean value", function() { it("throws on non-boolean value", function() {
expect(function() { expect(function() {
babel.transform("", { presets: [[es2015, { loose: 1 }]] }); transform("", { presets: [[es2015, { loose: 1 }]] });
}).toThrow(/must be a boolean/); }).toThrow(/must be a boolean/);
}); });
}); });
@ -20,7 +27,7 @@ describe("es2015 preset", function() {
describe("spec", function() { describe("spec", function() {
it("throws on non-boolean value", function() { it("throws on non-boolean value", function() {
expect(function() { expect(function() {
babel.transform("", { presets: [[es2015, { spec: 1 }]] }); transform("", { presets: [[es2015, { spec: 1 }]] });
}).toThrow(/must be a boolean/); }).toThrow(/must be a boolean/);
}); });
}); });
@ -28,31 +35,31 @@ describe("es2015 preset", function() {
describe("modules", function() { describe("modules", function() {
it("doesn't throw when passing one false", function() { it("doesn't throw when passing one false", function() {
expect(function() { expect(function() {
babel.transform("", { presets: [[es2015, { modules: false }]] }); transform("", { presets: [[es2015, { modules: false }]] });
}).not.toThrow(); }).not.toThrow();
}); });
it("doesn't throw when passing one of: 'commonjs', 'amd', 'umd', 'systemjs", function() { it("doesn't throw when passing one of: 'commonjs', 'amd', 'umd', 'systemjs", function() {
expect(function() { expect(function() {
babel.transform("", { presets: [[es2015, { modules: "commonjs" }]] }); transform("", { presets: [[es2015, { modules: "commonjs" }]] });
}).not.toThrow(); }).not.toThrow();
expect(function() { expect(function() {
babel.transform("", { presets: [[es2015, { modules: "amd" }]] }); transform("", { presets: [[es2015, { modules: "amd" }]] });
}).not.toThrow(); }).not.toThrow();
expect(function() { expect(function() {
babel.transform("", { presets: [[es2015, { modules: "umd" }]] }); transform("", { presets: [[es2015, { modules: "umd" }]] });
}).not.toThrow(); }).not.toThrow();
expect(function() { expect(function() {
babel.transform("", { presets: [[es2015, { modules: "systemjs" }]] }); transform("", { presets: [[es2015, { modules: "systemjs" }]] });
}).not.toThrow(); }).not.toThrow();
}); });
it("throws when passing neither false nor one of: 'commonjs', 'amd', 'umd', 'systemjs'", function() { it("throws when passing neither false nor one of: 'commonjs', 'amd', 'umd', 'systemjs'", function() {
expect(function() { expect(function() {
babel.transform("", { presets: [[es2015, { modules: 1 }]] }); transform("", { presets: [[es2015, { modules: 1 }]] });
}).toThrow(); }).toThrow();
}); });
}); });

View File

@ -42,15 +42,6 @@ function webpackBuild(opts) {
// Use the bundled config so that module syntax is passed through // Use the bundled config so that module syntax is passed through
// for Webpack. // for Webpack.
envName: "standalone", envName: "standalone",
// Some of the node_modules may have their own "babel" section in
// their project.json (or a ".babelrc" file). We need to ignore
// those as we're using our own Babel options.
babelrc: false,
// We explicitly load the `.babelrc.js` file since searching is
// turned off, but we still want to use the main config.
extends: "./.babelrc.js",
}, },
}, },
], ],

1059
yarn.lock

File diff suppressed because it is too large Load Diff