Skip to content

Commit

Permalink
Fixes #124 - Implemented "checkIsReturnTypeOrNestedWithIn" more genet…
Browse files Browse the repository at this point in the history
…ically. (#125)

* implemented "checkIsReturnTypeOrNestedWithIn" more genetically; it should now find nested types no matter how deeply nested or what they are nested within.
  • Loading branch information
RebeccaStevens authored Mar 11, 2019
1 parent 69b2213 commit 63f1e0d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 18 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

* Fixed rule readonly-array with option ignore-return-type not checking within union, intersection and conditional types. This fix should now catch all return types that contain a nested array. See [#124](https://github.com/jonaskello/tslint-immutable/issues/124). See PR [#125](https://github.com/jonaskello/tslint-immutable/pull/125)

## [v5.3.2] - 2019-03-07

### Fixed
Expand Down
24 changes: 6 additions & 18 deletions src/readonlyArrayRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,24 +135,12 @@ export function checkImplicitType(
return [];
}

function checkIsReturnTypeOrNestedWithIn(
node: ts.TypeReferenceNode | ts.ArrayTypeNode
): boolean {
const getRootTypeReferenceNode = (
typeNode: ts.TypeReferenceNode | ts.ArrayTypeNode | ts.TupleTypeNode
): ts.TypeReferenceNode | ts.ArrayTypeNode | ts.TupleTypeNode =>
utils.isTypeReferenceNode(typeNode.parent) ||
utils.isTupleTypeNode(typeNode.parent)
? getRootTypeReferenceNode(typeNode.parent)
: typeNode;

const rootTypeReferenceNode = getRootTypeReferenceNode(node);

return (
rootTypeReferenceNode.parent &&
isFunctionLikeDeclaration(rootTypeReferenceNode.parent) &&
rootTypeReferenceNode === rootTypeReferenceNode.parent.type
);
function checkIsReturnTypeOrNestedWithIn(node: ts.Node): boolean {
return node.parent
? isFunctionLikeDeclaration(node.parent) && node === node.parent.type
? true
: checkIsReturnTypeOrNestedWithIn(node.parent)
: false;
}

function isUntypedAndHasArrayLiteralExpressionInitializer(
Expand Down
18 changes: 18 additions & 0 deletions test/rules/readonly-array/ignore-return-type/function.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,21 @@ function foo(...numbers: ReadOnlyArray<number>): [number, Array<number>, number]

function foo(...numbers: ReadOnlyArray<number>): [number, number[], number] {
}


// Union Types

function foo(...numbers: ReadOnlyArray<number>): { a: Array<number> } | { b: string[] } {
}


// Intersection Types

function foo(...numbers: ReadOnlyArray<number>): { a: Array<number> } & { b: string[] } {
}


// Conditional Types

function foo<T>(x: T): T extends Array<number> ? string : number[] {
}

0 comments on commit 63f1e0d

Please sign in to comment.