Skip to content

Commit

Permalink
Fix ArrayType to produce correct fixes (palantir#1533)
Browse files Browse the repository at this point in the history
Fixes now parenthesize union, intersection and function types.
  • Loading branch information
ScottSWu authored and jkillian committed Sep 1, 2016
1 parent 9feb726 commit 7d4c1d6
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/rules/arrayTypeRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand All @@ -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));
Expand Down
9 changes: 9 additions & 0 deletions test/rules/array-type/array/test.ts.fix
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,12 @@ function barFunction(bar: ArrayClass<String>[]) {
function bazFunction(baz: Arr<ArrayClass<String>>) {
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)[];
12 changes: 12 additions & 0 deletions test/rules/array-type/array/test.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,15 @@ function barFunction(bar: ArrayClass<String>[]) {
function bazFunction(baz: Arr<ArrayClass<String>>) {
return baz.map(e => e.baz);
}

let fooVar: Array<(c: number) => number>;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [Array type using 'Array<T>' is forbidden. Use 'T[]' instead.]
let barVar: ((c: number) => number)[];

type fooUnion = Array<string|number|boolean>;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [Array type using 'Array<T>' is forbidden. Use 'T[]' instead.]
type barUnion = (string|number|boolean)[];

type fooIntersection = Array<string & number>;
~~~~~~~~~~~~~~~~~~~~~~ [Array type using 'Array<T>' is forbidden. Use 'T[]' instead.]
type barIntersection = (string & number)[];
9 changes: 9 additions & 0 deletions test/rules/array-type/generic/test.ts.fix
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,12 @@ function barFunction(bar: Array<ArrayClass<String>>) {
function bazFunction(baz: Arr<ArrayClass<String>>) {
return baz.map(e => e.baz);
}

let fooVar: Array<(c: number) => number>;
let barVar: Array<(c: number) => number>;

type fooUnion = Array<string|number|boolean>;
type barUnion = Array<string|number|boolean>;

type fooIntersection = Array<string & number>;
type barIntersection = Array<string & number>;
12 changes: 12 additions & 0 deletions test/rules/array-type/generic/test.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,15 @@ function barFunction(bar: ArrayClass<String>[]) {
function bazFunction(baz: Arr<ArrayClass<String>>) {
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<T>' instead.]

type fooUnion = Array<string|number|boolean>;
type barUnion = (string|number|boolean)[];
~~~~~~~~~~~~~~~~~~~~~~~~~ [Array type using 'T[]' is forbidden. Use 'Array<T>' instead.]

type fooIntersection = Array<string & number>;
type barIntersection = (string & number)[];
~~~~~~~~~~~~~~~~~~~ [Array type using 'T[]' is forbidden. Use 'Array<T>' instead.]

0 comments on commit 7d4c1d6

Please sign in to comment.