Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sandromaglione/eslint-plugin-fp-ts-strict
ESLint plugin for typescript to enforce fp-ts functions to avoid the most common javascript problems
https://github.com/sandromaglione/eslint-plugin-fp-ts-strict
code-style eslint eslint-fp-ts eslint-plugin eslint-rules fp-ts fp-ts-strict fp-ts-types functional functional-js functional-programming
Last synced: about 5 hours ago
JSON representation
ESLint plugin for typescript to enforce fp-ts functions to avoid the most common javascript problems
- Host: GitHub
- URL: https://github.com/sandromaglione/eslint-plugin-fp-ts-strict
- Owner: SandroMaglione
- License: mit
- Created: 2021-10-13T14:24:32.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2021-10-30T08:58:15.000Z (about 3 years ago)
- Last Synced: 2024-11-11T11:07:46.440Z (4 days ago)
- Topics: code-style, eslint, eslint-fp-ts, eslint-plugin, eslint-rules, fp-ts, fp-ts-strict, fp-ts-types, functional, functional-js, functional-programming
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/eslint-plugin-fp-ts-strict
- Size: 7.81 KB
- Stars: 17
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# `fp-ts-strict`
---
> fp-ts can help you write safer code, but nothing stops you (or your teammates!) to write unsafe code. Well, it shouldn't be so!
[ESLint](https://eslint.org/) plugin used to enforce functional programming types and patterns using [fp-ts](https://github.com/gcanti/fp-ts).
```shell
npm install --save-dev eslint-plugin-fp-ts-strict
``````js
// .eslintrc.js
module.exports = {
parser: "@typescript-eslint/parser",
plugins: ["fp-ts-strict"],
extends: ["plugin:fp-ts-strict/recommended"],
};
```## Motivation
fp-ts is awesome. Using functional programming, you can make your code safer and easier to read. Nonetheless, it's always javascript we are talking about! Therefore, **nothing stops a distracted version of yourself from writing unsafe code!**
ESLint can help! ESLint can look at your code and point out your typing mistakes. Well, it's time to use ESLint in its full power to enforce functional programming with fp-ts.
Welcome `fp-ts-strict`!
## Rules
### `option-over-undefined`
Avoid using `undefined` in your type unions. Instead of using `Type | undefined`, use `Option`.
```ts
/** Example of invalid code 👎 */export const undefinedOption1 = (): string | undefined => {
return "abc";
};export function undefinedOption2(): number | undefined {
return 1;
}export function undefinedOption3(): number {
const a: number | undefined = 10;
return a ?? 0;
}
``````ts
/** Example of valid code 👍 */export const undefinedOption1 = (): Option => {
return "abc";
};export function undefinedOption2(): Option {
return 1;
}export function undefinedOption3(): number {
const a: Option = some(10);
return pipe(
a,
getOrElse((): number => 0)
);
}
```### `option-over-null`
Avoid using `null` in your type unions. Instead of using `Type | null`, use `Option`.
```ts
/** Example of invalid code 👎 */export const nullOption1 = (): string | null => {
return "abc";
};export function nullOption2(): number | null {
return 1;
}export function nullOption3(): number {
const a: number | null = 10;
return a ?? 0;
}
``````ts
/** Example of valid code 👍 */export const nullOption1 = (): Option => {
return "abc";
};export function nullOption2(): Option {
return 1;
}export function nullOption3(): number {
const a: Option = some(10);
return pipe(
a,
getOrElse((): number => 0)
);
}
```### `array-head`
Accessing the first element of an array using `array[0]` is unsafe, what if the array is empty?. Use [`head`](https://gcanti.github.io/fp-ts/modules/Array.ts.html#head) from fp-ts instead.
```ts
/** Example of invalid code 👎 */const list = [1, 2, 3, 4, 5];
export const headAccess = list[0];
``````ts
/** Example of valid code 👍 */const list = [1, 2, 3, 4, 5];
export const headAccess = pipe(list, head);
```### `array-lookup`
Accessing an element of an array using `array[1]` is unsafe, what if the element is undefined?. Use [`lookup`](https://gcanti.github.io/fp-ts/modules/Array.ts.html#lookup) from fp-ts instead.
```ts
/** Example of invalid code 👎 */const list = [1, 2, 3, 4, 5];
export const headAccess = list[1];
``````ts
/** Example of valid code 👍 */const list = [1, 2, 3, 4, 5];
export const headAccess = pipe(list, lookup(1));
```### `record-lookup`
Accessing an element of a record (map) using `record['a']` or `record.a` is unsafe, what if the element is undefined?. Use [`lookup`](https://gcanti.github.io/fp-ts/modules/Record.ts.html#lookup) from fp-ts instead.
```ts
/** Example of invalid code 👎 */const record: Record = { a: 1, b: 2, c: 3 };
export const recordLookup1 = record["ma"];
export const recordLookup2 = record.ma;
``````ts
/** Example of valid code 👍 */const record: Record = { a: 1, b: 2, c: 3 };
export const recordLookup = pipe(record, lookup("ma"));
```## 💡 More rules?
Of course! If you have any other rule in mind that you would like to propose, feel free to [report an issue](https://github.com/SandroMaglione/eslint-plugin-fp-ts-strict/issues). Make typescript safe again, together!
## 📃 Versioning
- v0.1.1 - 13 October 2021
- v0.1.0 - 13 October 2021## 😀 Support
Currently the best way to support me would be to follow me on my [**Twitter**](https://twitter.com/SandroMaglione).
Another option (or `Option`) would be to buy me a coffee.
## 👀 License
MIT License, see the [LICENSE.md](https://github.com/SandroMaglione/eslint-plugin-fp-ts-strict/blob/main/LICENSE) file for details.