Skip to content

Commit

Permalink
fix: account for bottom and top types in incompleteTypes unions (#1064)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaKGoldberg authored Nov 9, 2022
1 parent 4e84eb7 commit 60fee8c
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 13 deletions.
5 changes: 3 additions & 2 deletions src/mutations/creators.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { TextInsertMutation, TextSwapMutation } from "automutate";
import * as tsutils from "tsutils";
import * as ts from "typescript";

import { FileMutationsRequest } from "../mutators/fileMutator";
Expand Down Expand Up @@ -36,8 +37,8 @@ export const createTypeAdditionMutation = (
return undefined;
}

// If the original type was just something like Function or Object, replace it entirely
if (isKnownGlobalBaseType(declaredType)) {
// If the original type was a bottom type or just something like Function or Object, replace it entirely
if (tsutils.isTypeFlagSet(declaredType, ts.TypeFlags.Never | ts.TypeFlags.Unknown) || isKnownGlobalBaseType(declaredType)) {
return {
insertion: ` ${newTypeAlias}`,
range: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const visitPropertyDeclaration = (node: ts.PropertyDeclaration, request: FileMut
// Collect types later assigned to the property, and types initially declared by or inferred on the property
const assignedTypes = collectPropertyAssignedTypes(node, request);
const declaredType = getTypeAtLocationIfNotError(request, node);
if (declaredType === undefined) {
if (declaredType === undefined || tsutils.isTypeFlagSet(declaredType, ts.TypeFlags.Any)) {
return undefined;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,32 +29,53 @@
method() {
this.member = {
key: true,
}
};
}
}

class WithIncompleteObjectProperty {
member: string | { key: boolean; };

method() {
this.member = '';
this.member = "";
this.member = {
key: true,
}
};
}
}

class WithIncompleteNestedObjectProperty {
member: string | { middle: { deepKey: boolean; }; middleKey: number; };

method() {
this.member = '';
this.member = "";
this.member = {
middle: {
deepKey: true,
},
middleKey: 0,
}
};
}
}

class WithAny {
property: any;
}

const withAny = new WithAny();
withAny.property = "";

class WithUnknown {
property: string;
}

const withUnknown = new WithUnknown();
withUnknown.property = "";

class WithNever {
property: string;
}

const withNever = new WithNever();
withNever.property = "";
})();
Original file line number Diff line number Diff line change
Expand Up @@ -29,32 +29,53 @@
method() {
this.member = {
key: true,
}
};
}
}

class WithIncompleteObjectProperty {
member: string;

method() {
this.member = '';
this.member = "";
this.member = {
key: true,
}
};
}
}

class WithIncompleteNestedObjectProperty {
member: string;

method() {
this.member = '';
this.member = "";
this.member = {
middle: {
deepKey: true,
},
middleKey: 0,
}
};
}
}

class WithAny {
property: any;
}

const withAny = new WithAny();
withAny.property = "";

class WithUnknown {
property: unknown;
}

const withUnknown = new WithUnknown();
withUnknown.property = "";

class WithNever {
property: never;
}

const withNever = new WithNever();
withNever.property = "";
})();

0 comments on commit 60fee8c

Please sign in to comment.