forked from palantir/tslint
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding linebreak-style rule (palantir#1254)
fixes palantir#123
- Loading branch information
1 parent
bbbd3b1
commit 72883dd
Showing
14 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"rules": { | ||
"linebreak-style": [true, "LF"] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"rules": { | ||
"linebreak-style": [true, "LF"] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"rules": { | ||
"linebreak-style": [true, "CRLF"] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"rules": { | ||
"linebreak-style": [true, "CRLF"] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"rules": { | ||
"linebreak-style": [true, "LF"] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters