-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow Object Literal to be Explicit with String Names #1103
Comments
Uuuugh. I really dislike the idea of making formatting decisions based on tools which treat formatting information as semantic. Do those tools not have a way of providing that information out-of-band from the AST, say with a comment? |
Anyone using those tools (like React is planning on doing) wouldn't really like that noise since it's all over the place. |
@sebmarkbage this should be fixed with #1108. @bakkot I don't see keeping object keys as strings (when they originaly were) as a formatting decision, IMHO those should be kept. |
This statement confuses me.
|
@bakkot Agree, but if we change |
@yamafaktory Isn't the point of prettier to make your formatting decisions for you? It's a formatter. That's what it does. |
@bakkot I partially agree. Prettier is opinionated but in this case, I think this is somehow different than just indenting a piece of code in a different way or removing unnecessary parenthesis. Keeping the original quotes can also help if as a team you want to be consistent in the way you declare an object (let's say in big config file), e.g. https://prettier.github.io/prettier/#%7B%22content%22%3A%22var%20a%20%3D%20%7B%5Cn%20%20%5C%22prop%20as%20string%5C%22%3A%201%2C%5Cn%20%20%5C%22propA%5C%22%3A%202%2C%5Cn%20%20%5C%22propB%5C%22%3A%203%5Cn%7D%22%2C%22options%22%3A%7B%22printWidth%22%3A80%2C%22tabWidth%22%3A2%2C%22singleQuote%22%3Afalse%2C%22trailingComma%22%3A%22%22%2C%22bracketSpacing%22%3Atrue%2C%22jsxBracketSameLine%22%3Afalse%2C%22parser%22%3A%22%22%2C%22doc%22%3Afalse%7D%7D I will let @vjeux / @jlongster decide on that. |
I know there's an aversion to options, but I'd like to see a subset of the options eslint provides: The advantage is that if you always want to quote it (like this issue) it's possible, but if you want to be quote-less, that's also a possibility. I threw in |
@SimenB in this case none of those options would work, Google Closure Advanced mode gives a meaning to the presence or absence of quotes, so we cannot change them without changing the meaning of the program. |
Oh, didn't know that. A 4th. option, "don't touch" then 😁 Although I suppose one might be left with just the current ( |
The google closure use case is a pretty strong one. I think we should respect the original style of object keys. I can't imagine many people fight over whether or not keys should be as strings or not, so I don't think we're solving much of a pain point by being opinionated about it, and only make it harder for tools like closure. While you can infer that |
@jlongster, re:
This is exactly the sort of formatting issue which I want a tool like prettier to handle. (That's why I implemented the current behavior in the first place!) It would be easy enough to add yet another |
I agree that this is the sort of stuff I'd like prettier to normalize, but when it comes at the cost of something powerful like Closure's advanced mode (which is really amazing), I think we should back off on it. When there are places that we can make these decisions without affecting anything else very much, I'm all for it, but we have to also recognize the existence of other tools and how they work to some extent. |
Prettier should work to make code readable and consistent for humans, not tools. Can't people needing the quotes just add |
Hm. Does the Closure compiler compile @sebmarkbage thoughts? Would that work for your team? |
Changing all of our Closure annotated JavaScript to use It looks like the closure compiler doesn't treat them the same:
where and adding a I appreciate @jlongster's insight on recognizing the existence of other tools and how they work to some extent. |
It looks like this has the effect you want - property names don't get mangled - but it's a shame it doesn't treat them exactly the same. Maybe file a bug with them?
Sure there is; just write a codemod. It's super short: module.exports = function () {
return {
visitor: {
ObjectProperty(path, state) {
let node = path.node;
if (node.key.type !== 'StringLiteral' || node.computed) return;
node.computed = true;
}
}
};
} Save that as |
In practice, I haven't seen people successfully adopting the closure compiler with advanced mode outside of Google internal codebase and only a single person using it raised an issue about it. So, given this lack of usage, I'm going to close this issue for now. If React actually manages to do it, I may reconsider. |
same thing with classnames in jsx: <Something
className={cx({
'disabled': true,
'btn-primary': true,
})}
/> would not want the |
there must be an option and let me choose whether to remove or keep the quotes |
Turns into:
Note that the object literal lost its explicit string encoding. This breaks Google Closure Compiler's advanced mode heuristic/convention around property name mangling. Uglify has this mode too.
Maybe an explicit mode would be good for this? Or should it be default?
The text was updated successfully, but these errors were encountered: