Added no compare neg zero lint rule
MODIFY
packages/@romejs/js-compiler/__rtests__/lint.ts
packages/@romejs/js-compiler/__rtests__/lint.ts
@@ -93,3 +93,35 @@ test('format enabled in project config should result in regenerated file', async
);
t.is(res.src, "foobar('yes');\n");
});
+
+test.only('disallows comparing negative zero', async t => {
+ const sourceTextA = '(1 >= -0)';
+
+ const sourceTextB = '(1 >= 0)';
+
+ const res1 = await testLint(sourceTextA, LINT_AND_FORMAT_ENABLED_CONFIG);
+ t.looksLike(res1.diagnostics, [
+ {
+ category: 'lint/noCompareNegZero',
+ filename: 'unknown',
+ language: 'js',
+ message: "Do not use the '>=' operator to compare against -0.",
+ mtime: undefined,
+ sourceType: 'module',
+ origins: [{category: 'lint'}],
+ end: {
+ column: 8,
+ index: 8,
+ line: 1,
+ },
+ start: {
+ column: 1,
+ index: 1,
+ line: 1,
+ },
+ },
+ ]);
+
+ const res2 = await testLint(sourceTextB, LINT_AND_FORMAT_ENABLED_CONFIG);
+ t.looksLike(res2.diagnostics, []);
+});

MODIFY
packages/@romejs/js-compiler/transforms/lint/index.ts
packages/@romejs/js-compiler/transforms/lint/index.ts
@@ -9,10 +9,12 @@ import defaultExportSameBasename from './defaultExportSameBasename';
import undeclaredVariables from './undeclaredVariables';
import unusedVariables from './unusedVariables';
import emptyBlocks from './emptyBlocks';
+import noCompareNegZero from './noCompareNegZero';
export const lintTransforms = [
undeclaredVariables,
defaultExportSameBasename,
unusedVariables,
emptyBlocks,
+ noCompareNegZero,
];

ADD
packages/@romejs/js-compiler/transforms/lint/noCompareNegZero.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 {AnyNode} from '@romejs/js-ast';
+import {Path} from '@romejs/js-compiler';
+
+const OPERATORS_TO_CHECK = new Set([
+ '>',
+ '>=',
+ '<',
+ '<=',
+ '==',
+ '===',
+ '!=',
+ '!==',
+]);
+
+const isNegZero = (node: AnyNode) =>
+ node?.type === 'UnaryExpression' &&
+ node.operator === '-' &&
+ node.argument?.type === 'NumericLiteral' &&
+ node.argument.value === 0;
+
+export default {
+ name: 'noCompareNegZero',
+ enter(path: Path) {
+ const {node} = path;
+ if (
+ node.type === 'BinaryExpression' &&
+ OPERATORS_TO_CHECK.has(node.operator) &&
+ (isNegZero(node.left) || isNegZero(node.right))
+ ) {
+ path.context.addNodeDiagnostic(node, {
+ category: 'lint/noCompareNegZero',
+ message: `Do not use the '${node.operator}' operator to compare against -0.`,
+ });
+ }
+
+ return node;
+ },
+};

Added no compare neg zero lint rule