Skip to content

Commit 5ccdf99

Browse files
authored
refactor: improve ts expression unwrapping (#951)
1 parent f04e82a commit 5ccdf99

File tree

12 files changed

+51
-22
lines changed

12 files changed

+51
-22
lines changed

packages/plugins/eslint-plugin-react-x/src/rules/no-access-state-in-setstate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export const RULE_FEATURES = [
1717
export type MessageID = CamelCase<typeof RULE_NAME>;
1818

1919
function getName(node: TSESTree.Expression | TSESTree.PrivateIdentifier): string | _ {
20-
if (AST.isTypeExpression(node)) {
20+
if (AST.isTsOnlyExpression(node)) {
2121
return getName(node.expression);
2222
}
2323
if (node.type === T.Identifier || node.type === T.PrivateIdentifier) {

packages/plugins/eslint-plugin-react-x/src/rules/no-direct-mutation-state.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export const RULE_FEATURES = [
1717
export type MessageID = CamelCase<typeof RULE_NAME>;
1818

1919
function getName(node: TSESTree.Expression | TSESTree.PrivateIdentifier): string | _ {
20-
if (AST.isTypeExpression(node)) {
20+
if (AST.isTsOnlyExpression(node)) {
2121
return getName(node.expression);
2222
}
2323
if (node.type === T.Identifier || node.type === T.PrivateIdentifier) {

packages/plugins/eslint-plugin-react-x/src/rules/no-duplicate-key.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export default createRule<[], MessageID>({
7171
if (!AST.isFunction(iter)) return;
7272
const arg0 = call?.arguments[0];
7373
if (call == null || arg0 == null) return;
74-
if (AST.unwrapTypeExpression(arg0) !== iter) {
74+
if (AST.getEcmaExpression(arg0) !== iter) {
7575
return;
7676
}
7777
keyedEntries.set(call, {

packages/plugins/eslint-plugin-react-x/src/rules/no-unused-class-component-members.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ const LIFECYCLE_METHODS = new Set([
4242
// anywhere that a literal may be used as a key (e.g., member expressions,
4343
// method definitions, ObjectExpression property keys).
4444
function getName(node: TSESTree.Expression | TSESTree.PrivateIdentifier): string | _ {
45-
if (AST.isTypeExpression(node)) {
45+
if (AST.isTsOnlyExpression(node)) {
4646
return getName(node.expression);
4747
}
4848
if (node.type === T.Identifier || node.type === T.PrivateIdentifier) {

packages/plugins/eslint-plugin-react-x/src/rules/no-unused-state.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export const RULE_FEATURES = [
1818
export type MessageID = CamelCase<typeof RULE_NAME>;
1919

2020
function getName(node: TSESTree.Expression | TSESTree.PrivateIdentifier): string | _ {
21-
if (AST.isTypeExpression(node)) {
21+
if (AST.isTsOnlyExpression(node)) {
2222
return getName(node.expression);
2323
}
2424
if (node.type === T.Identifier || node.type === T.PrivateIdentifier) {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import type { TSESTree } from "@typescript-eslint/types";
2+
3+
import { isTsOnlyExpression } from "./is";
4+
import type { TSESTreeTSOnlyExpression } from "./types";
5+
6+
/**
7+
* Gets the ecma expression from a TS expression.
8+
* @param node The TS expression.
9+
* @returns The ecma expression within the TS expression.
10+
*/
11+
export function getEcmaExpression(node: TSESTree.Node): Exclude<
12+
TSESTree.Node,
13+
TSESTreeTSOnlyExpression
14+
> {
15+
if (isTsOnlyExpression(node)) {
16+
return getEcmaExpression(node.expression);
17+
}
18+
return node;
19+
}

packages/utilities/ast/src/get-function-identifier.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { _ } from "@eslint-react/eff";
1111
import type { TSESTree } from "@typescript-eslint/types";
1212
import { AST_NODE_TYPES as T } from "@typescript-eslint/types";
1313

14-
import { isOneOf, isTypeExpression } from "./is";
14+
import { isOneOf, isTsAssertionExpression } from "./is";
1515
import type { TSESTreeFunction } from "./types";
1616

1717
export function getFunctionIdentifier(node: TSESTree.Expression | TSESTreeFunction): TSESTree.Identifier | _ {
@@ -55,7 +55,7 @@ export function getFunctionIdentifier(node: TSESTree.Expression | TSESTreeFuncti
5555
// const MaybeComponent = (() => {})!;
5656
// const MaybeComponent = (() => {}) as FunctionComponent;
5757
// const MaybeComponent = (() => {}) satisfies FunctionComponent;
58-
case isTypeExpression(node.parent):
58+
case isTsAssertionExpression(node.parent):
5959
return getFunctionIdentifier(node.parent);
6060
}
6161
return _;

packages/utilities/ast/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export * from "./find-parent-node";
22
export * from "./function-init-path";
33
export * from "./get-class-identifier";
4+
export * from "./get-ecma-expression";
45
export * from "./get-function-identifier";
56
export * from "./get-literal-value-type";
67
export * from "./get-nested-call-expressions";
@@ -20,4 +21,3 @@ export * from "./is-this-expression";
2021
export * from "./to-readable-node-name";
2122
export * from "./to-readable-node-type";
2223
export type * from "./types";
23-
export * from "./unwrap-type-expression";
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import type { TSESTree } from "@typescript-eslint/types";
22
import { AST_NODE_TYPES as T } from "@typescript-eslint/types";
33

4-
import { isTypeExpression } from "./is";
4+
import { getEcmaExpression } from "./get-ecma-expression";
55

66
export function isThisExpression(node: TSESTree.Expression) {
7-
if (isTypeExpression(node)) return isThisExpression(node.expression);
8-
return node.type === T.ThisExpression;
7+
return getEcmaExpression(node).type === T.ThisExpression;
98
}

packages/utilities/ast/src/is.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,15 @@ export const isLeftHandSideExpressionType = isOneOf([
154154
T.TSTypeAssertion,
155155
]);
156156

157-
export const isTypeExpression = isOneOf([
157+
export const isTsOnlyExpression = isOneOf([
158+
T.TSAsExpression,
159+
T.TSTypeAssertion,
160+
T.TSNonNullExpression,
161+
T.TSSatisfiesExpression,
162+
T.TSInstantiationExpression,
163+
]);
164+
165+
export const isTsAssertionExpression = isOneOf([
158166
T.TSAsExpression,
159167
T.TSTypeAssertion,
160168
T.TSNonNullExpression,

0 commit comments

Comments
 (0)