Add negationElse lint rule
MODIFY
packages/@romejs/diagnostics/categories.ts
packages/@romejs/diagnostics/categories.ts
@@ -103,6 +103,7 @@ type LintDiagnosticCategory =
| 'lint/inconsiderateLanguage'
| 'lint/jsxKey'
| 'lint/jsxNoCommentText'
+ | 'lint/negationElse'
| 'lint/noArguments'
| 'lint/noAsyncPromiseExecutor'
| 'lint/noCatchAssign'

MODIFY
packages/@romejs/diagnostics/descriptions.ts
packages/@romejs/diagnostics/descriptions.ts
@@ -291,6 +291,10 @@ export const descriptions = createMessages({
},
// @romejs/js-compiler
LINT: {
+ NEGATION_ELSE: {
+ category: 'lint/negationElse',
+ message: 'Invert the blocks when you have a negation test',
+ },
STYLE_PROP_OBJECT: {
category: 'lint/stylePropObject',
message: '<emphasis>style</emphasis> property value must be an object.',

MODIFY
packages/@romejs/js-compiler/lint/rules/index.ts
packages/@romejs/js-compiler/lint/rules/index.ts
@@ -17,6 +17,7 @@ import getterReturn from './regular/getterReturn';
import inconsiderateLanguage from './regular/inconsiderateLanguage';
import jsxKey from './react/jsxKey';
import jsxNoCommentText from './react/jsxNoCommentText';
+import negationElse from './regular/negationElse';
import noArguments from './regular/noArguments';
import noAsyncPromiseExecutor from './regular/noAsyncPromiseExecutor';
import noCatchAssign from './regular/noCatchAssign';
@@ -70,6 +71,7 @@ export const lintTransforms = [
inconsiderateLanguage,
jsxKey,
jsxNoCommentText,
+ negationElse,
noArguments,
noAsyncPromiseExecutor,
noCatchAssign,

ADD
packages/@romejs/js-compiler/lint/rules/regular/negationElse.test.ts
+/**
+ * Copyright (c) Facebook, Inc. and its affiliates.
+ *
+ * This source code is licensed under the MIT license found in the
+ * LICENSE file in the root directory of this source tree.
+ */
+
+import {test} from 'rome';
+import {testLintMultiple} from '../testHelpers';
+
+test(
+ 'negation else',
+ async (t) => {
+ await testLintMultiple(
+ t,
+ [
+ // INVALID
+ 'if (!true) {consequent;} else {alternate;}',
+ '!true ? consequent : alternate',
+ // VALID
+ 'if (!true) {consequent;}',
+ 'true ? consequent : alternate',
+ ],
+ {category: 'lint/negationElse'},
+ );
+ },
+);

ADD
packages/@romejs/js-compiler/lint/rules/regular/negationElse.ts
+/**
+ * Copyright (c) Facebook, Inc. and its affiliates.
+ *
+ * This source code is licensed under the MIT license found in the
+ * LICENSE file in the root directory of this source tree.
+ */
+
+import {Path} from '@romejs/js-compiler';
+import {AnyExpression, UnaryExpression} from '@romejs/js-ast';
+import {descriptions} from '@romejs/diagnostics';
+
+function isNegation(node: AnyExpression): node is UnaryExpression {
+ return (
+ node.type === 'UnaryExpression' &&
+ node.prefix === true &&
+ node.operator === '!'
+ );
+}
+
+export default {
+ name: 'negationElse',
+ enter(path: Path) {
+ const {node} = path;
+
+ if (
+ node.type === 'IfStatement' &&
+ node.alternate !== undefined &&
+ isNegation(node.test)
+ ) {
+ return path.context.addFixableDiagnostic(
+ {
+ old: node,
+ fixed: {
+ ...node,
+ test: node.test.argument,
+ consequent: node.alternate,
+ alternate: node.consequent,
+ },
+ },
+ descriptions.LINT.NEGATION_ELSE,
+ );
+ }
+
+ if (node.type === 'ConditionalExpression' && isNegation(node.test)) {
+ return path.context.addFixableDiagnostic(
+ {
+ old: node,
+ fixed: {
+ ...node,
+ test: node.test.argument,
+ consequent: node.alternate,
+ alternate: node.consequent,
+ },
+ },
+ descriptions.LINT.NEGATION_ELSE,
+ );
+ }
+
+ return node;
+ },
+};

Add negationElse lint rule