From f563858c444868bbeb3559d163463ca757a400d3 Mon Sep 17 00:00:00 2001 From: HamletDRC Date: Thu, 11 Aug 2016 11:21:01 +0200 Subject: [PATCH] [Issue #173] fix fixNoVarKeywordFormatter to handle windows line feeds closes #173 --- src/fixNoVarKeywordFormatter.ts | 12 ++++--- tests/FixNoVarKeywordFormatterTests.ts | 50 ++++++++++++++++++++++++-- 2 files changed, 54 insertions(+), 8 deletions(-) diff --git a/src/fixNoVarKeywordFormatter.ts b/src/fixNoVarKeywordFormatter.ts index 15397c01a..6e621f053 100644 --- a/src/fixNoVarKeywordFormatter.ts +++ b/src/fixNoVarKeywordFormatter.ts @@ -1,6 +1,6 @@ 'use strict'; -import {RuleFailure, RuleFailurePosition} from 'tslint/lib/language/rule/rule'; +import {RuleFailure} from 'tslint/lib/language/rule/rule'; import {BaseFormatter} from './utils/BaseFormatter'; /** @@ -12,14 +12,16 @@ export class Formatter extends BaseFormatter { constructor() { super('no-var-keyword', (failure: RuleFailure): void => { + const fileName: string = failure.getFileName(); - const start: RuleFailurePosition = failure.getStartPosition(); const fileContents: string = this.readFile(fileName); + const end: number = failure.getEndPosition().getPosition(); - const leftSide: string = fileContents.substring(0, start.getPosition()); - const rightSide: string = fileContents.substring(start.getPosition() + 3); + let leftSide: string = fileContents.substring(0, end); + leftSide = leftSide.replace(/var$/, 'let'); + const rightSide: string = fileContents.substring(end); + const newContent: string = leftSide + rightSide; - const newContent: string = leftSide + 'let' + rightSide; this.writeFile(fileName, newContent); /* tslint:disable:no-console */ console.log('Automatically converting var to let. Please re-compile and re-lint: ' + fileName); diff --git a/tests/FixNoVarKeywordFormatterTests.ts b/tests/FixNoVarKeywordFormatterTests.ts index 4a4355e0c..3b16431d7 100644 --- a/tests/FixNoVarKeywordFormatterTests.ts +++ b/tests/FixNoVarKeywordFormatterTests.ts @@ -44,10 +44,54 @@ var foo = bar; const formatter = new FixNoVarKeywordFormatterForTesting(input); formatter.format(TestHelper.runRule(ruleName, null, input).failures); - chai.expect(formatter.getOutput().trim()).to.equal( -` + chai.expect(formatter.getOutput()).to.equal( + ` let foo = bar; -`.trim()); +`); + }); + + it('should fix a var keyword with no proceeding carriage return', () : void => { + const input : string = `var foo = bar; +`; + + const formatter = new FixNoVarKeywordFormatterForTesting(input); + formatter.format(TestHelper.runRule(ruleName, null, input).failures); + chai.expect(formatter.getOutput()).to.equal( + `let foo = bar; +`); + }); + + it('should fix a var keyword with multiple proceeding carriage returns', () : void => { + const input : string = ` + + +var foo = bar; +`; + + const formatter = new FixNoVarKeywordFormatterForTesting(input); + formatter.format(TestHelper.runRule(ruleName, null, input).failures); + chai.expect(formatter.getOutput()).to.equal( + ` + + +let foo = bar; +`); + }); + + it('should fix a var keyword with windows line endings', () : void => { + const input : string = `\r\nvar foo = bar;\r\n`; + + const formatter = new FixNoVarKeywordFormatterForTesting(input); + formatter.format(TestHelper.runRule(ruleName, null, input).failures); + chai.expect(formatter.getOutput()).to.equal(`\r\nlet foo = bar;\r\n`); + }); + + it('should fix a var keyword with multiple windows line endings', () : void => { + const input : string = `\r\n var foo = bar;\r\n`; + + const formatter = new FixNoVarKeywordFormatterForTesting(input); + formatter.format(TestHelper.runRule(ruleName, null, input).failures); + chai.expect(formatter.getOutput()).to.equal(`\r\n let foo = bar;\r\n`); }); }); /* tslint:enable:quotemark */