Skip to content

Commit

Permalink
fix: diagnostics wrong position on unicode characters (#248)
Browse files Browse the repository at this point in the history
  • Loading branch information
antico5 committed Sep 8, 2022
1 parent 24ee5a0 commit 8bf3dd6
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
23 changes: 23 additions & 0 deletions server/src/services/validation/worker/build/solcCompile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,28 @@ export async function solcCompile(
});
}

// Normalize errors' sourceLocation to use utf-8 offsets instead of byte offsets
for (const error of output.errors) {
const source = input.sources[error.sourceLocation?.file];

if (source === undefined) {
continue;
}

const normalizeOffset = (text: string, offset: number) => {
return Buffer.from(text, "utf-8").slice(0, offset).toString("utf-8")
.length;
};

error.sourceLocation.start = normalizeOffset(
source.content,
error.sourceLocation.start
);
error.sourceLocation.end = normalizeOffset(
source.content,
error.sourceLocation.end
);
}

return { output, solcBuild };
}
13 changes: 13 additions & 0 deletions test/integration/tests/diagnostics/diagnostics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,17 @@ suite("diagnostics", function () {
'Contract "Counter" should be marked as abstract'
);
});

test.only("[diagnostics] unicode characters", async () => {
const uri = getDocUri(__dirname, "./UnicodeCharacters.sol");
await openFileInEditor(uri);

await checkOrWaitDiagnostic(
uri,
new vscode.Range(6, 4, 6, 14),
vscode.DiagnosticSeverity.Error,
"solidity",
"Different number of arguments in return statement"
);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.3;

contract UnicodeCharacters {
//ñññññ
function bar() public {
return 123;
}
}

0 comments on commit 8bf3dd6

Please sign in to comment.