Coder Social home page Coder Social logo

sandromaglione / eslint-plugin-fp-ts-strict Goto Github PK

View Code? Open in Web Editor NEW
17.0 3.0 0.0 8 KB

ESLint plugin for typescript to enforce fp-ts functions to avoid the most common javascript problems

Home Page: https://www.npmjs.com/package/eslint-plugin-fp-ts-strict

License: MIT License

JavaScript 100.00%
fp-ts fp-ts-types functional-programming eslint eslint-plugin eslint-rules functional-js eslint-fp-ts code-style fp-ts-strict

eslint-plugin-fp-ts-strict's Introduction

fp-ts-strict

GitHub: SandroMaglione Twitter: SandroMaglione


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 plugin used to enforce functional programming types and patterns using fp-ts.

npm install --save-dev eslint-plugin-fp-ts-strict
// .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<Type>.

/** 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;
}
/** Example of valid code ๐Ÿ‘ */

export const undefinedOption1 = (): Option<string> => {
  return "abc";
};

export function undefinedOption2(): Option<number> {
  return 1;
}

export function undefinedOption3(): number {
  const a: Option<number> = 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<Type>.

/** 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;
}
/** Example of valid code ๐Ÿ‘ */

export const nullOption1 = (): Option<string> => {
  return "abc";
};

export function nullOption2(): Option<number> {
  return 1;
}

export function nullOption3(): number {
  const a: Option<number> = 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 from fp-ts instead.

/** Example of invalid code ๐Ÿ‘Ž */

const list = [1, 2, 3, 4, 5];
export const headAccess = list[0];
/** 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 from fp-ts instead.

/** Example of invalid code ๐Ÿ‘Ž */

const list = [1, 2, 3, 4, 5];
export const headAccess = list[1];
/** 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 from fp-ts instead.

/** Example of invalid code ๐Ÿ‘Ž */

const record: Record<string, number> = { a: 1, b: 2, c: 3 };
export const recordLookup1 = record["ma"];
export const recordLookup2 = record.ma;
/** Example of valid code ๐Ÿ‘ */

const record: Record<string, number> = { 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. 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.

Another option (or Option) would be to buy me a coffee.

๐Ÿ‘€ License

MIT License, see the LICENSE.md file for details.

eslint-plugin-fp-ts-strict's People

Contributors

sandromaglione avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

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.