Skip to content

Commit

Permalink
adding linebreak-style rule (palantir#1254)
Browse files Browse the repository at this point in the history
  • Loading branch information
patsissons authored and jkillian committed May 17, 2016
1 parent bbbd3b1 commit 72883dd
Show file tree
Hide file tree
Showing 14 changed files with 100 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
# being reported to be 1 character longer

*.lint text eol=lf
/test/rules/linebreak-style/**/CRLF/*.lint text eol=crlf
48 changes: 48 additions & 0 deletions src/rules/linebreakStyleRule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import * as ts from "typescript";
import * as Lint from "../lint";

const OPTION_LINEBREAK_STYLE_CRLF = "CRLF";
const OPTION_LINEBREAK_STYLE_LF = "LF";

export class Rule extends Lint.Rules.AbstractRule {
public static FAILURE_STRINGS = {
CRLF: `Expected linebreak to be '${OPTION_LINEBREAK_STYLE_CRLF}'`,
LF: `Expected linebreak to be '${OPTION_LINEBREAK_STYLE_LF}'`,
};

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
const failures = <Lint.RuleFailure[]>[];
const scanner = ts.createScanner(
sourceFile.languageVersion,
false,
sourceFile.languageVariant,
sourceFile.getFullText()
);

const linebreakStyle = this.getOptions().ruleArguments[0] || OPTION_LINEBREAK_STYLE_LF;
const expectLF = linebreakStyle === OPTION_LINEBREAK_STYLE_CRLF;
const expectedEOL = expectLF ? "\r\n" : "\n";
const failureString = expectLF ? Rule.FAILURE_STRINGS.CRLF : Rule.FAILURE_STRINGS.LF;

for (let token = scanner.scan(); token !== ts.SyntaxKind.EndOfFileToken; token = scanner.scan()) {
if (token === ts.SyntaxKind.NewLineTrivia) {
const text = scanner.getTokenText();
if (text !== expectedEOL) {
failures.push(this.createFailure(sourceFile, scanner, failureString));
}
}
}

return failures;
}

public createFailure(sourceFile: ts.SourceFile, scanner: ts.Scanner, failure: string): Lint.RuleFailure {
// get the start of the current line
const start = sourceFile.getPositionOfLineAndCharacter(sourceFile.getLineAndCharacterOfPosition(scanner.getStartPos()).line, 0);
// since line endings are not visible, we simply end at the beginning of
// the line ending, which happens to be the start of the token.
const end = scanner.getStartPos();

return new Lint.RuleFailure(sourceFile, start, end, failure, this.getOptions().ruleName);
}
}
1 change: 1 addition & 0 deletions src/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
"rules/jsdocFormatRule.ts",
"rules/labelPositionRule.ts",
"rules/labelUndefinedRule.ts",
"rules/linebreakStyleRule.ts",
"rules/maxLineLengthRule.ts",
"rules/memberAccessRule.ts",
"rules/memberOrderingRule.ts",
Expand Down
Empty file.
5 changes: 5 additions & 0 deletions test/rules/linebreak-style/emptyFile/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"linebreak-style": [true, "LF"]
}
}
8 changes: 8 additions & 0 deletions test/rules/linebreak-style/failure/CRLF/test.ts.lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// this line uses CRLF
~~~~~~~~~~~~~~~~~~~~~~ [Expected linebreak to be 'LF']
// this line uses CRLF
~~~~~~~~~~~~~~~~~~~~~~ [Expected linebreak to be 'LF']
// this line uses CRLF
~~~~~~~~~~~~~~~~~~~~~~ [Expected linebreak to be 'LF']
// this line uses CRLF
~~~~~~~~~~~~~~~~~~~~~~ [Expected linebreak to be 'LF']
5 changes: 5 additions & 0 deletions test/rules/linebreak-style/failure/CRLF/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"linebreak-style": [true, "LF"]
}
}
8 changes: 8 additions & 0 deletions test/rules/linebreak-style/failure/LF/test.ts.lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// this line uses LF
~~~~~~~~~~~~~~~~~~~~ [Expected linebreak to be 'CRLF']
// this line uses LF
~~~~~~~~~~~~~~~~~~~~ [Expected linebreak to be 'CRLF']
// this line uses LF
~~~~~~~~~~~~~~~~~~~~ [Expected linebreak to be 'CRLF']
// this line uses LF
~~~~~~~~~~~~~~~~~~~~ [Expected linebreak to be 'CRLF']
5 changes: 5 additions & 0 deletions test/rules/linebreak-style/failure/LF/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"linebreak-style": [true, "CRLF"]
}
}
4 changes: 4 additions & 0 deletions test/rules/linebreak-style/success/CRLF/test.ts.lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// this line uses CRLF
// this line uses CRLF
// this line uses CRLF
// this line uses CRLF
5 changes: 5 additions & 0 deletions test/rules/linebreak-style/success/CRLF/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"linebreak-style": [true, "CRLF"]
}
}
4 changes: 4 additions & 0 deletions test/rules/linebreak-style/success/LF/test.ts.lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// this line uses LF
// this line uses LF
// this line uses LF
// this line uses LF
5 changes: 5 additions & 0 deletions test/rules/linebreak-style/success/LF/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"linebreak-style": [true, "LF"]
}
}
1 change: 1 addition & 0 deletions test/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
"../src/rules/jsdocFormatRule.ts",
"../src/rules/labelPositionRule.ts",
"../src/rules/labelUndefinedRule.ts",
"../src/rules/linebreakStyleRule.ts",
"../src/rules/maxLineLengthRule.ts",
"../src/rules/memberAccessRule.ts",
"../src/rules/memberOrderingRule.ts",
Expand Down

0 comments on commit 72883dd

Please sign in to comment.