Skip to content

Commit

Permalink
Removed func removeText & updated variableRegex
Browse files Browse the repository at this point in the history
  • Loading branch information
Denisoed committed Jun 5, 2024
1 parent 4a7291b commit 566a60c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
18 changes: 18 additions & 0 deletions example/files/index.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<template>
<div>
<h1>List</h1>
<ul>
<li v-for="item in list" :key="item">{{ item }}</li>
</ul>
</div>
</template>

<script setup>
const list = ['item1', 'item2', 'item3'];
const getItems = () => list;
const getFirstItem = () => {
return list[0];
};
</script>
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dead-code-checker",
"version": "1.0.1",
"version": "1.0.2",
"description": "Dead Code Checker is a tool for finding dead code in your JavaScript or TypeScript project. It helps to ensure a cleaner and more maintainable code base.",
"license": "MIT",
"repository": "https://github.com/denisoed/dead-code-checker",
Expand Down
10 changes: 3 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ class DeadCodeChecker {
const methodRegex = /([a-zA-Z0-9_]+)\s*\(([^)]*)\)\s*{/g;
const onlyInReturnRegex = /\breturn\s*{([^}]*)}/g;
const onlyModuleExportsRegex = /\bmodule\.exports\s*=\s*{([^}]*)}/g;
const variableRegex = /\b(?:const|let|var)\s+([a-zA-Z0-9_]+)\s*=?/g;
const variableRegex =
/\b(?:const|let|var)\s+([a-zA-Z0-9_]+)\s*(?:=|;|\r?\n|$)/g;

const declaredNames: { name: string; line: number }[] = [];

Expand Down Expand Up @@ -116,10 +117,6 @@ class DeadCodeChecker {
return fileContent.replace(/\/\*[\s\S]*?\*\/|\/\/.*/g, '');
}

private removeText(fileContent: string) {
return fileContent.replace(/(['"])(?:(?=(\\?))\2.)*?\1/g, '');
}

private displayReport() {
this.reportList.forEach(report => {
console.log(
Expand Down Expand Up @@ -153,10 +150,9 @@ class DeadCodeChecker {
for (const filePath of allFiles) {
const fileContent = fs.readFileSync(filePath, 'utf8');
const withoutComments = this.removeComments(fileContent);
const withoutTexts = this.removeText(withoutComments);
Object.keys(this.deadMap).forEach(name => {
const usageRegex = new RegExp(`\\b${name}\\b`, 'g');
const matches = withoutTexts.match(usageRegex);
const matches = withoutComments.match(usageRegex);
if (matches) {
this.deadMap[name].count += matches.length;
}
Expand Down

0 comments on commit 566a60c

Please sign in to comment.