Skip to content

Commit 63c4037

Browse files
authored
Merge pull request #33 from PraneshASP/fix/regex-unused-import
🔨 Fix regex for matching import statements
2 parents 22841b2 + 0405f92 commit 63c4037

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

src/commands/highlight-unused-imports.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,16 @@ async function unusedImportsActiveFile(editor) {
4242
const solhintRuleString = "solhint-disable no-unused-import"
4343
if (text.includes(solhintRuleString)) return;
4444

45-
const importRegex = /import\s+((?:\{.+?\}\s+from\s+)?(?:\".*?\"|'.*?'));/g;
46-
const imports = text.match(importRegex) || [];
45+
const importRegex = /(?<!\/\/\/?).*import\s+((?:\{.+?\}\s+from\s+)?(?:\".*?\"|'.*?'));/g;
46+
const importStatements = text.match(importRegex) || [];
4747
const unusedImportDecorations = [];
48+
for (const importStatement of importStatements) {
49+
// skip commented out import statements
50+
if (importStatement.startsWith("//") || importStatement.startsWith("/*")) return;
4851

49-
for (const importStatement of imports) {
5052
const imports = extractImports(importStatement);
5153
for (const item of imports) {
52-
const regex = new RegExp(item, 'g');
54+
const regex = new RegExp(`\\b${item}\\b`, 'g');
5355
const itemOccurrencesInImportStatement = (importStatement.replace(/\.sol\b/g, '').match(regex) || []).length;
5456
const totalOccurrencesOfItem = (text.match(new RegExp(`\\b${item}\\b`, 'gi')) || []).length;
5557
if (totalOccurrencesOfItem == itemOccurrencesInImportStatement) {

0 commit comments

Comments
 (0)