Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🔨 Fix regex #16

Merged
merged 1 commit into from
Jul 5, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions src/commands/highlight-unused-imports.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('/');
Expand Down Expand Up @@ -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);

Expand Down