UNPKG

eslint-plugin-de-morgan

Version:

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

55 lines (54 loc) 1.47 kB
'use strict' Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }) const findOutermostParenthesizedNode = require('./find-outermost-parenthesized-node.js') const getNodeContent = require('./get-node-content.js') const getSourceCode = require('./get-source-code.js') let getCodeInsideParentheses = code => { if (code.startsWith('!(')) { return code.slice(2, -1) } if (code.startsWith('(')) { return code.slice(1, -1) } return code } let hasMixedOperators = code => { let depth = 0 let operatorFound = null for (let i = 0; i < code.length; i++) { let char = code[i] if (char === '(') { depth++ continue } if (char === ')') { depth-- continue } if (depth !== 0) { continue } let twoChars = code.slice(i, i + 2) if (twoChars === '&&' || twoChars === '||') { if (operatorFound === null) { operatorFound = twoChars } else if (operatorFound !== twoChars) { return true } i++ } } return false } let isPureGroup = (node, context) => { let sourceCode = getSourceCode.getSourceCode(context).getText() let outermostNode = findOutermostParenthesizedNode.findOutermostParenthesizedNode( node, sourceCode, ) let fullCode = getNodeContent.getNodeContent(outermostNode, context) let innerCode = getCodeInsideParentheses(fullCode) return !hasMixedOperators(innerCode) } exports.isPureGroup = isPureGroup