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
Expand Up @@ -3,7 +3,7 @@ import tsx from "dedent";

import { JsxEmit } from "typescript";
import { defaultLanguageOptionsWithTypes, getProjectForJsxRuntime } from "../../../../../test";
import rule, { debug, RULE_NAME } from "./jsx-uses-react";
import rule, { RULE_NAME } from "./jsx-uses-react";

const ruleTester = new RuleTester({
languageOptions: {
Expand All @@ -16,8 +16,8 @@ const ruleTester = new RuleTester({
},
});

// eslint-disable-next-line @typescript-eslint/no-unused-expressions, @typescript-eslint/no-unnecessary-condition
debug
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
process.env["ESLINT_REACT_DEBUG"] === "1"
? ruleTester.run(RULE_NAME, rule, {
invalid: [
{
Expand Down
24 changes: 11 additions & 13 deletions packages/plugins/eslint-plugin-react-x/src/rules/jsx-uses-react.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { TSESTree } from "@typescript-eslint/types";
import type { RuleListener } from "@typescript-eslint/utils/ts-eslint";

import type { CamelCase } from "string-ts";
import { JsxRuntime, type RuleContext, type RuleFeature } from "@eslint-react/kit";
import { JsxRuntimeOptions, type RuleContext, type RuleFeature } from "@eslint-react/kit";
import { JsxEmit } from "typescript";
import { createRule } from "../utils";

Expand All @@ -12,8 +12,6 @@ export const RULE_FEATURES = [] as const satisfies RuleFeature[];

export type MessageID = CamelCase<typeof RULE_NAME>;

export const debug = false;

export default createRule<[], MessageID>({
meta: {
type: "problem",
Expand All @@ -32,15 +30,16 @@ export default createRule<[], MessageID>({
});

export function create(context: RuleContext<MessageID, []>): RuleListener {
const jsxOptions = JsxRuntime.getJsxRuntimeOptions(context);
const jsxAnnotation = JsxRuntime.getJsxRuntimeAnnotation(context);
const jsx = jsxAnnotation.jsxRuntime === "classic"
? JsxEmit.React
: jsxOptions.jsx;
if (jsx === JsxEmit.ReactJSX || jsx === JsxEmit.ReactJSXDev) return {};
const jsxRuntimeOptionsFromContext = JsxRuntimeOptions.getFromContext(context);
const jsxRuntimeOptionsFromAnnotation = JsxRuntimeOptions.getFromAnnotation(context);
const jsxRuntimeOptions = {
...jsxRuntimeOptionsFromContext,
...jsxRuntimeOptionsFromAnnotation,
};

const jsxFactory = jsxAnnotation.jsx ?? jsxOptions.jsxFactory;
const jsxFragmentFactory = jsxAnnotation.jsxFrag ?? jsxOptions.jsxFragmentFactory;
const { jsx, jsxFactory, jsxFragmentFactory } = jsxRuntimeOptions;

if (jsx === JsxEmit.ReactJSX || jsx === JsxEmit.ReactJSXDev) return {};

function handleJsxElement(node: TSESTree.Node) {
context.sourceCode.markVariableAsUsed(jsxFactory, node);
Expand All @@ -60,8 +59,7 @@ export function create(context: RuleContext<MessageID, []>): RuleListener {
}

function debugReport(context: RuleContext, node: TSESTree.Node, name: string) {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (!debug) return;
if (process.env["ESLINT_REACT_DEBUG"] !== "1") return;
context.report({
messageId: "jsxUsesReact",
node,
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

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

***

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

# JsxRuntimeOptions

## Type Aliases

- [JsxRuntimeOptions](type-aliases/JsxRuntimeOptions.md)

## Functions

- [getFromAnnotation](functions/getFromAnnotation.md)
- [getFromContext](functions/getFromContext.md)
- [make](functions/make.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) / [JsxRuntimeOptions](../README.md) / getFromAnnotation

# Function: getFromAnnotation()

> **getFromAnnotation**(`context`): [`JsxRuntimeOptions`](../type-aliases/JsxRuntimeOptions.md)

Get JsxRuntimeOptions from annotation

## Parameters

### context

[`RuleContext`](../../../../type-aliases/RuleContext.md)

The RuleContext

## Returns

[`JsxRuntimeOptions`](../type-aliases/JsxRuntimeOptions.md)

JsxRuntimeOptions
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

***

[@eslint-react/kit](../../../../README.md) / [JsxRuntime](../README.md) / getJsxRuntimeOptions
[@eslint-react/kit](../../../../README.md) / [JsxRuntimeOptions](../README.md) / getFromContext

# Function: getJsxRuntimeOptions()
# Function: getFromContext()

> **getJsxRuntimeOptions**(`context`): `object`
> **getFromContext**(`context`): `object`

Get JsxRuntimeOptions from RuleContext

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[**@eslint-react/kit**](../../../../README.md)

***

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

# Function: make()

> **make**(): [`JsxRuntimeOptions`](../type-aliases/JsxRuntimeOptions.md)

Create a JsxRuntimeOptions object

## Returns

[`JsxRuntimeOptions`](../type-aliases/JsxRuntimeOptions.md)

JsxRuntimeOptions
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

***

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

# Type Alias: JsxRuntimeOptions

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@

## Functions

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

***

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

# Function: getFromContext()

> **getFromContext**(): `void`
## Returns

`void`
6 changes: 5 additions & 1 deletion packages/utilities/kit/docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

## Namespaces

- [JsxRuntime](@eslint-react/namespaces/JsxRuntime/README.md)
- [JsxRuntimeOptions](@eslint-react/namespaces/JsxRuntimeOptions/README.md)
- [LanguagePreference](@eslint-react/namespaces/LanguagePreference/README.md)

## Type Aliases
Expand All @@ -18,6 +18,10 @@

## Variables

- [RE\_ANNOTATION\_JSX](variables/RE_ANNOTATION_JSX.md)
- [RE\_ANNOTATION\_JSX\_FRAG](variables/RE_ANNOTATION_JSX_FRAG.md)
- [RE\_ANNOTATION\_JSX\_IMPORT\_SOURCE](variables/RE_ANNOTATION_JSX_IMPORT_SOURCE.md)
- [RE\_ANNOTATION\_JSX\_RUNTIME](variables/RE_ANNOTATION_JSX_RUNTIME.md)
- [RE\_CAMEL\_CASE](variables/RE_CAMEL_CASE.md)
- [RE\_CONSTANT\_CASE](variables/RE_CONSTANT_CASE.md)
- [RE\_JAVASCRIPT\_PROTOCOL](variables/RE_JAVASCRIPT_PROTOCOL.md)
Expand Down
11 changes: 11 additions & 0 deletions packages/utilities/kit/docs/variables/RE_ANNOTATION_JSX.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[**@eslint-react/kit**](../README.md)

***

[@eslint-react/kit](../README.md) / RE\_ANNOTATION\_JSX

# Variable: RE\_ANNOTATION\_JSX

> `const` **RE\_ANNOTATION\_JSX**: [`RegExp`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/RegExp)

Regular expression for matching a `@jsx` annotation comment.
11 changes: 11 additions & 0 deletions packages/utilities/kit/docs/variables/RE_ANNOTATION_JSX_FRAG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[**@eslint-react/kit**](../README.md)

***

[@eslint-react/kit](../README.md) / RE\_ANNOTATION\_JSX\_FRAG

# Variable: RE\_ANNOTATION\_JSX\_FRAG

> `const` **RE\_ANNOTATION\_JSX\_FRAG**: [`RegExp`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/RegExp)

Regular expression for matching a `@jsxFrag` annotation comment.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[**@eslint-react/kit**](../README.md)

***

[@eslint-react/kit](../README.md) / RE\_ANNOTATION\_JSX\_IMPORT\_SOURCE

# Variable: RE\_ANNOTATION\_JSX\_IMPORT\_SOURCE

> `const` **RE\_ANNOTATION\_JSX\_IMPORT\_SOURCE**: [`RegExp`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/RegExp)

Regular expression for matching a `@jsxImportSource` annotation comment.
11 changes: 11 additions & 0 deletions packages/utilities/kit/docs/variables/RE_ANNOTATION_JSX_RUNTIME.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[**@eslint-react/kit**](../README.md)

***

[@eslint-react/kit](../README.md) / RE\_ANNOTATION\_JSX\_RUNTIME

# Variable: RE\_ANNOTATION\_JSX\_RUNTIME

> `const` **RE\_ANNOTATION\_JSX\_RUNTIME**: [`RegExp`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/RegExp)

Regular expression for matching a `@jsxRuntime` annotation comment.
Loading