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

Add support to fix align rule #2097

Merged
merged 1 commit into from
Jan 22, 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
10 changes: 8 additions & 2 deletions src/rules/alignRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,14 @@ class AlignWalker extends Lint.RuleWalker {
for (const node of nodes.slice(1)) {
const curPos = this.getLineAndCharacterOfPosition(node.getStart());
if (curPos.line !== prevPos.line && curPos.character !== alignToColumn) {
this.addFailureAtNode(node, kind + Rule.FAILURE_STRING_SUFFIX);
break;
const diff = alignToColumn - curPos.character;
let fix;
if (0 < diff) {
fix = this.createFix(this.appendText(node.getStart(), " ".repeat(diff)));
} else {
fix = this.createFix(this.deleteText(node.getStart() + diff, -diff));
}
this.addFailureAtNode(node, kind + Rule.FAILURE_STRING_SUFFIX, fix);
}
prevPos = curPos;
}
Expand Down
149 changes: 149 additions & 0 deletions test/rules/align/arguments/test.ts.fix
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
function invalidParametersAlignment1(a: number,
b: number) {
var i = 0;
}

function invalidParametersAlignment2(a: number, b: number,
c: number) {
var i = 0;
}

function invalidParametersAlignment3(a: number,
b: number,
c: number) {
var i = 0;
}

class C1 {
invalidParametersAlignment(a: number,
b: number)
{
}
}

class InvalidAlignmentInConstructor {
constructor(a: number,
str: string)
{
}
}

var invalidParametersAlignment4 = function(xxx: foo,
yyy: bar) { return true; }

function validParametersAlignment1(a: number, b: number) {
var i = 0;
}

function validParametersAlignment2(a: number, b: number,
c: number) {
var i = 0;
}

function validParametersAlignment3(a: number,
b: number,
c: number) {
var i = 0;
}

function validParametersAlignment4(
a: number,
b: number,
c: number) {
var i = 0;
}

var validParametersAlignment6 = function(xxx: foo,
yyy: bar) { return true; }

///////

function invalidArgumentsAlignment1()
{
f(10,
'abcd', 0);
}

function invalidArgumentsAlignment2()
{
f(10,
'abcd',
0);

}

class Foo {
constructor(a: number,
str: string)
{
}
}

var invalidConstructorArgsAlignment = new foo(10,
"abcd");

function validArgumentsAlignment1()
{
f(101, 'xyz', 'abc');
}

function validArgumentsAlignment2()
{
f(1,
2,
3,
4);
}

function validArgumentsAlignment3()
{
f(
1,
2,
3,
4);
}

function validArgumentsAlignment3()
{
f(1, 2,
3, 4);
}

////////

function invalidStatementsAlignment1()
{
var i = 0;
var j = 0;
var k = 1;
}

function invalidStatementsAlignment1()
{
var i = 0;
{
var j = 0;
var k = 1;
}
}

function validStatementsAlignment1()
{
var i = 0;
var j = 0;
var k = 1;
}

function validStatementsAlignment2()
{
var i = 0;
{
var j = 0;
var k = 1;
}
}

function shouldntCrash() {
let f = new Foo;
}
1 change: 1 addition & 0 deletions test/rules/align/parameters/test.js.lint
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ function invalidParametersAlignment3(a,
b,
~ [parameters are not aligned]
c) {
~ [parameters are not aligned]
var i = 0;
}

Expand Down
32 changes: 32 additions & 0 deletions test/rules/align/parameters/test.ts.fix
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
function invalidParametersAlignment1(a: number,
b: number) {
var i = 0;
}

function invalidParametersAlignment2(a: number, b: number,
c: number) {
var i = 0;
}

function invalidParametersAlignment3(a: number,
b: number,
c: number) {
var i = 0;
}

class C1 {
invalidParametersAlignment(a: number,
b: number)
{
}
}

class InvalidAlignmentInConstructor {
constructor(a: number,
str: string)
{
}
}

var invalidParametersAlignment4 = function(xxx: foo,
yyy: bar) { return true; }
1 change: 1 addition & 0 deletions test/rules/align/parameters/test.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ function invalidParametersAlignment3(a: number,
b: number,
~~~~~~~~~ [parameters are not aligned]
c: number) {
~~~~~~~~~ [parameters are not aligned]
var i = 0;
}

Expand Down
149 changes: 149 additions & 0 deletions test/rules/align/statements/test.ts.fix
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
function invalidParametersAlignment1(a: number,
b: number) {
var i = 0;
}

function invalidParametersAlignment2(a: number, b: number,
c: number) {
var i = 0;
}

function invalidParametersAlignment3(a: number,
b: number,
c: number) {
var i = 0;
}

class C1 {
invalidParametersAlignment(a: number,
b: number)
{
}
}

class InvalidAlignmentInConstructor {
constructor(a: number,
str: string)
{
}
}

var invalidParametersAlignment4 = function(xxx: foo,
yyy: bar) { return true; }

function validParametersAlignment1(a: number, b: number) {
var i = 0;
}

function validParametersAlignment2(a: number, b: number,
c: number) {
var i = 0;
}

function validParametersAlignment3(a: number,
b: number,
c: number) {
var i = 0;
}

function validParametersAlignment4(
a: number,
b: number,
c: number) {
var i = 0;
}

var validParametersAlignment6 = function(xxx: foo,
yyy: bar) { return true; }


///////

function invalidArgumentsAlignment1()
{
f(10,
'abcd', 0);
}

function invalidArgumentsAlignment2()
{
f(10,
'abcd',
0);
}

class Foo {
constructor(a: number,
str: string)
{
}
}

var invalidConstructorArgsAlignment = new foo(10,
"abcd");

function validArgumentsAlignment1()
{
f(101, 'xyz', 'abc');
}

function validArgumentsAlignment2()
{
f(1,
2,
3,
4);
}

function validArgumentsAlignment3()
{
f(
1,
2,
3,
4);
}

function validArgumentsAlignment3()
{
f(1, 2,
3, 4);
}

////////

function invalidStatementsAlignment1()
{
var i = 0;
var j = 0;
var k = 1;
}

function invalidStatementsAlignment1()
{
var i = 0;
{
var j = 0;
var k = 1;
}
}

function validStatementsAlignment1()
{
var i = 0;
var j = 0;
var k = 1;
}

function validStatementsAlignment2()
{
var i = 0;
{
var j = 0;
var k = 1;
}
}

function shouldntCrash() {
let f = new Foo;
}