npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

eslint-plugin-mobx

v0.0.13

Published

ESLint rules for MobX

Downloads

156,247

Readme

eslint-plugin-mobx

Mobx specific linting rules for eslint.

Installation

npm install --save-dev eslint @typescript-eslint/parser eslint-plugin-mobx

Configuration

Legacy Config

// .eslintrc.js
module.exports = {
    parser: "@typescript-eslint/parser",
    // Include "mobx" in plugins array:
    plugins: ["mobx"],
    // Either extend our recommended configuration:
    extends: "plugin:mobx/recommended",
    // ...or specify and customize individual rules:
    rules: {
        // these values are the same as recommended
        "mobx/exhaustive-make-observable": "warn",
        "mobx/unconditional-make-observable": "error",
        "mobx/missing-make-observable": "error",
        "mobx/missing-observer": "warn"
    }
}

Flat Config

// eslint.config.js
import pluginMobx from "eslint-plugin-mobx"

export default [
    // ...

    // Either extend our recommended configuration:
    pluginMobx.flatConfigs.recommended,

    // ...or specify and customize individual rules:
    {
        plugins: { mobx: pluginMobx },
        rules: {
            // these values are the same as recommended
            "mobx/exhaustive-make-observable": "warn",
            "mobx/unconditional-make-observable": "error",
            "mobx/missing-make-observable": "error",
            "mobx/missing-observer": "warn"
        }
    }
]

Rules

mobx/exhaustive-make-observable

Makes sure that makeObservable annotates all fields defined on class or object literal. To exclude a field, annotate it using field: false. Does not support fields introduced by constructor (this.foo = 5). Does not warn about annotated non-existing fields (there is a runtime check, but the autofix removing the field could be handy...). Autofix adds field: true for each missing field by default. You can change this behaviour by specifying options in your eslint config:

{
    "rules": {
        "mobx/exhaustive-make-observable": ["error", { "autofixAnnotation": false }]
    }
}

This is a boolean value that controls if the field is annotated with true or false. If you are migrating an existing project using makeObservable and do not want this rule to override your current usage (even if it may be wrong), you should run the autofix with the annotation set to false to maintain existing behaviour: eslint --no-eslintrc --fix --rule='mobx/exhaustive-make-observable: [2, { "autofixAnnotation": false }]' .

mobx/missing-make-observable

When using decorators (eg @observable foo = 5), makes sure that makeObservable(this) is called in a constructor. Autofix creates a constructor if necessary and adds makeObservable(this) at it's end.

mobx/unconditional-make-observable

Makes sure the make(Auto)Observable(this) is called unconditionally inside a constructor.

mobx/missing-observer

Makes sure every React component is wrapped with observer. A React component is considered to be any class extending from Component or React.Component and any function which name has the first letter capitalized (for anonymous functions the name is inferred from variable). These are all considered components:

class Cmp extends React.Component { }
class Cmp extends Component { }
const Cmp = class extends React.Component { }
const Cmp = class extends Component { }
class extends Component { }
class extends React.Component { }

function Named() { }
const foo = function Named() { }
const Anonym = function () { };
const Arrow = () => { };

Autofix wraps the component with observer and if necessary declares a constant of the same name: const Name = observer(function Name() {}). It's a bit opinionated and can lead to a lot of false positives depending on your conventions. You will probably want to combine this rule with overrides option, eg:

// .eslintrc.js
"overrides": [
  {
    "files": ["*.jsx"],
    "rules": {
      "mobx/missing-observer": "error"
    }
  }
]

mobx/no-anonymous-observer (deprecated)

Deprecated in favor of react/display-name + componentWrapperFunctions. Example of .eslintrc:

{
  "rules": {
    "react/display-name": "warn"
  },
  "settings": {
    "componentWrapperFunctions": [
      "observer"
    ]
  }
}

Forbids anonymous functions or classes as observer components. Improves debugging experience and avoids problem with inability to customize displayName. Plays nice with eslint-plugin-react-hooks and mobx/missing-observer as both of these don't recognize anonymous function as component. Autofix infers the name from variable if possible.