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
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as AST from "@eslint-react/ast";
import * as ER from "@eslint-react/core";
import { type RuleContext, type RuleFeature } from "@eslint-react/kit";
import { ContextDetection, type RuleContext, type RuleFeature } from "@eslint-react/kit";
import { getSettingsFromContext } from "@eslint-react/shared";
import { AST_NODE_TYPES as T, type TSESTree } from "@typescript-eslint/types";
import type { RuleListener } from "@typescript-eslint/utils/ts-eslint";
Expand Down Expand Up @@ -71,6 +71,6 @@ export function create(context: RuleContext<MessageID, []>): RuleListener {

function isDevelopmentOnlyCheck(node: TSESTree.Node) {
if (node.type !== T.IfStatement) return false;
if (AST.isProcessEnvNodeEnvCompare(node.test, "!==", "production")) return true;
if (ContextDetection.isProcessEnvNodeEnvCompare(node.test, "!==", "production")) return true;
return false;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as AST from "@eslint-react/ast";
import * as ER from "@eslint-react/core";
import type { RuleContext, RuleFeature } from "@eslint-react/kit";
import { ContextDetection } from "@eslint-react/kit";
import type { TSESTree } from "@typescript-eslint/types";
import type { RuleListener } from "@typescript-eslint/utils/ts-eslint";
import type { CamelCase } from "string-ts";
Expand Down Expand Up @@ -65,7 +66,7 @@ export function create(context: RuleContext<MessageID, []>): RuleListener {
continue;
}
// Skip hooks that are in a vi mock callback
if (ER.isInViMockCallback(context, node)) {
if (AST.findParentNode(node, ContextDetection.isViMockCallback) != null) {
continue;
}
context.report({
Expand Down
1 change: 0 additions & 1 deletion packages/utilities/ast/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,5 @@ export * from "./ast-node-equal";
export * from "./ast-node-format";
export * from "./ast-node-is";
export type * from "./ast-node-types";
export * from "./ast-process-env";
export * from "./ast-property-name";
export * from "./ast-then";
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[**@eslint-react/kit**](../../../README.md)

***

[@eslint-react/kit](../../../README.md) / ContextDetection

# ContextDetection

## Functions

- [isProcessEnvNodeEnv](functions/isProcessEnvNodeEnv.md)
- [isProcessEnvNodeEnvCompare](functions/isProcessEnvNodeEnvCompare.md)
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[**@eslint-react/kit**](../../../../README.md)

***

[@eslint-react/kit](../../../../README.md) / [ContextDetection](../README.md) / isProcessEnvNodeEnv

# Function: isProcessEnvNodeEnv()

> **isProcessEnvNodeEnv**(`node`): `node is MemberExpression`

Check if the given node is a member expression that accesses `process.env.NODE_ENV`

## Parameters

### node

The AST node

`undefined` | `null` | `Node`

## Returns

`node is MemberExpression`

True if the node is a member expression that accesses `process.env.NODE_ENV`, false otherwise
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[**@eslint-react/kit**](../../../../README.md)

***

[@eslint-react/kit](../../../../README.md) / [ContextDetection](../README.md) / isProcessEnvNodeEnvCompare

# Function: isProcessEnvNodeEnvCompare()

> **isProcessEnvNodeEnvCompare**(`node`, `operator`, `value`): `node is BinaryExpression`

Check if the given node is a binary expression that compares `process.env.NODE_ENV` with a string literal

## Parameters

### node

The AST node

`undefined` | `null` | `Node`

### operator

The operator used in the comparison

`"==="` | `"!=="`

### value

The string literal value to compare against

`"development"` | `"production"`

## Returns

`node is BinaryExpression`

True if the node is a binary expression that compares `process.env.NODE_ENV` with the specified value, false otherwise
1 change: 1 addition & 0 deletions packages/utilities/kit/docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

## Namespaces

- [ContextDetection](@eslint-react/namespaces/ContextDetection/README.md)
- [JsxConfig](@eslint-react/namespaces/JsxConfig/README.md)
- [LanguagePreference](@eslint-react/namespaces/LanguagePreference/README.md)
- [RegExp](@eslint-react/namespaces/RegExp/README.md)
Expand Down
1 change: 1 addition & 0 deletions packages/utilities/kit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
},
"dependencies": {
"@eslint-react/eff": "workspace:*",
"@eslint-react/ast": "workspace:*",
"@typescript-eslint/utils": "^8.40.0",
"ts-pattern": "^5.8.0",
"zod": "^4.0.17"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as AST from "@eslint-react/ast";
import type { unit } from "@eslint-react/eff";
import type { TSESTree } from "@typescript-eslint/types";
import { AST_NODE_TYPES as T } from "@typescript-eslint/types";
import { isLiteral } from "./ast-literal-is";
import { AST_NODE_TYPES as T, type TSESTree } from "@typescript-eslint/types";

/**
* Check if the given node is a member expression that accesses `process.env.NODE_ENV`
Expand Down Expand Up @@ -35,11 +34,29 @@ export function isProcessEnvNodeEnvCompare(
if (node == null) return false;
if (node.type !== T.BinaryExpression) return false;
if (node.operator !== operator) return false;
if (isProcessEnvNodeEnv(node.left) && isLiteral(node.right, "string")) {
if (isProcessEnvNodeEnv(node.left) && AST.isLiteral(node.right, "string")) {
return node.right.value === value;
}
if (isLiteral(node.left, "string") && isProcessEnvNodeEnv(node.right)) {
if (AST.isLiteral(node.left, "string") && isProcessEnvNodeEnv(node.right)) {
return node.left.value === value;
}
return false;
}

/**
* Checks if the given node is a `vi.mock` callback.
* @param node The node to check
* @returns `true` if the node is a `vi.mock` callback, otherwise `false`.
* @internal
*/
export function isViMockCallback(node: TSESTree.Node | null | unit): node is TSESTree.FunctionExpression {
return node != null
&& node.type === T.FunctionExpression
&& node.parent.type === T.CallExpression
&& node.parent.callee.type === T.MemberExpression
&& node.parent.callee.object.type === T.Identifier
&& node.parent.callee.object.name === "vi"
&& node.parent.callee.property.type === T.Identifier
&& node.parent.callee.property.name === "mock"
&& node.parent.arguments[1] === node;
}
1 change: 1 addition & 0 deletions packages/utilities/kit/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * as ContextDetection from "./ContextDetection";
export * as JsxConfig from "./JsxConfig";
export * as LanguagePreference from "./LanguagePreference";
export * as RegExp from "./RegExp";
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading