Skip to content
This repository has been archived by the owner on Jan 19, 2019. It is now read-only.

Added rule for "_" suffix for internal variables #31

Closed
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ In the .soliumrc.json file, add:
],
"zeppelin/no-unused-imports": [
"warning"
],
"zeppelin/internal-state-underscore-suffix": [
"warning"
]
}

Expand Down
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
};
38 changes: 38 additions & 0 deletions rules/internal-state-underscore-suffix.js
Original file line number Diff line number Diff line change
@@ -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) !== "_") &&
(emitted.node.visibility != "public")) {
context.report({
node: emitted.node,
message: `'${param}' does not have an underscore as suffix.`
});
}

}

return {
StateVariableDeclaration: inspectStateVariableDeclaration
};
}
};
175 changes: 175 additions & 0 deletions test/internal-state-underscore-suffix.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
/**
* @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-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) {
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) {
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: 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 internal underscore_suffix_ = false;

function testFunction(uint parameter) {
bool foo = true;
}
}`,
errors = Solium.lint(addPragma(code), userConfig);

errors.should.deepEqual([]);

done();
});

});