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

fix: handle undefined typeParameters #1495

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ export const expandReferencesForGenericTypes = (
if (
templatedDeclaration === undefined ||
!isNodeWithDefinedTypeParameters(templatedDeclaration) ||
templatedParentInstantiation.typeArguments === undefined
templatedParentInstantiation.typeArguments === undefined ||
templatedDeclaration.typeParameters === undefined
) {
continue;
}
Expand Down
2 changes: 1 addition & 1 deletion src/shared/nodeTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ 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 & {
rubiesonthesky marked this conversation as resolved.
Show resolved Hide resolved
typeParameters: ts.NodeArray<ts.TypeNode>;
typeParameters: ts.NodeArray<ts.TypeNode> | undefined;
};

export const isNodeWithType = (
Expand Down
80 changes: 80 additions & 0 deletions test/cases/regresssionTests/1427/expected.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import ts from "typescript";

(function () {
interface FunctionCallType {
parameters?: (ts.Type | undefined)[];
returnValue?: ts.Type;
}
interface CombinedFunctionType {
parameters: ts.Type[][];
returnValue?: ts.Type[];
}

const functionCallTypes: FunctionCallType[] = [];
const combinedType = functionCallTypes.reduce<CombinedFunctionType>(
(accumulator, functionCallType) => {
return {
parameters: combineParameters(
undefined,
accumulator.parameters,
functionCallType.parameters,
),
returnValue: collectOptionals(accumulator.returnValue, [
functionCallType.returnValue,
]).filter(isNotUndefined),
};
},
{
parameters: [],
returnValue: [],
},
);

const combineParameters = (
request: unknown,
previous: ts.Type[][],
next: (ts.Type | undefined)[] | undefined,
) => {
if (next === undefined) {
return previous;
}

const combined: ts.Type[][] = [];
let i: number;

for (i = 0; i < previous.length; i += 1) {
combined.push([...previous[i]]);

if (i < next.length && next[i] !== undefined) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
combined[i].push(next[i]!);
}
}

for (i; i < next.length; i += 1) {
if (next[i] !== undefined) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
combined.push([next[i]!]);
}
}

return combined;
};

const collectOptionals = <T>(
...arrays: (readonly T[] | undefined)[]
): T[] => {
const results: T[] = [];

for (const array of arrays) {
if (array !== undefined) {
results.push(...array);
}
}

return results;
};

const isNotUndefined = <T>(item: T | undefined): item is T =>
item !== undefined;
})();
80 changes: 80 additions & 0 deletions test/cases/regresssionTests/1427/original.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import ts from "typescript";

(function () {
interface FunctionCallType {
parameters?: (ts.Type | undefined)[];
returnValue?: ts.Type;
}
interface CombinedFunctionType {
parameters: ts.Type[][];
returnValue?: ts.Type[];
}

const functionCallTypes: FunctionCallType[] = [];
const combinedType = functionCallTypes.reduce<CombinedFunctionType>(
(accumulator, functionCallType) => {
return {
parameters: combineParameters(
undefined,
accumulator.parameters,
functionCallType.parameters,
),
returnValue: collectOptionals(accumulator.returnValue, [
functionCallType.returnValue,
]).filter(isNotUndefined),
};
},
{
parameters: [],
returnValue: [],
},
);

const combineParameters = (
request: unknown,
previous: ts.Type[][],
next: (ts.Type | undefined)[] | undefined,
) => {
if (next === undefined) {
return previous;
}

const combined: ts.Type[][] = [];
let i: number;

for (i = 0; i < previous.length; i += 1) {
combined.push([...previous[i]]);

if (i < next.length && next[i] !== undefined) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
combined[i].push(next[i]!);
}
}

for (i; i < next.length; i += 1) {
if (next[i] !== undefined) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
combined.push([next[i]!]);
}
}

return combined;
};

const collectOptionals = <T>(
...arrays: (readonly T[] | undefined)[]
): T[] => {
const results: T[] = [];

for (const array of arrays) {
if (array !== undefined) {
results.push(...array);
}
}

return results;
};

const isNotUndefined = <T>(item: T | undefined): item is T =>
item !== undefined;
})();
rubiesonthesky marked this conversation as resolved.
Show resolved Hide resolved
3 changes: 3 additions & 0 deletions test/cases/regresssionTests/1427/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"files": ["actual.ts"]
}
5 changes: 5 additions & 0 deletions test/cases/regresssionTests/1427/typestat.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"fixes": {
"incompleteTypes": true
}
}
Loading