From 7648568d63b9b6e51b7e75268d2cca44b46b8cf4 Mon Sep 17 00:00:00 2001 From: PraneshASP Date: Mon, 26 Jun 2023 20:21:03 +0530 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A8=20fix=20regex?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/commands/highlight-unused-imports.js | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/commands/highlight-unused-imports.js b/src/commands/highlight-unused-imports.js index 1434455..ef444d6 100644 --- a/src/commands/highlight-unused-imports.js +++ b/src/commands/highlight-unused-imports.js @@ -4,18 +4,21 @@ const decorationType = vscode.window.createTextEditorDecorationType({ backgroundColor: 'rgba(255, 0, 0, 0.3)' }); function extractImports(importStatement) { - const itemsRegex = /import\s*\{(.*?)\}\s*from.*/; - const fileRegex = /\/([^\/"]+)\.sol\";/; + const itemsRegex = /import\s*\{(.*?)\}\s*from.*/g; + const fileRegex = /import\s*['"]([^'"]+)['"];/g; - const itemsMatch = importStatement.match(itemsRegex); + const itemsMatch = itemsRegex.exec(importStatement); if (itemsMatch) { // This was an import with named items. // Split the match into individual items, removing leading/trailing whitespace. - const items = itemsMatch[1].split(',').map(item => item.trim()); + const items = itemsMatch[1].split(',').map(item => { + const parts = item.trim().split(/\s+as\s+/); + return parts.length === 2 ? parts[1] : parts[0]; + }); return items; } else { // This was an import with a file. - const fileMatch = importStatement.match(fileRegex); + const fileMatch = fileRegex.exec(importStatement); if (fileMatch) { // Extract the contract/file name from the path. const filePathParts = fileMatch[1].split('/'); @@ -43,9 +46,9 @@ async function unusedImportsActiveFile(editor) { const imports = extractImports(importStatement); for (const item of imports) { const regex = new RegExp(item, 'g'); - const itemOccurancesInImportStatement = (importStatement.replace(/\.sol\b/g, '').match(regex) || []).length; + const itemOccurrencesInImportStatement = (importStatement.replace(/\.sol\b/g, '').match(regex) || []).length; const totalOccurrencesOfItem = (text.match(new RegExp(`\\b${item}\\b`, 'gi')) || []).length; - if (totalOccurrencesOfItem == itemOccurancesInImportStatement) { + if (totalOccurrencesOfItem == itemOccurrencesInImportStatement) { const lineIndex = editor.document.getText().split('\n').findIndex(line => line.includes(importStatement)); const range = new vscode.Range(editor.document.lineAt(lineIndex).range.start, editor.document.lineAt(lineIndex).range.end);