From 7f41ace190dfdcc38c91eb79c0cdf3df877f2643 Mon Sep 17 00:00:00 2001 From: Leo Arias Date: Sun, 7 Oct 2018 02:17:14 +0000 Subject: [PATCH] rules: underscore prefix for private state variables --- README.md | 3 + index.js | 3 +- ...ivate-state-variables-underscore-prefix.js | 38 ++++++++ ...ivate-state-variables-underscore-prefix.js | 95 +++++++++++++++++++ 4 files changed, 138 insertions(+), 1 deletion(-) create mode 100644 rules/private-state-variables-underscore-prefix.js create mode 100644 test/private-state-variables-underscore-prefix.js diff --git a/README.md b/README.md index aaa7d21..0efbe71 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,9 @@ In the .soliumrc.json file, add: ], "zeppelin/no-unused-imports": [ "warning" + ], + "zeppelin/private-state-variables-underscore-prefix": [ + "warning" ] } diff --git a/index.js b/index.js index 88ce902..3270c76 100644 --- a/index.js +++ b/index.js @@ -17,6 +17,7 @@ module.exports = { "no-arithmetic-operations": require("./rules/no-arithmetic-operations"), "no-state-variable-shadowing": require("./rules/no-state-variable-shadowing"), "no-unchecked-send": require("./rules/no-unchecked-send"), - "no-unused-imports": require("./rules/no-unused-imports") + "no-unused-imports": require("./rules/no-unused-imports"), + "private-state-variables-underscore-prefix": require("./rules/private-state-variables-underscore-prefix") } }; diff --git a/rules/private-state-variables-underscore-prefix.js b/rules/private-state-variables-underscore-prefix.js new file mode 100644 index 0000000..ee01376 --- /dev/null +++ b/rules/private-state-variables-underscore-prefix.js @@ -0,0 +1,38 @@ +/** + * @fileoverview Report private state variables without underscore prefix. + */ + +"use strict"; + +module.exports = { + meta: { + docs: { + recommended: true, + type: "warning", + description: "Report private state variables without underscore prefix" + }, + schema: [] + }, + + create: function(context) { + + function inspectStateVariableDeclaration(emitted) { + if (emitted.exit) { + return; + } + let stateVariable = emitted.node.name; + if ((stateVariable.charAt(0) !== "_") && + (emitted.node.visibility == "private")) { + context.report({ + node: emitted.node, + message: `'${stateVariable}' does not have an underscore as prefix.` + }); + } + + } + + return { + StateVariableDeclaration: inspectStateVariableDeclaration + }; + } +}; diff --git a/test/private-state-variables-underscore-prefix.js b/test/private-state-variables-underscore-prefix.js new file mode 100644 index 0000000..0ee22c3 --- /dev/null +++ b/test/private-state-variables-underscore-prefix.js @@ -0,0 +1,95 @@ +/** + * @fileoverview Tests for private-state-variables-underscore-prefix + */ + +"use strict"; + +const dedent = require("dedent"); +const Solium = require("solium"); +const wrappers = require("./utils/wrappers"); +const addPragma = wrappers.addPragma; + +const userConfig = { + rules: { + "zeppelin/private-state-variables-underscore-prefix": 1 + } +}; + +describe("[RULE] private-state-variables-underscore-prefix: Rejections", function() { + + afterEach(function(done) { + Solium.reset(); + done(); + }); + + it("should reject private state variables without underscore prefix", function(done) { + const code = dedent` + contract TestContract { + bool private variableWithoutUnderscorePrefix = false; + }`, + errors = Solium.lint(addPragma(code), userConfig); + + errors.should.be.instanceof(Array); + errors.length.should.equal(1); + errors[0].message.should.equal( + "'variableWithoutUnderscorePrefix' does not have an underscore as prefix."); + done(); + }); +}); + +describe("[RULE] private-state-variables-underscore-prefix: Acceptances", function() { + + afterEach(function(done) { + Solium.reset(); + done(); + }); + + it("should accept private state variables with underscore prefix", function(done) { + const code = dedent` + contract TestContract { + bool private _variableWithUnderscorePrefix = false; + }`, + errors = Solium.lint(addPragma(code), userConfig); + + errors.should.deepEqual([]); + + done(); + }); + + it("should accept public state variables", function(done) { + const code = dedent` + contract TestContract { + bool public publicStateVariable = false; + }`, + errors = Solium.lint(addPragma(code), userConfig); + + errors.should.deepEqual([]); + + done(); + }); + + it("should accept internal state variables", function(done) { + const code = dedent` + contract TestContract { + bool internal internalStateVariable = false; + }`, + errors = Solium.lint(addPragma(code), userConfig); + + errors.should.deepEqual([]); + + done(); + }); + + it("should accept state variables with default visibility", function(done) { + const code = dedent` + contract TestContract { + bool stateVariableWithDefaultVisibility = false; + }`, + errors = Solium.lint(addPragma(code), userConfig); + + errors.should.deepEqual([]); + + done(); + }); + +});