Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

Commit

Permalink
[Issue #173] fix fixNoVarKeywordFormatter to handle windows line feeds
Browse files Browse the repository at this point in the history
closes #173
  • Loading branch information
HamletDRC committed Aug 11, 2016
1 parent f714746 commit f563858
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 8 deletions.
12 changes: 7 additions & 5 deletions src/fixNoVarKeywordFormatter.ts
Original file line number Diff line number Diff line change
@@ -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';

/**
Expand All @@ -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);
Expand Down
50 changes: 47 additions & 3 deletions tests/FixNoVarKeywordFormatterTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down

0 comments on commit f563858

Please sign in to comment.