Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added autofix functions for blank-lines rule + test cases #161

Merged
merged 3 commits into from
Jan 7, 2018

Conversation

GabrielAlacchi
Copy link
Contributor

This PR is in regards to issue #94. I've added autofix functions for the rule, as well as did some work to ensure compatibility with windows style line endings \r\n vs \n by using the EOL property of node's OS package, as well as by modifying the regular expressions to use \s or \r?\n in certain instances. I've also applied the generalization for the has2ConsecutiveBlankLines(string) to deal with N consecutive blank lines, thus changing the signature to hasNConsecutiveBlankLines(string, n).

There are two autofix functions that have been added.
First is the autofix function for the inspectChild linting inspection, which checks if children of top level nodes are succeeded by at least 1 blank line. It simply fixes the problem by inserting a blank line.

The second is the autofix function for the inspectProgram linting inspection, which checks if top level nodes have 2 spaces preceeding them and any accompanying comments. The fixing function uses the following strategy.

  • Starting at the line of the node which causes the linting error.
  • Iteratively find the first blank line which is directly preceding the node which isn't a comment or part of a block comment. The loop which does this also skips entire block comments so that we can deal with situations like this

// Test
/*
 Hi
*/
contract Foo {}
  • Once the first blank line is found, start counting blank lines until another comment or line of code is reaching.
  • Insert the required number of newlines right before the topmost comment in order to ensure there are 2 spaces. This step can also be generalized to N lines as well in the future if necessary.

The latter fix is elaborate, but it manages to fix all of the test code snippets in the acceptances test case of the blank-lines unit test file. I've copied most of those code snippets as well as writing 1-2 more for the unit tests that have been added for these functions.

// TODO: generalize this method for N consec. blank lines in future
function has2ConsecutiveBlankLines(string) {
return /\n[ \t]*\n[ \t]*\n/.test(string);
let eol = require("os").EOL;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use const for all things require()d and use it in place of let wherever you can.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I'll apply all the ES6 refactoring changes you requested to the rest of the rule and tests as needed, and not just my code. Might as well kill 2 birds with one stone.

// - Insert the desired number of line break characters right before the comment

let line = sourceCode.getLine(topLevelNode) - 1, // The line number
maxLines = spacing.split("\n").length,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure we don't need to use EOL instead of just \n over here? And in all the places where you're using \n only?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code would be equivalent either way since \r\n includes \n. Although you could argue that splitting by \n alone causes inconsistent behavior across OSes since on windows there would be an extra \r in the substrings resulting from the split. In the context of counting the number of lines it's not really a big deal if the strings have that extra \r. If you'd prefer to use EOL for the sake of consistency however that's a good reason and I could definitely change it up.

Copy link
Owner

@duaraghav8 duaraghav8 Jan 7, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok that makes sense. But let's use EOL

  1. for consistency
  2. so there's no scope of confusion about what kind of linebreak to use where for those who read this rule's code in future

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good. My latest commit should cover it.

let line = sourceCode.getLine(topLevelNode) - 1, // The line number
maxLines = spacing.split("\n").length,
bottomLine = sourceCode.getLine(topLevelNode),
multipleLines = spacing.indexOf("\n") > -1, // Whether there are multiple lines
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use includes() method in place of indexOf(...) > -1 at all places where you need to check whether an item exists inside an array/string. The indexOf() is a bad habit I introduced long back when I wasn't aware of includes().
eg

// returns boolean
myArray.includes(90)
myString.includes("\n")

@@ -330,3 +330,150 @@ describe("[RULE] blank-lines: Rejections", function() {
});

});


describe("[RULE] blank-lines: Fixes", function() {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use () => { in place of function() {. In general, use all the ES6 features you can except for import statements. Solium was originally written in es5 so you will see es5-style code at a lot of places but we're now encouraging es6!

done();
});

it("Should correct multiline functions not followed by a blank line", function(done) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above. use done => {

@duaraghav8
Copy link
Owner

@GabrielAlacchi Just minor changes needed, everything looks great!

@duaraghav8 duaraghav8 merged commit 8a68d8c into duaraghav8:master Jan 7, 2018
@duaraghav8
Copy link
Owner

awesome!

@GabrielAlacchi GabrielAlacchi deleted the blank-lines branch January 8, 2018 02:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants