UNPKG

13.8 kBMarkdownView Raw
1<div align="center">
2
3![eslint-logo](docs/assets/eslint-functional-logo.png?sanitize=true)
4
5# eslint-plugin-functional
6
7[![npm version](https://img.shields.io/npm/v/eslint-plugin-functional.svg?style=flat)](https://www.npmjs.com/package/eslint-plugin-functional)
8[![Release](https://github.com/eslint-functional/eslint-plugin-functional/actions/workflows/release.yml/badge.svg)](https://github.com/eslint-functional/eslint-plugin-functional/actions/workflows/release.yml)
9[![Coverage Status](https://codecov.io/gh/eslint-functional/eslint-plugin-functional/branch/main/graph/badge.svg)](https://codecov.io/gh/eslint-functional/eslint-plugin-functional)
10[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg?style=flat)](https://github.com/semantic-release/semantic-release)
11[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat)](https://github.com/prettier/prettier)
12[![MIT license](https://img.shields.io/github/license/eslint-functional/eslint-plugin-functional.svg?style=flat)](https://opensource.org/licenses/MIT)
13[![GitHub Discussions](https://img.shields.io/github/discussions/eslint-functional/eslint-plugin-functional)](https://github.com/eslint-functional/eslint-plugin-functional/discussions)
14
15An [ESLint](http://eslint.org) plugin to disable mutation and promote functional programming in JavaScript and TypeScript.
16
17</div>
18
19## Donate
20
21[Any donations would be much appreciated](./DONATIONS.md). πŸ˜„
22
23### Enterprise Users
24
25`eslint-plugin-functional` is available as part of the Tidelift Subscription.
26
27Tidelift is working with the maintainers of `eslint-plugin-functional` and a growing network of open source maintainers to ensure your open source software supply chain meets enterprise standards now and into the future.
28[Learn more.](https://tidelift.com/subscription/pkg/npm-eslint-plugin-functional?utm_source=npm-eslint-plugin-functional&utm_medium=referral&utm_campaign=enterprise&utm_term=repo)
29
30## Getting Started
31
32[See our getting started guide](./GETTING_STARTED.md).
33
34## Rulesets
35
36The following rulesets are made available by this plugin.
37
38Presets:
39
40- **Strict** (`plugin:functional/strict`)\
41 Enforce recommended rules designed to strictly enforce functional programming.
42
43- **Recommended** (`plugin:functional/recommended`)\
44 Has the same goal as the `strict` preset but a little more lenient, allowing for functional-like coding styles and nicer integration with non-functional 3rd-party libraries.
45
46- **Lite** (`plugin:functional/lite`)\
47 Good if you're new to functional programming or are converting a large codebase.
48
49Categorized:
50
51- **Currying** (`plugin:functional/currying`)\
52 JavaScript functions support syntax that is not compatible with curried functions. To enforce currying, this syntax should be prevented.
53
54- **No Exceptions** (`plugin:functional/no-exceptions`)\
55 Functional programming style does not use run-time exceptions. Instead expressions produces values to indicate errors.
56
57- **No Mutations** (`plugin:functional/no-mutations`)\
58 Prevent mutating any data as that's not functional
59
60- **No Other Paradigms** (`plugin:functional/no-other-paradigms`)\
61 JavaScript is multi-paradigm, allowing not only functional, but object-oriented as well as other programming styles. To promote a functional style, prevent the use of other paradigm styles.
62
63- **No Statements** (`plugin:functional/no-statements`)\
64 In functional programming everything is an expression that produces a value. JavaScript has a lot of syntax that is just statements that does not produce a value. That syntax has to be prevented to promote a functional style.
65
66- **Stylistic** (`plugin:functional/stylistic`)\
67 Enforce code styles that can be considered to be more functional.
68
69Other:
70
71- **All** (`plugin:functional/all`)\
72 Enables all rules defined in this plugin.
73
74- **Off** (`plugin:functional/off`)\
75 Disable all rules defined in this plugin.
76
77- **Disable Type Checked** (`plugin:functional/disable-type-checked`)\
78 Disable all rules that require type information.
79
80- **External Vanilla Recommended** (`plugin:functional/external-vanilla-recommended`)\
81 Configures recommended [vanilla ESLint](https://www.npmjs.com/package/eslint) rules.
82
83- **External Typescript Recommended** (`plugin:functional/external-typescript-recommended`)\
84 Configures recommended [TypeScript ESLint](https://www.npmjs.com/package/@typescript-eslint/eslint-plugin) rules.
85 Enabling this ruleset will also enable the vanilla one.
86
87The [below section](#rules) gives details on which rules are enabled by each ruleset.
88
89## Rules
90
91<!-- prettier-ignore-start -->
92<!-- markdownlint-disable -->
93<!-- begin auto-generated rules list -->
94
95πŸ’Ό Configurations enabled in.\
96⚠️ Configurations set to warn in.\
97🚫 Configurations disabled in.\
98β˜‘οΈ Set in the `lite` configuration.\
99βœ… Set in the `recommended` configuration.\
100πŸ”’ Set in the `strict` configuration.\
101🎨 Set in the `stylistic` configuration.\
102πŸ”§ Automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/user-guide/command-line-interface#--fix).\
103πŸ’‘ Manually fixable by [editor suggestions](https://eslint.org/docs/latest/use/core-concepts#rule-suggestions).\
104πŸ’­ Requires [type information](https://typescript-eslint.io/linting/typed-linting).\
105❌ Deprecated.
106
107### Currying
108
109| Name | Description | πŸ’Ό | ⚠️ | 🚫 | πŸ”§ | πŸ’‘ | πŸ’­ | ❌ |
110| :----------------------------------------------------------- | :----------------------------- | :-------------------------- | :- | :- | :- | :- | :- | :- |
111| [functional-parameters](docs/rules/functional-parameters.md) | Enforce functional parameters. | β˜‘οΈ βœ… πŸ”’ ![badge-currying][] | | | | | | |
112
113### No Exceptions
114
115| Name | Description | πŸ’Ό | ⚠️ | 🚫 | πŸ”§ | πŸ’‘ | πŸ’­ | ❌ |
116| :------------------------------------------------------- | :----------------------------------------------------- | :------------------------------- | :- | :--- | :- | :- | :- | :- |
117| [no-promise-reject](docs/rules/no-promise-reject.md) | Disallow rejecting promises. | | | | | | | |
118| [no-throw-statements](docs/rules/no-throw-statements.md) | Disallow throwing exceptions. | β˜‘οΈ βœ… πŸ”’ ![badge-no-exceptions][] | | | | | | |
119| [no-try-statements](docs/rules/no-try-statements.md) | Disallow try-catch[-finally] and try-finally patterns. | πŸ”’ ![badge-no-exceptions][] | | β˜‘οΈ βœ… | | | | |
120
121### No Mutations
122
123| NameΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β  | Description | πŸ’Ό | ⚠️ | 🚫 | πŸ”§ | πŸ’‘ | πŸ’­ | ❌ |
124| :--------------------------------------------------------------------------- | :-------------------------------------------------------------- | :------------------------------ | :- | :- | :- | :- | :- | :- |
125| [immutable-data](docs/rules/immutable-data.md) | Enforce treating data as immutable. | β˜‘οΈ βœ… πŸ”’ ![badge-no-mutations][] | | | | | πŸ’­ | |
126| [no-let](docs/rules/no-let.md) | Disallow mutable variables. | β˜‘οΈ βœ… πŸ”’ ![badge-no-mutations][] | | | | | | |
127| [prefer-immutable-types](docs/rules/prefer-immutable-types.md) | Require function parameters to be typed as certain immutability | β˜‘οΈ βœ… πŸ”’ ![badge-no-mutations][] | | | πŸ”§ | πŸ’‘ | πŸ’­ | |
128| [prefer-readonly-type](docs/rules/prefer-readonly-type.md) | Prefer readonly types over mutable types. | | | | πŸ”§ | | πŸ’­ | ❌ |
129| [type-declaration-immutability](docs/rules/type-declaration-immutability.md) | Enforce the immutability of types based on patterns. | β˜‘οΈ βœ… πŸ”’ ![badge-no-mutations][] | | | πŸ”§ | πŸ’‘ | πŸ’­ | |
130
131### No Other Paradigms
132
133| NameΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β  | Description | πŸ’Ό | ⚠️ | 🚫 | πŸ”§ | πŸ’‘ | πŸ’­ | ❌ |
134| :------------------------------------------------------- | :------------------------------------------------------------------------ | :------------------------------------ | :- | :--- | :- | :- | :- | :- |
135| [no-classes](docs/rules/no-classes.md) | Disallow classes. | β˜‘οΈ βœ… πŸ”’ ![badge-no-other-paradigms][] | | | | | | |
136| [no-mixed-types](docs/rules/no-mixed-types.md) | Restrict types so that only members of the same kind are allowed in them. | β˜‘οΈ βœ… πŸ”’ ![badge-no-other-paradigms][] | | | | | πŸ’­ | |
137| [no-this-expressions](docs/rules/no-this-expressions.md) | Disallow this access. | πŸ”’ ![badge-no-other-paradigms][] | | β˜‘οΈ βœ… | | | | |
138
139### No Statements
140
141| Name | Description | πŸ’Ό | ⚠️ | 🚫 | πŸ”§ | πŸ’‘ | πŸ’­ | ❌ |
142| :------------------------------------------------------------------- | :--------------------------------------------- | :------------------------------- | :- | :- | :- | :- | :- | :- |
143| [no-conditional-statements](docs/rules/no-conditional-statements.md) | Disallow conditional statements. | βœ… πŸ”’ ![badge-no-statements][] | | β˜‘οΈ | | | πŸ’­ | |
144| [no-expression-statements](docs/rules/no-expression-statements.md) | Disallow expression statements. | βœ… πŸ”’ ![badge-no-statements][] | | β˜‘οΈ | | | πŸ’­ | |
145| [no-loop-statements](docs/rules/no-loop-statements.md) | Disallow imperative loops. | β˜‘οΈ βœ… πŸ”’ ![badge-no-statements][] | | | | | | |
146| [no-return-void](docs/rules/no-return-void.md) | Disallow functions that don't return anything. | β˜‘οΈ βœ… πŸ”’ ![badge-no-statements][] | | | | | πŸ’­ | |
147
148### Stylistic
149
150| NameΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β  | Description | πŸ’Ό | ⚠️ | 🚫 | πŸ”§ | πŸ’‘ | πŸ’­ | ❌ |
151| :--------------------------------------------------------------------- | :--------------------------------------------------------------------- | :- | :- | :- | :- | :- | :- | :- |
152| [prefer-property-signatures](docs/rules/prefer-property-signatures.md) | Prefer property signatures over method signatures. | 🎨 | | | | | πŸ’­ | |
153| [prefer-tacit](docs/rules/prefer-tacit.md) | Replaces `x => f(x)` with just `f`. | | 🎨 | | | πŸ’‘ | πŸ’­ | |
154| [readonly-type](docs/rules/readonly-type.md) | Require consistently using either `readonly` keywords or `Readonly<T>` | 🎨 | | | πŸ”§ | | πŸ’­ | |
155
156<!-- end auto-generated rules list -->
157<!-- markdownlint-restore -->
158<!-- prettier-ignore-end -->
159
160[badge-currying]: https://img.shields.io/badge/-currying-red.svg
161[badge-no-exceptions]: https://img.shields.io/badge/-no--exceptions-blue.svg
162[badge-no-mutations]: https://img.shields.io/badge/-no--mutations-orange.svg
163[badge-no-other-paradigms]: https://img.shields.io/badge/-no--other--paradigms-yellow.svg
164[badge-no-statements]: https://img.shields.io/badge/-no--statements-purple.svg
165
166## External Recommended Rules
167
168In addition to the above rules, there are a few other rules we recommended.
169
170These rules are what are included in the _external recommended_ rulesets.
171
172### Vanilla Rules
173
174- [no-var](https://eslint.org/docs/rules/no-var)\
175 Without this rule, it is still possible to create mutable `var` variables.
176
177- [no-param-reassign](https://eslint.org/docs/rules/no-param-reassign)\
178 Don't allow function parameters to be reassigned, they should be treated as constants.
179
180- [prefer-const](https://eslint.org/docs/rules/prefer-const)\
181 This rule provides a helpful fixer when converting from an imperative code style to a functional one.
182
183### Typescript Rules
184
185- [@typescript-eslint/prefer-readonly](https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/eslint-plugin/docs/rules/prefer-readonly.md)\
186 This rule is helpful when working with classes.
187
188- [@typescript-eslint/switch-exhaustiveness-check](https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/eslint-plugin/docs/rules/switch-exhaustiveness-check.md)\
189 Although our [no-conditional-statements](./docs/rules/no-conditional-statements.md) rule also performs this check,
190 this rule has a fixer that will implement the unimplemented cases which can be useful.
191
192## Contributing
193
194[See our contributing guide](./CONTRIBUTING.md).
195
196## Prior work
197
198This project started as a port of [tslint-immutable](https://github.com/jonaskello/tslint-immutable)
199which was originally inspired by [eslint-plugin-immutable](https://github.com/jhusain/eslint-plugin-immutable).