Skip to content

Commit

Permalink
whitespace: use os.EOL (#162) & remove dead code
Browse files Browse the repository at this point in the history
  • Loading branch information
duaraghav8 committed Jan 13, 2018
1 parent 48fa83d commit 197c87a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 82 deletions.
26 changes: 1 addition & 25 deletions lib/rules/comma-whitespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,6 @@ module.exports = {

let sourceCode = context.getSourceCode();

function inspectObjectExpression(emitted) {
let node = emitted.node, properties = node.properties;

function inspectObjectPropForWhitespace(prop) {
let charAfterProp = sourceCode.getNextChar(prop);

(charAfterProp !== ",") && context.report({
node: prop,
location: {
column: sourceCode.getEndingColumn(prop) + 1
},
message: "There should be no whitespace or comments between object property and the comma following it."
});
}

if (emitted.exit) {
return;
}

//CHECK FOR COMMA WHITESPACE
properties.slice(0, -1).forEach(inspectObjectPropForWhitespace);
}


function inspectCallExpression(emitted) {
let node = emitted.node,
Expand Down Expand Up @@ -146,8 +123,7 @@ module.exports = {
VariableDeclarationTuple: inspectVariableDeclarationTuple,
SequenceExpression: inspectSequenceExpression,
ArrayExpression: inspectArrayExpression,
CallExpression: inspectCallExpression,
ObjectExpression: inspectObjectExpression
CallExpression: inspectCallExpression
};
}
};
58 changes: 8 additions & 50 deletions lib/rules/whitespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

"use strict";

const { EOL } = require("os"),
jse = require("js-string-escape");

module.exports = {

meta: {
Expand Down Expand Up @@ -49,8 +52,11 @@ module.exports = {
return;
}

let codeAfterCurrentAssignment = sourceCode.getNextChars(node.value, 3);
validationRegexp = /^((,[^ \n\/].)|(, [^ \n\/])|(,\n[^\n\/]))$/;
let escapedEOL = jse(EOL),
codeAfterCurrentAssignment = sourceCode.getNextChars(node.value, 3);

validationRegexp = new RegExp(
`^((,[^ ${escapedEOL}\\/].)|(, [^ ${escapedEOL}\\/])|(,${escapedEOL}[^${escapedEOL}\\/]))$`);

(!validationRegexp.test(codeAfterCurrentAssignment)) && context.report({
location: {
Expand Down Expand Up @@ -144,55 +150,7 @@ module.exports = {
});
}


function inspectObjectExpression(emitted) {
let node = emitted.node, properties = node.properties;

if (emitted.exit) {
return;
}

//for a 0-property object, ensure that ObjectExpression node's code is simply '{}'
if (!properties.length) {
let nodeCode = sourceCode.getText(node);

(nodeCode !== "{}") && context.report({
node: node,
message: "An empty Object Expression shouldn't have any whitespace or comments between the braces, i.e., '{}'."
});

return;
}

let lastProperty = properties.slice(-1) [0];

//if whole ObjectExpression spans over multiple lines, then below checks don't apply to it
if (sourceCode.getLine(node) !== sourceCode.getEndingLine(lastProperty)) {
return;
}

let charBeforeFirstProp = sourceCode.getPrevChar(properties [0]),
charAfterLastProp = sourceCode.getNextChar(lastProperty);

(charBeforeFirstProp !== "{") && context.report({
node: properties [0],
location: {
column: sourceCode.getColumn(properties [0]) - 1
},
message: "There should be no whitespace or comments between the opening brace '{' and first property."
});

(charAfterLastProp !== "}") && context.report({
node: lastProperty,
location: {
column: sourceCode.getEndingColumn(lastProperty) + 1
},
message: "There should be no whitespace or comments between the last property and closing brace '}'."
});
}

return {
ObjectExpression: inspectObjectExpression,
BlockStatement: inspectBlockStatement,
MemberExpression: inspectMemberExpression,
NameValueAssignment: inspectNameValueAssignment
Expand Down
12 changes: 5 additions & 7 deletions test/lib/rules/whitespace/whitespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@

"use strict";

let Solium = require("../../../../lib/solium");
let wrappers = require("../../../utils/wrappers");
let toContract = wrappers.toContract;
let toFunction = wrappers.toFunction;
let addPragma = wrappers.addPragma;
const { EOL } = require("os");
const Solium = require("../../../../lib/solium"),
wrappers = require("../../../utils/wrappers"), { toContract, toFunction, addPragma } = wrappers;

let userConfig = {
"custom-rules-filename": null,
Expand Down Expand Up @@ -194,7 +192,7 @@ describe("[RULE] whitespace: Acceptances", function() {
done();
});

it("should allow the code that provides nothing to check, i.e., no arguments in CallExpression / no properties in ObjectExpression", function(done) {
it("should allow the code that provides nothing to check, i.e., no arguments in CallExpression", function(done) {
let code = "call ();",
errors = Solium.lint(toFunction(code), userConfig);

Expand Down Expand Up @@ -229,7 +227,7 @@ describe("[RULE] whitespace: Acceptances", function() {
});

it("should allow acceptable comma whitespace between Name-Value declarations", function(done) {
let code = "myStruct ({a:100, b:9028,\nc: 19082,d:\"hello world\",\n\t\te:\"this passes YOLO\"});",
let code = `myStruct ({a:100, b:9028,${EOL}c: 19082,d:\"hello world\",${EOL}\t\te:\"this passes YOLO\"});`,
errors = Solium.lint(toFunction(code), userConfig);

errors.constructor.name.should.equal("Array");
Expand Down

0 comments on commit 197c87a

Please sign in to comment.