Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
surenpoghosian committed Aug 24, 2024
2 parents 3e9d190 + d3882c0 commit bca43ac
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 40 deletions.
78 changes: 39 additions & 39 deletions .github/workflows/release-package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,42 +22,42 @@ jobs:
- name: Run tests
run: npm test

publish-npm:
needs: build
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 20

- name: Configure npm authentication
run: echo "//registry.npmjs.org/:_authToken=${{ secrets.NPM_TOKEN }}" > ~/.npmrc

- name: Publish to npm
run: npm publish --access public

publish-gpr:
needs: build
runs-on: ubuntu-latest
permissions:
packages: write
contents: read
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 20
registry-url: https://npm.pkg.github.com/

- name: Configure npm to use GitHub Packages registry
run: echo "//npm.pkg.github.com/:_authToken=${{ secrets.GIT_TOKEN }}" > ~/.npmrc

- name: Publish to GitHub Packages
run: npm publish
publish-npm:
needs: build
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 20

- name: Configure npm authentication
run: echo "//registry.npmjs.org/:_authToken=${{ secrets.NPM_TOKEN }}" > ~/.npmrc

- name: Publish to npm
run: npm publish --access public

publish-gpr:
needs: build
runs-on: ubuntu-latest
permissions:
packages: write
contents: read
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 20
registry-url: https://npm.pkg.github.com/

- name: Configure npm to use GitHub Packages registry
run: echo "//npm.pkg.github.com/:_authToken=${{ secrets.GIT_TOKEN }}" > ~/.npmrc

- name: Publish to GitHub Packages
run: npm publish
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 bca43ac

Please sign in to comment.