Solium Documentation: Release 1.0.0
Solium Documentation: Release 1.0.0
Release 1.0.0
1 User Guide 3
1.1 Quickstart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.3 Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.4 Configuring the Linter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.5 Automatic code formatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.6 Sharable Configs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.7 Plugins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.8 List of Style Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.9 IDE & Editor Integrations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1.10 Migrating to v1.0.0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1.11 Roadmap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2 Developer Guide 15
2.1 Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.2 Installation & Setting up the Development Enviroment . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.3 Writing a Core Rule . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
2.4 Developing a Sharable Config . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
2.5 Developing a Plugin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
2.6 Building this documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
3 Experimental Features 29
3.1 List of Experimental features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
4 Known Issues 31
5 Contributing 33
6 About 35
6.1 Community . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
i
ii
Solium Documentation, Release 1.0.0
Solium analyzes your Solidity code for style & security issues and fixes them.
Standardize Smart Contract practices across your organisation. Integrate with your build system. Deploy with confi-
dence!
Solium does not strictly follow Solidity Style Guide. The practices it enforces by default are best practices for the
community at large.
Keyword Index, Search Page
Contents 1
Solium Documentation, Release 1.0.0
2 Contents
CHAPTER 1
User Guide
1.1 Quickstart
• npm install -g solium
• cd myDapp
• solium --init
• solium -d contracts/ or solium -d . or solium -f myContract.sol
Fix stuff
• solium -f myContract.sol --fix
• git diff myContract.sol
1.2 Installation
Since this documentation is for Solium v1, we’re going to neglect v0. Furthermore, v0 is now deprecated and we
highly recommend you to move to v1.
Use npm install -g solium.
Verify that all is working fine using solium -V.
In any of the supported Linux distros, sudo snap install solium --edge
Note: If you’re using vim with syntastic, and prefer to use a locally installed version of Solium (rather than a global
version), you can install syntastic local solium to automatically load the local version in packages that have installed
3
Solium Documentation, Release 1.0.0
their own.
1.3 Usage
cd to your DApp directory and run solium --init. This will produce .soliumrc.json and .
soliumignore files in your root directory. Both are to be commited to version control.
Now run solium --dir . to lint all .sol files in your directory and sub-directories.
If you want to run the linter over a specific file, use solium --file myContract.sol.
You can also run solium so it watches your directory for changes and automatically re-lints the contracts: solium
--watch --dir contracts/.
By default, solium looks for the .soliumrc.json configuration file in your current directory. You can override this
setting by using the --config/-c option like solium -d contracts/ -c ../configs/.soliumrc.
json.
Solium supports multiple output formats:
• Pretty (Default): --reporter pretty
• GCC: --reporter gcc
Use solium --help for more information on usage.
Note: If all your contracts reside inside a directory like contracts/, you can instead run solium --dir
contracts.
After linting over your code, Solium produces either warnings, errors or both. The app exits with a non-zero code
ONLY if 1 or more errors were found. So if all you got was warnings, solium exits with code 0.
Whether an issue should be flagged as an error or warning by its rule is configurable through .soliumrc.json.
Think of Solium as a 2-sided engine. 1 side accepts the Solidity smart contracts along with a configuration and the
other a set of rule implementations.
Solium’s job is to execute the rules on the contracts based on the configuration!
You can configure solium in several ways. You can choose which all rules to apply on your code, what should their
severity be (either error or warning) and you can pass them options to modify their behavior. Rule implementations
will always contain default behavior, so its fine if you don’t pass any options to a rule.
Solium contains some core rules and allows for third party developers to write plugins.
The .soliumrc.json created in the initialisation phase contains some default configurations for you to get started.
{
"extends": "BASE RULESET",
"plugins": ["security"],
"rules": {
"RULE NAME": ["SEVERITY", "PARAMETERS"],
"RULE NAME": "ONLY SEVERITY"
}
}
Note: soliumrc must contain at least one of extends, plugins and rules properties.
Note: Severity can be expressed either as a string or integer. error = 2, warning = 1. off = 0, which means the
rule is turned off.
Comment Directives can be used to configure Solium to ignore specific pieces of code. They follow the pattern
solium-disable<optional suffix>.
If you only use the directive, Solium disables all rules for the marked code. If that’s not desirable, specify the rules to
disable after the directive, separated by comma.
• Disable linting on a specific line
contract Foo {
/* solium-disable-next-line */
function() {
var bar = 'Hello world'; // solium-disable-line quotes
/* solium-disable */
contract Foo {
...
}
For the times when you’re feeling lazy, just run solium -d contracts/ --fix to fix your lint issues. This
doesn’t fix all your problems (nothing fixes all your problems) but all lint issues that CAN be fixed WILL be fixed, if
the rule implementation that flags the issue also contains a fix for it.
Warning: Solium fixes your code in-place, so your original file is over-written. It is therefore recommended that
you use this feature after ensuring that your original files are easily recoverable (recovering can be as simple as
git checkout). You have been warned.
Note: It is not guaranteed that all the fixes will be applied to your contract code. Below is a brief explanation of why
it is so. Skip to the next section if you don’t wish to know the details, they’re not necessary as long as you accept the
idea.
Note: In case of the A, B, C example, its easy to conclude that if you run the linter with autofixing twice, you will
have applied all 3 fixes. The first run applies A and B, whilst the second run will apply C as well, because this time
there is no A to conflict with.
Note: Fixes for all possible errors have not been implemented yet. Whichever rules have the fix mechanism (for eg-
the quotes rule does) will apply it. More fixes will be added in future, you can see the list of rules below to know
which rules are currently able to apply fixes. So if you see a warning/error despite using --fix, its because that issue
wasn’t resolved by the autofix mechanism (either because the fix doesn’t exist at the moment or due to a conflict).
The list of rules in Solium will keep growing over time. After a point, its just overkill to spend time specifying rules,
their severities and options in your soliumrc every time you create a new Solidity Project. At that time, you can
either choose to inherit solium:recommended or solium:all configuration or borrow configurations written
by others.
A Sharable Config allows you to borrow someone else’s soliumrc configuration. The idea is to simply pick a style to
follow and focus on your business problem instead of making your own style specification.
Even if there are 1 or 2 rules that you disagree with in someone else’s sharable config, you can always inherit it and
override those rules in your soliumrc!
Sharable Configs are installed via NPM. All solium SCs will have a prefix solium-config-. Distributors of
sharable configs are encouraged to add solium and soliumconfig as tags in their NPM modules to make them
more discoverable.
Suppose Consensys releases their own sharable config called solium-config-consensys. Here’s how you’d go
about using it, assuming you already have solium globally installed:
• Run npm install -g solium-config-consensys
• Now, in your .soliumrc.json, set the value of extends key to consensys and remove the rules key
altogether. Your config file should now look something like:
{
"extends": "consensys"
}
Note: The above assumes that you completely follow consensys’s style spec. If, say, you don’t agree with how
they’ve configured a rule race-conditions. You can override this rule and add your own spec inside the rules
key. This way, you follow all rules as specified in consensys’ sharable config except race-condition, which you
specify yourself.
{
"extends": "consensys",
"rules": {
"race-condition": ["error", {"reentrancy": true, "cross-function":
˓→false}, 100, "foobar"]
}
}
That’s it! Now you can run solium -d contracts/ to see the difference.
Note that you didn’t have to specify the prefix of the sharable config. Whether you’re specifying a config or a
plugin name, you should omit their prefixes (solium-config- for configs & solium-plugin- for plugins).
So if you have installed a config solium-config-foo-bar, you should have "extends": "foo-bar" in
your .soliumrc.json. Solium will resolve the actual npm module name for you.
Note: Internally, Solium simply require() s the config module. So as long as require() is able to find a module
named solium-config-consensys, it doesn’t matter whether you install your config globally or locally and
link it.
Note: 1 limitation here is that Sharable configs can currently not import Plugins. This means SCs can only configure
the core rules provided by Solium. Plugin importing is a work in progress, please be patient!
1.7 Plugins
Plugins allow Third party developers to write their own rules and re-distribute them via NPM. Every solium plu-
gin module has the prefix solium-plugin-. Plugin developers are encouraged to include the tags solium and
soliumplugin in their modules for easy discoverability.
Once you install a plugin, you can specify it inside plugins array and configure its rules inside rules exactly like
how you configure solium’s core rules. Plugin rules too can contain fixes if the developer supplies them. There’s no
1.7. Plugins 7
Solium Documentation, Release 1.0.0
special way of applying these fixes. Simply lint with the --fix option and fixes for both core rules and pugin rules
will be applied to your code.
Coming back to our previous example - Consensys’ solium-plugin-consensys:
• Install the plugin using npm install -g solium-plugin-consensys
• Add the plugin’s entry into your .soliumrc.json:
{
"extends": "solium:recommended",
"plugins": ["consensys"]
}
Note: Just like in sharable configs, don’t specify the plugin prefix. Simply specify the plugin name. So if a plugin ex-
ists on NPM by the name of solium-plugin-foo-bar, you need only specify "plugins": ["foo-bar"].
• In the rules object, you can configure the plugin’s rules by adding an entry "<PLUGIN NAME>/
<RULE NAME>": "<SEVERITY>" or "<PLUGIN NAME>/<RULE NAME>": ["<SEVERITY>",
"<OPTIONS>"].
{
"extends": "solium:recommended",
"plugins": ["consensys"],
"rules": {
"consensys/race-conditions": "error",
"consensys/foobar": [1, true, "Hello world"]
}
}
• The above configuration means you’ve applied all the rules supplied by the plugin and modified the behaviour
of 2 of them. Try running the linter using solium -d contracts/.
If you simply specify a plugin and do not configure any of its rules, all the rules provided by the plugin are applied on
your code with their default severities and no additional options. If you wish to change the behaviour of any of the
rules of a plugin, you have to configure them inside “rules”.
You should check the plugin’s documentation provided by the plugin developer to know the list of rules provided and
the options they accept.
Note: Just like in sharable configs, solium internally require() s the plugin module. So as long as require() is able
to find a module named solium-plugin-consensys, it doesn’t matter whether you install your plugin globally
or locally and link it.
Starting v1.0.1, Solium comes pre-installed with its official security plugin (view on NPM) containing lint rules
for best security practices. These rules have been taken from Consensys recommended practices and Solium’s Rule
Wishlist thread.
You can get information about all the rules this plugin supplies on its README.
When you run solium --init, the .soliumrc.json created for you contains the entry "plugins":
["security"]. This means all security rules will by default be applied during linting.
We recommend that you keep the security plugin applied without modifying behaviour of any of its rules. But
if you still wish to configure them or remove the plugin altogether, you can.
Note: See security plugin if you’re looking for documentation on Solium’s security rules.
Below is the list of style rules supplied by Solium. By default, solium:recommended is extended by your soli-
umrc, which enables all lint rules recommended for general audience (See solium-recommended). You can choose to
further configure their severities inside your soliumrc itself. If you choose solium:all instead, all core rules are
enabled except for the deprecated ones. Enabling a deprecated rule will display a warning message on Solium CLI.
These rules may or may not contain fixes. Their fixes will be applied on the code if you use the --fix flag in your
lint command. Some rules even take options that can modify their behavior.
For eg- your choice of indentation might be Tab or 4 spaces or 2 spaces. What indentation is enforced is configurable.
If you’re currently using Solium v0 and wish to migrate to v1, then this section is for you.
Note: If you simply upgrade to Solium v1 right now and lint your project with v0’s configuration files, it will work
fine (but will give you a deprecation warning) since v1 has been built in a backward-compatible manner. The only
2 exception to this are the discontinuation of custom-rules-filename attribute and --sync option - these
features provided negligible benefit.
{
"custom-rules-filename": null,
"rules": {
"imports-on-top": false,
"variable-declarations": false,
"array-declarations": true,
"operator-whitespace": true,
"lbrace": true,
"mixedcase": true,
"camelcase": true,
"uppercase": true,
"no-empty-blocks": true,
"no-unused-vars": true,
"quotes": true,
"indentation": true,
"whitespace": true,
"deprecated-suicide": true,
"pragma-on-top": true
}
}
You:
• Only had to specify those rules separately whose behaviour you need to change. Set a rule to 0 or off to turn
it off. Other values can be 1/warning or 2/error.
• Set up the indentation rule to enforce 4 spaces (replace 4 with any other integer or tab).
• Instructed Solium to enforce double quotes for strings (change that to single if you so desire).
• Instructed Solium to import all other non-deprecated rules and enable them by default.
Note: Alternatively, you can back up your current .soliumrc.json and .soliumignore (if you made changes
to it), then run solium init (after installing v1). You can then make changes to the new .soliumrc.json.
v0 allows you to inject custom rule implementations using the custom-rules-filename attribute in your .
soliumrc.json. This feature is now deprecated. If you specify a file, the linter would simply throw a warning
informing you that the custom rules supplied will not be applied while linting.
Custom rule injection has now been replaced by Solium Plugins.
"consensys/race-conditions": "error",
"consensys/foobar": [1, true, "Hello world"],
"foobar/baz": 1
}
}
To learn about the new format, please see Configuring the Linter.
Note that v1 still accepts the old soliumrc format but throws a format deprecation warning.
Note: Unless you’re developing rules (whether core or plugins) for Solium, you can skip this part.
module.exports = {
meta: {
docs: {
recommended: true,
type: 'warning',
description: 'This is a rule'
},
schema: [],
fixable: 'code'
},
create(context) {
function lintIfStatement(emitted) {
context.report({
node: emitted.node,
fix(fixer) {
// magic
}
});
}
return {
IfStatement: lintIfStatement
};
}
};
There have been additions in the Solium API. However, there are no breaking changes.
• When using the lint(sourceCode, config) method (where config is your soliumrc configuration),
you can now pass an options object inside config to modify Linter behavior. You can specify the
returnInternalIssues option whose value is Boolean. If true, solium returns internal issues (like
deprecation warnings) in the error list. If false, the method behaves exactly like in v0, and doesn’t spit out
any warnings (even if, for eg, you’re using deprecated rules).
• The API now exposes another method lintAndFix(). Guess what it does? Please refer to the developer
guide on how to use this method to retrieve lint errors as well as the fixed solidity code along with a list of fixes
applied.
v0’s CLI allowed the --sync flag so a user could sync their .soliumrc.json with the newly added rules after
updating solium. sync was not a great design choice and so we’ve removed it. v1 is designed in a way such that core
developers can keep adding more rules to solium and a user doesn’t need to do anything apart from installing an update
in order to use that rule. It gets applied automatically.
1.11 Roadmap
Developer Guide
2.1 Architecture
Solium is organized as a module that exposes an API for any javascript application to use. The user would supply a
source code string along with a configuration object that determines what exactly solium does with the code.
Solium refers to an engine (a middleman) that accepts user input (source code & configuration) from one side and
rule implementations from another. A rule implementation can refer to any piece of code that operates on the given
solidity code’s Abstract Syntax Tree, points out flaws and suggests fixes.
In a sense, Solium is a generic engine that operates on any given solidity code. The linter itself is a special use case
of this engine where the “analyzer” refers to a set of rule implementations that tell whether something in the code
looks right or wrong. Because you can write plugins and get access to the complete solidity code, its AST and a
solium-exposed set of utility functions to operate on the AST, you can build anything on top of solium that you can
imagine!
Solium has a set of core rules for the purpose of linting code.
The frontend of the app is a CLI that a user uses to interact with the Solium module to get things done. The module
exposes 2 main functions for usage: lint() and lintAndFix().
Architecture will be explained in more detail in future.
Make sure you have Node.js and NPM installed on your system. Install Solium v1 as a local module using npm
install --save solium.
You can now use Solium like:
15
Solium Documentation, Release 1.0.0
"extends": "solium:recommended",
"plugins": ["security"],
"rules": {
"quotes": ["error", "double"],
"double-quotes": [2], // returns a rule deprecation warning
"pragma-on-top": 1
},
errors.forEach(console.log);
internal: true,
line: -1,
column: -1
},
{
ruleName: 'quotes',
type: 'error',
node: { type: 'Literal', value: 'hello', start: 79, end: 86 },
message: '\'hello\': String Literals must be quoted with double
˓→quotes only.',
line: 7,
column: 15,
fix: { range: [Array], text: '"hello"' }
}
]
• You can use the lintAndFix() function as demonstrated in the following example:
const Solium = require('solium'),
sourceCode = 'contract fOO_bar { string hola = \'hello\'; }';
});
console.log(result);
{
originalSourceCode: 'pragma solidity ^0.4.0;\n\n\nimport "./hello.sol";
˓→ \n\ncontract Foo {\n\tstring hola = \'hello\';\n}\n',
fixesApplied:[
{
ruleName: 'quotes',
type: 'error',
node: [Object],
message: '\'hello\': String Literals must be quoted with
˓→double quotes only.',
line: 7,
column: 15,
fix: [Object]
}
],
fixedSourceCode: 'pragma solidity ^0.4.0;\n\n\nimport "./hello.sol";
˓→\n\ncontract Foo {\n\tstring hola = "hello";\n}\n',
errorMessages: [
{
type: 'warning',
message: '[DEPRECATED] Rule "double-quotes" is deprecated.
˓→Please use "quotes" instead.',
internal: true,
line: -1,
column: -1 },
{ ruleName: 'double-quotes',
type: 'warning',
node: [Object],
message: '\'hello\': String Literals must be quoted with
˓→"double quotes" only.',
line: 7,
column: 15
}
]
}
Note: The input supplied to lint() and lintAndFix() is the same. Its the output format that differs.
To write a core rule for Solium, please start by raising an issue on github describing your proposal. You can check out
some of the rules in the roadmap in our Rules Wishlist.
Note: You are allowed (even encouraged) to write any code you wish to contribute in ES6.
Say you want to develop a new rule foo-bar. Here’s how you’d go about it:
Create a file foo-bar.js inside lib/rules. This is the main implementation of your rule. Use the below template to
implement your core rule:
module.exports = {
meta: {
docs: {
recommended: true,
type: 'warning', // 'warning' | 'error' | 'off'
description: 'This is my foobar rule'
},
schema: [],
fixable: 'code'
},
if (emitted.exit) { return; }
context.report({
node: node
fix: function(fixer) {
// magic
},
message: 'Oh snap! A lint error:('
});
}
return {
IfStatement: lintIfStatement
};
}
};
Your rule should expose an object that contains 2 attributes - meta object which describes the rule and create()
function that actually lints over the given solidity code.
meta
• Contains docs object used to describe the rule.
• The schema object is used to describe the schema of options the user can pass to this rule via soliumrc config
(see AJV). This ensure that a valid set of options are passed to your rule. You can see the schema of quotes rule
to understand how to write the schema for your rule.
• The fixable attribute can have value as either code or whitespace. Set this attribute if your rule also
contains fixes for the issues you report. Use whitespace if your rule only add/removes whitespace from the
code. Else use code.
• When a rule needs to be deprecated, we can add deprecated: true inside meta. We can add
replacedBy: ["RULE NAME"] inside meta.docs if this rule is to be replaced by a new rule (see dep-
recated example).
Note: replacedBy doesn’t force the linter to apply the new rule. Instead, it only throws a warning to the user,
notifying them that they’re using a deprecated rule and should consider moving to the new rule(s) specified inside
replacedBy array. Try adding double-quotes: "error" inside rules inside your .soliumrc.json
and running the linter.
create()
This function is responsible for actual processing of the contract code, determining whether something is wrong or
not, reporting an issue and suggesting fixes. create() must return an object whose Key is an AST node type, and value
is the function to execute on that node. So, for example, IfStatement is the type of the AST node representing an
if clause and block in solidity.
Note: To know which node type you need to capture, install solparse, parse some sample code into AST, then examine
the particular node of interest for its type field. Specify that type as your return object key. You can see any rule
implementation to understand what create()’s return object looks like.
The create() function receives a context object, which allows you to access the solidity code to be linted and many
other things to help your rule work its magic.
• context.options - undefined if user doesn’t supply any options to your rule through soliumrc. An
Array of options otherwise. Solium ensures that the options passed inside the array are fully compliant with the
schema you define for each of them in meta. So if a user specifies foo-bar: ['error', 'hello',
110, {a: [99]}], then foo-bar rule’s context.options contains the array ['hello', 110,
{a: [99]}] (all but the first item, because the first is the severity of the rule). See options example.
• context.getSourceCode() - returns a SourceCode object that gives you access to the solidity code and
several functions to operate on it and AST nodes.
The functions exposed by SourceCode object are as follows:
1. getText (node) - get source code for the specified node. If no arguments given, it returns the complete
source code
2. getTextOnLine (lineNumber) - get the complete text on the specified line number (lineNumber is an
Integer)
3. getLine (node) - get the line number on which the specified node’s code starts
4. getEndingLine (node) - get the line number on which the specified node’s code ends
5. getColumn (node) - get column no. of the first character of the specified node’s code
6. getEndingColumn (node) - get column no. of the last character of the specified node’s code
7. getParent (node) - get the parent node of the specified node
8. getNextChar (node) - get 1 character after the code of specified node
9. getPrevChar (node) - get 1 character before the code of specified node
10. getNextChars (node, charCount) - get charCount no. of characters after the code of specified node
11. getPrevChars (node, charCount) - get charCount no. of characters befre the code of specified node
12. isASTNode (arg) - Returns true if the given argument is a valid (Spider-Monkey compliant) AST Node
13. getStringBetweenNodes (prevNode, nextNode) - get the complete code between 2 specified
nodes. (The code ranges from prevNode.end (inclusive) to nextNode.start (exclusive) )
• context.report() - Lastly, the context object provides you with a clean interface to report lint issues:
context.report({
node, // the AST node retrieved through emitted.node (see below)
fix(fixer) { // [OPTIONAL]
if (wantToApplyFix) {
return [fixer.replaceText(node, "hello world!!")];
}
return null;
},
message: 'Lint issue raised yayy!',
location: { // [OPTIONAL]
line: 9, // [OPTIONAL]
column: 20 // [OPTIONAL]
}
});
See report with fix example and report with location example.
Note: If you’re supplying the fix() function, make sure you specify the fixable attribute in meta.
Your fix() function will receive a fixer object that exposes several functions so you can tell Solium how to fix
the raised lint issue. Every fixer function you call returns a fixer packet. Solium understands how to work with this
packet. Your fix function must return either a single fixer packet, an array of fixer packets or null.
Note: Returning a null results in the particlar fix function being ignored. This is convenient when, under certain
conditions, you don’t want to apply any fixes. This means that fix(fixer) { return null; } is equivalent
to not supplying a fix() function in the error object at all. See the context.report() example above.
Warning: Multiple fixer packets inside the array must not overlap, else Solium throws an error. For eg- the
first packet tries to remove the first 10 characters from the solidity code, whereas another packet tries to replace
them by, say, “hello world”. This results in an overlap and hence the complete fix is not valid. However, if the
replacement begins at the 11th character, then there is no conflict and so your fix is valid!
Note: A common use case for exit is when you want your rule to access the whole contract’s AST Node (type
Program) at the end, ie, when all other rules are done reporting their rules. Then you could specify if(!emitted.
exit) { return; }.
• emitted.node - is the AST Node object of type specified as the key in your return object. So if, for eg, your
create() returns { ForStatement: inspectForLoop }, then you can access the AST Node represent-
ing the for loop in solidity like:
create(context) {
function inspectForLoop(emitted) {
const {node} = emitted;
console.log (node.type); // prints "ForStatement" and the node
˓→has appropriate properties of 'for' statement
• Inside the test/lib/rules, creating a new directory foo-bar and a file inside this directory foo-bar.
js (see test examples).
• Now paste the below template in test/lib/rules/foo-bar/foo-bar.js:
/**
* @fileoverview Description of the rule
* @author YOUR NAME <your@email>
*/
'use strict';
// Solium should only lint using your rule so only issues flagged by your rule are
˓→reported
// so you can easily test it. Replace foo-bar with your rule name.
var config = {
"rules": {
"foo-bar": "error" // alternatively - ["error" OR "warning",
˓→options acc. to meta.schema of rule]
}
};
Solium.reset();
done();
});
});
Solium.reset();
done();
});
});
Note: ESLint allows us to disable linting on specific pieces of code. This should only be used after a brief discussion
about why it’s suitable.
Note: Running npm test also prints coverage stats at the bottom of the CLI output. It creates the coverage
directory whose index.html can be opened in any browser to view the same. Write enough tests to keep the
coverage for the rule above 90%.
The purpose of a sharable config is for an organisation to just pick up a solidity style spec to work with and focus on
the coding part instead of getting into a tabs vs. spaces debate. You install the SC and specify its name without prefix
as value of the extends key in your soliumrc config. Something like:
{
"extends": "foobar"
}
Note: For reasons discussed on our blog, we have reserved a few NPM solium config module names. If you find
your organisation’s name in the list in the blog, please follow the instructions at the bottom of the blog to claim your
module.
module.exports = {
rules: {
quotes: ["error", "double"],
indentation: ["warning", 4],
"pragma-on-top": 1,
...
}
};
{
...
"peerDependencies": {
"solium": "^1.0.0"
}
}
Read about Peer Dependencies on NPM. You’re now ready to test your config.
Solium internally simply require()``s the config you extends from in your soliumrc.
So as long as require() can resolve the name ``solium-config-foobar, it doesn’t care
Note: It is a good practice to specify all the rules in your sharable config. This ensures that you decided how each
rule is to be treated and that you didn’t forget about any of them. If you wish to turn a rule off, simply specify its value
as off or 0. See list of all rules on User Guide. See example configuration solium all ruleset.
Note: It is good practice to turn off all the deprecated rules. See the Rule List in User Guide to know which rules are
now deprecated.
Plugins allow third party developers to write rule implementations that work with solium and re-distribute them for
use. Plugins too are distributed via NPM, have the prefix solium-plugin- and should, as a best practice, have the
tags solium, solidity and soliumplugin.
As an example, you can check out Solium’s official Security Plugin.
Note: For reasons discussed on our blog, we have reserved a few NPM solium plugin module names. If you find
your organisation’s name in the list in the blog, please follow the instructions at the bottom of the blog to claim your
module.
Start by creating a directory to contain your plugin (lets call the plugin baz)
• mkdir solium-plugin-baz
• cd solium-plugin-baz
• npm init Fill in the appropriate details and don’t forget to add the tags mentioned above
• Specify the peerDependencies attribute in your package.json like:
{
...
"peerDependencies": {
"solium": "^1.0.0"
}
}
module.exports = {
meta: {
description: 'Plugin description'
},
rules: {
foo: {
meta: {
docs: {
recommended: true,
type: 'warning',
description: 'Rule description'
},
schema: []
},
create: function (context) {
function inspectProgram (emitted) {
if (emitted.exit) { return; }
context.report ({
node: emitted.node,
message: 'The rule baz/foo reported
˓→an error successfully.'
});
}
return {
Program: inspectProgram
};
}
}
}
};
Note: In the above example, you can set the type property to off. The effect of this is that the rule exists in your
plugin but is disabled by default. This feature can be used when you require that a user only purposely enable the
rule (probaby because it may not be desirable for general audience).
Notice that every rule you define inside the rules object has the exact same schema as the core rule described
above. So if you know how to implement a core rule, you need not learn anything new to implement a plugin rule.
"scripts": {
"test": "mocha --require should --reporter spec --recursive"
},
• Run npm link to make this plugin globally available. (You can confirm that it worked by going to any random
directory in your system, firing up Nodejs REPL and run require('solium-plugin-baz')).
• Write your tests inside the test/ directory following the below pattern:
errors.should.be.Array ();
errors.should.have.size (2);
// Add further tests to examine the error objects
// Once your tests have finished, call below functions to safely exit
Solium.reset ();
done ();
});
});
Notice that the schema of plugin rule tests is the same as that of core rule tests.
• Now run the tests using npm test and resolve any failures that occur.
• As another (optional) test, you can also go to your DApp directory and add your plugin’s entry in .soliumrc.
json to see if its working properly:
{
"plugins": ["baz"],
"rules": {
"baz/foo": "error"
}
}
Note: This documentation builds successfully with Sphinx v1.5 but fails with v1.6. Although we haven’t yet fully
investigated whether its a problem with our docs or Sphinx, we recommend you to install v1.5 in order to see the
changes you’ve made.
• Make the changes to the docs as you see fit, then run make html while still inside docs/. If there were no
RST errors, the docs should build successfully.
• Open up docs/_build/html/index.html in your favourite browser to see the changed.
• Once you’re satisfied, you can commit the changes you made in the RST docs and send a PR.
Experimental Features
At any given time, Solium might have a few experimental features in production. They’re experimental in order to
determine whether and to what extent they’re beneficial. And we could really use your feedback on experimental
features!
These features haven’t been mentioned anywhere in the developer and user guides to ensure that you’re aware when
you’re using an experimental feature in your workflow.
Only this section contains the list of the features. They are subject to change or even removal in subsequent releases.
You can either use them temporarily or freeze the Solium version in your app to ensure thay always work for you (not
recommended).
As a rule of thumb, never use the features listed in this section in your production apps.
3.1.1 v1.0.8
• Intuitive Util methods to aid rule devs determine node types. PR: 149 Trial ends on: 25th Dec ‘17
See experimental features issue for discussions on them.
29
Solium Documentation, Release 1.0.0
Known Issues
While Solium is being actively maintained, a few major issues are still lurking around and we thought it best to make
you aware of them so you don’t spend time discovering them instead. We’re working on resolving these issues so
please be patient.
• On large code bases, the linter is slightly slow. Solium heavily relies on a Solidity parsing engine to operate on
your contract code. This parser is currently a bottleneck (see speed issue). Good news is that we’ve devised the
strategy to tackle this and are actively working on it as you read this!
• Solium is currently file-aware instead of being project-aware. What this means is that while linting, Solium
doesn’t have the context of all the contracts and how they may be using the contract currently being linted. A
consequence of this is that the linter currently flags a state variable as unused if it doesn’t find its usage in the
same contract, whereas its clearly possible that you’re import ing the contract elsewhere to use that variable
(See issue). This is a fairly critical problem and will be resolved in a future release. We believe a codebase-aware
linter would be much more powerful because of its broader context.
If you discover any other pain points while using Solium, we encourage you to open up an issue.
Or if you don’t feel like going through the formality of detailing the error(s), tap us with your problems on our Gitter
Channel.
31
Solium Documentation, Release 1.0.0
Contributing
We’re constantly looking out for awesome people to join our community and help make Solium a world-class static
analyser that keeps production code in check. There are various opportunities for you to contribute to Solium, regard-
less of whether you’re new to the project or deeply familiar with it.
A few areas where we could use some help are:
• All the issues on our repository tagged with Contributors needed and Help wanted. These include
adding new rules, moving the codebase to ES6, fixing some of the existing rules (like whitespace or indentation).
• If you’re, by now, pretty familiar with Solium’s codebase, you could also fix the un-tagged issues.
• Feedback & Suggestions - always welcome. The author of solium sucks at User experience and would love
to hear about what pain points still exist in your smart contract development workflow (regardless of whether
they’re directly related to solium or not).
• This documentation! Yep, we could certainly use more eyeballs that can correct typos, paraphrase instructions,
introduce diagrams or simply make the docs much cleaner.
33
Solium Documentation, Release 1.0.0
34 Chapter 5. Contributing
CHAPTER 6
About
6.1 Community
35
Solium Documentation, Release 1.0.0
• Christopher Gewecke
• Ulrich Petri
• Leo Arias
• Alex Chapman
• Chih Cheng Liang
• Jooraj Bednar
• Juan Blanco
• Florian Sey
• Alex Step
• Travis Jacobs
• Remco Bloemen
• Brett Sun
• Franco Victorio
• Gabriel Alacchi
36 Chapter 6. About
Index
A U
architecture, 15 usage, 4
automatic code formatting, 5
W
B writing core rule, 17
building documentation, 26
C
community, 35
configuring the linter, 4
configuring with comments, 5
D
developing sharable config, 22
developing solium plugin, 24
I
IDE and Editor integrations, 11
installation, 3
installing and setting up the development environment, 15
L
list of core rules, 9
list of experimental features, 29
M
migration guide, 11
P
plugins, 7
Q
quickstart, 3
R
roadmap, 14
S
sharable configs, 6
37