Plugins can have falsy default options (#6348)

master
Jack Bates 2019-09-12 08:52:48 -07:00 committed by Lipis
parent ee43140f2c
commit af3084a63f
5 changed files with 22 additions and 7 deletions

View File

@ -71,7 +71,8 @@ function normalize(options, opts) {
const pluginDefaults = supportOptions
.filter(
optionInfo =>
optionInfo.pluginDefaults && optionInfo.pluginDefaults[plugin.name]
optionInfo.pluginDefaults &&
optionInfo.pluginDefaults[plugin.name] !== undefined
)
.reduce(
(reduced, optionInfo) =>

View File

@ -65,7 +65,9 @@ function getSupportInfo(version, opts) {
})
.map(option => {
const filteredPlugins = plugins.filter(
plugin => plugin.defaultOptions && plugin.defaultOptions[option.name]
plugin =>
plugin.defaultOptions &&
plugin.defaultOptions[option.name] !== undefined
);
const pluginDefaults = filteredPlugins.reduce((reduced, plugin) => {
reduced[plugin.name] = plugin.defaultOptions[option.name];

View File

@ -49,5 +49,7 @@ test("should work with foo plugin instance", () => {
JSON.stringify(
prettier.format(input, { parser: "foo-parser", plugins: [fooPlugin] })
)
).toMatchInlineSnapshot(`"\\"tabWidth:8\\""`);
).toMatchInlineSnapshot(
`"\\"{\\\\\\"tabWidth\\\\\\":8,\\\\\\"bracketSpacing\\\\\\":false}\\""`
);
});

View File

@ -8,7 +8,10 @@ describe("plugin default options should work", () => {
["--stdin-filepath", "example.foo", "--plugin=./plugin"],
{ input: "hello-world" }
).test({
stdout: "tabWidth:8",
stdout: JSON.stringify({
tabWidth: 8,
bracketSpacing: false
}),
stderr: "",
status: 0,
write: []
@ -21,7 +24,10 @@ describe("overriding plugin default options should work", () => {
["--stdin-filepath", "example.foo", "--plugin=./plugin", "--tab-width=4"],
{ input: "hello-world" }
).test({
stdout: "tabWidth:4",
stdout: JSON.stringify({
tabWidth: 4,
bracketSpacing: false
}),
stderr: "",
status: 0,
write: []

View File

@ -9,7 +9,8 @@ module.exports = {
}
],
defaultOptions: {
tabWidth: 8
tabWidth: 8,
bracketSpacing: false
},
parsers: {
"foo-parser": {
@ -20,7 +21,10 @@ module.exports = {
printers: {
"foo-ast": {
print: (path, options) =>
options.tabWidth ? `tabWidth:${options.tabWidth}` : path.getValue().text
JSON.stringify({
tabWidth: options.tabWidth,
bracketSpacing: options.bracketSpacing
})
}
}
};