Skip to content

Instantly share code, notes, and snippets.

@OliverJAsh
Created January 22, 2020 12:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OliverJAsh/c1ac5966d6d656cb601b54ef59379a99 to your computer and use it in GitHub Desktop.
Save OliverJAsh/c1ac5966d6d656cb601b54ef59379a99 to your computer and use it in GitHub Desktop.
ESLint `no-logical-not`
/** @type {import('eslint').Rule.RuleModule} */
const noLogicalNot = {
meta: { fixable: 'code' },
create: context => {
/** @type {(node: import('estree').Node) => void} */
const listener = node => {
context.report({
node,
message: 'Prefer `x === false`.',
fix: fixer => {
if (node.range === undefined) {
throw new Error('unreachable');
} else {
const start = node.range[0];
return [
fixer.removeRange([start, start + 1]),
fixer.insertTextAfter(node, ' === false'),
];
}
},
});
};
return {
["UnaryExpression[operator='!']"]: listener,
};
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment