Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Refactor linebreak-style and add fixer #2394

Merged
merged 2 commits into from
Mar 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
*.lint text eol=lf
*.fix text eol=lf
/test/rules/linebreak-style/**/CRLF/*.lint text eol=crlf
/test/rules/linebreak-style/**/LF/*.fix text eol=crlf
56 changes: 21 additions & 35 deletions src/rules/linebreakStyleRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,48 +39,34 @@ export class Rule extends Lint.Rules.AbstractRule {
optionExamples: [`[true, "${OPTION_LINEBREAK_STYLE_LF}"]`, `[true, "${OPTION_LINEBREAK_STYLE_CRLF}"]`],
type: "maintainability",
typescriptOnly: false,
hasFix: true,
Copy link
Contributor

Choose a reason for hiding this comment

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

nice

};
/* tslint:enable:object-literal-sort-keys */

public static FAILURE_STRINGS = {
CRLF: `Expected linebreak to be '${OPTION_LINEBREAK_STYLE_CRLF}'`,
LF: `Expected linebreak to be '${OPTION_LINEBREAK_STYLE_LF}'`,
};
public static FAILURE_CRLF = `Expected linebreak to be '${OPTION_LINEBREAK_STYLE_CRLF}'`;
public static FAILURE_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 ruleArguments = this.getOptions().ruleArguments;
const linebreakStyle = ruleArguments.length > 0 ? 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;
return this.applyWithFunction(sourceFile, walk, this.ruleArguments.indexOf(OPTION_LINEBREAK_STYLE_CRLF) !== -1);
}
}

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));
}
function walk(ctx: Lint.WalkContext<boolean>) {
const expectedCr = ctx.options;
const sourceText = ctx.sourceFile.text;
const lineStarts = ctx.sourceFile.getLineStarts();
for (let i = 1; i < lineStarts.length; ++i) {
const lineEnd = lineStarts[i] - 1;
if (sourceText[lineEnd - 1] === "\r") {
if (!expectedCr) {
ctx.addFailure(lineStarts[i - 1], lineEnd - 1, Rule.FAILURE_LF, ctx.createFix(
Lint.Replacement.deleteText(lineEnd - 1, 1),
));
}
} else if (expectedCr) {
ctx.addFailure(lineStarts[i - 1], lineEnd, Rule.FAILURE_CRLF, ctx.createFix(
Lint.Replacement.appendText(lineEnd, "\r"),
));
}

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);
}
}
4 changes: 4 additions & 0 deletions test/rules/linebreak-style/failure/CRLF/test.js.fix
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
4 changes: 4 additions & 0 deletions test/rules/linebreak-style/failure/CRLF/test.ts.fix
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
4 changes: 4 additions & 0 deletions test/rules/linebreak-style/failure/LF/test.js.fix
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
4 changes: 4 additions & 0 deletions test/rules/linebreak-style/failure/LF/test.ts.fix
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