Skip to content

Commit c2464e4

Browse files
author
duaraghav8
committed
Add tests for rule no-trailing-whitespace
1 parent 74f031f commit c2464e4

File tree

9 files changed

+159
-13
lines changed

9 files changed

+159
-13
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Changelog
22

3-
## 1.2.4 ()
3+
## 1.2.4 (2019-04-08)
44
- Added rule `no-trailing-whitespace` to warn the user when code, comment or blank lines contain trailing whitespaces. This rule will supply the `fix` functionality in a future release.
55
- Added `getLines()` sourceCode utility function for rule developers. This method returns the source code split into lines.
66
- Added `getComments()` sourceCode utility function for rule developers. This method returns a list of AST Nodes representing comments in the source code.

config/rulesets/solium-all.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ module.exports = {
4040
"error-reason": "warning",
4141
"visibility-first": "warning",
4242
"constructor": "warning",
43+
"no-trailing-whitespace": "warning",
4344

4445
// Turn OFF all deprecated rules
4546
"double-quotes": "off",

config/rulesets/solium-recommended.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ module.exports = {
4141
"function-order": "off",
4242
"conditionals-whitespace": "off",
4343
"no-experimental": "off",
44+
"no-trailing-whitespace": "warning",
4445

4546
// Disable deprecated rules
4647
"double-quotes": "off",

docs/developer-guide.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ You're now ready to write your tests (see `shouldjs documentation <https://shoul
400400
After writing your tests, add an entry for your rule ``foo-bar`` in `solium json <https://github.com/duaraghav8/Solium/blob/master/config/solium.json>`_.
401401
You also need to add your rule's entry to the ``List of Style Rules`` section in User Guide.
402402

403-
Finally, add an entry for your rule in `solium all <https://github.com/duaraghav8/Solium/blob/master/config/rulesets/solium-all.js>`_ ruleset: ``foo-bar: <SEVERITY>`` where severity should be how your rule should be treated by default (as an error or warning). Severity should be same as what you specified in your rule's ``meta.docs.type``.
403+
Finally, add an entry for your rule in `solium all <https://github.com/duaraghav8/Solium/blob/master/config/rulesets/solium-all.js>`_ and `solium recommended <https://github.com/duaraghav8/Solium/blob/master/config/rulesets/solium-recommended.js>`_ rulesets: ``foo-bar: <SEVERITY>`` where severity should be how your rule should be treated by default (as an error or warning). Severity should be same as what you specified in your rule's ``meta.docs.type``.
404404

405405
Now run ``npm run lint`` to let eslint work its magic. Resolve any lint issues you might see in your rule & test files.
406406
Run ``npm test`` and resolve any failures.

lib/rules/no-trailing-whitespace.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ function create(context) {
6060

6161
codeLines.forEach((line, i) => {
6262
if (
63-
(isBlankLine(line) && skipBlankLines) ||
64-
(isLineInsideAComment(line, i+1, comments, sourceCode) && ignoreComments)
63+
(skipBlankLines && isBlankLine(line)) ||
64+
(ignoreComments && isLineInsideAComment(line, i+1, comments, sourceCode))
6565
) {
6666
return;
6767
}
@@ -109,9 +109,7 @@ module.exports = {
109109
ignoreComments: { type: "boolean" }
110110
},
111111
additionalProperties: false
112-
}],
113-
114-
fixable: "code"
112+
}]
115113
},
116114

117115
create

lib/solium.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ module.exports = (function() {
127127
let message = `[Deprecated] Rule "${name}" is deprecated.`;
128128

129129
if (rule.meta.docs.replacedBy) {
130-
message += " Please use " + rule.meta.docs.replacedBy.map(function(rn) {
130+
message += " Use " + rule.meta.docs.replacedBy.map(function(rn) {
131131
return "\"" + rn + "\"";
132132
}).join(", ") + " instead.";
133133
}

test/lib/rules.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ describe("Checking exported rules object", function() {
212212
// We extend ALL solium core rules and eliminate a few by setting their severity to 0.
213213
// The rest of the rules should all be available.
214214
// The below count will keep changing with every change in the number of core rules that exist in solium.
215-
Object.keys(ruleDescriptions).length.should.equal(29);
215+
Object.keys(ruleDescriptions).length.should.equal(30);
216216

217217
done();
218218
});
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/**
2+
* @fileoverview Tests for no-trailing-whitespace rule
3+
* @author Raghav Dua <duaraghav8@gmail.com>
4+
*/
5+
6+
"use strict";
7+
8+
9+
const Solium = require("../../../../lib/solium");
10+
11+
const config = {
12+
"rules": {
13+
"no-trailing-whitespace": "error"
14+
}
15+
};
16+
17+
18+
describe("[RULE] no-trailing-whitespace: Acceptances", () => {
19+
20+
it("should accept lines having no trailing whitespaces", done => {
21+
const code = `
22+
23+
contract Foo {
24+
// a comment
25+
function baz() returns(uint) {
26+
/*
27+
another happy comment
28+
on multiple lines
29+
*/
30+
31+
32+
callHelloWorld(
33+
100, /* another block comment */
34+
"voila!",
35+
36+
0x1892873871198
37+
);
38+
}
39+
}`;
40+
41+
Solium.lint(code, config).should.be.empty();
42+
done();
43+
});
44+
45+
it("should accept comments & blank lines with trailing whitespaces if they're ignored", done => {
46+
const configLocal = {
47+
"rules": {
48+
"no-trailing-whitespace": [
49+
"error",
50+
{ "skipBlankLines": true, "ignoreComments": true }
51+
]
52+
}
53+
};
54+
55+
const code = `
56+
\t\t \t
57+
contract Foo {
58+
// a comment
59+
function baz() returns(uint) {
60+
/* \t
61+
another happy comment
62+
on multiple lines\t
63+
*/
64+
65+
\t \t
66+
callHelloWorld(
67+
100, /* another block comment */
68+
"voila!",
69+
\t
70+
0x1892873871198
71+
);
72+
} // another line comment\t\t
73+
74+
75+
function /* hello */ bax() /* world */ returns(string) { /* this is a stretched \t
76+
comment */
77+
return "hello";
78+
}
79+
}`;
80+
81+
Solium.lint(code, configLocal).should.be.empty();
82+
done();
83+
});
84+
85+
});
86+
87+
88+
describe("[RULE] no-trailing-whitespace: Rejections", () => {
89+
90+
it("should reject code, comment & blank lines with trailing whitespaces", done => {
91+
const code = `
92+
\t\t \t
93+
contract Foo {
94+
// a comment
95+
function baz() returns(uint) {
96+
/* \t
97+
another happy comment
98+
on multiple lines\t
99+
*/
100+
101+
\t \t
102+
callHelloWorld(
103+
100, /* another block comment */
104+
"voila!",
105+
\t
106+
0x1892873871198\t
107+
);
108+
} // another line comment\t\t
109+
110+
111+
function /* hello */ bax() /* world */ returns(string) { /* this is a stretched \t
112+
comment */
113+
return "hello";
114+
}
115+
}`;
116+
117+
Solium.lint(code, config).should.have.size(16);
118+
done();
119+
});
120+
121+
});

test/lib/solium.js

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -866,7 +866,14 @@ describe("Solium.lint() comment directives", () => {
866866
867867
868868
`;
869-
const errors = Solium.lint(code, { "extends": "solium:all" });
869+
const config = {
870+
"extends": "solium:all",
871+
"rules": {
872+
"no-trailing-whitespace": "off"
873+
}
874+
};
875+
876+
const errors = Solium.lint(code, config);
870877

871878
errors.should.be.Array();
872879
errors.should.have.size(10); // This no. can change if changes are made in any rules from solium:all ruleset
@@ -876,7 +883,13 @@ describe("Solium.lint() comment directives", () => {
876883
});
877884

878885
it("should respect solium-disable", done => {
879-
const config = { "extends": "solium:all" };
886+
const config = {
887+
"extends": "solium:all",
888+
"rules": {
889+
"no-trailing-whitespace": "off"
890+
}
891+
};
892+
880893
let code = `// \t solium-disable
881894
contract blah{}
882895
contract f {
@@ -1392,7 +1405,13 @@ describe("Solium.lint() comment directives", () => {
13921405
});
13931406

13941407
it("should respect solium-disable-line", done => {
1395-
const config = { "extends": "solium:all" };
1408+
const config = {
1409+
"extends": "solium:all",
1410+
"rules": {
1411+
"no-trailing-whitespace": "off"
1412+
}
1413+
};
1414+
13961415
let code = `
13971416
contract blah{}// \t solium-disable-line
13981417
contract f {
@@ -1759,7 +1778,13 @@ describe("Solium.lint() comment directives", () => {
17591778
});
17601779

17611780
it("should respect solium-disable-next-line", done => {
1762-
const config = { "extends": "solium:all" };
1781+
const config = {
1782+
"extends": "solium:all",
1783+
"rules": {
1784+
"no-trailing-whitespace": "off"
1785+
}
1786+
};
1787+
17631788
let code = `
17641789
contract blah{}// \t solium-disable-next-line
17651790
contract f {

0 commit comments

Comments
 (0)