Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,12 @@ const ruleNames = getAllRuleNames(); // ['no-relative-paths', 'expo-image-import
| --------------------- | -------- | --------------------------------------------- |
| `prefer-lucide-icons` | warning | Prefer lucide-react/lucide-react-native icons |

### Agent Code Quality Rules

| Rule | Severity | Description |
| ------------- | -------- | --------------------------------- |
| `no-any-type` | warning | Avoid using TypeScript "any" type |

---

## Rule Details
Expand Down
2 changes: 2 additions & 0 deletions src/rules/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { transitionPreferBlankStack } from './transition-prefer-blank-stack';
import { preferGuardClauses } from './prefer-guard-clauses';
import { noTypeAssertion } from './no-type-assertion';
import { noManualRetryLoop } from './no-manual-retry-loop';
import { noAnyType } from './no-any-type';

export const rules: Record<string, RuleFunction> = {
'no-relative-paths': noRelativePaths,
Expand Down Expand Up @@ -69,4 +70,5 @@ export const rules: Record<string, RuleFunction> = {
'prefer-guard-clauses': preferGuardClauses,
'no-type-assertion': noTypeAssertion,
'no-manual-retry-loop': noManualRetryLoop,
'no-any-type': noAnyType,
};
24 changes: 24 additions & 0 deletions src/rules/no-any-type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import traverse from '@babel/traverse';
import type { File } from '@babel/types';
import type { LintResult } from '../types';

const RULE_NAME = 'no-any-type';

export function noAnyType(ast: File, _code: string): LintResult[] {
const results: LintResult[] = [];

traverse(ast, {
TSAnyKeyword(path) {
const { loc } = path.node;
results.push({
rule: RULE_NAME,
message: 'Avoid using "any" type. Use a specific type, "unknown", or a generic instead',
line: loc?.start.line ?? 0,
column: loc?.start.column ?? 0,
severity: 'warning',
});
},
});

return results;
}
2 changes: 1 addition & 1 deletion tests/config-modes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(34);
expect(ruleNames.length).toBe(35);
});
});
});
77 changes: 77 additions & 0 deletions tests/no-any-type.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { describe, it, expect } from 'vitest';
import { lintJsxCode } from '../src';

const config = { rules: ['no-any-type'] };

describe('no-any-type rule', () => {
it('should detect any in variable type annotation', () => {
const code = `const x: any = 5;`;
const results = lintJsxCode(code, config);
expect(results).toHaveLength(1);
expect(results[0].rule).toBe('no-any-type');
expect(results[0].severity).toBe('warning');
});

it('should detect any in function parameter', () => {
const code = `function foo(x: any) { return x; }`;
const results = lintJsxCode(code, config);
expect(results).toHaveLength(1);
});

it('should detect any in return type', () => {
const code = `function foo(): any { return 5; }`;
const results = lintJsxCode(code, config);
expect(results).toHaveLength(1);
});

it('should detect any in generic type parameter', () => {
const code = `const items: Array<any> = [];`;
const results = lintJsxCode(code, config);
expect(results).toHaveLength(1);
});

it('should detect multiple any usages', () => {
const code = `
function process(input: any): any {
const temp: any = input;
return temp;
}
`;
const results = lintJsxCode(code, config);
expect(results).toHaveLength(3);
});

it('should allow unknown type', () => {
const code = `const x: unknown = 5;`;
const results = lintJsxCode(code, config);
expect(results).toHaveLength(0);
});

it('should allow specific types', () => {
const code = `const x: string = 'hello';`;
const results = lintJsxCode(code, config);
expect(results).toHaveLength(0);
});

it('should allow generic types', () => {
const code = `function identity<T>(x: T): T { return x; }`;
const results = lintJsxCode(code, config);
expect(results).toHaveLength(0);
});

it('should detect any in interface properties', () => {
const code = `
interface Foo {
data: any;
}
`;
const results = lintJsxCode(code, config);
expect(results).toHaveLength(1);
});

it('should detect any in type aliases', () => {
const code = `type Callback = (event: any) => void;`;
const results = lintJsxCode(code, config);
expect(results).toHaveLength(1);
});
});
Loading