Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion packages/core/src/utils/is-react-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export function isReactAPI(api: string): isReactAPI.ReturnType {
{
if (node == null) return false;
const getText = (n: TSESTree.Node) => context.sourceCode.getText(n);
const name = AST.stringify(node, getText);
const name = AST.toString(node, getText);
if (name === api) return true;
if (name.substring(name.indexOf(".") + 1) === api) return true;
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,15 +221,15 @@ export function useNoDirectSetStateInUseEffect<Ctx extends RuleContext>(
const setStateCalls = getSetStateCalls(name, context.sourceCode.getScope(callee));
for (const setStateCall of setStateCalls) {
onViolation(context, setStateCall, {
name: AST.stringify(setStateCall, (n) => context.sourceCode.getText(n)),
name: AST.toString(setStateCall, (n) => context.sourceCode.getText(n)),
});
}
}
for (const id of setupFunctionIdentifiers) {
const setStateCalls = getSetStateCalls(id.name, context.sourceCode.getScope(id));
for (const setStateCall of setStateCalls) {
onViolation(context, setStateCall, {
name: AST.stringify(setStateCall, (n) => context.sourceCode.getText(n)),
name: AST.toString(setStateCall, (n) => context.sourceCode.getText(n)),
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export function create(context: RuleContext<MessageID, []>): RuleListener {
if (!AST.isFunction(iter)) return;
const arg0 = call?.arguments[0];
if (call == null || arg0 == null) return;
if (AST.getEcmaExpression(arg0) !== iter) {
if (AST.getJSExpression(arg0) !== iter) {
return;
}
keyedEntries.set(call, {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { TSESTreeClass } from "../types";
import type { TSESTreeClass } from "../ast-node";

import path from "node:path";
import { parseForESLint } from "@typescript-eslint/parser";
Expand All @@ -7,7 +7,7 @@ import { simpleTraverse } from "@typescript-eslint/typescript-estree";

import { describe, expect, it } from "vitest";
import { getFixturesRootDir } from "../../../../../test";
import { getClassIdentifier } from "../get-class-identifier";
import { getClassIdentifier } from "../ast-class-id";

function parse(code: string) {
return parseForESLint(code, {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { TSESTreeFunction } from "../types";
import type { TSESTreeFunction } from "../ast-node";

import path from "node:path";
import { parseForESLint } from "@typescript-eslint/parser";
Expand All @@ -7,8 +7,8 @@ import { simpleTraverse } from "@typescript-eslint/typescript-estree";

import { describe, expect, it } from "vitest";
import { getFixturesRootDir } from "../../../../../test";
import { getFunctionIdentifier } from "../get-function-identifier";
import { isFunction } from "../is";
import { getFunctionIdentifier } from "../ast-function-id";
import { isFunction } from "../ast-is";

function parse(code: string) {
return parseForESLint(code, {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { TSESTreeFunction } from "../types";
import type { TSESTreeFunction } from "../ast-node";

import path from "node:path";
import { parseForESLint } from "@typescript-eslint/parser";
Expand All @@ -8,8 +8,8 @@ import tsx from "dedent";

import { describe, expect, it } from "vitest";
import { getFixturesRootDir } from "../../../../../test";
import { getNestedReturnStatements } from "../get-nested-return-statements";
import { isFunction } from "../is";
import { isFunction } from "../ast-is";
import { getNestedReturnStatements } from "../ast-return-statement";

function parse(code: string) {
return parseForESLint(code, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,11 @@ export function isArrayFromCall(node: TSESTree.Node, loose = true): node is TSES
const name = node.callee.property.name;
return name === "from" || (loose && name.startsWith("from"));
}

export function isArrayMapCall(node: TSESTree.Node, loose = true): node is TSESTree.CallExpression {
if (node.type !== T.CallExpression) return false;
if (node.callee.type !== T.MemberExpression) return false;
if (node.callee.property.type !== T.Identifier) return false;
const name = node.callee.property.name;
return name === "map" || (loose && name.endsWith("Map"));
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { TSESTree } from "@typescript-eslint/types";
import type { TSESTreeClass } from "./types";
import type { TSESTreeClass } from "./ast-node";
import { _ } from "@eslint-react/eff";

import { AST_NODE_TYPES as T } from "@typescript-eslint/types";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,27 @@
import type { TSESTree } from "@typescript-eslint/types";
import { AST_NODE_TYPES as T } from "@typescript-eslint/typescript-estree";
import { ASTUtils } from "@typescript-eslint/utils";
import type { TSESTreeTypeExpression } from "./ast-node";

import { AST_NODE_TYPES as T } from "@typescript-eslint/types";
import { is, isTypeExpression } from "./ast-is";

/**
* Recursively get the inner expression until it's not a TypeExpression
* @param node - The node to get the expression from
* @returns The inner expression
*/
export function getJSExpression(node: TSESTree.Node): Exclude<
TSESTree.Node,
TSESTreeTypeExpression
> {
if (isTypeExpression(node)) {
return getJSExpression(node.expression);
}
return node;
}

export function isThisExpression(node: TSESTree.Expression) {
return getJSExpression(node).type === T.ThisExpression;
}

/**
* Get all nested expressions of type T in an expression like node
Expand All @@ -10,7 +31,7 @@ import { ASTUtils } from "@typescript-eslint/utils";
*/
// dprint-ignore
export function getNestedExpressionsOfType<TNodeType extends T>(type: TNodeType): (node: TSESTree.Node) => Extract<TSESTree.Node, { type: TNodeType }>[] {
const isNodeOfType = ASTUtils.isNodeOfType(type);
const isNodeOfType = is(type);
return function(node) {
const boundGetNestedExpressionsOfType = getNestedExpressionsOfType(type);
const expressions: Extract<TSESTree.Node, { type: TNodeType }>[] = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
*/

import type { TSESTree } from "@typescript-eslint/types";
import type { TSESTreeFunction } from "./types";
import type { TSESTreeFunction } from "./ast-node";
import { _ } from "@eslint-react/eff";

import { AST_NODE_TYPES as T } from "@typescript-eslint/types";
import { isMethodOrProperty, isTypeAssertionExpression } from "./is";
import { isMethodOrProperty, isTypeAssertionExpression } from "./ast-is";

export function getFunctionIdentifier(node: TSESTree.Expression | TSESTreeFunction): TSESTree.Identifier | _ {
switch (true) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { TSESTree } from "@typescript-eslint/types";
import type { TSESTreeFunction } from "./types";
import type { TSESTreeFunction } from "./ast-node";
import { _ } from "@eslint-react/eff";

import { AST_NODE_TYPES as T } from "@typescript-eslint/types";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { TSESTreeFunction } from "./types";
import type { TSESTreeFunction } from "./ast-node";

import { AST_NODE_TYPES as T } from "@typescript-eslint/types";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { TSESTree } from "@typescript-eslint/types";

Check warning on line 1 in packages/utilities/ast/src/ast-is.ts

View workflow job for this annotation

GitHub Actions / check

'TSESTree' is defined but never used

Check warning on line 1 in packages/utilities/ast/src/ast-is.ts

View workflow job for this annotation

GitHub Actions / Publish

'TSESTree' is defined but never used
import { or } from "@eslint-react/eff";
import { AST_NODE_TYPES as T } from "@typescript-eslint/types";
import { ASTUtils } from "@typescript-eslint/utils";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import type { TSESTree } from "@typescript-eslint/types";
import { AST_NODE_TYPES as T } from "@typescript-eslint/types";
import { isOneOf } from "./is";
import { isMultiLine } from "./is-multi-line";
import { isOneOf } from "./ast-is";

/**
* Check if a node is multiline
* @param node The AST node to check
* @returns `true` if the node is multiline
*/
export function isMultiLine(node: TSESTree.Node) {
return node.loc.start.line !== node.loc.end.line;
}

/**
* Check if a node is a line break
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable local/prefer-eqeq-nullish-comparison */
import type { TSESTree } from "@typescript-eslint/types";
import { AST_NODE_TYPES as T } from "@typescript-eslint/types";

Expand All @@ -22,6 +21,7 @@ export function isLiteral(node: TSESTree.Node, type?: LiteralType) {
case "boolean":
return typeof node.value === "boolean";
case "null":
// eslint-disable-next-line local/prefer-eqeq-nullish-comparison
return node.value === null;
case "number":
return typeof node.value === "number";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
import type { _ } from "@eslint-react/eff";
import type { TSESTree } from "@typescript-eslint/types";
import { AST_NODE_TYPES as T } from "@typescript-eslint/types";
import { isLiteral } from "./is-literal";
import { isProcessEnvNodeEnv } from "./is-process-env-node-env";
import { isLiteral } from "./ast-literal-is";

/**
* Check if the given node is a member expression that accesses `process.env.NODE_ENV`
* @param node The AST node
* @returns True if the node is a member expression that accesses `process.env.NODE_ENV`, false otherwise
*/
export function isProcessEnvNodeEnv(node: TSESTree.Node | null | _): node is TSESTree.MemberExpression {
return node != null
&& node.type === T.MemberExpression
&& node.object.type === T.MemberExpression
&& node.object.object.type === T.Identifier
&& node.object.object.name === "process"
&& node.object.property.type === T.Identifier
&& node.object.property.name === "env"
&& node.property.type === T.Identifier
&& node.property.name === "NODE_ENV";
}

/**
* Check if the given node is a binary expression that compares `process.env.NODE_ENV` with a string literal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { _ } from "@eslint-react/eff";
import { AST_NODE_TYPES as T, type TSESTree } from "@typescript-eslint/types";

import { getEcmaExpression } from "./get-ecma-expression";
import { isTypeExpression } from "./is";
import { getJSExpression } from "./ast-expression";
import { isTypeExpression } from "./ast-is";

export function getPropertyName(node: TSESTree.Node): string | _ {
if (isTypeExpression(node)) {
return getPropertyName(getEcmaExpression(node));
return getPropertyName(getJSExpression(node));
}
if (node.type === T.Identifier || node.type === T.PrivateIdentifier) {
return node.name;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import type { TSESTree } from "@typescript-eslint/types";
import { AST_NODE_TYPES as T } from "@typescript-eslint/types";
import { simpleTraverse } from "@typescript-eslint/typescript-estree";

import { findParentNode } from "./find-parent-node";
import { isFunction } from "./is";
import { findParentNode } from "./ast-hierarchy";
import { isFunction } from "./ast-is";

/**
* Gets the nested return statements in the node that are within the same function
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ import type { TSESTree } from "@typescript-eslint/types";
import { AST_NODE_TYPES as T } from "@typescript-eslint/types";
import { delimiterCase, replace, toLowerCase } from "string-ts";

import { getLiteralValueType } from "./get-literal-value-type";
import { isJSX } from "./is";
import { isJSX } from "./ast-is";

function getLiteralValueType(input: bigint | boolean | null | number | string | symbol) {
// eslint-disable-next-line local/prefer-eqeq-nullish-comparison
if (input === null) return "null";
return typeof input;
}

/**
* Returns human readable node type for given AST node
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,25 @@ import { AST_NODE_TYPES as T } from "@typescript-eslint/types";
* @param getText A function that returns the text of the node in the source code
* @returns Human readable node name
*/
export function stringify(node: TSESTree.Node, getText: (node: TSESTree.Node) => string): string {
export function toString(node: TSESTree.Node, getText: (node: TSESTree.Node) => string): string {
switch (node.type) {
case T.CallExpression:
return stringify(node.callee, getText);
return toString(node.callee, getText);
case T.Identifier:
case T.PrivateIdentifier:
return node.name;
case T.JSXIdentifier:
return `<${node.name}>`;
case T.JSXMemberExpression:
return `${stringify(node.object, getText)}.${stringify(node.property, getText)}`;
return `${toString(node.object, getText)}.${toString(node.property, getText)}`;
case T.JSXNamespacedName:
return `${node.namespace.name}:${node.name.name}`;
case T.JSXText:
return node.value;
case T.Literal:
return node.raw;
case T.MemberExpression:
return `${stringify(node.object, getText)}.${stringify(node.property, getText)}`;
return `${toString(node.object, getText)}.${toString(node.property, getText)}`;
default:
return getText(node);
}
Expand Down
19 changes: 0 additions & 19 deletions packages/utilities/ast/src/get-ecma-expression.ts

This file was deleted.

5 changes: 0 additions & 5 deletions packages/utilities/ast/src/get-literal-value-type.ts

This file was deleted.

44 changes: 19 additions & 25 deletions packages/utilities/ast/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
export * from "./find-parent-node";
export * from "./function-init-path";
export * from "./get-array-method-callback-index-param-position";
export * from "./get-class-identifier";
export * from "./get-ecma-expression";
export * from "./get-function-identifier";
export * from "./get-literal-value-type";
export * from "./get-nested-expressions";
export * from "./get-nested-identifiers";
export * from "./get-nested-return-statements";
export * from "./get-property-name";
export * from "./is";
export * from "./is-array-from-call";
export * from "./is-array-map-call";
export * from "./is-empty-function";
export * from "./is-line-break";
export * from "./is-literal";
export * from "./is-multi-line";
export * from "./is-node-equal";
export * from "./is-process-env-node-env";
export * from "./is-process-env-node-env-compare";
export * from "./is-this-expression";
export * from "./stringify";
export * from "./to-readable-node-type";
export type * from "./types";
export * from "./ast-array-method";
export * from "./ast-array-method-callback";
export * from "./ast-class-id";
export * from "./ast-expression";
export * from "./ast-function-id";
export * from "./ast-function-init-path";
export * from "./ast-function-is";
export * from "./ast-hierarchy";
export * from "./ast-id";
export * from "./ast-is";
export * from "./ast-line";
export * from "./ast-literal-is";
export type * from "./ast-node";
export * from "./ast-node-equal";
export * from "./ast-process-env-node-env";
export * from "./ast-property-name";
export * from "./ast-return-statement";
export * from "./ast-to-readable-node-type";
export * from "./ast-to-string";
10 changes: 0 additions & 10 deletions packages/utilities/ast/src/is-array-map-call.ts

This file was deleted.

10 changes: 0 additions & 10 deletions packages/utilities/ast/src/is-multi-line.ts

This file was deleted.

Loading
Loading