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

Fix ArrayType to produce correct fixes #1533

Merged
merged 1 commit into from
Sep 1, 2016
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
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.]