Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #124 - Implemented "checkIsReturnTypeOrNestedWithIn" more genetically. #125

Merged
merged 2 commits into from
Mar 11, 2019
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
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[] {
}