-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/surenpoghosian/code-diagnose
- Loading branch information
Showing
6 changed files
with
157 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { Hint } from '../Reports/Hint'; | ||
import { BasePattern } from './BasePattern'; | ||
import * as ts from 'typescript'; | ||
|
||
export class DeadCodePattern extends BasePattern { | ||
analyze(content: string): Hint[] { | ||
const sourceFile = ts.createSourceFile('file.ts', content, ts.ScriptTarget.ES2015, true); | ||
const hints: Hint[] = []; | ||
const declaredVariables = new Set<string>(); | ||
const usedVariables = new Set<string>(); | ||
|
||
const visit = (node: ts.Node) => { | ||
if (ts.isVariableDeclaration(node) && ts.isIdentifier(node.name)) { | ||
declaredVariables.add(node.name.text); | ||
} else if (ts.isParameter(node) && ts.isIdentifier(node.name)) { | ||
declaredVariables.add(node.name.text); | ||
} else if (ts.isIdentifier(node)) { | ||
// Check if the identifier is used within an expression | ||
const parent = node.parent; | ||
if (parent && !ts.isVariableDeclaration(parent) && !ts.isParameter(parent)) { | ||
usedVariables.add(node.text); | ||
} | ||
} | ||
ts.forEachChild(node, visit); | ||
}; | ||
|
||
visit(sourceFile); | ||
|
||
// Detect unused variables | ||
for (const variable of declaredVariables) { | ||
if (!usedVariables.has(variable)) { | ||
const hint = new Hint(`Unused variable '${variable}' detected.`); | ||
hints.push(hint); | ||
} | ||
} | ||
|
||
return hints; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { DeadCodePattern } from '../src/Patterns/DeadCodePattern'; | ||
import { Hint } from '../src/Reports/Hint'; | ||
|
||
describe('DeadCodePattern', () => { | ||
const deadCodePattern = new DeadCodePattern(); | ||
|
||
it('should detect unused variables', () => { | ||
const content = ` | ||
const unusedVar = 42; | ||
const usedVar = 7; | ||
console.log(usedVar); | ||
`; | ||
|
||
const hints: Hint[] = deadCodePattern.analyze(content); | ||
|
||
expect(hints).toHaveLength(1); | ||
expect(hints[0].message).toMatch(/Unused variable 'unusedVar' detected/); | ||
}); | ||
|
||
it('should detect unused function parameters', () => { | ||
const content = ` | ||
function myFunction(usedParam, unusedParam) { | ||
console.log(usedParam); | ||
} | ||
`; | ||
|
||
const hints: Hint[] = deadCodePattern.analyze(content); | ||
|
||
expect(hints).toHaveLength(1); | ||
expect(hints[0].message).toMatch(/Unused variable 'unusedParam' detected/); | ||
}); | ||
|
||
it('should not flag used variables', () => { | ||
const content = ` | ||
const usedVar1 = 42; | ||
const usedVar2 = 7; | ||
console.log(usedVar1, usedVar2); | ||
`; | ||
|
||
const hints: Hint[] = deadCodePattern.analyze(content); | ||
|
||
expect(hints).toHaveLength(0); | ||
}); | ||
|
||
it('should not flag global variables or imports', () => { | ||
const content = ` | ||
import { someFunction } from 'someModule'; | ||
const usedVar = 7; | ||
console.log(usedVar); | ||
someFunction(); | ||
`; | ||
|
||
const hints: Hint[] = deadCodePattern.analyze(content); | ||
|
||
expect(hints).toHaveLength(0); | ||
}); | ||
|
||
it('should detect unused variables within functions', () => { | ||
const content = ` | ||
function myFunction() { | ||
const unusedVar = 42; | ||
const usedVar = 7; | ||
console.log(usedVar); | ||
} | ||
`; | ||
|
||
const hints: Hint[] = deadCodePattern.analyze(content); | ||
|
||
expect(hints).toHaveLength(1); | ||
expect(hints[0].message).toMatch(/Unused variable 'unusedVar' detected/); | ||
}); | ||
}); |