Skip to content
프로젝트

azat-io/eslint-plugin-de-morgan

GitHub에서 보기

소개

🧵 ESLint plugin for transforming negated boolean expressions via De Morgan’s laws

기타 프로젝트

이 수치를 측정하는 방법

What you can do

  • Clarity: Rewriting complex negations often results in expressions that more clearly communicate the underlying logic.
  • Avoiding Logical Errors: When dealing with nested logical expressions, small mistakes in the placement of negations can lead to subtle bugs. By enforcing a consistent style based on well-known laws, the plugin helps reduce such errors.
  • Simplification: In some cases, the transformed expression may be simpler to evaluate and optimize, both for human readers and for compilers / interpreters.

측정된 스타 증가

in 24h
0
in 7 days
0
in 15 days
0
in 1 month
기록 수집 중

서로 겹치는 기간의 순증가량이며 일별 합계가 아닙니다. 측정값이 없으면 비워 둡니다. 스타는 관심도를 나타내며 품질을 보장하지 않습니다.

README.md

Original

ESLint Plugin De Morgan

ESLint Plugin De Morgan logo

Version Monthly Download Code Coverage GitHub License

An ESLint plugin that enforces logical consistency by transforming negated boolean expressions according to De Morgan’s laws.

This plugin automatically rewrites negated conjunctions and disjunctions to improve code clarity and reduce potential logical errors.

Why

In Boolean algebra, De Morgan’s laws are two transformation rules that are both valid rules of inference. They are named after Augustus De Morgan and are fundamental in the fields of mathematics, computer science, and digital logic. The laws state that:

First Law:

¬(A ∧ B) ≡ (¬A) ∨ (¬B)

Second Law:

¬(A ∨ B) ≡ (¬A) ∧ (¬B)

Using these principles, the plugin provides two ESLint rules:

  • no-negated-conjunction — Transforms negated conjunctions (i.e. expressions of the form !(A && B)) into the equivalent disjunction of negations (!A || !B).
  • no-negated-disjunction — Transforms negated disjunctions (i.e. expressions of the form !(A || B)) into the equivalent conjunction of negations (!A && !B).

These transformations are grounded in Boolean algebra and can help make the logic of your code more explicit and easier to understand.

Why Use De Morgan’s Laws?

De Morgan’s laws are a cornerstone of Boolean algebra and have several practical benefits in programming:

  • Clarity: Rewriting complex negations often results in expressions that more clearly communicate the underlying logic.

  • Avoiding Logical Errors: When dealing with nested logical expressions, small mistakes in the placement of negations can lead to subtle bugs. By enforcing a consistent style based on well-known laws, the plugin helps reduce such errors.

  • Simplification: In some cases, the transformed expression may be simpler to evaluate and optimize, both for human readers and for compilers / interpreters.

Examples

Base example

Before:

if (!(a && b)) {
  /* ... */
}

After:

if (!a || !b) {
  /* ... */
}

More complex example

Before:

if (!(a || !b || c >= 10 || d !== e)) {
  /* ... */
}

After:

if (!a && b && !(c >= 10) && d === e) {
  /* ... */
}

Installation

You'll first need to install ESLint or Oxlint:

npm install --save-dev eslint
# or
npm install --save-dev oxlint

Next, install eslint-plugin-de-morgan:

npm install --save-dev eslint-plugin-de-morgan

Usage

The easiest way to use eslint-plugin-de-morgan is to use ready-made config.

Flat Config (eslint.config.js)

import deMorgan from 'eslint-plugin-de-morgan'

export default [
  deMorgan.configs.recommended,
]

Legacy Config (.eslintrc.js)

module.exports = {
  extends: [
    'plugin:de-morgan/recommended-legacy',
  ],
}

Oxlint (.oxlintrc.json)

Oxlint loads the plugin as a JS plugin and does not use its ready-made configs, so enable the rules directly:

{
  "jsPlugins": ["eslint-plugin-de-morgan"],
  "rules": {
    "de-morgan/no-negated-conjunction": "error",
    "de-morgan/no-negated-disjunction": "error"
  }
}

Rules

🔧 Automatically fixable by the --fix CLI option.

Name Description 🔧
no-negated-conjunction Transforms the negation of a conjunction into the equivalent disjunction 🔧
no-negated-disjunction Transforms the negation of a disjunction into the equivalent conjunction 🔧

Further Reading

Versioning Policy

This plugin is following Semantic Versioning and ESLint's Semantic Versioning Policy.

Contributing

See Contributing Guide.

License

MIT © Azat S.

관련 프로젝트 살펴보기

불러오는 중…