fix: allow plugin instance (#5760) (#5763)

master
Wenlu Wang 2019-01-18 22:40:18 +08:00 committed by Lucas Duailibe
parent 72d9732f1d
commit 1dea4ef6f2
4 changed files with 40 additions and 6 deletions

View File

@ -7,6 +7,7 @@ const path = require("path");
const resolve = require("resolve");
const thirdParty = require("./third-party");
const internalPlugins = require("./internal-plugins");
const partition = require("../utils/partition");
function loadPlugins(plugins, pluginSearchDirs) {
if (!plugins) {
@ -24,7 +25,12 @@ function loadPlugins(plugins, pluginSearchDirs) {
}
}
const externalManualLoadPluginInfos = plugins.map(pluginName => {
const [externalPluginNames, externalPluginInstances] = partition(
plugins,
plugin => typeof plugin === "string"
);
const externalManualLoadPluginInfos = externalPluginNames.map(pluginName => {
let requirePath;
try {
// try local files
@ -69,12 +75,14 @@ function loadPlugins(plugins, pluginSearchDirs) {
const externalPlugins = uniqBy(
externalManualLoadPluginInfos.concat(externalAutoLoadPluginInfos),
"requirePath"
).map(externalPluginInfo =>
Object.assign(
{ name: externalPluginInfo.name },
eval("require")(externalPluginInfo.requirePath)
)
.map(externalPluginInfo =>
Object.assign(
{ name: externalPluginInfo.name },
eval("require")(externalPluginInfo.requirePath)
)
)
);
.concat(externalPluginInstances);
return internalPlugins.concat(externalPlugins);
}

14
src/utils/partition.js Normal file
View File

@ -0,0 +1,14 @@
"use strict";
module.exports = function partition(array, fn) {
const a = [];
const b = [];
array.forEach(item => {
if (fn(item)) {
a.push(item);
} else {
b.push(item);
}
});
return [a, b];
};

View File

@ -4,6 +4,8 @@ exports[`html parser should handle CRLF correctly 1`] = `"\\"<!--\\\\r\\\\n tes
exports[`markdown parser should handle CRLF correctly 1`] = `"\\"\`\`\`\\\\r\\\\n\\\\r\\\\n\\\\r\\\\n\`\`\`\\\\r\\\\n\\""`;
exports[`should work with foo plugin instance 1`] = `"\\"tabWidth:8\\""`;
exports[`typescript parser should throw the first error when both JSX and non-JSX mode failed 1`] = `
"Expression expected. (9:7)
7 | );

View File

@ -1,6 +1,7 @@
"use strict";
const prettier = require("prettier/local");
const fooPlugin = require("../plugins/defaultOptions/plugin");
test("yaml parser should handle CRLF correctly", () => {
const input = "a:\r\n 123\r\n";
@ -41,3 +42,12 @@ test("markdown parser should handle CRLF correctly", () => {
JSON.stringify(prettier.format(input, { parser: "markdown" }))
).toMatchSnapshot();
});
test("should work with foo plugin instance", () => {
const input = "a:\r\n 123\r\n";
expect(
JSON.stringify(
prettier.format(input, { parser: "foo-parser", plugins: [fooPlugin] })
)
).toMatchInlineSnapshot(`"\\"tabWidth:8\\""`);
});