Add doubleEquals lint rule
MODIFY
packages/@romejs/diagnostics/categories.ts
packages/@romejs/diagnostics/categories.ts
@@ -23,6 +23,7 @@ export type DiagnosticCategory =
| 'lint/caseSingleStatement'
| 'lint/defaultExportSameBasename'
| 'lint/disabled'
+ | 'lint/doubleEquals'
| 'lint/duplicateImportSource'
| 'lint/emptyBlocks'
| 'lint/getterReturn'

MODIFY
packages/@romejs/diagnostics/descriptions.ts
packages/@romejs/diagnostics/descriptions.ts
@@ -264,6 +264,30 @@ export const descriptions = createMessages(
fixable: true,
},
+ DOUBLE_EQUALS: {
+ category: 'lint/doubleEquals',
+ message: 'Use === instead of ==',
+ advice: [
+ {
+ type: 'log',
+ category: 'info',
+ message: '== is only allowed when comparing against null',
+ },
+ ],
+ },
+
+ NEGATE_DOUBLE_EQUALS: {
+ category: 'lint/doubleEquals',
+ message: 'Use !== instead of !=',
+ advice: [
+ {
+ type: 'log',
+ category: 'info',
+ message: '!= is only allowed when comparing against null',
+ },
+ ],
+ },
+
NO_CATCH_ASSIGN: {
category: 'lint/noCatchAssign',
message: "Don't reassign catch parameters",

ADD
packages/@romejs/js-compiler/transforms/lint/doubleEquals.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 '../../api/lint.test';
+
+test('double equals', async (t) => {
+ await testLintMultiple(t, [
+ // VALID
+ 'foo == null',
+ 'foo != null',
+ 'null == foo',
+ 'null != foo',
+
+ // INVALID
+ 'foo == bar',
+ ], {category: 'lint/doubleEquals'});
+});

ADD
packages/@romejs/js-compiler/transforms/lint/doubleEquals.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 {AnyNode} from '@romejs/js-ast';
+import {descriptions} from '@romejs/diagnostics';
+
+export default {
+ name: 'doubleEquals',
+ enter(path: Path): AnyNode {
+ const {node, context} = path;
+
+ if (node.type === 'BinaryExpression' && node.right.type !== 'NullLiteral' &&
+ node.left.type !== 'NullLiteral') {
+ if (node.operator === '!=') {
+ context.addNodeDiagnostic(node, descriptions.LINT.NEGATE_DOUBLE_EQUALS);
+ }
+
+ if (node.operator === '==') {
+ context.addNodeDiagnostic(node, descriptions.LINT.DOUBLE_EQUALS);
+ }
+ }
+
+ return node;
+ },
+};

MODIFY
packages/@romejs/js-compiler/transforms/lint/index.ts
packages/@romejs/js-compiler/transforms/lint/index.ts
// Add imports in alphabetical order.
import caseSingleStatement from './caseSingleStatement';
import defaultExportSameBasename from './defaultExportSameBasename';
+import doubleEquals from './doubleEquals';
import duplicateImportSource from './duplicateImportSource';
import emptyBlocks from './emptyBlocks';
import getterReturn from './getterReturn';
@@ -49,6 +50,7 @@ export const lintTransforms = [
defaultExportSameBasename,
duplicateImportSource,
caseSingleStatement,
+ doubleEquals,
emptyBlocks,
getterReturn,
noArguments,

Add doubleEquals lint rule