Giter VIP home page Giter VIP logo

Comments (8)

jfmengels avatar jfmengels commented on July 16, 2024 1

Hmm, not sure it makes sense. If you only allow this to be mutated in the constructor, then you don't really need it, and you can do it with closures.

function Person(age, firstName, lastName) {
  const isOver18 = age > 18;
  return {
    age: age,
    fullName: function() {
      return firstName + ' ' + lastName;
    }
  };
}

If you have an other use-case for this, I'm happy to hear it :)

from eslint-plugin-fp.

deepu105 avatar deepu105 commented on July 16, 2024 1

@jfmengels I was about to open a new issue then I came across this thread, I found this plugin useful and I guess being able to configure mutation of this only inside constructor is somthing thats useful in react apps especially, as you woul often assign this.state = .. in constructor but you wouldnt want to do that outside the constructor as state shouldnt be mutated. so somethink like below might be bit more flexible

"fp/no-mutation": ["error", {
  "commonjs": true,
  "allowThis": true,
  "allowThisInConstructor": true,
  "exceptions": [
    {"object": "foo", "property": "bar"}
  ]
}]

or even better would be if we can just specify method in the exceptions

"fp/no-mutation": ["error", {
  "commonjs": true,
  "allowThis": true,
  "exceptions": [
    {"object": "this", "property": "state", "method": "constructor"}
  ]
}]

from eslint-plugin-fp.

dtinth avatar dtinth commented on July 16, 2024

Maybe this should have an option to allow this just inside constructors (like how final fields work in Java). 😄

from eslint-plugin-fp.

scottnonnenberg avatar scottnonnenberg commented on July 16, 2024

As you guessed on the original issue, I wanted support for this for React apps. Without it, as soon as you need to keep hold of a ref for some reason, you resort to eslint-disable. If it's not that common, maybe those exceptions are adequate.

from eslint-plugin-fp.

jfmengels avatar jfmengels commented on July 16, 2024

@scottnonnenberg I haven't used React for a while, and haven't got far enough as to play with refs. Do you mind giving me an example of that use-case?

from eslint-plugin-fp.

scottnonnenberg avatar scottnonnenberg commented on July 16, 2024

Straight from my GatsbyJS-based blog:

  componentDidMount() {
    catchLinks(this.parentNode, href => this.context.router.push(href));
  }

  getParent(ref) {
    this.parentNode = ref; // eslint-disable-line
  }

  render() {
    return <div ref={this.getParent} />;
  }

from eslint-plugin-fp.

jfmengels avatar jfmengels commented on July 16, 2024

Thanks!

Ok, I'll add a this exception behind a flag.
By the way, you should try to avoid using eslint-disable-line without specifying the rules to ignore, as you might hide useful reports. See https://github.com/sindresorhus/eslint-plugin-xo/blob/master/docs/rules/no-abusive-eslint-disable.md :)

from eslint-plugin-fp.

jfmengels avatar jfmengels commented on July 16, 2024

Added in 69982ad, and landed in 1.3.0. Thanks folks!

from eslint-plugin-fp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.