All Projects → danielnixon → eslint-plugin-total-functions

danielnixon / eslint-plugin-total-functions

Licence: MIT license
An ESLint plugin to enforce the use of total functions (and prevent the use of partial functions) in TypeScript.

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to eslint-plugin-total-functions

Eslint Plugin Eslint Comments
Additional ESLint rules for directive comments of ESLint.
Stars: ✭ 221 (+206.94%)
Mutual labels:  eslint, eslint-plugin
eslint-plugin-react-hook-form
ESLint plugin for react-hook-form
Stars: ✭ 27 (-62.5%)
Mutual labels:  eslint, eslint-plugin
Eslint Plugin Ember
An ESlint plugin that provides set of rules for Ember Applications based on commonly known good practices.
Stars: ✭ 240 (+233.33%)
Mutual labels:  eslint, eslint-plugin
Eslint Plugin Boundaries
Eslint plugin checking architecture boundaries between elements
Stars: ✭ 157 (+118.06%)
Mutual labels:  eslint, eslint-plugin
eslint-plugin-editorconfig
An ESLint plugin to enforce EditorConfig rules
Stars: ✭ 22 (-69.44%)
Mutual labels:  eslint, eslint-plugin
Eslint Plugin Lodash
ESLint rules for lodash
Stars: ✭ 208 (+188.89%)
Mutual labels:  eslint, eslint-plugin
eslint-plugin-pug
An ESLint plugin for linting inline scripts in Pug files
Stars: ✭ 17 (-76.39%)
Mutual labels:  eslint, eslint-plugin
Eslint Plugin I18n Json
Fully extendable eslint plugin for JSON i18n translation files.
Stars: ✭ 101 (+40.28%)
Mutual labels:  eslint, eslint-plugin
eslint-plugin-svelte
ESLint plugin for Svelte using AST
Stars: ✭ 22 (-69.44%)
Mutual labels:  eslint, eslint-plugin
eslint-plugin-rulesdir
An ESLint plugin to load project-specific ESLint rules
Stars: ✭ 28 (-61.11%)
Mutual labels:  eslint, eslint-plugin
Eslint Plugin Unicorn
Various awesome ESLint rules
Stars: ✭ 2,157 (+2895.83%)
Mutual labels:  eslint, eslint-plugin
eslint-plugin-layout-shift
ESLint plugin to force responsive media elements to set the width/height attributes
Stars: ✭ 15 (-79.17%)
Mutual labels:  eslint, eslint-plugin
Eslint Import Resolver Alias
a simple Node behavior import resolution plugin for eslint-plugin-import, supporting module alias
Stars: ✭ 121 (+68.06%)
Mutual labels:  eslint, eslint-plugin
Eslint Plugin Ava
ESLint rules for AVA
Stars: ✭ 209 (+190.28%)
Mutual labels:  eslint, eslint-plugin
Eslint Plugin Css Modules
Project status: NOT MAINTAINED; Checks that you are using the existent css/scss classes, no more no less
Stars: ✭ 115 (+59.72%)
Mutual labels:  eslint, eslint-plugin
Eslint Plugin Mocha
ESLint rules for mocha
Stars: ✭ 249 (+245.83%)
Mutual labels:  eslint, eslint-plugin
Eslint Mdx
ESLint Parser/Plugin for MDX
Stars: ✭ 89 (+23.61%)
Mutual labels:  eslint, eslint-plugin
Typescript Eslint
✨ Monorepo for all the tooling which enables ESLint to support TypeScript
Stars: ✭ 10,831 (+14943.06%)
Mutual labels:  eslint, eslint-plugin
html-eslint
ESLint plugin for linting HTML
Stars: ✭ 72 (+0%)
Mutual labels:  eslint, eslint-plugin
eslint-plugin-disable
Disable ESLint plugins using file path patterns and inline comments
Stars: ✭ 51 (-29.17%)
Mutual labels:  eslint, eslint-plugin

TypeScript Total Functions - ESLint Plugin

Build Status Type Coverage Test Coverage Mutation testing badge Known Vulnerabilities Total alerts Language grade: JavaScript Codacy Badge npm

An ESLint plugin to enforce the use of total functions (and prevent the use of partial functions) in TypeScript. If you like your types to tell the truth, this is the ESLint plugin for you.

Version Matrix

TypeScript ESLint eslint-plugin-total-functions Suppported?
4.5.4 8.5.0 5.0.0
4.4.2 7.32.0 4.10.1 No
4.3.5 7.30.0 4.8.0 No
4.1.2 7.12.0 4.7.2 No
4.0.2 7.9.0 3.3.0 No

Installation

yarn add --dev eslint-plugin-total-functions \
  @typescript-eslint/parser \
  eslint \
  typescript

Setup

Option 1

Use eslint-config-typed-fp which includes this plugin among others.

Option 2

  1. Turn on TypeScript's strict mode and noUncheckedIndexedAccess option.
  2. Set up ESLint + TypeScript.
  3. Turn on eslint-plugin-functional (recommended). Its rules related to mutation and OO are more important than this plugin's rules and they'll help keep your types honest.
  4. Update your .eslintrc.js:
module.exports = {
  parser: "@typescript-eslint/parser",
  parserOptions: {
    project: "./tsconfig.json",
    ecmaVersion: 2018,
    sourceType: "module"
  },
  extends: [
+    "plugin:total-functions/recommended",
  ],
  plugins: [
+    "total-functions",
  ],
};

Alternatively you can configure individual rules separately (see below).

Rules

Rule Recommended All Fixer?
require-strict-mode
no-unsafe-type-assertion
no-unsafe-readonly-mutable-assignment
no-unsafe-mutable-readonly-assignment Not yet
no-unsafe-optional-property-assignment Not yet

total-functions/require-strict-mode

The world is a very strange place when strict mode is disabled. This rule enforces strict mode and noUncheckedIndexedAccess mode (which is sadly not included under the strict umbrella).

total-functions/no-unsafe-type-assertion

Bans unsafe type assertions, for example:

type Foo = { readonly bar: number };
const foo = {} as Foo; // This compiles
foo.bar.toString(); // This explodes at runtime

This is similar to the consistent-type-assertions rule from typescript-eslint, however:

  1. this rule is weaker than consistent-type-assertions with its assertionStyle option set to never -- this rule will permit type assertions that it considers safe as opposed to blanket banning all type assertions, and
  2. this rule is stronger than consistent-type-assertions with its objectLiteralTypeAssertions option set to never, for example:
type Foo = { readonly bar: number };
const foo = {};
const foo2 = foo as Foo; // Flagged by this rule, but not by consistent-type-assertions (unless you set assertionStyle to never)
foo2.bar.toString(); // This explodes at runtime

For examples of type assertions that this rule considers valid and invalid, see no-unsafe-type-assertion.test.ts.

See TypeScript issue #7481 for a request to fix this at the language level.

total-functions/no-unsafe-readonly-mutable-assignment

Bans unsafe assignment of readonly values to mutable values (which can lead to surprising mutation in the readonly value). This includes passing readonly values as arguments to functions that expect mutable parameters.

For examples of assignment that this rule considers valid and invalid, see no-unsafe-readonly-mutable-assignment.test.ts.

See TypeScript issue #13347 for a request to fix this at the language level.

total-functions/no-unsafe-mutable-readonly-assignment

The inverse counterpart to no-unsafe-readonly-mutable-assignment. This rule bans unsafe assignment of mutable values to readonly values (which just like the inverse can lead to surprising mutation in the readonly value).

This rule is often noisy in practice so, unlike no-unsafe-readonly-mutable-assignment, is excluded from the recommended config.

Note that the following is considered an assignment from mutable to readonly:

  type ReadonlyA = { readonly a: string };
  const readonlyA: ReadonlyA = { a: "" };

The solution is to append as const to the RHS:

  type ReadonlyA = { readonly a: string };
  const readonlyA: ReadonlyA = { a: "" } as const;

For examples of assignment that this rule considers valid and invalid, see no-unsafe-mutable-readonly-assignment.test.ts.

total-functions/no-unsafe-optional-property-assignment

Optional properties (those with a ? after their name) interact badly with TypeScript's structural type system in a way that can lead to unsoundness. Example:

type Foo = { readonly foo: string };
type Bar = Foo & { readonly bar?: () => unknown };

const thing = { foo: "foo", bar: "bar" };
const foo: Foo = thing;
const bar: Bar = foo;

if (bar.bar !== undefined) {
    bar.bar(); // explodes at runtime
}

I find this scenario particularly vexing because it doesn't require type assertions, or plain JS with incorrect *.d.ts typings, or anything 'loose' like that. You can pull it off with otherwise nicely typed, functional TypeScript (strict mode enabled, no interfaces, no classes, everything readonly, everything const, no type assertions, no plain JS, etc).

This rule bans assignment from one type to another, if:

  1. the destination type has an optional property, and
  2. the source type has no matching property (either optional or otherwise).

This rule is excluded from the recommended config until #83 lands.

See Also

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].