fvictorio / solhint-plugin-prettier

A Solhint plugin for checking your contracts style
29 stars 13 forks source link

How to use prettier rules from .solhint.json to format code? #9

Closed progsmile closed 2 years ago

progsmile commented 2 years ago

Hello! Thanks for package! I want to specify prettier rules within .solhint.json and not within .prettierrc. When I do linting solhint -f table contracts/**/*.sol, prettier rules are considered.

But next commands does not work correctly when trying to format:

.solhint.json

{
  "extends": "solhint:recommended",
  "plugins": ["prettier"],
  "rules": {
    "compiler-version": ["error", "^0.8.10"],
    "func-name-mixedcase": "off",
    "reason-string": "off",

    "prettier/prettier": [
      "error",
      {
        "printWidth": 120,
        "tabWidth": 2,
        "useTabs": false,
        "singleQuote": false,
        "bracketSpacing": false,
        "explicitTypes": "always"
      }
    ]
  }
}

Packages:

{
...
    "prettier": "^2.5.1",
    "prettier-plugin-solidity": "^1.0.0-beta.19",
    "solhint": "^3.3.6",
    "solhint-plugin-prettier": "^0.0.5"
}

Please help to pick correct formatting command and params. Thanks!

fvictorio commented 2 years ago

As far as I remember, this is not supported:

    "prettier/prettier": [
      "error",
      {
        "printWidth": 120,
        "tabWidth": 2,
        "useTabs": false,
        "singleQuote": false,
        "bracketSpacing": false,
        "explicitTypes": "always"
      }
    ]

You have to specify your prettier options in your .prettierrc, not in your solhint config.

progsmile commented 2 years ago

I've come to solution to import rules in prettier.config.js.

const solhint = require('./.solhint.json')

module.exports = {
  "overrides": [
    {
      "files": "*.sol",
      "options": solhint.rules['prettier/prettier'][1]
    }
  ]
}
fvictorio commented 2 years ago

That works but notice that you are making a really unnecessary indirection. The second value in the prettier/prettier rule in your solhint.json is ignored by solhint. I think it would be better if you just specified those options directly in the prettier.config.js file.

Besides, if this plugin in the future adds some options, then your current config will be invalid.

progsmile commented 2 years ago

It was attempt to use lint and style rules within 1 file. I've tested .solhint.json without prettier/prettier section and linting does not work from prettier.config.js. I run it with solhint -f table contracts/**/*.sol. Any other commands to apply?

fvictorio commented 2 years ago

It was attempt to use lint and style rules within 1 file

I don't think that's possible. I think technically this rule could allow that, but it would add a lot of complexity (e.g., who has precedence?).

I've tested .solhint.json without prettier/prettier section and linting does not work from prettier.config.js

Maybe you have a wrong idea of what this plugin does. The only thing that this prettier/prettier rule does is to "run" prettier and let you know which parts of your code are not properly formatted. This is especially useful for IDEs to indicate those spots. But other than that, you could remove this rule and use npx prettier --check instead. Does that make sense?

progsmile commented 2 years ago

@fvictorio thanks for your assistance! Make sense.