From 003a75587711670a599ce15ea1e348f2ca5bd138 Mon Sep 17 00:00:00 2001 From: Scott Wu Date: Sat, 27 Aug 2016 20:20:45 -0400 Subject: [PATCH] Fix ArrayType to produce correct fixes Fixes now parenthesize union, intersection and function types. --- src/rules/arrayTypeRule.ts | 16 ++++++++++------ test/rules/array-type/array/test.ts.fix | 9 +++++++++ test/rules/array-type/array/test.ts.lint | 12 ++++++++++++ test/rules/array-type/generic/test.ts.fix | 9 +++++++++ test/rules/array-type/generic/test.ts.lint | 12 ++++++++++++ 5 files changed, 52 insertions(+), 6 deletions(-) diff --git a/src/rules/arrayTypeRule.ts b/src/rules/arrayTypeRule.ts index 9beab5854bc..88957a03679 100644 --- a/src/rules/arrayTypeRule.ts +++ b/src/rules/arrayTypeRule.ts @@ -37,10 +37,11 @@ class ArrayTypeWalker extends Lint.RuleWalker { public visitArrayType(node: ts.ArrayTypeNode) { if (this.hasOption(OPTION_GENERIC)) { const typeName = node.elementType; + const parens = typeName.kind === ts.SyntaxKind.ParenthesizedType ? 1 : 0; const fix = new Lint.Fix(Rule.metadata.ruleName, [ - this.appendText(typeName.getStart(), "Array<"), + this.createReplacement(typeName.getStart(), parens, "Array<"), // Delete the square brackets and replace with an angle bracket - this.createReplacement(typeName.getEnd(), node.getEnd() - typeName.getEnd(), ">"), + this.createReplacement(typeName.getEnd() - parens, node.getEnd() - typeName.getEnd() + parens, ">"), ]); this.addFailure(this.createFailure(node.getStart(), node.getWidth(), Rule.FAILURE_STRING_GENERIC, fix)); } @@ -59,13 +60,16 @@ class ArrayTypeWalker extends Lint.RuleWalker { this.createReplacement(node.getStart(), node.getWidth(), "any[]"), ]); } else if (typeArgs && typeArgs.length === 1) { - const typeStart = typeArgs[0].getStart(); - const typeEnd = typeArgs[0].getEnd(); + const type = typeArgs[0]; + const typeStart = type.getStart(); + const typeEnd = type.getEnd(); + const parens = type.kind === ts.SyntaxKind.UnionType || + type.kind === ts.SyntaxKind.FunctionType || type.kind === ts.SyntaxKind.IntersectionType; fix = new Lint.Fix(Rule.metadata.ruleName, [ // Delete Array and the first angle bracket - this.deleteText(node.getStart(), typeStart - node.getStart()), + this.createReplacement(node.getStart(), typeStart - node.getStart(), parens ? "(" : ""), // Delete the last angle bracket and replace with square brackets - this.createReplacement(typeEnd, node.getEnd() - typeEnd, "[]"), + this.createReplacement(typeEnd, node.getEnd() - typeEnd, (parens ? ")" : "") + "[]"), ]); } this.addFailure(this.createFailure(node.getStart(), node.getWidth(), Rule.FAILURE_STRING_ARRAY, fix)); diff --git a/test/rules/array-type/array/test.ts.fix b/test/rules/array-type/array/test.ts.fix index fcaf684a099..b7330653375 100644 --- a/test/rules/array-type/array/test.ts.fix +++ b/test/rules/array-type/array/test.ts.fix @@ -27,3 +27,12 @@ function barFunction(bar: ArrayClass[]) { function bazFunction(baz: Arr>) { return baz.map(e => e.baz); } + +let fooVar: ((c: number) => number)[]; +let barVar: ((c: number) => number)[]; + +type fooUnion = (string|number|boolean)[]; +type barUnion = (string|number|boolean)[]; + +type fooIntersection = (string & number)[]; +type barIntersection = (string & number)[]; diff --git a/test/rules/array-type/array/test.ts.lint b/test/rules/array-type/array/test.ts.lint index 7b2886b21d2..3a6358a0c7c 100644 --- a/test/rules/array-type/array/test.ts.lint +++ b/test/rules/array-type/array/test.ts.lint @@ -36,3 +36,15 @@ function barFunction(bar: ArrayClass[]) { function bazFunction(baz: Arr>) { return baz.map(e => e.baz); } + +let fooVar: Array<(c: number) => number>; + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [Array type using 'Array' is forbidden. Use 'T[]' instead.] +let barVar: ((c: number) => number)[]; + +type fooUnion = Array; + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [Array type using 'Array' is forbidden. Use 'T[]' instead.] +type barUnion = (string|number|boolean)[]; + +type fooIntersection = Array; + ~~~~~~~~~~~~~~~~~~~~~~ [Array type using 'Array' is forbidden. Use 'T[]' instead.] +type barIntersection = (string & number)[]; diff --git a/test/rules/array-type/generic/test.ts.fix b/test/rules/array-type/generic/test.ts.fix index fd723976ebb..2ff563dadcd 100644 --- a/test/rules/array-type/generic/test.ts.fix +++ b/test/rules/array-type/generic/test.ts.fix @@ -27,3 +27,12 @@ function barFunction(bar: Array>) { function bazFunction(baz: Arr>) { return baz.map(e => e.baz); } + +let fooVar: Array<(c: number) => number>; +let barVar: Array<(c: number) => number>; + +type fooUnion = Array; +type barUnion = Array; + +type fooIntersection = Array; +type barIntersection = Array; diff --git a/test/rules/array-type/generic/test.ts.lint b/test/rules/array-type/generic/test.ts.lint index 1397ceaedd2..ea17df64b82 100644 --- a/test/rules/array-type/generic/test.ts.lint +++ b/test/rules/array-type/generic/test.ts.lint @@ -34,3 +34,15 @@ function barFunction(bar: ArrayClass[]) { function bazFunction(baz: Arr>) { return baz.map(e => e.baz); } + +let fooVar: Array<(c: number) => number>; +let barVar: ((c: number) => number)[]; + ~~~~~~~~~~~~~~~~~~~~~~~~~ [Array type using 'T[]' is forbidden. Use 'Array' instead.] + +type fooUnion = Array; +type barUnion = (string|number|boolean)[]; + ~~~~~~~~~~~~~~~~~~~~~~~~~ [Array type using 'T[]' is forbidden. Use 'Array' instead.] + +type fooIntersection = Array; +type barIntersection = (string & number)[]; + ~~~~~~~~~~~~~~~~~~~ [Array type using 'T[]' is forbidden. Use 'Array' instead.]