UNPKG

eslint-plugin-de-morgan

Version:

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

32 lines (31 loc) 1.33 kB
import { Expression } from 'estree' import { Rule } from 'eslint' /** * Toggles the negation of the given expression. The function applies the * following strategies in order: * * 1. If the expression is a `BinaryExpression` with a supported binary operator * (`===`, `!==`, `==`, or `!=`), it returns the expression with the operator * toggled. * 2. If the expression is a boolean literal, it returns the toggled boolean * literal. * 3. If the expression is a `UnaryExpression` (i.e. Already negated), it will * return the expression without the leading '!'. * 4. Otherwise, it returns the expression with a '!' prepended.. * * When `canStripNegation` is false, an already negated expression keeps an * exact double negation (`!x` becomes `!!x`) instead of being stripped to the * bare operand. Stripping is only truthiness-preserving, so it is unsafe in * positions where the produced value itself is observable. * * @param node - The expression node to toggle negation on. * @param context - The ESLint rule context. * @param canStripNegation - Whether a leading '!' may be stripped from an * already negated expression. * @returns The toggled expression. */ export declare function toggleNegation( node: Expression, context: Rule.RuleContext, canStripNegation?: boolean, ): string