Skip to content

Commit

Permalink
Merge pull request #4 from surenpoghosian/dead-code-pattern
Browse files Browse the repository at this point in the history
[dead-code-pattern] add dead-code-pattern mvp version
  • Loading branch information
surenpoghosian authored Aug 24, 2024
2 parents ef94f2a + 59cedf5 commit a8ce65f
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 1 deletion.
2 changes: 2 additions & 0 deletions dist/Analyzers/TypeScriptAnalyzer.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/Analyzers/TypeScriptAnalyzer.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/Analyzers/TypeScriptAnalyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { MiddleManPattern } from '../Patterns/MiddleManPattern';
import { PrimitiveObsessionPattern } from '../Patterns/PrimitiveObsessionPattern';
import { SpeculativeGeneralityPattern } from '../Patterns/SpeculativeGeneralityPattern';
import { SwitchStatementOverusePattern } from '../Patterns/SwitchStatementOverusePattern';
import { DeadCodePattern } from '../Patterns/DeadCodePattern';

export class TypeScriptAnalyzer extends BaseAnalyzer {
private patterns = [
Expand All @@ -33,6 +34,7 @@ export class TypeScriptAnalyzer extends BaseAnalyzer {
new PrimitiveObsessionPattern(),
new SpeculativeGeneralityPattern(),
new SwitchStatementOverusePattern(),
new DeadCodePattern(),
// Add other patterns here...
];

Expand Down
39 changes: 39 additions & 0 deletions src/Patterns/DeadCodePattern.ts
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;
}
}
74 changes: 74 additions & 0 deletions tests/DeadCodePattern.test.ts
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/);
});
});

0 comments on commit a8ce65f

Please sign in to comment.