Restrict Babel's plugins/presets to a single target. (#5547)

This commit is contained in:
Logan Smyth
2017-04-17 11:45:49 -07:00
committed by GitHub
parent c59e9f5f0e
commit 2b86d353d6
31 changed files with 463 additions and 110 deletions

View File

@@ -116,11 +116,13 @@ describe("api", function () {
});
it("exposes the resolvePlugin method", function() {
assert.equal(babel.resolvePlugin("nonexistent-plugin"), null);
assert.throws(() => babel.resolvePlugin("nonexistent-plugin"),
/Cannot find module 'babel-plugin-nonexistent-plugin'/);
});
it("exposes the resolvePreset method", function() {
assert.equal(babel.resolvePreset("nonexistent-preset"), null);
assert.throws(() => babel.resolvePreset("nonexistent-preset"),
/Cannot find module 'babel-preset-nonexistent-preset'/);
});
it("transformFile", function (done) {

View File

@@ -1,9 +0,0 @@
module.exports = function () {
return {
visitor: {
StringLiteral: function (path) {
path.node.value = "after";
},
},
};
};

View File

@@ -1,19 +0,0 @@
module.exports = function (context, options, fileContext) {
if (/resolve-addons-relative-to-file$/.test(fileContext.dirname)) {
return {
plugins: [plugin],
};
}
return {};
};
function plugin () {
return {
visitor: {
StringLiteral: function (path) {
path.node.value =
path.node.value.toUpperCase();
},
},
};
}

View File

@@ -1,56 +1,398 @@
import assert from "assert";
import async from "async";
import * as babel from "../lib/index";
import fs from "fs";
import path from "path";
// Test that plugins & presets are resolved relative to `filename`.
describe("addon resolution", function () {
it("addon resolution", function (done) {
const fixtures = {};
const paths = {};
const base = path.join(__dirname, "fixtures", "resolution");
paths.fixtures = path.join(
__dirname,
"fixtures",
"resolution",
"resolve-addons-relative-to-file"
);
beforeEach(function() {
this.cwd = process.cwd();
process.chdir(base);
});
async.each(
["actual", "expected"],
function (key, mapDone) {
paths[key] = path.join(paths.fixtures, key + ".js");
fs.readFile(paths[key], { encoding: "utf8" }, function (err, data) {
if (err) return mapDone(err);
fixtures[key] = data.trim();
mapDone();
});
},
fixturesReady
);
afterEach(function() {
process.chdir(this.cwd);
});
function fixturesReady (err) {
if (err) return done(err);
it("should find module: presets", function() {
process.chdir("module-paths");
const orignalCwd = process.cwd();
try {
process.chdir(paths.fixtures);
babel.transform("", {
filename: "filename.js",
babelrc: false,
presets: [
"module:preset",
],
});
});
const actual = babel.transform(fixtures.actual, {
babelrc: false,
filename: paths.actual,
plugins: ["addons/plugin"],
presets: ["addons/preset"],
}).code;
it("should find module: plugins", function() {
process.chdir("module-paths");
assert.equal(actual, fixtures.expected);
} finally {
process.chdir(orignalCwd);
}
babel.transform("", {
filename: "filename.js",
babelrc: false,
plugins: [
"module:plugin",
],
});
});
done();
}
// fixturesReady
it("should find standard presets", function() {
process.chdir("standard-paths");
babel.transform("", {
filename: "filename.js",
babelrc: false,
presets: [
"mod",
],
});
});
it("should find standard plugins", function() {
process.chdir("standard-paths");
babel.transform("", {
filename: "filename.js",
babelrc: false,
plugins: [
"mod",
],
});
});
it("should find standard presets with an existing prefix", function() {
process.chdir("standard-paths");
babel.transform("", {
filename: "filename.js",
babelrc: false,
presets: [
"babel-preset-mod",
],
});
});
it("should find standard plugins with an existing prefix", function() {
process.chdir("standard-paths");
babel.transform("", {
filename: "filename.js",
babelrc: false,
plugins: [
"babel-plugin-mod",
],
});
});
it("should find @babel scoped presets", function() {
process.chdir("babel-org-paths");
babel.transform("", {
filename: "filename.js",
babelrc: false,
presets: [
"@babel/foo",
],
});
});
it("should find @babel scoped plugins", function() {
process.chdir("babel-org-paths");
babel.transform("", {
filename: "filename.js",
babelrc: false,
plugins: [
"@babel/foo",
],
});
});
it("should find @babel scoped presets with an existing prefix", function() {
process.chdir("babel-org-paths");
babel.transform("", {
filename: "filename.js",
babelrc: false,
presets: [
"@babel/preset-foo",
],
});
});
it("should find @babel scoped plugins", function() {
process.chdir("babel-org-paths");
babel.transform("", {
filename: "filename.js",
babelrc: false,
plugins: [
"@babel/plugin-foo",
],
});
});
it("should find @foo scoped presets", function() {
process.chdir("foo-org-paths");
babel.transform("", {
filename: "filename.js",
babelrc: false,
presets: [
"@foo/mod",
],
});
});
it("should find @foo scoped plugins", function() {
process.chdir("foo-org-paths");
babel.transform("", {
filename: "filename.js",
babelrc: false,
plugins: [
"@foo/mod",
],
});
});
it("should find @foo scoped presets with an existing prefix", function() {
process.chdir("foo-org-paths");
babel.transform("", {
filename: "filename.js",
babelrc: false,
presets: [
"@foo/babel-preset-mod",
],
});
});
it("should find @foo scoped plugins with an existing prefix", function() {
process.chdir("foo-org-paths");
babel.transform("", {
filename: "filename.js",
babelrc: false,
plugins: [
"@foo/babel-plugin-mod",
],
});
});
it("should find relative path presets", function() {
process.chdir("relative-paths");
babel.transform("", {
filename: "filename.js",
babelrc: false,
presets: [
"./dir/preset.js",
],
});
});
it("should find relative path plugins", function() {
process.chdir("relative-paths");
babel.transform("", {
filename: "filename.js",
babelrc: false,
plugins: [
"./dir/plugin.js",
],
});
});
it("should find module file presets", function() {
process.chdir("nested-module-paths");
babel.transform("", {
filename: "filename.js",
babelrc: false,
presets: [
"mod/preset",
],
});
});
it("should find module file plugins", function() {
process.chdir("nested-module-paths");
babel.transform("", {
filename: "filename.js",
babelrc: false,
plugins: [
"mod/plugin",
],
});
});
it("should find @foo scoped module file presets", function() {
process.chdir("scoped-nested-module-paths");
babel.transform("", {
filename: "filename.js",
babelrc: false,
presets: [
"@foo/mod/preset",
],
});
});
it("should find @foo scoped module file plugins", function() {
process.chdir("scoped-nested-module-paths");
babel.transform("", {
filename: "filename.js",
babelrc: false,
plugins: [
"@foo/mod/plugin",
],
});
});
it("should find @babel scoped module file presets", function() {
process.chdir("babel-scoped-nested-module-paths");
babel.transform("", {
filename: "filename.js",
babelrc: false,
presets: [
"@babel/mod/preset",
],
});
});
it("should find @babel scoped module file plugins", function() {
process.chdir("babel-scoped-nested-module-paths");
babel.transform("", {
filename: "filename.js",
babelrc: false,
plugins: [
"@babel/mod/plugin",
],
});
});
it("should throw about module: usage for presets", function() {
process.chdir("throw-module-paths");
assert.throws(() => {
babel.transform("", {
filename: "filename.js",
babelrc: false,
presets: [
"foo",
],
});
// eslint-disable-next-line max-len
}, /Cannot find module 'babel-preset-foo'.*\n- If you want to resolve "foo", use "module:foo"/);
});
it("should throw about module: usage for plugins", function() {
process.chdir("throw-module-paths");
assert.throws(() => {
babel.transform("", {
filename: "filename.js",
babelrc: false,
plugins: [
"foo",
],
});
// eslint-disable-next-line max-len
}, /Cannot find module 'babel-plugin-foo'.*\n- If you want to resolve "foo", use "module:foo"/);
});
it("should throw about @babel usage for presets", function() {
process.chdir("throw-babel-paths");
assert.throws(() => {
babel.transform("", {
filename: "filename.js",
babelrc: false,
presets: [
"foo",
],
});
// eslint-disable-next-line max-len
}, /Cannot find module 'babel-preset-foo'.*\n- Did you mean "@babel\/foo"\?/);
});
it("should throw about @babel usage for plugins", function() {
process.chdir("throw-babel-paths");
assert.throws(() => {
babel.transform("", {
filename: "filename.js",
babelrc: false,
plugins: [
"foo",
],
});
// eslint-disable-next-line max-len
}, /Cannot find module 'babel-plugin-foo'.*\n- Did you mean "@babel\/foo"\?/);
});
it("should throw about passing a preset as a plugin", function() {
process.chdir("throw-opposite-paths");
assert.throws(() => {
babel.transform("", {
filename: "filename.js",
babelrc: false,
presets: [
"testplugin",
],
});
// eslint-disable-next-line max-len
}, /Cannot find module 'babel-preset-testplugin'.*\n- Did you accidentally pass a preset as a plugin\?/);
});
it("should throw about passing a plugin as a preset", function() {
process.chdir("throw-opposite-paths");
assert.throws(() => {
babel.transform("", {
filename: "filename.js",
babelrc: false,
plugins: [
"testpreset",
],
});
// eslint-disable-next-line max-len
}, /Cannot find module 'babel-plugin-testpreset'.*\n- Did you accidentally pass a plugin as a preset\?/);
});
it("should throw about missing presets", function() {
process.chdir("throw-missing-paths");
assert.throws(() => {
babel.transform("", {
filename: "filename.js",
babelrc: false,
presets: [
"foo",
],
});
}, /Cannot find module 'babel-preset-foo'/);
});
it("should throw about missing plugins", function() {
process.chdir("throw-missing-paths");
assert.throws(() => {
babel.transform("", {
filename: "filename.js",
babelrc: false,
plugins: [
"foo",
],
});
}, /Cannot find module 'babel-plugin-foo'/);
});
});