From d8ff9c21b1cf026dfe73873d9433fbb892d77221 Mon Sep 17 00:00:00 2001 From: Claudio Viquez Date: Fri, 24 Aug 2018 07:46:17 -0600 Subject: [PATCH 1/2] Added rule for "_" suffix for internal variables --- README.md | 3 ++ index.js | 3 +- rules/internal-state-underscore-suffix.js | 38 +++++++++++++ test/internal-state-underscore-suffix.js | 65 +++++++++++++++++++++++ 4 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 rules/internal-state-underscore-suffix.js create mode 100644 test/internal-state-underscore-suffix.js diff --git a/README.md b/README.md index 301ccb6..4846724 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,9 @@ In the .soliumrc.json file, add: ], "zeppelin/no-unused-imports": [ "warning" + ], + "zeppelin/internal-state-underscore-suffix": [ + "warning" ] } diff --git a/index.js b/index.js index 88ce902..2216d11 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"), + "internal-state-underscore-suffix": require("./rules/internal-state-underscore-suffix") } }; diff --git a/rules/internal-state-underscore-suffix.js b/rules/internal-state-underscore-suffix.js new file mode 100644 index 0000000..039fdc4 --- /dev/null +++ b/rules/internal-state-underscore-suffix.js @@ -0,0 +1,38 @@ +/** + * @fileoverview Report internal state variables without underscore suffix. + */ + +"use strict"; + +module.exports = { + meta: { + docs: { + recommended: true, + type: "error", + description: "Report internal state variables without underscore suffix" + }, + schema: [] + }, + + create: function(context) { + + function inspectStateVariableDeclaration(emitted) { + if (emitted.exit) { + return; + } + let param = emitted.node.name; + if ( (param.charAt(param.length - 1) !== "_") && + (param.visibility != "public")) { + context.report({ + node: emitted.node, + message: `'${param}' does not have an underscore as suffix.` + }); + } + + } + + return { + StateVariableDeclaration: inspectStateVariableDeclaration + }; + } +}; diff --git a/test/internal-state-underscore-suffix.js b/test/internal-state-underscore-suffix.js new file mode 100644 index 0000000..2c8b6e6 --- /dev/null +++ b/test/internal-state-underscore-suffix.js @@ -0,0 +1,65 @@ +/** + * @fileoverview Tests for internal-state-underscore-suffix rule + */ + +"use strict"; + +const dedent = require("dedent"); +const Solium = require("solium"); +const wrappers = require("./utils/wrappers"); +const addPragma = wrappers.addPragma; + +const userConfig = { + rules: { + "zeppelin/internal-state-underscore-suffix": 1 + } +}; + +describe("[RULE] internal-state-underscore-suffix: Rejections", function() { + + afterEach(function(done) { + Solium.reset(); + done(); + }); + + it("should reject internal state variables without underscore suffix", function(done) { + const code = dedent` + contract TestContract { + + bool internal_no_underscore_suffix = false; + + function testFunction(uint parameter) {} + }`, + errors = Solium.lint(addPragma(code), userConfig); + + errors.should.be.instanceof(Array); + errors.length.should.equal(1); + errors[0].message.should.equal( + "'internal_no_underscore_suffix' does not have an underscore as suffix."); + done(); + }); +}); + +describe("[RULE] internal-state-underscore-suffix: Acceptances", function() { + + afterEach(function(done) { + Solium.reset(); + done(); + }); + + it("should accept internal state variables with underscore suffix", function(done) { + const code = dedent` + contract TestContract { + + bool public internal_underscore_suffix_ = false; + + function testFunction(uint parameter) {} + }`, + errors = Solium.lint(addPragma(code), userConfig); + + errors.should.deepEqual([]); + + done(); + }); + +}); From d312340d969adae591864ae07c5b146f523bc8be Mon Sep 17 00:00:00 2001 From: Claudio Viquez Date: Sat, 1 Sep 2018 08:37:36 -0600 Subject: [PATCH 2/2] Tests for explicit private & internal declarations --- rules/internal-state-underscore-suffix.js | 2 +- test/internal-state-underscore-suffix.js | 120 +++++++++++++++++++++- 2 files changed, 116 insertions(+), 6 deletions(-) diff --git a/rules/internal-state-underscore-suffix.js b/rules/internal-state-underscore-suffix.js index 039fdc4..ee968b4 100644 --- a/rules/internal-state-underscore-suffix.js +++ b/rules/internal-state-underscore-suffix.js @@ -22,7 +22,7 @@ module.exports = { } let param = emitted.node.name; if ( (param.charAt(param.length - 1) !== "_") && - (param.visibility != "public")) { + (emitted.node.visibility != "public")) { context.report({ node: emitted.node, message: `'${param}' does not have an underscore as suffix.` diff --git a/test/internal-state-underscore-suffix.js b/test/internal-state-underscore-suffix.js index 2c8b6e6..b21ba37 100644 --- a/test/internal-state-underscore-suffix.js +++ b/test/internal-state-underscore-suffix.js @@ -15,6 +15,112 @@ const userConfig = { } }; +describe("[RULE] internal-state-underscore-suffix-implicit: Rejections", function() { + + afterEach(function(done) { + Solium.reset(); + done(); + }); + + it("should reject implicit internal state variables without underscore suffix", function(done) { + const code = dedent` + contract TestContract { + + bool implicit_no_underscore_suffix = false; + + function testFunction(uint parameter) { + bool foo = true; + } + }`, + errors = Solium.lint(addPragma(code), userConfig); + + errors.should.be.instanceof(Array); + errors.length.should.equal(1); + errors[0].message.should.equal( + "'implicit_no_underscore_suffix' does not have an underscore as suffix."); + done(); + }); +}); + +describe("[RULE] internal-state-underscore-suffix-implicit: Acceptances", function() { + + afterEach(function(done) { + Solium.reset(); + done(); + }); + + it("should accept implicit internal state variables with underscore suffix", function(done) { + const code = dedent` + contract TestContract { + + bool internal_underscore_suffix_ = false; + + function testFunction(uint parameter) { + bool foo = true; + } + }`, + errors = Solium.lint(addPragma(code), userConfig); + + errors.should.deepEqual([]); + + done(); + }); + +}); + +describe("[RULE] internal-state-underscore-suffix-private: Rejections", function() { + + afterEach(function(done) { + Solium.reset(); + done(); + }); + + it("should reject private state variables without underscore suffix", function(done) { + const code = dedent` + contract TestContract { + + bool private no_underscore_suffix = false; + + function testFunction(uint parameter) { + bool foo = true; + } + }`, + errors = Solium.lint(addPragma(code), userConfig); + + errors.should.be.instanceof(Array); + errors.length.should.equal(1); + errors[0].message.should.equal( + "'no_underscore_suffix' does not have an underscore as suffix."); + done(); + }); +}); + +describe("[RULE] internal-state-underscore-suffix-private: Acceptances", function() { + + afterEach(function(done) { + Solium.reset(); + done(); + }); + + it("should accept private state variables with underscore suffix", function(done) { + const code = dedent` + contract TestContract { + + bool private underscore_suffix_ = false; + + function testFunction(uint parameter) { + bool foo = true; + } + }`, + errors = Solium.lint(addPragma(code), userConfig); + + errors.should.deepEqual([]); + + done(); + }); + +}); + describe("[RULE] internal-state-underscore-suffix: Rejections", function() { afterEach(function(done) { @@ -26,16 +132,18 @@ describe("[RULE] internal-state-underscore-suffix: Rejections", function() { const code = dedent` contract TestContract { - bool internal_no_underscore_suffix = false; + bool internal no_underscore_suffix = false; - function testFunction(uint parameter) {} + function testFunction(uint parameter) { + bool foo = true; + } }`, errors = Solium.lint(addPragma(code), userConfig); errors.should.be.instanceof(Array); errors.length.should.equal(1); errors[0].message.should.equal( - "'internal_no_underscore_suffix' does not have an underscore as suffix."); + "'no_underscore_suffix' does not have an underscore as suffix."); done(); }); }); @@ -51,9 +159,11 @@ describe("[RULE] internal-state-underscore-suffix: Acceptances", function() { const code = dedent` contract TestContract { - bool public internal_underscore_suffix_ = false; + bool internal underscore_suffix_ = false; - function testFunction(uint parameter) {} + function testFunction(uint parameter) { + bool foo = true; + } }`, errors = Solium.lint(addPragma(code), userConfig);