beautifier / js-beautify

Beautifier for javascript
https://beautifier.io
MIT License
8.6k stars 1.38k forks source link

`include+` and `include-` properties #1668

Open alexreg opened 5 years ago

alexreg commented 5 years ago

Description

I propose the addition of include+ and include- properties in the configuration options, which respectively add and remove items from the include list. At the moment, only the entire list can be replaced/redefined, which is quite unergonomic and does not compose well with presets, etc.

Bonus

It would be very useful to be able to specify a function to the option that takes an HTML node object as a parameter, and returns a boolean value to specify whether the element should be beautified (or conversely, whether it should be considered inline). Returning "undefined" would fall back to the static include/include+/include- properties in options. This feature would allow you to only inline for example ul.classA > li elements (to use CSS notation). Adding support for such CSS/jQuery notation to the existing include family of properties is an alternative solution.

Input

With this new feature, I expect this configuration:

"inline+": [ "li" ]

to be equivalent to:

"inline": [ "a", "abbr", "area", "audio", "b", "bdi", "bdo", "br", "button", "canvas", "cite", "code", "data", "datalist", "del", "dfn", "em", "embed", "i", "iframe", "img", "input", "ins", "kbd", "keygen", "label", "map", "mark", "math", "meter", "noscript", "object", "output", "progress", "q", "ruby", "s", "samp", "select", "small", "span", "strong", "sub", "sup", "svg", "template", "textarea", "time", "u", "var", "video", "wbr", "text", "acronym", "big", "strike", "tt", "li" ] }

Likewise, I expect this configuration:

"inline-": [ "a" ]

to be equivalent to:

"inline": [ "abbr", "area", "audio", "b", "bdi", "bdo", "br", "button", "canvas", "cite", "code", "data", "datalist", "del", "dfn", "em", "embed", "i", "iframe", "img", "input", "ins", "kbd", "keygen", "label", "map", "mark", "math", "meter", "noscript", "object", "output", "progress", "q", "ruby", "s", "samp", "select", "small", "span", "strong", "sub", "sup", "svg", "template", "textarea", "time", "u", "var", "video", "wbr", "text", "acronym", "big", "strike", "tt" ] }
bitwiseman commented 5 years ago

Thanks @alexreg. Due to the way CLI options are parsed, this might need to be implemented as:

"inline": [ "+", "li" ]

And

"inline": [ "-", "a" ]
alexreg commented 5 years ago

@bitwiseman Sure. I mean, that's slightly less intuitive, but if it makes the implementation a lot easier, then sounds fair. Does the bonus feature (in either form) sound doable to you too?

bitwiseman commented 5 years ago

@alexreg Re Bonus feature: It is possible, but it definitely won't be implemented as part if this feature.
Also, to be clear, it is unlikely I'll have time to implement it - it would need someone else to take ownership of it.
Please open a new issue and we can discuss it there.

bitwiseman commented 1 year ago

Implementation would probably go in these two methods:

In

If we went with the "inline": [ "+", "li" ] syntax, those methods would need to look at the first element in the array to determine if it was an add or subtract case instead of the default set case.

As an alternative, those methods could look for option named <optionName> and if not found take the default. Then look for <optionName>-add followed by <optionName>-rm and process those on top of the current value. Then return.