You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Of course it would be the best if compatibility is kept (a JS3 code is compatible with JS3 up to JS6 config).
I don't know what would be the best way to implement that, maybe via an options in package.json:
{
"standard": {
"compatibility": "es2015"
}
}
Reacted by Jeffrey Heifetz, Moved to @ad-si, Brenton Simpson, Cristian Pallarés, Ara L Abcarians, David Dias, Quentin Sommer, Ruben Bridgewater, Michał Mieluch, Arturs Jansons and 7 more
I can see wanting to use no-var and object-shorthand on ES6 codebases -- those seem useful.
Personally, I don't think always using const when possible actually helps to catch real-world bugs. If I used it, I'd only want to do it for actual ALL_CAPITAL constants that I declare at the top of my file. Doing it for every single variable that happens to not be modified seems like overkill, and won't actually catch bugs, just create extra work.
Not a fan of comma dangle. Currently, we actively disallow it. Enabling it would cause almost every single dependent to start failing -- can't do that.
The question is how do get no-var and object-shorthand for people on ES6 codebases. Are these things always going to require options?
Would it also make sense to enable specific features? I currently compile my javascript as below and would love to have console.log.bind(console) to be disallowed in favour of ::console.log.
no-var is really good because I always miss it in at least a few places in the file.
I'm on the fence about comma dangle but to be fair it does make it easier to move lines up and down. Often when I do that there will be a line in the middle without comma which makes for a syntax error.
I disagree: declaring vars with const instead of let does not require extra work but forces the dev to explicitly indicate when he intends to modify a variable. I think it encourages immutability.
I agree forcing comma dangle is not viable solution as it would break compatibility but allowing it for ES>3 would not create any issues and allow cleaner diffs.
Reacted by Alex Chesters, Alex K, Josh Anderson and Linus Unnebäck
Defaulting to const seems like a good thing; if all code is moving to be block-scoped anyway const seems like the better solution. I've been using it for a while, and it's helped me catch at least a few bugs in longer files that hadn't been written with care.
I also agree with @julien-f that using const / let specifies intent more clearly; having mutation be the exception rather than the rule makes code easier to reason about for reviewers and authors alike.
I can see wanting to use no-var and object-shorthand on ES6 codebases -- those seem useful.
Agreed.
Personally, I don't think always using const when possible actually helps to catch real-world bugs. If I used it, I'd only want to do it for actual ALL_CAPITAL constants that I declare at the top of my file. Doing it for every single variable that happens to not be modified seems like overkill, and won't actually catch bugs, just create extra work.
Although I am usually a massive proponent for const, I think you may be right. const gives us a tonne of benefits in statically typed languages because it can provide guarantees about the data itself, allowing us to strongly enforce functional purity.
This is not so in JS, as the constness stops at the top level assignment, and is not transitive in any way.
So, while it pains me to say it, I think prefer-const should remain disabled.
've been using it for a while, and it's helped me catch at least a few bugs in longer files that hadn't been written with care.
@yoshuawuyts maybe I'm just being short sighted, but can you elaborate on this?
I think it encourages immutability.
@julien-f this is the one positive I can see. Immutability rules. But IMHO, it is almost always better to be enforced through libraries like immutable.js then it is at the scope level.
Perhaps this rule could help push in that direction though?
The question is how do get no-var and object-shorthand for people on ES6 codebases. Are these things always going to require options?
We should ask eslint if we we can add an option OPTION-if-es6 (like, always-if-es6) or something, maybe a way to override rules if es6 is detected in anyway.
That way we can solve a multitude of issues simultaneously!
@dcousens I was leading a refactoring effort at a previous job where ~1k-2k LOC files weren't uncommon. At that point it was impossible to remember all variables available in scope, and accidentally redeclaring variables happened. const provided a mechanism to warn about this, what else would've been manual debugging. Arguably eslint:no-redeclare serves the same purpose, so maybe that point is moot for projects that rely on standard / eslint. (ツ)_/¯
Actually I'm curious what the reasons againstprefer-const are / why let is preferred. Off the top of my head:
pro const
intent is communicated explicitely
once past the learning curve it's not more work (based on my own experience)
possibly faster (vm optimizations?)
discourages random mutations (to some extent)
works in node>=0.10 where let doesn't
con(st) const
breaks existing modules (not a problem if an es6 flag can be enabled)
using const + let requires different reasoning about variables (learning curve)
two characters longer than let
const allows mutating object properties, which can be confusing for newcomers
Does that sound about right? Let me know if I missed anything.
@feross It's because the semantic of const is not obvious, it guarantee that the variable will always point to the same value, not that this value will not be changed.
If, in Node, exports was declared as const exports = modules.exports it would have prevented tons of mistakes from beginners which affects an object to exports instead of module.exports.
@julien-f I think that's a pretty weak guarantee and it's not worth forcing users to use it in every possible situation where it could conceivably be used. Rules have to be worth the pain they cause, and I'm not yet convinced this rule is worth it.
Let's agree to disagree of the meaning of these signals: for me let means that the value of this variable will change during its lifetime, and const that it will not.
I agree with you on the meaning of those signals. Our disagreement seems to be that I want to consider potential futures, where as you only want to consider the current state of the code.
I've listed the main one in the first message (comma-dangle is a must for code reviews!), but also the ability to set the ecmaVersion to ensure the code is strictly ES3/5/2015 etc.
Oh, how I would love to have comma-dangle 😄 I don't know how many times I've talked about starting trailing-standard with various people 😂 Unfortunately it would break a lot of the ecosystem...
I don't see a problem with adding prefer-const right away since it only complains when using let instead of const, and where let is supported, const is as well.
Personally, I would like to see no-var and object-shorthand just enabled in the next major. If anyone is targeting an older environment, they can simply stay on 12.x. We could maintain that line as well for some time as well...
Activity
julien-f commentedon Jun 15, 2015
I think
prefer-constcan be used unconditionally because it only applies tolets, notvars.julien-f commentedon Jun 24, 2015
No opinions on this?
feross commentedon Jun 24, 2015
I can see wanting to use no-var and object-shorthand on ES6 codebases -- those seem useful.
Personally, I don't think always using
constwhen possible actually helps to catch real-world bugs. If I used it, I'd only want to do it for actualALL_CAPITALconstants that I declare at the top of my file. Doing it for every single variable that happens to not be modified seems like overkill, and won't actually catch bugs, just create extra work.Not a fan of comma dangle. Currently, we actively disallow it. Enabling it would cause almost every single dependent to start failing -- can't do that.
The question is how do get no-var and object-shorthand for people on ES6 codebases. Are these things always going to require options?
LinusU commentedon Jun 24, 2015
I think this is a very good idea.
Would it also make sense to enable specific features? I currently compile my javascript as below and would love to have
console.log.bind(console)to be disallowed in favour of::console.log.no-varis really good because I always miss it in at least a few places in the file.I'm on the fence about comma dangle but to be fair it does make it easier to move lines up and down. Often when I do that there will be a line in the middle without comma which makes for a syntax error.
@feross What do you mean with this?
As long as we specify somehow that the code is ECMAScript 6 it shouldn't require any more options. There is no reason to use
varin ES6 code.julien-f commentedon Jun 24, 2015
I disagree: declaring vars with
constinstead ofletdoes not require extra work but forces the dev to explicitly indicate when he intends to modify a variable. I think it encourages immutability.I agree forcing comma dangle is not viable solution as it would break compatibility but allowing it for ES>3 would not create any issues and allow cleaner diffs.
LinusU commentedon Jun 24, 2015
I really think that we should either enforce it or forbid it. Letting the user decide for themselves just creates more bike-sheding?
feross commentedon Jun 24, 2015
Remember, you can always "fork" standard if you care a ton about a single rule. Add your own rule with
.eslintrclike this:{ "extends": ["standard", "standard-react"], "rules": { "comma-dangle": [2, "always"] } }yoshuawuyts commentedon Jun 24, 2015
Defaulting to
constseems like a good thing; if all code is moving to be block-scoped anywayconstseems like the better solution. I've been using it for a while, and it's helped me catch at least a few bugs in longer files that hadn't been written with care.I also agree with @julien-f that using
const/letspecifies intent more clearly; having mutation be the exception rather than the rule makes code easier to reason about for reviewers and authors alike.dcousens commentedon Jun 25, 2015
Agreed.
Although I am usually a massive proponent for
const, I think you may be right.constgives us a tonne of benefits in statically typed languages because it can provide guarantees about the data itself, allowing us to strongly enforce functional purity.This is not so in JS, as the
constness stops at the top level assignment, and is not transitive in any way.So, while it pains me to say it, I think
prefer-constshould remain disabled.@yoshuawuyts maybe I'm just being short sighted, but can you elaborate on this?
@julien-f this is the one positive I can see. Immutability rules. But IMHO, it is almost always better to be enforced through libraries like immutable.js then it is at the scope level.
Perhaps this rule could help push in that direction though?
We should ask eslint if we we can add an option
OPTION-if-es6(like,always-if-es6) or something, maybe a way to override rules ifes6is detected in anyway.That way we can solve a multitude of issues simultaneously!
(e.g #167)
yoshuawuyts commentedon Jun 25, 2015
@dcousens I was leading a refactoring effort at a previous job where ~1k-2k LOC files weren't uncommon. At that point it was impossible to remember all variables available in scope, and accidentally redeclaring variables happened.
constprovided a mechanism to warn about this, what else would've been manual debugging. Arguablyeslint:no-redeclareserves the same purpose, so maybe that point is moot for projects that rely onstandard/eslint. (ツ)_/¯Actually I'm curious what the reasons against
prefer-constare / whyletis preferred. Off the top of my head:pro const
node>=0.10whereletdoesn'tcon(st) const
es6flag can be enabled)const+letrequires different reasoning about variables (learning curve)letconstallows mutating object properties, which can be confusing for newcomersDoes that sound about right? Let me know if I missed anything.
dcousens commentedon Jun 25, 2015
Looks right to me.
I guess a
procould be encouraging immutability as well.feross commentedon Jun 25, 2015
Here's a concrete problem I have with
prefer-const. Look at this code:With
prefer-const, I get this error:My problems with
constin this situation are:optionsare actually modified, soconstisn't providing any real protection here, and it's just plain confusing.constevery little helper variable, likeoptionsin this example, will generate a ton of errors, and I'm not convinced it's worth the cost.julien-f commentedon Jun 25, 2015
@feross It's because the semantic of
constis not obvious, it guarantee that the variable will always point to the same value, not that this value will not be changed.If, in Node,
exportswas declared asconst exports = modules.exportsit would have prevented tons of mistakes from beginners which affects an object toexportsinstead ofmodule.exports.feross commentedon Jun 25, 2015
@julien-f I think that's a pretty weak guarantee and it's not worth forcing users to use it in every possible situation where it could conceivably be used. Rules have to be worth the pain they cause, and I'm not yet convinced this rule is worth it.
29 remaining items
julien-f commentedon Jun 12, 2018
Let's agree to disagree of the meaning of these signals: for me
letmeans that the value of this variable will change during its lifetime, andconstthat it will not.kyeotic commentedon Jun 12, 2018
I agree with you on the meaning of those signals. Our disagreement seems to be that I want to consider potential futures, where as you only want to consider the current state of the code.
sara0871 commentedon Jul 4, 2018
👍
stale commentedon Oct 2, 2018
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
julien-f commentedon Oct 9, 2018
FYI, I've started using more and more ESLint instead of Standard exactly for this reason (being able to target a specific version of ES, like ES5).
LinusU commentedon Oct 10, 2018
@julien-f which rules is it that you enable on top of standard? Maybe it's time to just include them 🤔
julien-f commentedon Oct 10, 2018
I've listed the main one in the first message (
comma-dangleis a must for code reviews!), but also the ability to set theecmaVersionto ensure the code is strictly ES3/5/2015 etc.LinusU commentedon Oct 10, 2018
Ah, right, sorry didn't look at the top comment 😄
Oh, how I would love to have comma-dangle 😄 I don't know how many times I've talked about starting
trailing-standardwith various people 😂 Unfortunately it would break a lot of the ecosystem...I don't see a problem with adding
prefer-constright away since it only complains when usingletinstead ofconst, and whereletis supported,constis as well.Personally, I would like to see
no-varandobject-shorthandjust enabled in the next major. If anyone is targeting an older environment, they can simply stay on 12.x. We could maintain that line as well for some time as well...LinusU commentedon Oct 10, 2018
@julien-f put up a PR for☺️ standard/eslint-config-standard#133
prefer-constLinusU commentedon Oct 10, 2018
Hmmm,
no-varis not looking too good 😅object-shorthand:Hmm, I would like to do some more research and see how many of those are actually targeting older environments 🤔 💭