From b12030496f0c25185b89a9ebed640dc9bcf858a7 Mon Sep 17 00:00:00 2001 From: Daniel C Date: Fri, 13 Feb 2026 17:45:35 -0800 Subject: [PATCH] Add no-loose-equality lint rule --- README.md | 23 +++++++++- src/rules/index.ts | 2 + src/rules/no-loose-equality.ts | 33 ++++++++++++++ tests/config-modes.test.ts | 2 +- tests/no-loose-equality.test.ts | 76 +++++++++++++++++++++++++++++++++ 5 files changed, 134 insertions(+), 2 deletions(-) create mode 100644 src/rules/no-loose-equality.ts create mode 100644 tests/no-loose-equality.test.ts diff --git a/README.md b/README.md index e23d047..dce545e 100644 --- a/README.md +++ b/README.md @@ -91,7 +91,7 @@ const allResults = lintJsxCode(code, { const ruleNames = getAllRuleNames(); // ['no-relative-paths', 'expo-image-import', ...] ``` -## Available Rules (33 total) +## Available Rules (34 total) ### Expo Router Rules @@ -164,6 +164,7 @@ const ruleNames = getAllRuleNames(); // ['no-relative-paths', 'expo-image-import | ---------------------- | -------- | --------------------------------------------------------- | | `prefer-guard-clauses` | warning | Use early returns instead of nesting if statements | | `no-type-assertion` | warning | Avoid `as` type casts; use type narrowing or proper types | +| `no-loose-equality` | warning | Use === and !== instead of == and != (except == null) | ### General Rules @@ -420,6 +421,26 @@ if (typeof data === 'string') { const user: User = response.data; ``` +### `no-loose-equality` + +```typescript +// Bad - loose equality +if (a == b) { +} +if (x != 'hello') { +} + +// Good - strict equality +if (a === b) { +} +if (x !== 'hello') { +} + +// OK - == null is idiomatic for null/undefined check +if (value == null) { +} +``` + --- ## Adding a New Rule diff --git a/src/rules/index.ts b/src/rules/index.ts index 690e186..488792d 100644 --- a/src/rules/index.ts +++ b/src/rules/index.ts @@ -32,6 +32,7 @@ import { transitionSharedTagMismatch } from './transition-shared-tag-mismatch'; import { transitionPreferBlankStack } from './transition-prefer-blank-stack'; import { preferGuardClauses } from './prefer-guard-clauses'; import { noTypeAssertion } from './no-type-assertion'; +import { noLooseEquality } from './no-loose-equality'; export const rules: Record = { 'no-relative-paths': noRelativePaths, @@ -67,4 +68,5 @@ export const rules: Record = { 'transition-prefer-blank-stack': transitionPreferBlankStack, 'prefer-guard-clauses': preferGuardClauses, 'no-type-assertion': noTypeAssertion, + 'no-loose-equality': noLooseEquality, }; diff --git a/src/rules/no-loose-equality.ts b/src/rules/no-loose-equality.ts new file mode 100644 index 0000000..d189c8f --- /dev/null +++ b/src/rules/no-loose-equality.ts @@ -0,0 +1,33 @@ +import traverse from '@babel/traverse'; +import * as t from '@babel/types'; +import type { File } from '@babel/types'; +import type { LintResult } from '../types'; + +const RULE_NAME = 'no-loose-equality'; + +export function noLooseEquality(ast: File, _code: string): LintResult[] { + const results: LintResult[] = []; + + traverse(ast, { + BinaryExpression(path) { + const { operator, left, right } = path.node; + + if (operator !== '==' && operator !== '!=') return; + + // Allow == null and != null (idiomatic null/undefined check) + if (t.isNullLiteral(right) || t.isNullLiteral(left)) return; + + const strict = operator === '==' ? '===' : '!=='; + const { loc } = path.node; + results.push({ + rule: RULE_NAME, + message: `Use '${strict}' instead of '${operator}' for strict equality comparison.`, + line: loc?.start.line ?? 0, + column: loc?.start.column ?? 0, + severity: 'warning', + }); + }, + }); + + return results; +} diff --git a/tests/config-modes.test.ts b/tests/config-modes.test.ts index 1f3d1f0..51cdb4e 100644 --- a/tests/config-modes.test.ts +++ b/tests/config-modes.test.ts @@ -100,7 +100,7 @@ describe('config modes', () => { expect(ruleNames).toContain('no-relative-paths'); expect(ruleNames).toContain('expo-image-import'); expect(ruleNames).toContain('no-stylesheet-create'); - expect(ruleNames.length).toBe(33); + expect(ruleNames.length).toBe(34); }); }); }); diff --git a/tests/no-loose-equality.test.ts b/tests/no-loose-equality.test.ts new file mode 100644 index 0000000..daaf68c --- /dev/null +++ b/tests/no-loose-equality.test.ts @@ -0,0 +1,76 @@ +import { describe, it, expect } from 'vitest'; +import { lintJsxCode } from '../src'; + +const RULE = 'no-loose-equality'; + +function lint(code: string) { + return lintJsxCode(code, { rules: [RULE] }); +} + +describe(RULE, () => { + it('flags == comparison', () => { + const code = `if (a == b) {}`; + const results = lint(code); + expect(results).toHaveLength(1); + expect(results[0].rule).toBe(RULE); + expect(results[0].message).toContain('==='); + }); + + it('flags != comparison', () => { + const code = `if (a != b) {}`; + const results = lint(code); + expect(results).toHaveLength(1); + expect(results[0].message).toContain('!=='); + }); + + it('flags == with string literal', () => { + const code = `if (x == 'hello') {}`; + const results = lint(code); + expect(results).toHaveLength(1); + }); + + it('flags == with number', () => { + const code = `if (count == 0) {}`; + const results = lint(code); + expect(results).toHaveLength(1); + }); + + it('allows == null (idiomatic null check)', () => { + const code = `if (value == null) {}`; + const results = lint(code); + expect(results).toHaveLength(0); + }); + + it('allows != null (idiomatic null check)', () => { + const code = `if (value != null) {}`; + const results = lint(code); + expect(results).toHaveLength(0); + }); + + it('allows null == value (reverse null check)', () => { + const code = `if (null == value) {}`; + const results = lint(code); + expect(results).toHaveLength(0); + }); + + it('allows === comparison', () => { + const code = `if (a === b) {}`; + const results = lint(code); + expect(results).toHaveLength(0); + }); + + it('allows !== comparison', () => { + const code = `if (a !== b) {}`; + const results = lint(code); + expect(results).toHaveLength(0); + }); + + it('flags multiple loose comparisons', () => { + const code = ` + if (a == b) {} + if (c != d) {} + `; + const results = lint(code); + expect(results).toHaveLength(2); + }); +});