Skip to content

Commit

Permalink
Merge pull request palantir#46 from gscshoyru/no-consecutive-blank-lines
Browse files Browse the repository at this point in the history
Added no-consecutive-blank-lines rule
  • Loading branch information
ashwinr committed Dec 5, 2013
2 parents c083c3f + b144497 commit 3bd33e5
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ Supported Rules
* `eofline` enforces the file to end with a newline.
* `forin` enforces a `for ... in` statement to be filtered with an `if` statement.*
* `indent` enforces consistent indentation levels (currently disabled).
* `interface-name` enforces the rule that interface names must begin with a capital 'I'
* `interface-name` enforces the rule that interface names must begin with a capital 'I'
* `label-position` enforces labels only on sensible statements.
* `label-undefined` checks that labels are defined before usage.
* `max-line-length` sets the maximum length of a line.
* `no-arg` disallows access to `arguments.callee`.
* `no-bitwise` disallows bitwise operators.
* `no-console` disallows access to the specified functions on `console`. Rule options are functions to ban on the console variable.
* `no-consecutive-blank-lines` disallows having more than one blank line in a row in a file
* `no-construct` disallows access to the constructors of `String`, `Number`, and `Boolean`.
* `no-debugger` disallows `debugger` statements.
* `no-duplicate-key` disallows duplicate keys in object literals.
Expand Down
55 changes: 55 additions & 0 deletions src/rules/noConsecutiveBlankLinesRule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright 2013 Palantir Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/// <reference path='../../lib/tslint.d.ts' />

export class Rule extends Lint.Rules.AbstractRule {
static FAILURE_STRING = "consecutive blank lines are disallowed";

public apply(syntaxTree: TypeScript.SyntaxTree): Lint.RuleFailure[] {
return this.applyWithWalker(new BlankLinesWalker(syntaxTree, this.getOptions()));
}
}

class BlankLinesWalker extends Lint.RuleWalker {
// starting with 1 to cover the case where the file starts with two blank lines
private newLinesInARowSeenSoFar = 1;

public visitToken(token: TypeScript.ISyntaxToken): void {
this.findConsecutiveBlankLinesFromTriva(token.leadingTrivia().toArray(), this.position());
this.newLinesInARowSeenSoFar = 0;
this.findConsecutiveBlankLinesFromTriva(token.trailingTrivia().toArray(),
this.position() + token.leadingTriviaWidth() + token.width());

super.visitToken(token);
}

private findConsecutiveBlankLinesFromTriva(triviaList: TypeScript.ISyntaxTrivia[], currentPosition: number) {
triviaList.forEach((triviaItem) => {
if (triviaItem.isNewLine()) {
this.newLinesInARowSeenSoFar += 1;
if (this.newLinesInARowSeenSoFar >= 3) {
var failure = this.createFailure(currentPosition, 1, Rule.FAILURE_STRING);
this.addFailure(failure);
}
} else {
this.newLinesInARowSeenSoFar = 0;
}
currentPosition += triviaItem.fullWidth();
});
}

}
15 changes: 15 additions & 0 deletions test/files/rules/blanklines.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@


class Clazz { // comment

public funcxion() {

// also comment


console.log("test");

}


}
36 changes: 36 additions & 0 deletions test/rules/noConsecutiveBlankLinesRuleTests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2013 Palantir Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/// <reference path='../references.ts' />

describe("<no-consecutive-blank-lines>", () => {
var NoConsecutiveBlankLinesRule = Lint.Test.getRule("no-consecutive-blank-lines");

it("ensures comments start with a space and a lowercase letter", () => {
var fileName = "rules/blanklines.test.ts";
var createFailure = Lint.Test.createFailuresOnFile(fileName, NoConsecutiveBlankLinesRule.FAILURE_STRING);
var expectedFailure1 = createFailure([2, 1], [3, 1]);
var expectedFailure2 = createFailure([9, 1], [10, 1]);
var expectedFailure3 = createFailure([14, 1], [15, 1]);

var actualFailures = Lint.Test.applyRuleOnFile(fileName, NoConsecutiveBlankLinesRule);

Lint.Test.assertContainsFailure(actualFailures, expectedFailure1);
Lint.Test.assertContainsFailure(actualFailures, expectedFailure2);
Lint.Test.assertContainsFailure(actualFailures, expectedFailure3);
assert.lengthOf(actualFailures, 3);
});
});

0 comments on commit 3bd33e5

Please sign in to comment.