-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathno-negated-conjunction.ts
73 lines (68 loc) · 2.39 KB
/
no-negated-conjunction.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import type { UnaryExpression } from 'estree'
import type { Rule } from 'eslint'
import { createTestWithParameters } from '../utils/create-test-with-parameters'
import { hasNegationInsideParens } from '../utils/has-negation-inside-parens'
import { hasBooleanContext } from '../utils/has-boolean-context'
import { applyToProperty } from '../utils/apply-to-property'
import { getNodeContent } from '../utils/get-node-content'
import { isConjunction } from '../utils/is-conjunction'
import { sanitizeCode } from '../utils/sanitize-code'
import { isPureGroup } from '../utils/is-pure-group'
import { isNegated } from '../utils/is-negated'
import { transform } from '../utils/transform'
import { repository } from '../package.json'
import { not } from '../utils/not'
import { or } from '../utils/or'
type ParentedExpression = Rule.NodeParentExtension & UnaryExpression
export default {
create: context => ({
UnaryExpression: (node: ParentedExpression) => {
let test = createTestWithParameters(node, context)
if (
test(
isNegated,
applyToProperty('argument', isConjunction),
isPureGroup,
or(hasBooleanContext, not(hasNegationInsideParens)),
)
) {
let shouldWrapInParens = isConjunction(node.parent)
let fixedExpression = transform({
expressionType: 'conjunction',
shouldWrapInParens,
context,
node,
})
if (fixedExpression) {
let originalExpression = getNodeContent(node, context)
context.report({
data: {
original: sanitizeCode(originalExpression),
fixed: sanitizeCode(fixedExpression),
},
fix: fixer => fixer.replaceText(node, fixedExpression),
messageId: 'convertNegatedConjunction',
node,
})
}
}
},
}),
meta: {
docs: {
description:
'Transforms the negation of a conjunction !(A && B) into the ' +
'equivalent !A || !B according to De Morgan’s law',
url: `https://github.com/${repository}/blob/main/docs/no-negated-conjunction.md`,
category: 'Best Practices',
recommended: true,
},
messages: {
convertNegatedConjunction:
'Replace negated conjunction `{{ original }}` with `{{ fixed }}`',
},
type: 'suggestion',
fixable: 'code',
schema: [],
},
} satisfies Rule.RuleModule