Skip to content

Different settings for different JS versions #159

Closed

Description

@julien-f

I think it would make sense for some settings to differ based on which JS version is targeted:

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"
  }
}

Activity

  1. julien-f commented on Jun 15, 2015

    @julien-f
    Author

    I think prefer-const can be used unconditionally because it only applies to lets, not vars.

  2. julien-f commented on Jun 24, 2015

    @julien-f
    Author

    No opinions on this?

  3. feross commented on Jun 24, 2015

    @feross
    Member

    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?

  4. LinusU commented on Jun 24, 2015

    @LinusU
    Member

    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.

    babel src -s --optional es7.functionBind --out-dir lib

    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.

    @feross What do you mean with this?

    Are these things always going to require options?

    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 var in ES6 code.

  5. julien-f commented on Jun 24, 2015

    @julien-f
    Author

    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.

  6. LinusU commented on Jun 24, 2015

    @LinusU
    Member

    I really think that we should either enforce it or forbid it. Letting the user decide for themselves just creates more bike-sheding?

  7. feross commented on Jun 24, 2015

    @feross
    Member

    Remember, you can always "fork" standard if you care a ton about a single rule. Add your own rule with .eslintrc like this:

    {
      "extends": ["standard", "standard-react"],
      "rules": {
        "comma-dangle": [2, "always"]
      }
    }
  8. yoshuawuyts commented on Jun 24, 2015

    @yoshuawuyts
    Contributor

    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.

  9. dcousens commented on Jun 25, 2015

    @dcousens
    Member

    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!

    (e.g #167)

  10. yoshuawuyts commented on Jun 25, 2015

    @yoshuawuyts
    Contributor

    @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 against prefer-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.

  11. dcousens commented on Jun 25, 2015

    @dcousens
    Member

    Looks right to me.
    I guess a pro could be encouraging immutability as well.

  12. feross commented on Jun 25, 2015

    @feross
    Member

    Here's a concrete problem I have with prefer-const. Look at this code:

    'use strict'
    const http = require('http')
    
    function fetchGoogle (cb) {
      let options = {
        hostname: 'www.google.com',
        port: 80,
        path: '/'
      }
    
      if (Math.random() > 0.5) options.headers = { DNT: 1 }
    
      http.get(options, cb)
    }
    
    fetchGoogle(function (res) {
      console.log('got ' + res.statusCode)
    })

    With prefer-const, I get this error:

    t.js:6:6: `options` is never modified, use `const` instead.
    

    My problems with const in this situation are:

    1. The properties on options are actually modified, so const isn't providing any real protection here, and it's just plain confusing.
    2. Having to const every little helper variable, like options in this example, will generate a ton of errors, and I'm not convinced it's worth the cost.
  13. julien-f commented on Jun 25, 2015

    @julien-f
    Author

    @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.

  14. feross commented on Jun 25, 2015

    @feross
    Member

    @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.

  15. 29 remaining items

  16. julien-f commented on Jun 12, 2018

    @julien-f
    Author

    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.

  17. kyeotic commented on Jun 12, 2018

    @kyeotic

    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.

  18. sara0871 commented on Jul 4, 2018

    @sara0871

    👍

  19. stale commented on Oct 2, 2018

    @stale

    This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

  20. julien-f commented on Oct 9, 2018

    @julien-f
    Author

    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).

  21. LinusU commented on Oct 10, 2018

    @LinusU
    Member

    @julien-f which rules is it that you enable on top of standard? Maybe it's time to just include them 🤔

  22. julien-f commented on Oct 10, 2018

    @julien-f
    Author

    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.

  23. LinusU commented on Oct 10, 2018

    @LinusU
    Member

    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-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...

  24. LinusU commented on Oct 10, 2018

    @LinusU
    Member

    @julien-f put up a PR for prefer-const ☺️ standard/eslint-config-standard#133

  25. LinusU commented on Oct 10, 2018

    @LinusU
    Member

    Hmmm, no-var is not looking too good 😅

    # tests 265
    # pass  47
    # fail  218
    

    object-shorthand:

    # tests 265
    # pass  171
    # fail  94
    

    Hmm, I would like to do some more research and see how many of those are actually targeting older environments 🤔 💭

  26. locked as resolved and limited conversation to collaborators on Jan 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions