Skip to content

Commit

Permalink
Fixed node nullability and custom mutators
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaKGoldberg committed Mar 24, 2024
1 parent fc69ef6 commit a2d90bf
Show file tree
Hide file tree
Showing 26 changed files with 242 additions and 173 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ const expressionRefersToOriginalType = (
// This can go wrong easily: e.g. with multiple type arguments that have intermixed usages
if (
isTypeArgumentsType(expressionNodeType) &&
expressionNodeType.typeArguments.includes(originalType)
expressionNodeType.typeArguments?.includes(originalType)

Check failure on line 271 in src/mutators/builtIn/fixIncompleteTypes/fixIncompleteInterfaceOrTypeLiteralGenerics/collectGenericNodeReferences.ts

View workflow job for this annotation

GitHub Actions / lint

Unnecessary optional chain on a non-nullish value
) {
return true;
}
Expand Down
3 changes: 0 additions & 3 deletions src/runtime/providers/createCoreMutationsProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ export const createCoreMutationsProvider = (
const enabledFixes = Object.keys(options.fixes).filter(
(key) => options.fixes[key as keyof typeof options.fixes],
);
if (!enabledFixes.length) {
return undefined;
}

const fileNamesAndServicesCache = createFileNamesAndServicesCache(options);
const waveTracker = new WaveTracker();
Expand Down
2 changes: 1 addition & 1 deletion src/shared/typeNodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export type TypeWithTypeArguments = ts.Type & {
export const isTypeArgumentsType = (
type: ts.Type,
): type is TypeWithTypeArguments => {
return "typeArguments" in type;
return "typeArguments" in type && !!type.typeArguments;
};

export type TypeWithOptionalTypeArguments = ts.Type & {
Expand Down
2 changes: 1 addition & 1 deletion test/cases/custom mutators/empty/expected.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(function() {
(function () {
console.log("Hello, world!");

function ignoreChanges(): string {
Expand Down
2 changes: 1 addition & 1 deletion test/cases/custom mutators/one/actual.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(function () {
/* foo */ (function () {
console.log("Hello, world!");

function ignoreChanges(): string {
Expand Down
2 changes: 1 addition & 1 deletion test/cases/custom mutators/one/expected.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* foo */ (function() {
/* foo */ (function () {
console.log("Hello, world!");

function ignoreChanges(): string {
Expand Down
2 changes: 1 addition & 1 deletion test/cases/custom mutators/two/actual.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(function () {
/* 2 */ /* 1 */ (function () {
console.log("Hello, world!");

function ignoreChanges(): string {
Expand Down
2 changes: 1 addition & 1 deletion test/cases/custom mutators/two/expected.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* 2 */ /* 1 */ (function() {
/* 2 */ /* 1 */ (function () {
console.log("Hello, world!");

function ignoreChanges(): string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,5 @@ private _withGetterAndSetter: string;
get withGetterAndSetter() {
return this._withGetterAndSetter;
}

}
})();
})();
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,5 @@ private _withGetterAndSetter: string;
get withGetterAndSetter() {
return this._withGetterAndSetter;
}

}
})();
})();
71 changes: 43 additions & 28 deletions test/cases/fixes/noInferableTypes/parameters/actual.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,60 @@
(function () {
// Inferable literals
const inferableBoolean = (input = false) => { };
const inferableNull = (input = null) => { };
const inferableNumber = (input = 0) => { };
const inferableRegExp = (input = /./) => { };
const inferableString = (input = "") => { };
const inferableUndefined = (input = undefined) => { };
const inferableBoolean = (input = false) => {};
const inferableNull = (input = null) => {};
const inferableNumber = (input = 0) => {};
const inferableRegExp = (input = /./) => {};
const inferableString = (input = "") => {};
const inferableUndefined = (input = undefined) => {};

// Inferable arrays
const inferableBooleans = (input = [false]) => { };
const inferableNulls = (input = [null]) => { };
const inferableNumbers = (input = [0]) => { };
const inferableRegExps = (input = [/./]) => { };
const inferableStrings = (input = [""]) => { };
const inferableUndefineds = (input = [undefined]) => { };
const inferableBooleans = (input = [false]) => {};
const inferableNulls = (input = [null]) => {};
const inferableNumbers = (input = [0]) => {};
const inferableRegExps = (input = [/./]) => {};
const inferableStrings = (input = [""]) => {};
const inferableUndefineds = (input = [undefined]) => {};

// Inferable multi-type arrays
const inferableNullOrStrings = (input = [null, ""]) => { };
const inferableNumberOrRegExps = (input = [0, /./]) => { };
const inferableNullOrStrings = (input = [null, ""]) => {};
const inferableNumberOrRegExps = (
input = [0, /./],
) => {};

// Non-inferable multi-type arrays
const nonInferableNullOrStrings = (input: (null | string)[] = [null]) => { };
const nonInferableNumberOrRegExps = (input: (number | RegExp)[] = [0]) => { };

const nonInferableNullOrStrings = (input: (null | string)[] = [null]) => {};
const nonInferableNumberOrRegExps = (input: (number | RegExp)[] = [0]) => {};

// Class instances

class Parent { parent = true; }
class Child extends Parent { child = true; }
class Parent {
parent = true;
}
class Child extends Parent {
child = true;
}

// Inferable direct class instances
const takesParent = (parent = new Parent()) => { };
const takesChild = (child = new Child()) => { };
const takesParent = (parent = new Parent()) => {};
const takesChild = (child = new Child()) => {};

// Non-inferable direct class instances
const takesParentAsChild = (child: Parent = new Child()) => { };
const takesParentAsChild = (child: Parent = new Child()) => {};

// Non-inferable union class instances
const takesParentOrChildAsParent = (either: Parent | Child = new Parent()) => { };
const takesParentOrChildAsChild = (either: Parent | Child = new Child()) => { };
const takesParentOrUndefinedAsParent = (child: Parent | undefined = new Parent()) => { };
const takesParentOrUndefinedAsChild = (child: Parent | undefined = new Child()) => { };
const takesChildOrUndefinedAsChild = (child: Child | undefined = new Child()) => { };
})();
const takesParentOrChildAsParent = (
either: Parent | Child = new Parent(),
) => {};
const takesParentOrChildAsChild = (
either: Parent | Child = new Child(),
) => {};
const takesParentOrUndefinedAsParent = (
child: Parent | undefined = new Parent(),
) => {};
const takesParentOrUndefinedAsChild = (
child: Parent | undefined = new Child(),
) => {};
const takesChildOrUndefinedAsChild = (
child: Child | undefined = new Child(),
) => {};
})();
71 changes: 43 additions & 28 deletions test/cases/fixes/noInferableTypes/parameters/expected.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,60 @@
(function () {
// Inferable literals
const inferableBoolean = (input = false) => { };
const inferableNull = (input = null) => { };
const inferableNumber = (input = 0) => { };
const inferableRegExp = (input = /./) => { };
const inferableString = (input = "") => { };
const inferableUndefined = (input = undefined) => { };
const inferableBoolean = (input = false) => {};
const inferableNull = (input = null) => {};
const inferableNumber = (input = 0) => {};
const inferableRegExp = (input = /./) => {};
const inferableString = (input = "") => {};
const inferableUndefined = (input = undefined) => {};

// Inferable arrays
const inferableBooleans = (input = [false]) => { };
const inferableNulls = (input = [null]) => { };
const inferableNumbers = (input = [0]) => { };
const inferableRegExps = (input = [/./]) => { };
const inferableStrings = (input = [""]) => { };
const inferableUndefineds = (input = [undefined]) => { };
const inferableBooleans = (input = [false]) => {};
const inferableNulls = (input = [null]) => {};
const inferableNumbers = (input = [0]) => {};
const inferableRegExps = (input = [/./]) => {};
const inferableStrings = (input = [""]) => {};
const inferableUndefineds = (input = [undefined]) => {};

// Inferable multi-type arrays
const inferableNullOrStrings = (input = [null, ""]) => { };
const inferableNumberOrRegExps = (input = [0, /./]) => { };
const inferableNullOrStrings = (input = [null, ""]) => {};
const inferableNumberOrRegExps = (
input = [0, /./],
) => {};

// Non-inferable multi-type arrays
const nonInferableNullOrStrings = (input: (null | string)[] = [null]) => { };
const nonInferableNumberOrRegExps = (input: (number | RegExp)[] = [0]) => { };

const nonInferableNullOrStrings = (input: (null | string)[] = [null]) => {};
const nonInferableNumberOrRegExps = (input: (number | RegExp)[] = [0]) => {};

// Class instances

class Parent { parent = true; }
class Child extends Parent { child = true; }
class Parent {
parent = true;
}
class Child extends Parent {
child = true;
}

// Inferable direct class instances
const takesParent = (parent = new Parent()) => { };
const takesChild = (child = new Child()) => { };
const takesParent = (parent = new Parent()) => {};
const takesChild = (child = new Child()) => {};

// Non-inferable direct class instances
const takesParentAsChild = (child: Parent = new Child()) => { };
const takesParentAsChild = (child: Parent = new Child()) => {};

// Non-inferable union class instances
const takesParentOrChildAsParent = (either: Parent | Child = new Parent()) => { };
const takesParentOrChildAsChild = (either: Parent | Child = new Child()) => { };
const takesParentOrUndefinedAsParent = (child: Parent | undefined = new Parent()) => { };
const takesParentOrUndefinedAsChild = (child: Parent | undefined = new Child()) => { };
const takesChildOrUndefinedAsChild = (child: Child | undefined = new Child()) => { };
})();
const takesParentOrChildAsParent = (
either: Parent | Child = new Parent(),
) => {};
const takesParentOrChildAsChild = (
either: Parent | Child = new Child(),
) => {};
const takesParentOrUndefinedAsParent = (
child: Parent | undefined = new Parent(),
) => {};
const takesParentOrUndefinedAsChild = (
child: Parent | undefined = new Child(),
) => {};
const takesChildOrUndefinedAsChild = (
child: Child | undefined = new Child(),
) => {};
})();
11 changes: 7 additions & 4 deletions test/cases/fixes/noInferableTypes/propertyDeclarations/actual.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
(function () {
class Parent { parent = true; }
class Child extends Parent { child = true; }
class Parent {
parent = true;
}
class Child extends Parent {
child = true;
}

class ContainsInferables {
// Inferable literals
Expand All @@ -27,7 +31,6 @@
public nonInferableNullOrStrings: (null | string)[] = [null];
public nonInferableNumberOrRegExps: (number | RegExp)[] = [0];


// Class instances

// Inferable direct class instances
Expand All @@ -44,4 +47,4 @@
public takesParentOrUndefinedAsChild: Parent | undefined = new Child();
public takesChildOrUndefinedAsChild: Child | undefined = new Child();
}
})();
})();
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
(function () {
class Parent { parent = true; }
class Child extends Parent { child = true; }
class Parent {
parent = true;
}
class Child extends Parent {
child = true;
}

class ContainsInferables {
// Inferable literals
Expand All @@ -27,7 +31,6 @@
public nonInferableNullOrStrings: (null | string)[] = [null];
public nonInferableNumberOrRegExps: (number | RegExp)[] = [0];


// Class instances

// Inferable direct class instances
Expand All @@ -44,4 +47,4 @@
public takesParentOrUndefinedAsChild: Parent | undefined = new Child();
public takesChildOrUndefinedAsChild: Child | undefined = new Child();
}
})();
})();
11 changes: 7 additions & 4 deletions test/cases/fixes/noInferableTypes/variableDeclarations/actual.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
(function () {
class Parent { parent = true; }
class Child extends Parent { child = true; }
class Parent {
parent = true;
}
class Child extends Parent {
child = true;
}

// Inferable const literals
const constInferableBoolean = false;
Expand Down Expand Up @@ -50,7 +54,6 @@
let letNonInferableNullOrStrings: (null | string)[] = [null];
let letNonInferableNumberOrRegExps: (number | RegExp)[] = [0];


// Class instances

// Inferable const direct class instances
Expand Down Expand Up @@ -80,4 +83,4 @@
let letTakesParentOrUndefinedAsParent: Parent | undefined = new Parent();
let letTakesParentOrUndefinedAsChild: Parent | undefined = new Child();
let letTakesChildOrUndefinedAsChild: Child | undefined = new Child();
})();
})();
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
(function () {
class Parent { parent = true; }
class Child extends Parent { child = true; }
class Parent {
parent = true;
}
class Child extends Parent {
child = true;
}

// Inferable const literals
const constInferableBoolean = false;
Expand Down Expand Up @@ -50,7 +54,6 @@
let letNonInferableNullOrStrings: (null | string)[] = [null];
let letNonInferableNumberOrRegExps: (number | RegExp)[] = [0];


// Class instances

// Inferable const direct class instances
Expand Down Expand Up @@ -80,4 +83,4 @@
let letTakesParentOrUndefinedAsParent: Parent | undefined = new Parent();
let letTakesParentOrUndefinedAsChild: Parent | undefined = new Child();
let letTakesChildOrUndefinedAsChild: Child | undefined = new Child();
})();
})();
Loading

0 comments on commit a2d90bf

Please sign in to comment.