Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(feat): Add new rule: Named Mapping Parameters #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Create a `.solhint.json` in your root project directory:
"chainlink-solidity/no-hardhat-imports": "warn",
"chainlink-solidity/inherited-constructor-args-not-in-contract-definition": "warn",
"chainlink-solidity/no-block-single-if-reverts": "warn",
"chainlink-solidity/named-mapping-parameters": "warn",
"chainlink-solidity/explicit-returns": "warn"
}
}
Expand Down Expand Up @@ -73,13 +74,14 @@ src/Counter.sol
## Rules

| Rule Id | Description |
|---------------------------------------------------------|---------------------------------------------------------------------------------------|
| ------------------------------------------------------- | ------------------------------------------------------------------------------------- |
| `prefix-internal-functions-with-underscore` | Naming convention |
| `prefix-private-functions-with-underscore` | Naming convention |
| `prefix-storage-variables-with-s-underscore` | Naming convention |
| `prefix-immutable-variables-with-i` | Naming convention |
| `prefix-interfaces-with-i` | Naming convention |
| `all-caps-constant-storage-variables` | Naming convention |
| `named-mapping-parameters` | Naming convention |
| `no-hardhat-imports` | Leftover `hardhat/*.sol` imports not allowed |
| `inherited-constructor-args-not-in-contract-definition` | Inherited contract constructor arguments should be specified in the constructor block |
| `no-block-single-if-reverts` | Omit curly braces for single-line guard clauses |
Expand Down
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const NoHardhatImports = require('./rules/noHardhatImports.js');
const InheritedConstructorArgsNotInContractDefinition = require('./rules/inheritedConstructorArgsNotInContractDefinition.js');
const NoBlockSingleIfReverts = require('./rules/noBlockSingleIfReverts.js');
const ExplicitReturns = require('./rules/explicitReturns.js');
const NamedMappingParameters = require('./rules/namedMappingParameters.js');

module.exports = [
PrefixInternalFunctionsWithUnderscore,
Expand All @@ -21,4 +22,5 @@ module.exports = [
InheritedConstructorArgsNotInContractDefinition,
NoBlockSingleIfReverts,
ExplicitReturns,
NamedMappingParameters
];
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@chainlink/solhint-plugin-chainlink-solidity",
"version": "1.3.0",
"version": "1.4.0",
"main": "index.js"
}
99 changes: 99 additions & 0 deletions rules/namedMappingParameters.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
const meta = {
type: "naming",

docs: {
description: `Mapping types should have named parameters.`,
category: "Style Guide Rules",
notes: [
{
note: "Named parameters in mapping types were added in solidity version 0.8.18",
note: "If using an early version, this rule will be ignored",
},
],
examples: {
good: [
{
description: "Mapping declaration has named parameters",
code: "mapping(address reporter => bool isAllowed) private s_accessControl;",
},
],
bad: [
{
description: "Mapping declaration DOES NOT have named parameters",
code: "mapping(address => bool) private s_accessControl;",
},
],
},
},

isDefault: false,
recommended: true,
defaultSetup: "warn",

schema: null,
};

class NamedMappingParameters {
supportedVersion = "0.8.18";

constructor(reporter, config) {
this.ruleId = "named-mapping-parameters";
this.reporter = reporter;
this.config = config;
this.meta = meta;
this.active = false;
}

PragmaDirective(ctx) {
const { name, value } = ctx;

if (name === "solidity") {
const [
supportedMajorVersion,
supportedMinorVersion,
supportedPatchVersion,
] = this.supportedVersion.split(".");

const [currentMajorVersion, currentMinorVersion, currentPatchVersion] =
value.replace(/[^0-9.]/g, "").split(".");

if (
currentMajorVersion >= supportedMajorVersion &&
currentMinorVersion >= supportedMinorVersion &&
currentPatchVersion >= supportedPatchVersion
) {
this.active = true;
}
}
}

ContractDefinition(ctx) {
if (this.active) {
const { subNodes } = ctx;
for (const subNode of subNodes) {
const { type: subNodeType, variables } = subNode;
if (subNodeType === "StateVariableDeclaration") {
for (const variable of variables) {
const {
type: variableType,
typeName: { type, keyName, valueName },
} = variable;
if (
variableType === "VariableDeclaration" &&
type === "Mapping" &&
(keyName === null || valueName === null)
) {
this.reporter.error(
subNode,
this.ruleId,
`Mappings must be declared with named parameters`
);
}
}
}
}
}
}
}

module.exports = NamedMappingParameters;