eslint-plugin-de-morgan

repository·main·Indexed 43 days ago

https://github.com/azat-io/eslint-plugin-de-morgan

ESLint plugin version 2.1.3 for transforming negated boolean expressions via De Morgan’s laws. It provides two auto-fixable rules: `no-negated-conjunction` to transform `!(A && B)` into `!A || !B`, and `no-negated-disjunction` to transform `!(A || B)` into `!A && !B`. Supports both Flat Config and Legacy Config setups.

Tokens
1.6K
Snippets
10
Records
12
Agent score
54%

What's inside eslint-plugin-de-morgan

  1. How De Morgan's laws are applied to code

    main

    The plugin transforms negated boolean expressions to improve clarity and reduce errors.

    Example 1: Negated Conjunction

    • Before: if (!(a && b))
    • After: if (!a || !b)

    Example 2: Complex Negated Disjunction

    • Before: if (!(a || !b || c >= 10 || d !== e))
    • After: if (!a && b && !(c >= 10) && d === e)
    // Before
    if (!(a && b)) {
      /* ... */
    }
    
    // After
    if (!a || !b) {
      /* ... */
    }
  2. Configure ESLint using Flat Config (eslint.config.js)

    main

    For modern ESLint setups using Flat Config, import the plugin and use the deMorgan.configs.recommended object in your configuration array.

    import deMorgan from 'eslint-plugin-de-morgan'
    
    export default [
      deMorgan.configs.recommended,
    ]
  3. Use the recommended configurations for eslint-plugin-de-morgan

    main

    The plugin provides two pre-defined configurations to quickly enable its rules.

    • For the modern Flat Config (eslint.config.js), use the recommended config.
    • For the Legacy Config (.eslintrc.js), use the recommended-legacy config.

    Both configurations enable all available rules with the 'error' severity level.

    // Flat Config (eslint.config.js)
    import deMorgan from 'eslint-plugin-de-morgan';
    
    export default [
      deMorgan.configs.recommended,
    ];
    
    // Legacy Config (.eslintrc.js)
    module.exports = {
      plugins: ['de-morgan'],
      extends: ['plugin:de-morgan/recommended-legacy'],
    };
  4. Rules in eslint-plugin-de-morgan

    main

    The plugin provides two rules that enforce logical consistency by applying De Morgan's laws. Both rules are automatically fixable using the ESLint --fix CLI option.

    NameDescriptionFixable
    no-negated-conjunctionTransforms the negation of a conjunction (e.g., !(A && B)) into the equivalent disjunction (!A || !B)Yes
    no-negated-disjunctionTransforms the negation of a disjunction (e.g., !(A || B)) into the equivalent conjunction (!A && !B)Yes
  5. Use the no-negated-conjunction rule

    main

    The no-negated-conjunction rule enforces De Morgan's first law by transforming the negation of a conjunction into an equivalent disjunction of negations. This improves readability by making it explicit that at least one condition must be false, rather than wrapping a group of conditions in a negation.

    Rule Application

    The rule applies only when the operand of the negation is a pure conjunction (all operands are combined with && at the same nesting level). It will not apply if the expression inside the negation contains a mix of logical operators (e.g., !(a && b || c)).

    // From this:
    if (!(a && b)) {
      /* ... */
    }
    
    // To this:
    if (!a || !b) {
      /* ... */
    }
  6. Use the no-negated-disjunction rule

    main

    The no-negated-disjunction rule transforms the negation of a disjunction into an equivalent conjunction of negations, following De Morgan's second law.

    Instead of using a negated disjunction (e.g., !(a || b)), the rule enforces the use of individual negations joined by AND (e.g., !a && !b). This makes boolean logic more explicit and easier to read by clearly indicating that all individual conditions must be false.

    Application Criteria

    • The rule only applies when the operand of the negation is a pure disjunction. This means all operands must be combined with || at the same nesting level.
    • The rule will not apply if the expression inside the negation contains a mix of different logical operators (e.g., !(a || b && c)).
    // Before
    if (!(a || b)) {
      /* ... */
    }
    
    // After
    if (!a && !b) {
      /* ... */
    }
  7. Available rules in eslint-plugin-de-morgan

    main

    The plugin exports the following rules which can be used individually in your ESLint configuration:

    • no-negated-conjunction: Detects and suggests simplification of negated conjunctions (e.g., !(a && b) to !a || !b).
    • no-negated-disjunction: Detects and suggests simplification of negated disjunctions (e.g., !(a || b) to !a && !b).

    When using these rules manually, prefix them with the plugin name: de-morgan/no-negated-conjunction or de-morgan/no-negated-disjunction.

    // Example manual rule configuration
    export default [
      {
        rules: {
          'de-morgan/no-negated-conjunction': 'error',
          'de-morgan/no-negated-disjunction': 'warn',
        },
      },
    ];