Skip to content

Commit

Permalink
fix: handle undefined typeParameters (#1495)
Browse files Browse the repository at this point in the history
<!-- 👋 Hi, thanks for sending a PR to TypeStat! 💖.
Please fill out all fields below and make sure each item is true and [x]
checked.
Otherwise we may not be able to review your PR. -->

## PR Checklist

- [x] Addresses an existing open issue: fixes #1427 
- [x] That issue was marked as [`status: accepting
prs`](https://github.com/JoshuaKGoldberg/TypeStat/issues?q=is%3Aopen+is%3Aissue+label%3A%22status%3A+accepting+prs%22)
- [x] Steps in
[CONTRIBUTING.md](https://github.com/JoshuaKGoldberg/TypeStat/blob/main/.github/CONTRIBUTING.md)
were taken 🐙

## Overview

<!-- Description of what is changed and how the code change does that.
-->

`templatedDeclaration.typeParameters` was undefined in some situations,
which threw an error. Added new regression test category as this is not
really test about how the fixes work otherwise. Probably the test case
could be simplified, but I'm going with mindset that fixed bug is better
than none.

---------

Co-authored-by: rubiesonthesky <rubiesonthesky>
Co-authored-by: Josh Goldberg ✨ <[email protected]>
  • Loading branch information
rubiesonthesky and JoshuaKGoldberg authored Apr 1, 2024
1 parent e2ed0f5 commit 22c7ee3
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { FileMutationsRequest } from "../../../../shared/fileMutator.js";
import {
getIdentifyingTypeLiteralParent,
isNodeWithDefinedTypeArguments,
isNodeWithDefinedTypeParameters,
isNodeWithIdentifierName,
isNodeWithTypeParameters,
} from "../../../../shared/nodeTypes.js";
import { isTypeArgumentsType } from "../../../../shared/typeNodes.js";
import { getTypeAtLocationIfNotError } from "../../../../shared/types.js";
Expand Down Expand Up @@ -84,8 +84,9 @@ export const expandReferencesForGenericTypes = (
const templatedDeclaration = templatedDeclarationSymbol.valueDeclaration;
if (
templatedDeclaration === undefined ||
!isNodeWithDefinedTypeParameters(templatedDeclaration) ||
templatedParentInstantiation.typeArguments === undefined
!isNodeWithTypeParameters(templatedDeclaration) ||
templatedParentInstantiation.typeArguments === undefined ||
templatedDeclaration.typeParameters === undefined
) {
continue;
}
Expand Down
8 changes: 4 additions & 4 deletions src/shared/nodeTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ export type NodeWithDefinedTypeArguments = ts.Node & {

// TODO: make this a more specific type
// Will have to deal with instantiations (new Container<T>() { ... }) and declarations (class Container<T>() { ... }))
export type NodeWithDefinedTypeParameters = ts.Node & {
typeParameters: ts.NodeArray<ts.TypeNode>;
export type NodeWithTypeParameters = ts.Node & {
typeParameters: ts.NodeArray<ts.TypeNode> | undefined;
};

export const isNodeWithType = (
Expand All @@ -84,9 +84,9 @@ export const isNodeWithDefinedTypeArguments = (
return "typeArguments" in node;
};

export const isNodeWithDefinedTypeParameters = (
export const isNodeWithTypeParameters = (
node: ts.Node,
): node is NodeWithDefinedTypeParameters => {
): node is NodeWithTypeParameters => {
return "typeParameters" in node;
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
(function () {
interface Shape {
value?: unknown;
}

[].reduce<Shape>(() => ({}), {});
})();
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
(function () {
interface Shape {
value?: unknown;
}

[].reduce<Shape>(() => ({}), {});
})();
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"files": ["actual.ts"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"fixes": {
"incompleteTypes": true
}
}

0 comments on commit 22c7ee3

Please sign in to comment.