NomicFoundation / hardhat-vscode

Solidity and Hardhat support for Visual Studio Code
https://hardhat.org
MIT License
163 stars 36 forks source link

VS Code Solidity extension: Highlight Only Uninitialized Immutable Variables and Constructor #542

Open AbhinavMir opened 4 months ago

AbhinavMir commented 4 months ago

In the Solidity extension, currently, when immutable state variables are not initialized, the entire contract is highlighted in red. This is not optimal for understanding the issue at hand. It would be more helpful if only the uninitialized immutable variables and the related constructor were highlighted.

Steps to Reproduce:

  1. Declare an immutable state variable in a Solidity contract.
  2. Create a constructor without initializing the immutable variable.

Expected: Highlighting of immutable variable and constructor to be have red squiggly lines.

Actual: Everything is red, making it hard to see which immutable variables are the problem.

image

AbhinavMir commented 4 months ago

(Can take this up is this is an actual issue!)

kanej commented 4 months ago

This is a great suggestion for an improvement.

What is happening is that that solc is giving an error (solc 2658). The range that solc is giving in the source code is the entire contract:

image

With the extension we can manipulate that source range so that it is limited to the constructor, more specifically just to the constructor keyword.

The way this would be implemented is by providing a new CompilerDiagnistic for 2658. This diagnostic would provide no code actions but transform the solc error by constraining the source range by implementing fromHardhatCompilerError, and constraining to the constructor keyword. You can see a similar move here:

https://github.com/NomicFoundation/hardhat-vscode/blob/12bb234327561fb1c6877a2ced5ea2f6fc324a45/server/src/compilerDiagnostics/diagnostics/ConstrainMutability.ts#L25

AbhinavMir commented 4 months ago

Thanks! I can implement this, but I'm taking a break till end of week. Could you assign it to me so I can keep a tab on it and close it this weekend?

kanej commented 4 months ago

Hey @AbhinavMir I have assigned it to you. Thanks and good luck.