prettier-solidity / prettier-plugin-solidity
A Prettier plugin for automatically formatting your Solidity code.
AI Architecture Analysis
This repository is indexed by RepoMind. By analyzing prettier-solidity/prettier-plugin-solidity in our AI interface, you can instantly generate complete architecture diagrams, visualize control flows, and perform automated security audits across the entire codebase.
Our Agentic Context Augmented Generation (Agentic CAG) engine loads full source files into context on-demand, avoiding the fragmentation of traditional RAG systems. Ask questions about the architecture, dependencies, or specific features to see it in action.
Repository Overview (README excerpt)
Crawler viewprettier-plugin-solidity > ℹ️ This README is for the new major version of Prettier Plugin Solidity. > > The differences from v1 are minimal, but there are some breaking changes. > > To migrate from v1, follow the migration guide. > > If you're looking for the previous version, check out the v1 branch. A Prettier plugin for automatically formatting your Solidity code. Installation and usage Using in NodeJS Install both and : Run prettier on your contracts: You can add an npm script to run prettier on all your contracts: Or you can use it as part of your linting to check that all your code is prettified: > Prettier Solidity only works with valid code. If there is a syntax error, nothing will be done and a parser error will be thrown. Using in the Browser To use this package in the browser, you need to load Prettier's standalone bundle before loading the build provided in this package. Prettier's unpkg field points to , in a similar way this plugin points to . Once the scripts are loaded you will have access to the globals and . We follow Prettier's strategy for populating their plugins in the object , you can load other plugins like and Prettier will have access to multiple parsers. For more details, have a look at Prettier's documentation. Creating a package for the Browser If you are creating your own package to be run in a browser, you might want to import the standalone files directly. Configuration File Prettier provides a flexible system to configure the formatting rules of a project. For more information please refer to the documentation. The following is the default configuration internally used by this plugin. Note the use of the overrides property which allows for multiple configurations in case there are other languages in the project (i.e. JavaScript, JSON, Markdown). Since Prettier v3.0.0, the plugin search feature has been removed so we encourage adding our plugin to the configuration file. Most options are described in Prettier's documentation. Compiler Many versions of the Solidity compiler have changes that affect how the code should be formatted. This plugin, by default, tries to format the code in the most compatible way that is possible, but you can use the option to nudge it in the right direction. One example of this is import directives. Before , the compiler didn't accept multi-line import statements, so we always format them in a single line. But if you use the option to indicate that you are using a version greater or equal than , the plugin will use multi-line imports when it makes sense. The Solidity versions taken into consideration during formatting are: • : Versions prior had a bug that would not interpret correctly imports unless they are formatted in a single line. • : Introduced these changes • The type has been removed. It was an alias of . • Exponentiation is right associative, i.e., the expression is parsed as . Before 0.8.0, it was parsed as . You might have a multi-version project, where different files are compiled with different compilers. If that's the case, you can use overrides to have a more granular configuration: | Default | CLI Override | API Override | | --------------------------------------------------------------------------------------------- | --------------------- | ---------------------- | | Inferred from pragma statements when using parser None when using parser | | | Parser You can configure the parser used by Prettier Solidity. Two Solidity parsers are supported: • Slang (the default), a more powerful and correct parser that results in better formatting for some edge cases, especially when comments are involved. • Solidity Parser for JavaScript, an ANTLR-based parser. This is the version that Prettier Solidity v1 used by default. This parser is still supported in v2, but it's deprecated and will be removed in the next major version. You can use this parser by setting in your Prettier configuration. | Default | CLI Override | API Override | | ------- | ------------------- | -------------------- | | | | | Experimental Ternaries Mimicking prettier's new ternary formatting for the community to try. Valid options: • - Use curious ternaries, with the question mark after the condition. • - Retain the default behavior of ternaries; keep question marks on the same line as the consequent. | Default | CLI Override | API Override | | ------- | -------------------------- | ------------------------------- | | false | | | Integrations Vim To integrate this plugin with vim, first install . These instructions assume you are using . Add this to your configuration: We modified the instruction to also install this plugin. Then you'll have to configure the plugin to always use the version installed in the vim plugin's directory. The vim-plug directory depends on the value you use in : To check that everything is working, open a Solidity file and run . If you also want to autoformat every time you write the buffer, add these lines: Now Prettier will be run every time the file is saved. VSCode VSCode is not familiar with the Solidity language. There are two extensions that you can install to provide support for Solidity: • • We recommend using the extension: These extensions provide basic integration with Prettier; in most cases, no further action is needed. Make sure your editor has format-on-save set to true. When you save, VSCode will ask you what formatter would you like to use for the Solidity language. We recommend choosing . At this point VSCode's should have a configuration similar to this: If you want more control over other details, you should proceed to install . To interact with third-party plugins, will look in the project's npm modules, so you'll need to have and in your This will allow you to specify the version of the plugin in case you want to use the latest version of the plugin or need to freeze the formatting since new versions of this plugin will implement tweaks on the possible forma…