gajus / eslint-plugin-jsdoc

JSDoc specific linting rules for ESLint.
Other
1.1k stars 159 forks source link

A custom @use tag for restricting closure variables #128

Open brettz9 opened 5 years ago

brettz9 commented 5 years ago

While many of us use and enjoy using closures all over the place (or non-"pure" functions), taking advantage of their ability to borrow from their surrounding context, without any rule enforcement, such code can become a non-modular, non-unit-testable, non-externally-reusable spaghetti of functions, with too many contextual dependencies and preventing closures from being moved externally if necessary and may cause memory leaks/prevent garbage collection.

I'd like to see a non-standard JSDoc tag (akin to PHP's "use" language construct) to help with this, as well as to make no-shadow less unpleasant as detailed in the ESLint report.

I detailed such an idea at https://github.com/eslint/eslint/issues/11061 though it was rejected from ESLint proper.

While I don't think this should be a recommended rule, I think it could provide some useful linting qualities and wanted to get your thoughts.

Update: Would be nice to also support other tags for enforcing the degree of function purity like @setsProperty {type} target name, @addsProperty {type} target name, @removesProperty.

--- Want to back this issue? **[Post a bounty on it!](https://app.bountysource.com/issues/67270527-a-custom-use-tag-for-restricting-closure-variables?utm_campaign=plugin&utm_content=tracker%2F23037809&utm_medium=issues&utm_source=github)** We accept bounties via [Bountysource](https://app.bountysource.com/?utm_campaign=plugin&utm_content=tracker%2F23037809&utm_medium=issues&utm_source=github).
gajus commented 5 years ago

Go for it. Sounds like a useful addition.

brettz9 commented 5 years ago

I think it would also be interesting if config could allow reading of outside variables but prevent setting of them.

brettz9 commented 3 years ago

The rule in the MIT-based library at https://github.com/yannickcr/eslint-plugin-react/blob/master/lib/rules/prefer-stateless-function.js might be useful as a starting point for this rule. (https://github.com/purely-functional/eslint-plugin-pure/blob/master/docs/rules/pure.md might be of interest, but probably not of real use for our particular needs here.)