https://github.com/azat-io/eslint-plugin-de-morgan
š§µ ESLint plugin for transforming negated boolean expressions via DeĀ Morganās laws
https://github.com/azat-io/eslint-plugin-de-morgan
eslint eslint-plugin eslint-rules
Last synced: 5 months ago
JSON representation
š§µ ESLint plugin for transforming negated boolean expressions via DeĀ Morganās laws
- Host: GitHub
- URL: https://github.com/azat-io/eslint-plugin-de-morgan
- Owner: azat-io
- License: mit
- Created: 2025-02-09T12:10:50.000Z (8 months ago)
- Default Branch: main
- Last Pushed: 2025-05-01T08:38:27.000Z (5 months ago)
- Last Synced: 2025-05-01T09:34:51.467Z (5 months ago)
- Topics: eslint, eslint-plugin, eslint-rules
- Language: TypeScript
- Homepage:
- Size: 684 KB
- Stars: 269
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- Changelog: changelog.config.json
- Contributing: contributing.md
- License: license.md
- Code of conduct: .github/code_of_conduct.md
- Security: .github/security.md
Awesome Lists containing this project
- awesome - azat-io/eslint-plugin-de-morgan - š§µ ESLint plugin for transforming negated boolean expressions via DeĀ Morganās laws (TypeScript)
- awesome - azat-io/eslint-plugin-de-morgan - š§µ ESLint plugin for transforming negated boolean expressions via DeĀ Morganās laws (TypeScript)
- trackawesomelist - De Morgan (ā270) - Transforms logical expressions in code to make them easier to understand. (Recently Updated / [May 10, 2025](/content/2025/05/10/README.md))
- awesome-eslint - De Morgan - Transforms logical expressions in code to make them easier to understand. (Plugins / Code Quality)
- fucking-awesome-eslint - De Morgan - Transforms logical expressions in code to make them easier to understand. (Plugins / Code Quality)
README
# ESLint Plugin De Morgan
[](https://npmjs.com/package/eslint-plugin-de-morgan)
[](https://npmjs.com/package/eslint-plugin-de-morgan)
[](https://github.com/azat-io/eslint-plugin-de-morgan/blob/main/license.md)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:**
$\neg (A \land B) \equiv (\neg A) \lor (\neg B)$
**Second Law:**
$\neg (A \lor B) \equiv (\neg A) \land (\neg B)$
Using these principles, the plugin provides two ESLint rules:
- [no-negated-conjunction](https://github.com/azat-io/eslint-plugin-de-morgan/blob/main/docs/no-negated-conjunction.md) ā Transforms negated conjunctions (i.e. expressions of the form !(A && B)) into the equivalent disjunction of negations (!A || !B).
- [no-negated-disjunction](https://github.com/azat-io/eslint-plugin-de-morgan/blob/main/docs/no-negated-disjunction.md) ā 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.
For example:
```js
if (!(a && !b && c >= 10 && d !== e)) {
/* ... */
}
```Becomes:
```js
if (!a || b || c < 10 || d === e) {
/* ... */
}
```- **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.
## Installation
You'll first need to install [ESLint](https://eslint.org):
```sh
npm install --save-dev eslint
```Next, install `eslint-plugin-de-morgan`:
```sh
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`](https://eslint.org/docs/latest/use/configure/configuration-files))
```js
import deMorgan from 'eslint-plugin-de-morgan'export default [
deMorgan.configs.recommended,
]
```### Legacy Config ([`.eslintrc.js`](https://eslint.org/docs/latest/use/configure/configuration-files-deprecated))
```js
module.exports = {
extends: [
'plugin:de-morgan/recommended-legacy',
],
}
```## Rules
š§ Automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/use/command-line-interface#--fix).
| Name | Description | š§ |
| :--------------------- | :----------------------------------------------------------- | :-- |
| no-negated-conjunction | Transforms the negation of a conjunction into the equivalent | š§ |
| no-negated-disjunction | Transforms the negation of a disjunction into the equivalent | š§ |## Further Reading
- [De Morganās Laws](https://en.wikipedia.org/wiki/De_Morgan%27s_laws)
- [Boolean Algebra](https://en.wikipedia.org/wiki/Boolean_algebra)## Versioning Policy
This plugin is following [Semantic Versioning](https://semver.org/) and [ESLint's Semantic Versioning Policy](https://github.com/eslint/eslint#semantic-versioning-policy).
## Contributing
See [Contributing Guide](https://github.com/azat-io/eslint-plugin-de-morgan/blob/main/contributing.md).
## License
MIT Ā© [Azat S.](https://azat.io)