|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { analyzeCodebase } from '../analyzer.js'; |
| 3 | +import { noConsoleRule } from './no-console.js'; |
| 4 | +import { noEmptyFunctionRule } from './no-empty-function.js'; |
| 5 | +import { duplicateImportsRule } from './duplicate-imports.js'; |
| 6 | +import { noUnreachableAfterReturnRule } from './no-unreachable-after-return.js'; |
| 7 | +import { noThrowLiteralRule } from './no-throw-literal.js'; |
| 8 | + |
| 9 | +function analyze(code: string, rules: NonNullable<Parameters<typeof analyzeCodebase>[0]['options']>['rules']) { |
| 10 | + return analyzeCodebase({ |
| 11 | + files: [{ path: 'src/test.ts', content: code }], |
| 12 | + selectedDirectories: ['src'], |
| 13 | + options: { rules }, |
| 14 | + }); |
| 15 | +} |
| 16 | + |
| 17 | +describe('no-console rule', () => { |
| 18 | + it('detects console.log', () => { |
| 19 | + const code = 'function foo() { console.log("hi"); }'; |
| 20 | + const report = analyze(code, [noConsoleRule]); |
| 21 | + expect(report.issues.length).toBe(1); |
| 22 | + expect(report.issues[0].ruleId).toBe('no-console'); |
| 23 | + expect(report.issues[0].message).toContain('console.log'); |
| 24 | + }); |
| 25 | + |
| 26 | + it('detects console.warn and console.error', () => { |
| 27 | + const code = [ |
| 28 | + 'function a() { console.warn("w"); }', |
| 29 | + 'function b() { console.error("e"); }', |
| 30 | + ].join('\n'); |
| 31 | + const report = analyze(code, [noConsoleRule]); |
| 32 | + expect(report.issues.length).toBe(2); |
| 33 | + }); |
| 34 | + |
| 35 | + it('does not flag non-console calls', () => { |
| 36 | + const code = 'function foo() { logger.log("hi"); }'; |
| 37 | + const report = analyze(code, [noConsoleRule]); |
| 38 | + expect(report.issues.length).toBe(0); |
| 39 | + }); |
| 40 | + |
| 41 | + it('does not flag console without method call', () => { |
| 42 | + const code = 'const c = console;'; |
| 43 | + const report = analyze(code, [noConsoleRule]); |
| 44 | + expect(report.issues.length).toBe(0); |
| 45 | + }); |
| 46 | +}); |
| 47 | + |
| 48 | +describe('no-empty-function rule', () => { |
| 49 | + it('detects empty function declaration', () => { |
| 50 | + const code = 'function foo() {}'; |
| 51 | + const report = analyze(code, [noEmptyFunctionRule]); |
| 52 | + expect(report.issues.length).toBe(1); |
| 53 | + expect(report.issues[0].ruleId).toBe('no-empty-function'); |
| 54 | + }); |
| 55 | + |
| 56 | + it('detects empty arrow function', () => { |
| 57 | + const code = 'const fn = () => {};'; |
| 58 | + const report = analyze(code, [noEmptyFunctionRule]); |
| 59 | + expect(report.issues.length).toBe(1); |
| 60 | + }); |
| 61 | + |
| 62 | + it('does not flag function with body', () => { |
| 63 | + const code = 'function foo() { return 1; }'; |
| 64 | + const report = analyze(code, [noEmptyFunctionRule]); |
| 65 | + expect(report.issues.length).toBe(0); |
| 66 | + }); |
| 67 | + |
| 68 | + it('does not flag function with comment (intentional stub)', () => { |
| 69 | + const code = [ |
| 70 | + 'function foo() {', |
| 71 | + ' // intentionally empty', |
| 72 | + '}', |
| 73 | + ].join('\n'); |
| 74 | + const report = analyze(code, [noEmptyFunctionRule]); |
| 75 | + expect(report.issues.length).toBe(0); |
| 76 | + }); |
| 77 | +}); |
| 78 | + |
| 79 | +describe('duplicate-imports rule', () => { |
| 80 | + it('detects duplicate imports from same module', () => { |
| 81 | + const code = [ |
| 82 | + "import { a } from 'mod';", |
| 83 | + "import { b } from 'mod';", |
| 84 | + ].join('\n'); |
| 85 | + const report = analyze(code, [duplicateImportsRule]); |
| 86 | + expect(report.issues.length).toBe(1); |
| 87 | + expect(report.issues[0].ruleId).toBe('duplicate-imports'); |
| 88 | + expect(report.issues[0].message).toContain('mod'); |
| 89 | + }); |
| 90 | + |
| 91 | + it('does not flag imports from different modules', () => { |
| 92 | + const code = [ |
| 93 | + "import { a } from 'mod-a';", |
| 94 | + "import { b } from 'mod-b';", |
| 95 | + ].join('\n'); |
| 96 | + const report = analyze(code, [duplicateImportsRule]); |
| 97 | + expect(report.issues.length).toBe(0); |
| 98 | + }); |
| 99 | + |
| 100 | + it('does not flag single import', () => { |
| 101 | + const code = "import { a } from 'mod';"; |
| 102 | + const report = analyze(code, [duplicateImportsRule]); |
| 103 | + expect(report.issues.length).toBe(0); |
| 104 | + }); |
| 105 | +}); |
| 106 | + |
| 107 | +describe('no-unreachable-after-return rule', () => { |
| 108 | + it('detects unreachable code after return', () => { |
| 109 | + const code = [ |
| 110 | + 'function foo() {', |
| 111 | + ' return 1;', |
| 112 | + ' const x = 2;', |
| 113 | + '}', |
| 114 | + ].join('\n'); |
| 115 | + const report = analyze(code, [noUnreachableAfterReturnRule]); |
| 116 | + expect(report.issues.length).toBe(1); |
| 117 | + expect(report.issues[0].ruleId).toBe('no-unreachable-after-return'); |
| 118 | + expect(report.issues[0].message).toContain('Unreachable'); |
| 119 | + }); |
| 120 | + |
| 121 | + it('detects unreachable code after throw', () => { |
| 122 | + const code = [ |
| 123 | + 'function foo() {', |
| 124 | + ' throw new Error("fail");', |
| 125 | + ' console.log("never");', |
| 126 | + '}', |
| 127 | + ].join('\n'); |
| 128 | + const report = analyze(code, [noUnreachableAfterReturnRule]); |
| 129 | + expect(report.issues.length).toBe(1); |
| 130 | + }); |
| 131 | + |
| 132 | + it('does not flag reachable code', () => { |
| 133 | + const code = [ |
| 134 | + 'function foo(x: boolean) {', |
| 135 | + ' if (x) return 1;', |
| 136 | + ' return 2;', |
| 137 | + '}', |
| 138 | + ].join('\n'); |
| 139 | + const report = analyze(code, [noUnreachableAfterReturnRule]); |
| 140 | + expect(report.issues.length).toBe(0); |
| 141 | + }); |
| 142 | + |
| 143 | + it('does not flag type declarations after return', () => { |
| 144 | + const code = [ |
| 145 | + 'function foo() {', |
| 146 | + ' return 1;', |
| 147 | + ' type MyType = string;', |
| 148 | + '}', |
| 149 | + ].join('\n'); |
| 150 | + const report = analyze(code, [noUnreachableAfterReturnRule]); |
| 151 | + expect(report.issues.length).toBe(0); |
| 152 | + }); |
| 153 | +}); |
| 154 | + |
| 155 | +describe('no-throw-literal rule', () => { |
| 156 | + it('detects throw string literal', () => { |
| 157 | + const code = 'function foo() { throw "error"; }'; |
| 158 | + const report = analyze(code, [noThrowLiteralRule]); |
| 159 | + expect(report.issues.length).toBe(1); |
| 160 | + expect(report.issues[0].ruleId).toBe('no-throw-literal'); |
| 161 | + expect(report.issues[0].message).toContain('literal'); |
| 162 | + }); |
| 163 | + |
| 164 | + it('detects throw number literal', () => { |
| 165 | + const code = 'function foo() { throw 42; }'; |
| 166 | + const report = analyze(code, [noThrowLiteralRule]); |
| 167 | + expect(report.issues.length).toBe(1); |
| 168 | + }); |
| 169 | + |
| 170 | + it('does not flag throw new Error()', () => { |
| 171 | + const code = 'function foo() { throw new Error("fail"); }'; |
| 172 | + const report = analyze(code, [noThrowLiteralRule]); |
| 173 | + expect(report.issues.length).toBe(0); |
| 174 | + }); |
| 175 | + |
| 176 | + it('does not flag throw variable', () => { |
| 177 | + const code = [ |
| 178 | + 'function foo() {', |
| 179 | + ' const err = new Error("fail");', |
| 180 | + ' throw err;', |
| 181 | + '}', |
| 182 | + ].join('\n'); |
| 183 | + const report = analyze(code, [noThrowLiteralRule]); |
| 184 | + expect(report.issues.length).toBe(0); |
| 185 | + }); |
| 186 | + |
| 187 | + it('does not flag throw function call result', () => { |
| 188 | + const code = 'function foo() { throw createError(); }'; |
| 189 | + const report = analyze(code, [noThrowLiteralRule]); |
| 190 | + expect(report.issues.length).toBe(0); |
| 191 | + }); |
| 192 | +}); |
0 commit comments