chore: bump eslint and its plugins (#13412)

This commit is contained in:
Huáng Jùnliàng 2021-06-03 23:42:32 -04:00 committed by GitHub
parent 8720919665
commit 4eda1c8605
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
27 changed files with 906 additions and 724 deletions

View File

@ -30,13 +30,13 @@
"eslint": ">=7.5.0" "eslint": ">=7.5.0"
}, },
"dependencies": { "dependencies": {
"eslint-scope": "^5.1.0", "eslint-scope": "^5.1.1",
"eslint-visitor-keys": "^2.1.0", "eslint-visitor-keys": "^2.1.0",
"semver": "condition:BABEL_8_BREAKING ? ^7.3.4 : ^6.3.0" "semver": "condition:BABEL_8_BREAKING ? ^7.3.4 : ^6.3.0"
}, },
"devDependencies": { "devDependencies": {
"@babel/core": "workspace:*", "@babel/core": "workspace:*",
"dedent": "^0.7.0", "dedent": "^0.7.0",
"eslint": "^7.5.0" "eslint": "^7.27.0"
} }
} }

View File

@ -20,6 +20,6 @@
"eslint": ">=7.5.0" "eslint": ">=7.5.0"
}, },
"devDependencies": { "devDependencies": {
"eslint": "^7.5.0" "eslint": "^7.27.0"
} }
} }

View File

@ -31,6 +31,6 @@
}, },
"homepage": "https://babel.dev/", "homepage": "https://babel.dev/",
"devDependencies": { "devDependencies": {
"eslint": "^7.5.0" "eslint": "^7.27.0"
} }
} }

View File

@ -41,6 +41,6 @@
}, },
"devDependencies": { "devDependencies": {
"clone-deep": "^4.0.1", "clone-deep": "^4.0.1",
"eslint": "^7.5.0" "eslint": "^7.27.0"
} }
} }

View File

@ -15,6 +15,6 @@
"@babel/preset-env": "workspace:^7.12.13", "@babel/preset-env": "workspace:^7.12.13",
"@babel/preset-flow": "workspace:^7.12.13", "@babel/preset-flow": "workspace:^7.12.13",
"@babel/preset-react": "workspace:^7.12.13", "@babel/preset-react": "workspace:^7.12.13",
"eslint": "^7.5.0" "eslint": "^7.27.0"
} }
} }

View File

@ -8,8 +8,8 @@
"@babel/eslint-parser": "workspace:^7.14.4", "@babel/eslint-parser": "workspace:^7.14.4",
"@babel/preset-react": "workspace:^7.13.13", "@babel/preset-react": "workspace:^7.13.13",
"dedent": "^0.7.0", "dedent": "^0.7.0",
"eslint": "^7.5.0", "eslint": "^7.27.0",
"eslint-plugin-import": "^2.22.0", "eslint-plugin-import": "^2.23.4",
"npm-babel-parser": "npm:@babel/parser@^7.14.0" "npm-babel-parser": "npm:@babel/parser@^7.14.0"
} }
} }

View File

@ -33,10 +33,12 @@ const baseEslintOpts = {
* @param object opts * @param object opts
* @param function done * @param function done
*/ */
function lint(opts, done) { function lint(opts) {
readFixture(opts.fixture, (err, src) => { return new Promise((resolve, reject) => {
if (err) return done(err); readFixture(opts.fixture, (err, src) => {
done(null, eslint.linter.verify(src, opts.eslint)); if (err) return reject(err);
resolve(eslint.linter.verify(src, opts.eslint));
});
}); });
} }
@ -66,18 +68,14 @@ function strictSuite() {
eslintOpts.rules[ruleId] = [errorLevel, "never"]; eslintOpts.rules[ruleId] = [errorLevel, "never"];
["global-with", "function-with"].forEach(fixture => { ["global-with", "function-with"].forEach(fixture => {
it(`should error on ${fixture.match(/^[^-]+/)[0]} directive`, done => { it(`should error on ${
lint( fixture.match(/^[^-]+/)[0]
{ } directive`, async () => {
fixture: ["strict", fixture], const report = await lint({
eslint: eslintOpts, fixture: ["strict", fixture],
}, eslint: eslintOpts,
(err, report) => { });
if (err) return done(err); expect(report[0].ruleId).toBe(ruleId);
expect(report[0].ruleId).toBe(ruleId);
done();
},
);
}); });
}); });
}); });
@ -88,65 +86,41 @@ function strictSuite() {
}); });
eslintOpts.rules[ruleId] = [errorLevel, "global"]; eslintOpts.rules[ruleId] = [errorLevel, "global"];
it("shouldn't error on single global directive", done => { it("shouldn't error on single global directive", async () => {
lint( const report = await lint({
{ fixture: ["strict", "global-with"],
fixture: ["strict", "global-with"], eslint: eslintOpts,
eslint: eslintOpts, });
}, expect(report.length).toBe(0);
(err, report) => {
if (err) return done(err);
expect(report.length).toBe(0);
done();
},
);
}); });
it("should error twice on global directive: no and function directive: yes", done => { it("should error twice on global directive: no and function directive: yes", async () => {
lint( const report = await lint({
{ fixture: ["strict", "function-with"],
fixture: ["strict", "function-with"], eslint: eslintOpts,
eslint: eslintOpts, });
}, expect(report[0].ruleId).toBe(ruleId);
(err, report) => {
if (err) return done(err);
expect(report[0].ruleId).toBe(ruleId);
done();
},
);
}); });
it("should error on function directive", done => { it("should error on function directive", async () => {
lint( const report = await lint({
{ fixture: ["strict", "global-with-function-with"],
fixture: ["strict", "global-with-function-with"], eslint: eslintOpts,
eslint: eslintOpts, });
}, expect(report[0].ruleId).toBe(ruleId);
(err, report) => { // This is to make sure the test fails prior to adapting Babel AST
if (err) return done(err); // directive representation to ESLint format. Otherwise it reports an
expect(report[0].ruleId).toBe(ruleId); // error for missing global directive that masquerades as the expected
// This is to make sure the test fails prior to adapting Babel AST // result of the previous assertion.
// directive representation to ESLint format. Otherwise it reports an expect(report[0].nodeType).not.toBe("Program");
// error for missing global directive that masquerades as the expected
// result of the previous assertion.
expect(report[0].nodeType).not.toBe("Program");
done();
},
);
}); });
it("should error on no directive", done => { it("should error on no directive", async () => {
lint( const report = await lint({
{ fixture: ["strict", "none"],
fixture: ["strict", "none"], eslint: eslintOpts,
eslint: eslintOpts, });
}, expect(report[0].ruleId).toBe(ruleId);
(err, report) => {
if (err) return done(err);
expect(report[0].ruleId).toBe(ruleId);
done();
},
);
}); });
}); });
@ -156,63 +130,39 @@ function strictSuite() {
}); });
eslintOpts.rules[ruleId] = [errorLevel, "function"]; eslintOpts.rules[ruleId] = [errorLevel, "function"];
it("shouldn't error on single function directive", done => { it("shouldn't error on single function directive", async () => {
lint( const report = await lint({
{ fixture: ["strict", "none"],
fixture: ["strict", "function-with"], eslint: eslintOpts,
eslint: eslintOpts, });
}, expect(report.length).toBe(0);
(err, report) => {
if (err) return done(err);
expect(report.length).toBe(0);
done();
},
);
}); });
it("should error twice on function directive: no and global directive: yes", done => { it("should error twice on function directive: no and global directive: yes", async () => {
lint( const report = await lint({
{ fixture: ["strict", "global-with-function-without"],
fixture: ["strict", "global-with-function-without"], eslint: eslintOpts,
eslint: eslintOpts, });
}, [0, 1].forEach(i => {
(err, report) => { expect(report[i].ruleId).toBe(ruleId);
if (err) return done(err); });
[0, 1].forEach(i => {
expect(report[i].ruleId).toBe(ruleId);
});
done();
},
);
}); });
it("should error on only global directive", done => { it("should error on only global directive", async () => {
lint( const report = await lint({
{ fixture: ["strict", "global-with"],
fixture: ["strict", "global-with"], eslint: eslintOpts,
eslint: eslintOpts, });
}, expect(report[0].ruleId).toBe(ruleId);
(err, report) => {
if (err) return done(err);
expect(report[0].ruleId).toBe(ruleId);
done();
},
);
}); });
it("should error on extraneous global directive", done => { it("should error on extraneous global directive", async () => {
lint( const report = await lint({
{ fixture: ["strict", "global-with-function-with"],
fixture: ["strict", "global-with-function-with"], eslint: eslintOpts,
eslint: eslintOpts, });
}, expect(report[0].ruleId).toBe(ruleId);
(err, report) => { expect(report[0].nodeType.indexOf("Function")).toBe(-1);
if (err) return done(err);
expect(report[0].ruleId).toBe(ruleId);
expect(report[0].nodeType.indexOf("Function")).toBe(-1);
done();
},
);
}); });
}); });
} }

View File

@ -43,12 +43,12 @@
"babel-plugin-transform-charcodes": "^0.2.0", "babel-plugin-transform-charcodes": "^0.2.0",
"chalk": "^2.4.2", "chalk": "^2.4.2",
"charcodes": "^0.2.0", "charcodes": "^0.2.0",
"eslint": "^7.5.0", "eslint": "^7.27.0",
"eslint-import-resolver-node": "^0.3.3", "eslint-import-resolver-node": "^0.3.4",
"eslint-plugin-flowtype": "^4.6.0", "eslint-plugin-flowtype": "^5.7.2",
"eslint-plugin-import": "^2.22.0", "eslint-plugin-import": "^2.23.4",
"eslint-plugin-jest": "^23.8.2", "eslint-plugin-jest": "^24.3.6",
"eslint-plugin-prettier": "^3.1.2", "eslint-plugin-prettier": "^3.4.0",
"fancy-log": "^1.3.3", "fancy-log": "^1.3.3",
"flow-bin": "^0.123.0", "flow-bin": "^0.123.0",
"gulp": "^4.0.2", "gulp": "^4.0.2",

View File

@ -273,7 +273,7 @@ fs.readdirSync(fixtureLoc).forEach(function (binName) {
// copy .babelignore file to tmp directory // copy .babelignore file to tmp directory
opts.inFiles[".babelignore"] = helper.readFile(babelIgnoreLoc); opts.inFiles[".babelignore"] = helper.readFile(babelIgnoreLoc);
} }
// eslint-disable-next-line jest/valid-title
it(testName, buildTest(binName, testName, opts), 20000); it(testName, buildTest(binName, testName, opts), 20000);
}); });
}); });

View File

@ -33,6 +33,13 @@ function transformFile(filename, opts, cb) {
function transformFileSync(filename, opts) { function transformFileSync(filename, opts) {
return babel.transformFileSync(filename, { cwd, configFile: false, ...opts }); return babel.transformFileSync(filename, { cwd, configFile: false, ...opts });
} }
function transformFileAsync(filename, opts) {
return babel.transformFileAsync(filename, {
cwd,
configFile: false,
...opts,
});
}
function transformAsync(code, opts) { function transformAsync(code, opts) {
return babel.transformAsync(code, { cwd, configFile: false, ...opts }); return babel.transformAsync(code, { cwd, configFile: false, ...opts });
@ -147,20 +154,40 @@ describe("api", function () {
expect(babel.tokTypes).toBeDefined(); expect(babel.tokTypes).toBeDefined();
}); });
it("transformFile", function (done) { it("transformFile", function () {
const options = { const options = {
babelrc: false, babelrc: false,
}; };
Object.freeze(options); Object.freeze(options);
transformFile(cwd + "/fixtures/api/file.js", options, function (err, res) { return new Promise((resolve, reject) => {
if (err) return done(err); transformFile(
expect(res.code).toBe("foo();"); cwd + "/fixtures/api/file.js",
// keep user options untouched options,
expect(options).toEqual({ babelrc: false }); function (err, res) {
done(); if (err) return reject(err);
expect(res.code).toBe("foo();");
// keep user options untouched
expect(options).toEqual({ babelrc: false });
resolve();
},
);
}); });
}); });
it("transformFileAsync", async function () {
const options = {
babelrc: false,
};
Object.freeze(options);
const res = await transformFileAsync(
cwd + "/fixtures/api/file.js",
options,
);
expect(res.code).toBe("foo();");
// keep user options untouched
expect(options).toEqual({ babelrc: false });
});
it("transformFileSync", function () { it("transformFileSync", function () {
const options = { const options = {
babelrc: false, babelrc: false,
@ -759,38 +786,42 @@ describe("api", function () {
babelrc: false, babelrc: false,
}; };
it("only syntax plugin available", function (done) { it("only syntax plugin available", function () {
transformFile( return new Promise(resolve => {
cwd + "/fixtures/api/parsing-errors/only-syntax/file.js", transformFile(
options, cwd + "/fixtures/api/parsing-errors/only-syntax/file.js",
function (err) { options,
expect(err.message).toMatch( function (err) {
"Support for the experimental syntax 'pipelineOperator' isn't currently enabled (1:3):", expect(err.message).toMatch(
); "Support for the experimental syntax 'pipelineOperator' isn't currently enabled (1:3):",
expect(err.message).toMatch( );
"Add @babel/plugin-proposal-pipeline-operator (https://git.io/vb4SU) to the " + expect(err.message).toMatch(
"'plugins' section of your Babel config to enable transformation.", "Add @babel/plugin-proposal-pipeline-operator (https://git.io/vb4SU) to the " +
); "'plugins' section of your Babel config to enable transformation.",
done(); );
}, resolve();
); },
);
});
}); });
it("both syntax and transform plugin available", function (done) { it("both syntax and transform plugin available", function () {
transformFile( return new Promise(resolve => {
cwd + "/fixtures/api/parsing-errors/syntax-and-transform/file.js", transformFile(
options, cwd + "/fixtures/api/parsing-errors/syntax-and-transform/file.js",
function (err) { options,
expect(err.message).toMatch( function (err) {
"Support for the experimental syntax 'doExpressions' isn't currently enabled (1:2):", expect(err.message).toMatch(
); "Support for the experimental syntax 'doExpressions' isn't currently enabled (1:2):",
expect(err.message).toMatch( );
"Add @babel/plugin-proposal-do-expressions (https://git.io/vb4S3) to the " + expect(err.message).toMatch(
"'plugins' section of your Babel config to enable transformation.", "Add @babel/plugin-proposal-do-expressions (https://git.io/vb4S3) to the " +
); "'plugins' section of your Babel config to enable transformation.",
done(); );
}, resolve();
); },
);
});
}); });
}); });

View File

@ -188,25 +188,20 @@ describe("@babel/core config loading", () => {
const options2 = loadConfig(opts).options; const options2 = loadConfig(opts).options;
expect(options2.plugins.length).toBe(options1.plugins.length); expect(options2.plugins.length).toBe(options1.plugins.length);
for (let i = 0; i < options1.plugins.length; i++) { expect(options2.plugins[0]).not.toBe(options1.plugins[0]);
if (i === 0) { for (let i = 1; i < options1.plugins.length; i++) {
expect(options2.plugins[i]).not.toBe(options1.plugins[i]); expect(options2.plugins[i]).toBe(options1.plugins[i]);
} else {
expect(options2.plugins[i]).toBe(options1.plugins[i]);
}
} }
process.env.INVALIDATE_PLUGIN3 = true; process.env.INVALIDATE_PLUGIN3 = true;
const options3 = loadConfig(opts).options; const options3 = loadConfig(opts).options;
expect(options3.plugins.length).toBe(options1.plugins.length); expect(options3.plugins.length).toBe(options1.plugins.length);
expect(options3.plugins.length).toBe(6);
for (let i = 0; i < options1.plugins.length; i++) { expect(options3.plugins[0]).not.toBe(options1.plugins[0]);
if (i === 0 || i === 5) { expect(options3.plugins[5]).not.toBe(options1.plugins[5]);
expect(options3.plugins[i]).not.toBe(options1.plugins[i]); for (let i = 1; i < 5; i++) {
} else { expect(options3.plugins[i]).toBe(options1.plugins[i]);
expect(options3.plugins[i]).toBe(options1.plugins[i]);
}
} }
}); });
@ -219,26 +214,21 @@ describe("@babel/core config loading", () => {
const options2 = loadConfig(opts).options; const options2 = loadConfig(opts).options;
expect(options2.plugins.length).toBe(options1.plugins.length); expect(options2.plugins.length).toBe(options1.plugins.length);
expect(options2.plugins.length).toBe(6);
for (let i = 0; i < options1.plugins.length; i++) { expect(options2.plugins[5]).not.toBe(options1.plugins[5]);
if (i === 5) { for (let i = 0; i < 5; i++) {
expect(options2.plugins[i]).not.toBe(options1.plugins[i]); expect(options2.plugins[i]).toBe(options1.plugins[i]);
} else {
expect(options2.plugins[i]).toBe(options1.plugins[i]);
}
} }
process.env.INVALIDATE_PRESET2 = true; process.env.INVALIDATE_PRESET2 = true;
const options3 = loadConfig(opts).options; const options3 = loadConfig(opts).options;
expect(options3.plugins.length).toBe(options1.plugins.length); expect(options3.plugins.length).toBe(options1.plugins.length);
expect(options3.plugins.length).toBe(6);
for (let i = 0; i < options1.plugins.length; i++) { expect(options3.plugins[4]).not.toBe(options1.plugins[4]);
if (i === 4 || i === 5) { expect(options3.plugins[5]).not.toBe(options1.plugins[5]);
expect(options3.plugins[i]).not.toBe(options1.plugins[i]); for (let i = 0; i < 4; i++) {
} else { expect(options3.plugins[i]).toBe(options1.plugins[i]);
expect(options3.plugins[i]).toBe(options1.plugins[i]);
}
} }
}); });
@ -251,14 +241,14 @@ describe("@babel/core config loading", () => {
const options2 = loadConfig(opts).options; const options2 = loadConfig(opts).options;
expect(options2.plugins.length).toBe(options1.plugins.length); expect(options2.plugins.length).toBe(options1.plugins.length);
expect(options2.plugins.length).toBe(6);
expect(options2.plugins[0]).not.toBe(options1.plugins[0]);
expect(options2.plugins[1]).not.toBe(options1.plugins[1]);
expect(options2.plugins[4]).not.toBe(options1.plugins[4]);
expect(options2.plugins[5]).not.toBe(options1.plugins[5]);
for (let i = 0; i < options1.plugins.length; i++) { expect(options2.plugins[2]).toBe(options1.plugins[2]);
if (i === 0 || i === 1 || i === 4 || i === 5 || i === 6) { expect(options2.plugins[3]).toBe(options1.plugins[3]);
expect(options2.plugins[i]).not.toBe(options1.plugins[i]);
} else {
expect(options2.plugins[i]).toBe(options1.plugins[i]);
}
}
}); });
}); });
@ -318,14 +308,15 @@ describe("@babel/core config loading", () => {
plugins: opts.plugins.map(([plg, opt]) => [plg, { ...opt }]), plugins: opts.plugins.map(([plg, opt]) => [plg, { ...opt }]),
}).options; }).options;
expect(options2.plugins.length).toBe(options1.plugins.length); expect(options2.plugins.length).toBe(options1.plugins.length);
expect(options2.plugins.length).toBe(6);
for (let i = 0; i < options2.plugins.length; i++) { expect(options2.plugins[2]).not.toBe(options1.plugins[2]);
if (i === 2) {
expect(options2.plugins[i]).not.toBe(options1.plugins[i]); expect(options2.plugins[0]).toBe(options1.plugins[0]);
} else { expect(options2.plugins[1]).toBe(options1.plugins[1]);
expect(options2.plugins[i]).toBe(options1.plugins[i]); expect(options2.plugins[3]).toBe(options1.plugins[3]);
} expect(options2.plugins[4]).toBe(options1.plugins[4]);
} expect(options2.plugins[5]).toBe(options1.plugins[5]);
}); });
it("should invalidate the presets when given a fresh options", () => { it("should invalidate the presets when given a fresh options", () => {
@ -338,14 +329,15 @@ describe("@babel/core config loading", () => {
presets: opts.presets.map(([plg, opt]) => [plg, { ...opt }]), presets: opts.presets.map(([plg, opt]) => [plg, { ...opt }]),
}).options; }).options;
expect(options2.plugins.length).toBe(options1.plugins.length); expect(options2.plugins.length).toBe(options1.plugins.length);
expect(options2.plugins.length).toBe(6);
for (let i = 0; i < options2.plugins.length; i++) { expect(options2.plugins[3]).not.toBe(options1.plugins[3]);
if (i === 3) {
expect(options2.plugins[i]).not.toBe(options1.plugins[i]); expect(options2.plugins[0]).toBe(options1.plugins[0]);
} else { expect(options2.plugins[1]).toBe(options1.plugins[1]);
expect(options2.plugins[i]).toBe(options1.plugins[i]); expect(options2.plugins[2]).toBe(options1.plugins[2]);
} expect(options2.plugins[4]).toBe(options1.plugins[4]);
} expect(options2.plugins[5]).toBe(options1.plugins[5]);
}); });
it("should invalidate the programmatic plugins", () => { it("should invalidate the programmatic plugins", () => {
@ -357,14 +349,15 @@ describe("@babel/core config loading", () => {
const options2 = loadConfig(opts).options; const options2 = loadConfig(opts).options;
expect(options2.plugins.length).toBe(options1.plugins.length); expect(options2.plugins.length).toBe(options1.plugins.length);
expect(options2.plugins.length).toBe(6);
for (let i = 0; i < options1.plugins.length; i++) { expect(options2.plugins[2]).not.toBe(options1.plugins[2]);
if (i === 2) {
expect(options2.plugins[i]).not.toBe(options1.plugins[i]); expect(options2.plugins[0]).toBe(options1.plugins[0]);
} else { expect(options2.plugins[1]).toBe(options1.plugins[1]);
expect(options2.plugins[i]).toBe(options1.plugins[i]); expect(options2.plugins[3]).toBe(options1.plugins[3]);
} expect(options2.plugins[4]).toBe(options1.plugins[4]);
} expect(options2.plugins[5]).toBe(options1.plugins[5]);
}); });
it("should invalidate the programmatic presets and their children", () => { it("should invalidate the programmatic presets and their children", () => {
@ -376,14 +369,15 @@ describe("@babel/core config loading", () => {
const options2 = loadConfig(opts).options; const options2 = loadConfig(opts).options;
expect(options2.plugins.length).toBe(options1.plugins.length); expect(options2.plugins.length).toBe(options1.plugins.length);
expect(options2.plugins.length).toBe(6);
for (let i = 0; i < options1.plugins.length; i++) { expect(options2.plugins[3]).not.toBe(options1.plugins[3]);
if (i === 3) {
expect(options2.plugins[i]).not.toBe(options1.plugins[i]); expect(options2.plugins[0]).toBe(options1.plugins[0]);
} else { expect(options2.plugins[1]).toBe(options1.plugins[1]);
expect(options2.plugins[i]).toBe(options1.plugins[i]); expect(options2.plugins[2]).toBe(options1.plugins[2]);
} expect(options2.plugins[4]).toBe(options1.plugins[4]);
} expect(options2.plugins[5]).toBe(options1.plugins[5]);
}); });
it("should thrown when plugin is not valid", () => { it("should thrown when plugin is not valid", () => {

View File

@ -229,38 +229,31 @@ describe("option-manager", () => {
}); });
describe("presets", function () { describe("presets", function () {
function presetTest(name) { it.each([
it(name, function () { "es5_function",
const options = loadOptions({ "es5_object",
"es2015_default_function",
"es2015_default_object",
])("%p should work", name => {
const options = loadOptions({
presets: [path.join(cwd, "fixtures/option-manager/presets", name)],
});
expect(Array.isArray(options.plugins)).toBe(true);
expect(options.plugins).toHaveLength(1);
expect(options.presets).toHaveLength(0);
});
it.each([
["es2015_named", /Must export a default export when using ES6 modules/],
["es2015_invalid", /Unsupported format: string/],
["es5_invalid", /Unsupported format: string/],
])("%p should throw %p", (name, msg) => {
expect(() =>
loadOptions({
presets: [path.join(cwd, "fixtures/option-manager/presets", name)], presets: [path.join(cwd, "fixtures/option-manager/presets", name)],
}); }),
).toThrow(msg);
expect(Array.isArray(options.plugins)).toBe(true); });
expect(options.plugins).toHaveLength(1);
expect(options.presets).toHaveLength(0);
});
}
function presetThrowsTest(name, msg) {
it(name, function () {
expect(() =>
loadOptions({
presets: [path.join(cwd, "fixtures/option-manager/presets", name)],
}),
).toThrow(msg);
});
}
presetTest("es5_function");
presetTest("es5_object");
presetTest("es2015_default_function");
presetTest("es2015_default_object");
presetThrowsTest(
"es2015_named",
/Must export a default export when using ES6 modules/,
);
presetThrowsTest("es2015_invalid", /Unsupported format: string/);
presetThrowsTest("es5_invalid", /Unsupported format: string/);
}); });
}); });

View File

@ -740,15 +740,19 @@ describe("programmatic generation", function () {
expect(output).toBe(`"\\u8868\\u683C_\\u526F\\u672C"`); expect(output).toBe(`"\\u8868\\u683C_\\u526F\\u672C"`);
}); });
it("default", () => { if (process.env.BABEL_8_BREAKING) {
const output = generate(string).code; it("default", () => {
const output = generate(string).code;
if (process.env.BABEL_8_BREAKING) {
expect(output).toBe(`"表格_副本"`); expect(output).toBe(`"表格_副本"`);
} else { });
} else {
it("default in Babel 7", () => {
const output = generate(string).code;
expect(output).toBe(`"\\u8868\\u683C_\\u526F\\u672C"`); expect(output).toBe(`"\\u8868\\u683C_\\u526F\\u672C"`);
} });
}); }
}); });
describe("typescript interface declaration", () => { describe("typescript interface declaration", () => {

View File

@ -205,6 +205,7 @@ describe("bin/babel-node", function () {
opts.inFiles[".babelrc"] = helper.readFile(babelrcLoc); opts.inFiles[".babelrc"] = helper.readFile(babelrcLoc);
} }
// eslint-disable-next-line jest/valid-title
it(testName, buildTest(testName, opts), 20000); it(testName, buildTest(testName, opts), 20000);
}); });
}); });

View File

@ -53,17 +53,17 @@ describe("'decoratorsBeforeExport' option", function () {
run(AFTER, false, false); run(AFTER, false, false);
function run(code, before, throws) { function run(code, before, throws) {
const name = const codeTitle = code === BEFORE ? "before" : "after";
(before === undefined ? "default" : before) + if (throws) {
" - decorators " + test(`${before} - decorators ${codeTitle} export should throw`, function () {
(code === BEFORE ? "before" : "after") + expect(makeParser(code, { decoratorsBeforeExport: before })).toThrow();
"export"; });
} else {
test(name, function () { test(`${before} - decorators ${codeTitle} export should not throw`, function () {
const expectTheParser = expect( expect(
makeParser(code, { decoratorsBeforeExport: before }), makeParser(code, { decoratorsBeforeExport: before }),
); ).not.toThrow();
throws ? expectTheParser.toThrow() : expectTheParser.not.toThrow(); });
}); }
} }
}); });

View File

@ -91,7 +91,7 @@ describe("normalize-options", () => {
).toThrow(); ).toThrow();
}); });
it("allows exclusion of module plugins ", () => { it("allows exclusion of module plugins", () => {
expect(() => expect(() =>
normalizeOptions({ exclude: ["proposal-dynamic-import"] }), normalizeOptions({ exclude: ["proposal-dynamic-import"] }),
).not.toThrow(); ).not.toThrow();

View File

@ -4,21 +4,22 @@ import vm from "vm";
import { fileURLToPath } from "url"; import { fileURLToPath } from "url";
describe("browserify", function () { describe("browserify", function () {
it("@babel/register may be used without breaking browserify", function (done) { it("@babel/register may be used without breaking browserify", function () {
const bundler = browserify( const bundler = browserify(
path.join( path.join(
path.dirname(fileURLToPath(import.meta.url)), path.dirname(fileURLToPath(import.meta.url)),
"fixtures/browserify/register.js", "fixtures/browserify/register.js",
), ),
); );
return new Promise((resolve, reject) => {
bundler.bundle(function (err, bundle) {
if (err) return reject(err);
expect(bundle.length).toBeTruthy();
bundler.bundle(function (err, bundle) { // ensure that the code runs without throwing an exception
if (err) return done(err); vm.runInNewContext("var global = this;\n" + bundle, {});
expect(bundle.length).toBeTruthy(); resolve();
});
// ensure that the code runs without throwing an exception
vm.runInNewContext("var global = this;\n" + bundle, {});
done();
}); });
}); });
}); });

View File

@ -93,14 +93,15 @@ describe("@babel/register - caching", () => {
expect(get()).toEqual({}); expect(get()).toEqual({});
}); });
it("should create the cache after dirty", cb => { it("should create the cache after dirty", () => {
load(); load();
setDirty(); setDirty();
return new Promise(resolve => {
process.nextTick(() => { process.nextTick(() => {
expect(fs.existsSync(testCacheFilename)).toBe(true); expect(fs.existsSync(testCacheFilename)).toBe(true);
expect(get()).toEqual({}); expect(get()).toEqual({});
cb(); resolve();
});
}); });
}); });
@ -117,20 +118,22 @@ describe("@babel/register - caching", () => {
}); });
} }
it("should be disabled when CACHE_PATH is not allowed to write", cb => { it("should be disabled when CACHE_PATH is not allowed to write", () => {
writeCache({ foo: "bar" }, 0o466); writeCache({ foo: "bar" }, 0o466);
load(); load();
setDirty(); setDirty();
expect(get()).toEqual({ foo: "bar" }); expect(get()).toEqual({ foo: "bar" });
process.nextTick(() => { return new Promise(resolve => {
load(); process.nextTick(() => {
expect(get()).toEqual({}); load();
expect(consoleWarnSpy.mock.calls[0][0]).toContain( expect(get()).toEqual({});
"Babel could not write cache to file", expect(consoleWarnSpy.mock.calls[0][0]).toContain(
); "Babel could not write cache to file",
cb(); );
resolve();
});
}); });
}); });
}); });

View File

@ -145,7 +145,7 @@ describe("@babel/register", function () {
expect(sourceMapSupport).toBe(false); expect(sourceMapSupport).toBe(false);
}); });
it("returns concatenatable sourceRoot and sources", callback => { it("returns concatenatable sourceRoot and sources", async () => {
// The Source Maps R3 standard https://sourcemaps.info/spec.html states // The Source Maps R3 standard https://sourcemaps.info/spec.html states
// that `sourceRoot` is “prepended to the individual entries in the // that `sourceRoot` is “prepended to the individual entries in the
// source field.” If `sources` contains file names, and `sourceRoot` // source field.” If `sources` contains file names, and `sourceRoot`
@ -156,21 +156,15 @@ describe("@babel/register", function () {
// requires() another with @babel/register active, and I couldnt get // requires() another with @babel/register active, and I couldnt get
// that working inside a test, possibly because of jests mocking // that working inside a test, possibly because of jests mocking
// hooks, so we spawn a separate process. // hooks, so we spawn a separate process.
const output = await spawnNodeAsync([
spawnNode(["-r", registerFile, sourceMapTestFile], output => { "-r",
let err; registerFile,
sourceMapTestFile,
try { ]);
const sourceMap = JSON.parse(output); const sourceMap = JSON.parse(output);
expect(sourceMap.map.sourceRoot + sourceMap.map.sources[0]).toBe( expect(sourceMap.map.sourceRoot + sourceMap.map.sources[0]).toBe(
sourceMapNestedTestFile, sourceMapNestedTestFile,
); );
} catch (e) {
err = e;
}
callback(err);
});
}); });
test("hook transpiles with config", () => { test("hook transpiles with config", () => {
@ -196,29 +190,21 @@ describe("@babel/register", function () {
expect(result).toBe('"use strict";\n\nrequire("assert");'); expect(result).toBe('"use strict";\n\nrequire("assert");');
}); });
test("transforms modules used within register", callback => { test("transforms modules used within register", async () => {
// Need a clean environment without `convert-source-map` // Need a clean environment without `convert-source-map`
// already in the require cache, so we spawn a separate process // already in the require cache, so we spawn a separate process
spawnNode([internalModulesTestFile], output => { const output = await spawnNodeAsync([internalModulesTestFile]);
let err; const { convertSourceMap } = JSON.parse(output);
expect(convertSourceMap).toMatch("/* transformed */");
try {
const { convertSourceMap } = JSON.parse(output);
expect(convertSourceMap).toMatch("/* transformed */");
} catch (e) {
err = e;
}
callback(err);
});
}); });
}); });
function spawnNode(args, callback) { function spawnNodeAsync(args) {
const spawn = child.spawn(process.execPath, args, { cwd: __dirname }); const spawn = child.spawn(process.execPath, args, { cwd: __dirname });
let output = ""; let output = "";
let callback;
for (const stream of [spawn.stderr, spawn.stdout]) { for (const stream of [spawn.stderr, spawn.stdout]) {
stream.on("data", chunk => { stream.on("data", chunk => {
@ -229,4 +215,8 @@ function spawnNode(args, callback) {
spawn.on("close", function () { spawn.on("close", function () {
callback(output); callback(output);
}); });
return new Promise(resolve => {
callback = resolve;
});
} }

View File

@ -4,40 +4,40 @@ exports[`scope duplicate bindings catch const 1`] = `"Duplicate declaration \\"e
exports[`scope duplicate bindings catch let 1`] = `"Duplicate declaration \\"e\\""`; exports[`scope duplicate bindings catch let 1`] = `"Duplicate declaration \\"e\\""`;
exports[`scope duplicate bindings global class/class 1`] = `"Duplicate declaration \\"foo\\""`; exports[`scope duplicate bindings global class/class should fail 1`] = `"Duplicate declaration \\"foo\\""`;
exports[`scope duplicate bindings global class/const 1`] = `"Duplicate declaration \\"foo\\""`; exports[`scope duplicate bindings global class/const should fail 1`] = `"Duplicate declaration \\"foo\\""`;
exports[`scope duplicate bindings global class/function 1`] = `"Duplicate declaration \\"foo\\""`; exports[`scope duplicate bindings global class/function should fail 1`] = `"Duplicate declaration \\"foo\\""`;
exports[`scope duplicate bindings global class/let 1`] = `"Duplicate declaration \\"foo\\""`; exports[`scope duplicate bindings global class/let should fail 1`] = `"Duplicate declaration \\"foo\\""`;
exports[`scope duplicate bindings global class/var 1`] = `"Duplicate declaration \\"foo\\""`; exports[`scope duplicate bindings global class/var should fail 1`] = `"Duplicate declaration \\"foo\\""`;
exports[`scope duplicate bindings global const/class 1`] = `"Duplicate declaration \\"foo\\""`; exports[`scope duplicate bindings global const/class should fail 1`] = `"Duplicate declaration \\"foo\\""`;
exports[`scope duplicate bindings global const/const 1`] = `"Duplicate declaration \\"foo\\""`; exports[`scope duplicate bindings global const/const should fail 1`] = `"Duplicate declaration \\"foo\\""`;
exports[`scope duplicate bindings global const/function 1`] = `"Duplicate declaration \\"foo\\""`; exports[`scope duplicate bindings global const/function should fail 1`] = `"Duplicate declaration \\"foo\\""`;
exports[`scope duplicate bindings global const/let 1`] = `"Duplicate declaration \\"foo\\""`; exports[`scope duplicate bindings global const/let should fail 1`] = `"Duplicate declaration \\"foo\\""`;
exports[`scope duplicate bindings global const/var 1`] = `"Duplicate declaration \\"foo\\""`; exports[`scope duplicate bindings global const/var should fail 1`] = `"Duplicate declaration \\"foo\\""`;
exports[`scope duplicate bindings global function/class 1`] = `"Duplicate declaration \\"foo\\""`; exports[`scope duplicate bindings global function/class should fail 1`] = `"Duplicate declaration \\"foo\\""`;
exports[`scope duplicate bindings global function/let 1`] = `"Duplicate declaration \\"foo\\""`; exports[`scope duplicate bindings global function/let should fail 1`] = `"Duplicate declaration \\"foo\\""`;
exports[`scope duplicate bindings global let/class 1`] = `"Duplicate declaration \\"foo\\""`; exports[`scope duplicate bindings global let/class should fail 1`] = `"Duplicate declaration \\"foo\\""`;
exports[`scope duplicate bindings global let/const 1`] = `"Duplicate declaration \\"foo\\""`; exports[`scope duplicate bindings global let/const should fail 1`] = `"Duplicate declaration \\"foo\\""`;
exports[`scope duplicate bindings global let/function 1`] = `"Duplicate declaration \\"foo\\""`; exports[`scope duplicate bindings global let/function should fail 1`] = `"Duplicate declaration \\"foo\\""`;
exports[`scope duplicate bindings global let/let 1`] = `"Duplicate declaration \\"foo\\""`; exports[`scope duplicate bindings global let/let should fail 1`] = `"Duplicate declaration \\"foo\\""`;
exports[`scope duplicate bindings global let/var 1`] = `"Duplicate declaration \\"foo\\""`; exports[`scope duplicate bindings global let/var should fail 1`] = `"Duplicate declaration \\"foo\\""`;
exports[`scope duplicate bindings global var/class 1`] = `"Duplicate declaration \\"foo\\""`; exports[`scope duplicate bindings global var/class should fail 1`] = `"Duplicate declaration \\"foo\\""`;
exports[`scope duplicate bindings global var/let 1`] = `"Duplicate declaration \\"foo\\""`; exports[`scope duplicate bindings global var/let should fail 1`] = `"Duplicate declaration \\"foo\\""`;

View File

@ -34,13 +34,13 @@ function addDeoptTest(code, type, expectedType) {
describe("evaluation", function () { describe("evaluation", function () {
describe("evaluateTruthy", function () { describe("evaluateTruthy", function () {
it("it should work with null", function () { it("should work with null", function () {
expect( expect(
getPath("false || a.length === 0;").get("body")[0].evaluateTruthy(), getPath("false || a.length === 0;").get("body")[0].evaluateTruthy(),
).toBeUndefined(); ).toBeUndefined();
}); });
it("it should not mistake lack of confidence for falsy", function () { it("should not mistake lack of confidence for falsy", function () {
expect( expect(
getPath("foo || 'bar'").get("body")[0].evaluate().value, getPath("foo || 'bar'").get("body")[0].evaluate().value,
).toBeUndefined(); ).toBeUndefined();
@ -117,7 +117,7 @@ describe("evaluation", function () {
expect(eval_invalid_call.confident).toBe(false); expect(eval_invalid_call.confident).toBe(false);
}); });
it("it should not deopt vars in different scope", function () { it("should not deopt vars in different scope", function () {
const input = const input =
"var a = 5; function x() { var a = 5; var b = a + 1; } var b = a + 2"; "var a = 5; function x() { var a = 5; var b = a + 1; } var b = a + 2";
expect( expect(
@ -129,7 +129,7 @@ describe("evaluation", function () {
).toBe(7); ).toBe(7);
}); });
it("it should not deopt let/const inside blocks", function () { it("should not deopt let/const inside blocks", function () {
expect( expect(
getPath("let x = 5; { let x = 1; } let y = x + 5") getPath("let x = 5; { let x = 1; } let y = x + 5")
.get("body.2.declarations.0.init") .get("body.2.declarations.0.init")

View File

@ -16,7 +16,7 @@ function getPath(code) {
describe("inference", function () { describe("inference", function () {
describe("baseTypeStrictlyMatches", function () { describe("baseTypeStrictlyMatches", function () {
it("it should work with null", function () { it("should work with null", function () {
const path = getPath("var x = null; x === null") const path = getPath("var x = null; x === null")
.get("body")[1] .get("body")[1]
.get("expression"); .get("expression");
@ -27,7 +27,7 @@ describe("inference", function () {
expect(strictMatch).toBeTruthy(); expect(strictMatch).toBeTruthy();
}); });
it("it should work with numbers", function () { it("should work with numbers", function () {
const path = getPath("var x = 1; x === 2") const path = getPath("var x = 1; x === 2")
.get("body")[1] .get("body")[1]
.get("expression"); .get("expression");
@ -38,7 +38,7 @@ describe("inference", function () {
expect(strictMatch).toBeTruthy(); expect(strictMatch).toBeTruthy();
}); });
it("it should bail when type changes", function () { it("should bail when type changes", function () {
const path = getPath("var x = 1; if (foo) x = null;else x = 3; x === 2") const path = getPath("var x = 1; if (foo) x = null;else x = 3; x === 2")
.get("body")[2] .get("body")[2]
.get("expression"); .get("expression");
@ -50,7 +50,7 @@ describe("inference", function () {
expect(strictMatch).toBeFalsy(); expect(strictMatch).toBeFalsy();
}); });
it("it should differentiate between null and undefined", function () { it("should differentiate between null and undefined", function () {
const path = getPath("var x; x === null") const path = getPath("var x; x === null")
.get("body")[1] .get("body")[1]
.get("expression"); .get("expression");

View File

@ -578,12 +578,16 @@ describe("scope", () => {
), ),
); );
}; };
["let", "const"].forEach(name => { it("let", () => {
it(name, () => { const ast = createTryCatch("let");
const ast = createTryCatch(name);
expect(() => getPath(ast)).toThrowErrorMatchingSnapshot(); expect(() => getPath(ast)).toThrowErrorMatchingSnapshot();
}); });
it("const", () => {
const ast = createTryCatch("const");
expect(() => getPath(ast)).toThrowErrorMatchingSnapshot();
}); });
it("var", () => { it("var", () => {
@ -666,30 +670,34 @@ describe("scope", () => {
}; };
for (const [kind1, kind2, success] of cases) { for (const [kind1, kind2, success] of cases) {
it(`${kind1}/${kind2}`, () => { if (success) {
const ast = createAST(kind1, kind2); it(`${kind1}/${kind2} should succeed`, () => {
const ast = createAST(kind1, kind2);
if (success) {
expect(() => getPath(ast)).not.toThrow(); expect(() => getPath(ast)).not.toThrow();
} else { });
} else {
it(`${kind1}/${kind2} should fail`, () => {
const ast = createAST(kind1, kind2);
expect(() => getPath(ast)).toThrowErrorMatchingSnapshot(); expect(() => getPath(ast)).toThrowErrorMatchingSnapshot();
} });
}); }
if (kind1 !== kind2) { if (kind1 !== kind2) {
// todo: remove the if allowed // todo: remove the if allowed
if (kind1 === "const" && (kind2 === "function" || kind2 === "var")) { if (kind1 === "const" && (kind2 === "function" || kind2 === "var")) {
continue; continue;
} }
it(`${kind2}/${kind1}`, () => { if (success) {
const ast = createAST(kind2, kind1); it(`${kind2}/${kind1} should succeed`, () => {
const ast = createAST(kind2, kind1);
if (success) {
expect(() => getPath(ast)).not.toThrow(); expect(() => getPath(ast)).not.toThrow();
} else { });
} else {
it(`${kind2}/${kind1} should fail`, () => {
const ast = createAST(kind2, kind1);
expect(() => getPath(ast)).toThrowErrorMatchingSnapshot(); expect(() => getPath(ast)).toThrowErrorMatchingSnapshot();
} });
}); }
} }
} }
}); });

View File

@ -15,7 +15,7 @@ describe("asserts", () => {
if (k.startsWith("assert") && k !== "assertNode") { if (k.startsWith("assert") && k !== "assertNode") {
const nodeType = k.replace("assert", ""); const nodeType = k.replace("assert", "");
it(nodeType, () => { it(`${nodeType} on unknown AST type should throw`, () => {
expect(() => { expect(() => {
t[k]({ type: "FlavorTownDeclaration" }, {}); t[k]({ type: "FlavorTownDeclaration" }, {});
}).toThrow( }).toThrow(

View File

@ -1,12 +1,12 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP // Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`builders flow createTypeAnnotationBasedOnTypeof bigint 1`] = ` exports[`builders flow createTypeAnnotationBasedOnTypeof bigint is valid 1`] = `
Object { Object {
"type": "AnyTypeAnnotation", "type": "AnyTypeAnnotation",
} }
`; `;
exports[`builders flow createTypeAnnotationBasedOnTypeof function 1`] = ` exports[`builders flow createTypeAnnotationBasedOnTypeof function is valid 1`] = `
Object { Object {
"id": Object { "id": Object {
"name": "Function", "name": "Function",
@ -17,13 +17,13 @@ Object {
} }
`; `;
exports[`builders flow createTypeAnnotationBasedOnTypeof number 1`] = ` exports[`builders flow createTypeAnnotationBasedOnTypeof number is valid 1`] = `
Object { Object {
"type": "NumberTypeAnnotation", "type": "NumberTypeAnnotation",
} }
`; `;
exports[`builders flow createTypeAnnotationBasedOnTypeof object 1`] = ` exports[`builders flow createTypeAnnotationBasedOnTypeof object is valid 1`] = `
Object { Object {
"id": Object { "id": Object {
"name": "Object", "name": "Object",
@ -34,13 +34,13 @@ Object {
} }
`; `;
exports[`builders flow createTypeAnnotationBasedOnTypeof string 1`] = ` exports[`builders flow createTypeAnnotationBasedOnTypeof string is valid 1`] = `
Object { Object {
"type": "StringTypeAnnotation", "type": "StringTypeAnnotation",
} }
`; `;
exports[`builders flow createTypeAnnotationBasedOnTypeof symbol 1`] = ` exports[`builders flow createTypeAnnotationBasedOnTypeof symbol is valid 1`] = `
Object { Object {
"id": Object { "id": Object {
"name": "Symbol", "name": "Symbol",
@ -51,13 +51,13 @@ Object {
} }
`; `;
exports[`builders flow createTypeAnnotationBasedOnTypeof true 1`] = ` exports[`builders flow createTypeAnnotationBasedOnTypeof true is valid 1`] = `
Object { Object {
"type": "BooleanTypeAnnotation", "type": "BooleanTypeAnnotation",
} }
`; `;
exports[`builders flow createTypeAnnotationBasedOnTypeof undefined 1`] = ` exports[`builders flow createTypeAnnotationBasedOnTypeof undefined is valid 1`] = `
Object { Object {
"type": "VoidTypeAnnotation", "type": "VoidTypeAnnotation",
} }

View File

@ -22,7 +22,7 @@ describe("builders", function () {
for (const name in values) { for (const name in values) {
const value = values[name]; const value = values[name];
it(name, function () { it(`${name} is valid`, function () {
const result = createTypeAnnotationBasedOnTypeof(value); const result = createTypeAnnotationBasedOnTypeof(value);
expect(result).toMatchSnapshot(); expect(result).toMatchSnapshot();
}); });

869
yarn.lock

File diff suppressed because it is too large Load Diff