polish: throw human-friendly error when item-option pair is incorrectly unwrapped#10969
Conversation
The failing test is still a false positive
|
@nicolo-ribaudo Never mind, it turns out I have confused between |
loganfsmyth
left a comment
There was a problem hiding this comment.
I think the location for this check is not idea. Would you be open to doing this at
- When the plugin is instantiated:
- Where the preset is instantiated:
You should be able to catch the error there and see that it was a plain object immediately after a successfully-loaded plugin/preset. It also seems like it could be a lot nicer to append text to the error's message saying something like "maybe you forgot an extra pair of square brackets" instead of throwing a while separate error, and you could do it for the more general case withing special-casing the single-preset + plain object case.
|
@loganfsmyth Thanks for your suggestion. I am open to move the check to the reduce step, but I am hesitant to generalize this check to a very board case. There are various cases that one can generalize this check into.
This is the most generalized case and it almost replaces the original validation error because any error in the non-first config item will result to this error. I am opposed to this approach as it introduces too much noise for advanced users.
That means we will print this message on {
"presets": [
[
"@babel/preset-env",
{
"targets": {
"esmodules": true
}
},
"@babel/preset-typescript"
]
]
}Well in this case we can print this message as a better guess than 1). I can implement this requirement if we can reach consensus here.
I still favour the separate error message because it is more focused. Let's do a comparison: This is the error message while appending Unless we improve the error message logic, the leading double While this is the tuned message It emphasizes the possible copy-paste ready solution over the technical errors. I can prepend |
Part of what I'm saying is that I don't think we should replace the error at all. Just like we do with for instance, we could append to the "unknown property" validation error sayingIt looks like this object might be options for a plugin/preset, did you mean to wrap this in a set of [] to apply these options?
To help clarify things, something like I get your concerns over error message verbosity though, and I admit I don't feel that strongly. The main reason I comment on this is because the placement of the logic had surprised me. For your case, we could instead do and I think it would be clearer and avoid repeating the validation logic. |
0c6af2b to
798043c
Compare
| acc.push(loadPluginDescriptor(descriptor, context)); | ||
| } catch (e) { | ||
| // print special message for `plugins: ["@babel/foo", { foo: "option" }]` | ||
| if (i > 0 && e.code === "BABEL_UNKNOWN_PLUGIN_PROPERTY") { |
There was a problem hiding this comment.
If we are not checking if config.plugins is 2, we should also have a test with more plugins.
ea3cad9 to
7aecf80
Compare
This reverts commit 60dde89.
7aecf80 to
5a24975
Compare
nicolo-ribaudo
left a comment
There was a problem hiding this comment.
I think @loganfsmyth's concerns have been addressed.
I want to focus on the scenario I want to help instead of giving too much technical details of this PR. Anyway the approach is straightforward.
When you begin to use
babel, the config may be as simple as{ "presets": ["@babel/env"] }It works out of box. It is great.
After you have learned more about babel, you may begin to add options to preset-env, let's say you try
{ "presets": ["@babel/env", { "useBuiltIns": true }] }It looks quite intuitive, right? But unfortunately it will throw
The error message is technically correct. But this message is tremendously confusing even if you are a babel veteran because
.useBuiltInsis an option for preset-env, and especially after you have double check you didn't wrote it asuseBuiltins. But why babel throws unknown options for.useBuiltIns? Well when I was learning to configure babel, it takes me quite a while before I realize that I should add a[]to wrap the preset name and its optionspresets: [["@babel/env", { "useBuiltIns": true }]]In this PR we detect this error pattern and give a human friendly error message.