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
Rule for enforcing JSON-style hash keys #1050
Comments
👍 |
In JSON, object literals need to have double-quoted keys. Is that what you actually want to check for? Are single quotes fine as well? Seems a bit unclear to me what you're trying to achieve or why! |
-1 JS stores properties as strings internally, but why would you want to clutter the program code with |
It would be cool to be able to enforce var obj = {
'x' : 300,
'y' : 200
}; instead of var obj = {
x : 300,
y : 200
}; Consider this function: function objectify (x, y) {
return {
x : x,
y : y
};
} For readability, it is much clearer to explicitly use strings for the hash keys even though they are already interpreted as strings by JavaScript. Because the hash key |
The only case I can think of where the quoting is required in JS is for keys that have special characters var foo = {
"awesome-value" : 1
}; The unquoted version would already be caught as malformed JS though. |
it makes no sense to enforce those quotes where they aren't needed. i would prefer to have developers demonstrate that they understand the language by only quoting when necessary. i don't think it would be cool to be able to enforce that style at all when there is no benefit to it. |
@maxkueng I don't see anything cool about either of those examples. They look like more typing that results in more clutter. Besides, this function: function objectify (x, y) {
return {
x : x,
y : y
};
} ...is soon to be obsolete. In ES6, property definition shorthands will allow: function objectify(x, y) {
return { x, y };
}
var o = objectify(1, 2);
console.log(o);
/*
{
x: 1,
y: 2
}
*/ |
I believe the best solution would be to have a rule,
... and if we're really going for it, we could have an |
+1 mcandre's suggestion for flagging. While I can not find any style guide that points out that object keys must be quoted, NPM gives an example in theirs that is pro-quotes. I can argue both sides, but I'll drop it for now. |
@tomByrer are you seriously suggesting that code such as that should be taken seriously? I'm going to wash the blood from my eyeballs now. Thanks. |
@rwldrn Thank you for your feedback. I'm sorry it caused such a shock to you, I pray you did not require hospitalization. |
JSHint is moving away from styling options. No new styling feature requests will be accepted. Sorry. |
In JSON, hash keys must be strings. Could there be a rule for using strings for all hash keys in regular .js code?
The text was updated successfully, but these errors were encountered: