diff --git a/packages/plugins/eslint-plugin-react-x/src/rules/jsx-uses-react.spec.ts b/packages/plugins/eslint-plugin-react-x/src/rules/jsx-uses-react.spec.ts index 79fdd317f6..cfd06e739c 100644 --- a/packages/plugins/eslint-plugin-react-x/src/rules/jsx-uses-react.spec.ts +++ b/packages/plugins/eslint-plugin-react-x/src/rules/jsx-uses-react.spec.ts @@ -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: { @@ -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: [ { diff --git a/packages/plugins/eslint-plugin-react-x/src/rules/jsx-uses-react.ts b/packages/plugins/eslint-plugin-react-x/src/rules/jsx-uses-react.ts index 0bc234c9bb..00d92ed088 100644 --- a/packages/plugins/eslint-plugin-react-x/src/rules/jsx-uses-react.ts +++ b/packages/plugins/eslint-plugin-react-x/src/rules/jsx-uses-react.ts @@ -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"; @@ -12,8 +12,6 @@ export const RULE_FEATURES = [] as const satisfies RuleFeature[]; export type MessageID = CamelCase; -export const debug = false; - export default createRule<[], MessageID>({ meta: { type: "problem", @@ -32,15 +30,16 @@ export default createRule<[], MessageID>({ }); export function create(context: RuleContext): 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); @@ -60,8 +59,7 @@ export function create(context: RuleContext): 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, diff --git a/packages/utilities/kit/docs/@eslint-react/namespaces/JsxRuntime/README.md b/packages/utilities/kit/docs/@eslint-react/namespaces/JsxRuntime/README.md deleted file mode 100644 index 886ee55c2c..0000000000 --- a/packages/utilities/kit/docs/@eslint-react/namespaces/JsxRuntime/README.md +++ /dev/null @@ -1,28 +0,0 @@ -[**@eslint-react/kit**](../../../README.md) - -*** - -[@eslint-react/kit](../../../README.md) / JsxRuntime - -# JsxRuntime - -## Interfaces - -- [JsxRuntimeAnnotation](interfaces/JsxRuntimeAnnotation.md) - -## Type Aliases - -- [JsxRuntimeOptions](type-aliases/JsxRuntimeOptions.md) - -## Variables - -- [RE\_JSX\_RT\_ANNOTATION\_JSX](variables/RE_JSX_RT_ANNOTATION_JSX.md) -- [RE\_JSX\_RT\_ANNOTATION\_JSX\_FRAG](variables/RE_JSX_RT_ANNOTATION_JSX_FRAG.md) -- [RE\_JSX\_RT\_ANNOTATION\_JSX\_IMPORT\_SOURCE](variables/RE_JSX_RT_ANNOTATION_JSX_IMPORT_SOURCE.md) -- [RE\_JSX\_RT\_ANNOTATION\_JSX\_RUNTIME](variables/RE_JSX_RT_ANNOTATION_JSX_RUNTIME.md) - -## Functions - -- [getJsxRuntimeAnnotation](functions/getJsxRuntimeAnnotation.md) -- [getJsxRuntimeOptions](functions/getJsxRuntimeOptions.md) -- [make](functions/make.md) diff --git a/packages/utilities/kit/docs/@eslint-react/namespaces/JsxRuntime/functions/getJsxRuntimeAnnotation.md b/packages/utilities/kit/docs/@eslint-react/namespaces/JsxRuntime/functions/getJsxRuntimeAnnotation.md deleted file mode 100644 index b436142f1d..0000000000 --- a/packages/utilities/kit/docs/@eslint-react/namespaces/JsxRuntime/functions/getJsxRuntimeAnnotation.md +++ /dev/null @@ -1,25 +0,0 @@ -[**@eslint-react/kit**](../../../../README.md) - -*** - -[@eslint-react/kit](../../../../README.md) / [JsxRuntime](../README.md) / getJsxRuntimeAnnotation - -# Function: getJsxRuntimeAnnotation() - -> **getJsxRuntimeAnnotation**(`context`): [`JsxRuntimeAnnotation`](../interfaces/JsxRuntimeAnnotation.md) - -Get the a JsxRuntimeAnnotation object representing the JSX annotations in the file. - -## Parameters - -### context - -[`RuleContext`](../../../../type-aliases/RuleContext.md) - -The RuleContext - -## Returns - -[`JsxRuntimeAnnotation`](../interfaces/JsxRuntimeAnnotation.md) - -JsxRuntimeAnnotation diff --git a/packages/utilities/kit/docs/@eslint-react/namespaces/JsxRuntime/functions/make.md b/packages/utilities/kit/docs/@eslint-react/namespaces/JsxRuntime/functions/make.md deleted file mode 100644 index cdca78d594..0000000000 --- a/packages/utilities/kit/docs/@eslint-react/namespaces/JsxRuntime/functions/make.md +++ /dev/null @@ -1,13 +0,0 @@ -[**@eslint-react/kit**](../../../../README.md) - -*** - -[@eslint-react/kit](../../../../README.md) / [JsxRuntime](../README.md) / make - -# Function: make() - -> **make**(): [`JsxRuntimeAnnotation`](../interfaces/JsxRuntimeAnnotation.md) - -## Returns - -[`JsxRuntimeAnnotation`](../interfaces/JsxRuntimeAnnotation.md) diff --git a/packages/utilities/kit/docs/@eslint-react/namespaces/JsxRuntime/interfaces/JsxRuntimeAnnotation.md b/packages/utilities/kit/docs/@eslint-react/namespaces/JsxRuntime/interfaces/JsxRuntimeAnnotation.md deleted file mode 100644 index 2c5342f927..0000000000 --- a/packages/utilities/kit/docs/@eslint-react/namespaces/JsxRuntime/interfaces/JsxRuntimeAnnotation.md +++ /dev/null @@ -1,31 +0,0 @@ -[**@eslint-react/kit**](../../../../README.md) - -*** - -[@eslint-react/kit](../../../../README.md) / [JsxRuntime](../README.md) / JsxRuntimeAnnotation - -# Interface: JsxRuntimeAnnotation - -## Properties - -### jsx? - -> `optional` **jsx**: `string` - -*** - -### jsxFrag? - -> `optional` **jsxFrag**: `string` - -*** - -### jsxImportSource? - -> `optional` **jsxImportSource**: `string` - -*** - -### jsxRuntime? - -> `optional` **jsxRuntime**: `string` diff --git a/packages/utilities/kit/docs/@eslint-react/namespaces/JsxRuntime/variables/RE_JSX_RT_ANNOTATION_JSX.md b/packages/utilities/kit/docs/@eslint-react/namespaces/JsxRuntime/variables/RE_JSX_RT_ANNOTATION_JSX.md deleted file mode 100644 index 6fe04ea242..0000000000 --- a/packages/utilities/kit/docs/@eslint-react/namespaces/JsxRuntime/variables/RE_JSX_RT_ANNOTATION_JSX.md +++ /dev/null @@ -1,11 +0,0 @@ -[**@eslint-react/kit**](../../../../README.md) - -*** - -[@eslint-react/kit](../../../../README.md) / [JsxRuntime](../README.md) / RE\_JSX\_RT\_ANNOTATION\_JSX - -# Variable: RE\_JSX\_RT\_ANNOTATION\_JSX - -> `const` **RE\_JSX\_RT\_ANNOTATION\_JSX**: [`RegExp`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/RegExp) - -Regular expression for matching a `@jsx` annotation comment. diff --git a/packages/utilities/kit/docs/@eslint-react/namespaces/JsxRuntime/variables/RE_JSX_RT_ANNOTATION_JSX_FRAG.md b/packages/utilities/kit/docs/@eslint-react/namespaces/JsxRuntime/variables/RE_JSX_RT_ANNOTATION_JSX_FRAG.md deleted file mode 100644 index 90f098cd00..0000000000 --- a/packages/utilities/kit/docs/@eslint-react/namespaces/JsxRuntime/variables/RE_JSX_RT_ANNOTATION_JSX_FRAG.md +++ /dev/null @@ -1,11 +0,0 @@ -[**@eslint-react/kit**](../../../../README.md) - -*** - -[@eslint-react/kit](../../../../README.md) / [JsxRuntime](../README.md) / RE\_JSX\_RT\_ANNOTATION\_JSX\_FRAG - -# Variable: RE\_JSX\_RT\_ANNOTATION\_JSX\_FRAG - -> `const` **RE\_JSX\_RT\_ANNOTATION\_JSX\_FRAG**: [`RegExp`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/RegExp) - -Regular expression for matching a `@jsxFrag` annotation comment. diff --git a/packages/utilities/kit/docs/@eslint-react/namespaces/JsxRuntime/variables/RE_JSX_RT_ANNOTATION_JSX_IMPORT_SOURCE.md b/packages/utilities/kit/docs/@eslint-react/namespaces/JsxRuntime/variables/RE_JSX_RT_ANNOTATION_JSX_IMPORT_SOURCE.md deleted file mode 100644 index b3830aa371..0000000000 --- a/packages/utilities/kit/docs/@eslint-react/namespaces/JsxRuntime/variables/RE_JSX_RT_ANNOTATION_JSX_IMPORT_SOURCE.md +++ /dev/null @@ -1,11 +0,0 @@ -[**@eslint-react/kit**](../../../../README.md) - -*** - -[@eslint-react/kit](../../../../README.md) / [JsxRuntime](../README.md) / RE\_JSX\_RT\_ANNOTATION\_JSX\_IMPORT\_SOURCE - -# Variable: RE\_JSX\_RT\_ANNOTATION\_JSX\_IMPORT\_SOURCE - -> `const` **RE\_JSX\_RT\_ANNOTATION\_JSX\_IMPORT\_SOURCE**: [`RegExp`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/RegExp) - -Regular expression for matching a `@jsxImportSource` annotation comment. diff --git a/packages/utilities/kit/docs/@eslint-react/namespaces/JsxRuntime/variables/RE_JSX_RT_ANNOTATION_JSX_RUNTIME.md b/packages/utilities/kit/docs/@eslint-react/namespaces/JsxRuntime/variables/RE_JSX_RT_ANNOTATION_JSX_RUNTIME.md deleted file mode 100644 index b5a926599a..0000000000 --- a/packages/utilities/kit/docs/@eslint-react/namespaces/JsxRuntime/variables/RE_JSX_RT_ANNOTATION_JSX_RUNTIME.md +++ /dev/null @@ -1,11 +0,0 @@ -[**@eslint-react/kit**](../../../../README.md) - -*** - -[@eslint-react/kit](../../../../README.md) / [JsxRuntime](../README.md) / RE\_JSX\_RT\_ANNOTATION\_JSX\_RUNTIME - -# Variable: RE\_JSX\_RT\_ANNOTATION\_JSX\_RUNTIME - -> `const` **RE\_JSX\_RT\_ANNOTATION\_JSX\_RUNTIME**: [`RegExp`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/RegExp) - -Regular expression for matching a `@jsxRuntime` annotation comment. diff --git a/packages/utilities/kit/docs/@eslint-react/namespaces/JsxRuntimeOptions/README.md b/packages/utilities/kit/docs/@eslint-react/namespaces/JsxRuntimeOptions/README.md new file mode 100644 index 0000000000..50d78df1da --- /dev/null +++ b/packages/utilities/kit/docs/@eslint-react/namespaces/JsxRuntimeOptions/README.md @@ -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) diff --git a/packages/utilities/kit/docs/@eslint-react/namespaces/JsxRuntimeOptions/functions/getFromAnnotation.md b/packages/utilities/kit/docs/@eslint-react/namespaces/JsxRuntimeOptions/functions/getFromAnnotation.md new file mode 100644 index 0000000000..28888f30a3 --- /dev/null +++ b/packages/utilities/kit/docs/@eslint-react/namespaces/JsxRuntimeOptions/functions/getFromAnnotation.md @@ -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 diff --git a/packages/utilities/kit/docs/@eslint-react/namespaces/JsxRuntime/functions/getJsxRuntimeOptions.md b/packages/utilities/kit/docs/@eslint-react/namespaces/JsxRuntimeOptions/functions/getFromContext.md similarity index 72% rename from packages/utilities/kit/docs/@eslint-react/namespaces/JsxRuntime/functions/getJsxRuntimeOptions.md rename to packages/utilities/kit/docs/@eslint-react/namespaces/JsxRuntimeOptions/functions/getFromContext.md index 48add9403e..bb3d2ef630 100644 --- a/packages/utilities/kit/docs/@eslint-react/namespaces/JsxRuntime/functions/getJsxRuntimeOptions.md +++ b/packages/utilities/kit/docs/@eslint-react/namespaces/JsxRuntimeOptions/functions/getFromContext.md @@ -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 diff --git a/packages/utilities/kit/docs/@eslint-react/namespaces/JsxRuntimeOptions/functions/make.md b/packages/utilities/kit/docs/@eslint-react/namespaces/JsxRuntimeOptions/functions/make.md new file mode 100644 index 0000000000..6e834a2c7a --- /dev/null +++ b/packages/utilities/kit/docs/@eslint-react/namespaces/JsxRuntimeOptions/functions/make.md @@ -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 diff --git a/packages/utilities/kit/docs/@eslint-react/namespaces/JsxRuntime/type-aliases/JsxRuntimeOptions.md b/packages/utilities/kit/docs/@eslint-react/namespaces/JsxRuntimeOptions/type-aliases/JsxRuntimeOptions.md similarity index 76% rename from packages/utilities/kit/docs/@eslint-react/namespaces/JsxRuntime/type-aliases/JsxRuntimeOptions.md rename to packages/utilities/kit/docs/@eslint-react/namespaces/JsxRuntimeOptions/type-aliases/JsxRuntimeOptions.md index dbb58a7839..f61a8af132 100644 --- a/packages/utilities/kit/docs/@eslint-react/namespaces/JsxRuntime/type-aliases/JsxRuntimeOptions.md +++ b/packages/utilities/kit/docs/@eslint-react/namespaces/JsxRuntimeOptions/type-aliases/JsxRuntimeOptions.md @@ -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 diff --git a/packages/utilities/kit/docs/@eslint-react/namespaces/LanguagePreference/README.md b/packages/utilities/kit/docs/@eslint-react/namespaces/LanguagePreference/README.md index ee0f7eef6a..42891e3d78 100644 --- a/packages/utilities/kit/docs/@eslint-react/namespaces/LanguagePreference/README.md +++ b/packages/utilities/kit/docs/@eslint-react/namespaces/LanguagePreference/README.md @@ -12,4 +12,5 @@ ## Functions +- [getFromContext](functions/getFromContext.md) - [make](functions/make.md) diff --git a/packages/utilities/kit/docs/@eslint-react/namespaces/LanguagePreference/functions/getFromContext.md b/packages/utilities/kit/docs/@eslint-react/namespaces/LanguagePreference/functions/getFromContext.md new file mode 100644 index 0000000000..995323be65 --- /dev/null +++ b/packages/utilities/kit/docs/@eslint-react/namespaces/LanguagePreference/functions/getFromContext.md @@ -0,0 +1,13 @@ +[**@eslint-react/kit**](../../../../README.md) + +*** + +[@eslint-react/kit](../../../../README.md) / [LanguagePreference](../README.md) / getFromContext + +# Function: getFromContext() + +> **getFromContext**(): `void` + +## Returns + +`void` diff --git a/packages/utilities/kit/docs/README.md b/packages/utilities/kit/docs/README.md index 76cf4656da..c4116918a1 100644 --- a/packages/utilities/kit/docs/README.md +++ b/packages/utilities/kit/docs/README.md @@ -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 @@ -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) diff --git a/packages/utilities/kit/docs/variables/RE_ANNOTATION_JSX.md b/packages/utilities/kit/docs/variables/RE_ANNOTATION_JSX.md new file mode 100644 index 0000000000..15b9ab2014 --- /dev/null +++ b/packages/utilities/kit/docs/variables/RE_ANNOTATION_JSX.md @@ -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. diff --git a/packages/utilities/kit/docs/variables/RE_ANNOTATION_JSX_FRAG.md b/packages/utilities/kit/docs/variables/RE_ANNOTATION_JSX_FRAG.md new file mode 100644 index 0000000000..025f9f2b64 --- /dev/null +++ b/packages/utilities/kit/docs/variables/RE_ANNOTATION_JSX_FRAG.md @@ -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. diff --git a/packages/utilities/kit/docs/variables/RE_ANNOTATION_JSX_IMPORT_SOURCE.md b/packages/utilities/kit/docs/variables/RE_ANNOTATION_JSX_IMPORT_SOURCE.md new file mode 100644 index 0000000000..b23b7838f3 --- /dev/null +++ b/packages/utilities/kit/docs/variables/RE_ANNOTATION_JSX_IMPORT_SOURCE.md @@ -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. diff --git a/packages/utilities/kit/docs/variables/RE_ANNOTATION_JSX_RUNTIME.md b/packages/utilities/kit/docs/variables/RE_ANNOTATION_JSX_RUNTIME.md new file mode 100644 index 0000000000..0b4ef3932b --- /dev/null +++ b/packages/utilities/kit/docs/variables/RE_ANNOTATION_JSX_RUNTIME.md @@ -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. diff --git a/packages/utilities/kit/src/JsxRuntime/JsxRuntimeAnnotation.ts b/packages/utilities/kit/src/JsxRuntime/JsxRuntimeAnnotation.ts deleted file mode 100644 index 8da737e21b..0000000000 --- a/packages/utilities/kit/src/JsxRuntime/JsxRuntimeAnnotation.ts +++ /dev/null @@ -1,55 +0,0 @@ -import type { _ } from "@eslint-react/eff"; -import type { RuleContext } from "../Rule"; - -/** - * Regular expression for matching a `@jsx` annotation comment. - */ -export const RE_JSX_RT_ANNOTATION_JSX = /@jsx\s+(\S+)/u; - -/** - * Regular expression for matching a `@jsxFrag` annotation comment. - */ -export const RE_JSX_RT_ANNOTATION_JSX_FRAG = /@jsxFrag\s+(\S+)/u; - -/** - * Regular expression for matching a `@jsxRuntime` annotation comment. - */ -export const RE_JSX_RT_ANNOTATION_JSX_RUNTIME = /@jsxRuntime\s+(\S+)/u; - -/** - * Regular expression for matching a `@jsxImportSource` annotation comment. - */ -export const RE_JSX_RT_ANNOTATION_JSX_IMPORT_SOURCE = /@jsxImportSource\s+(\S+)/u; - -export interface JsxRuntimeAnnotation { - jsx?: _ | string; - jsxFrag?: _ | string; - jsxImportSource?: _ | string; - jsxRuntime?: _ | string; -} - -export function make(): JsxRuntimeAnnotation { - return {}; -} - -/** - * Get the a JsxRuntimeAnnotation object representing the JSX annotations in the file. - * @param context The RuleContext - * @returns JsxRuntimeAnnotation - */ -export function getJsxRuntimeAnnotation(context: RuleContext) { - const JsxRuntimeAnnotation = make(); - if (!context.sourceCode.text.includes("@jsx")) return JsxRuntimeAnnotation; - const allComments = context.sourceCode.getAllComments(); - for (const comment of allComments) { - const jsx = comment.value.match(RE_JSX_RT_ANNOTATION_JSX); - const jsxFrag = comment.value.match(RE_JSX_RT_ANNOTATION_JSX_FRAG); - const jsxRuntime = comment.value.match(RE_JSX_RT_ANNOTATION_JSX_RUNTIME); - const jsxImportSource = comment.value.match(RE_JSX_RT_ANNOTATION_JSX_IMPORT_SOURCE); - JsxRuntimeAnnotation.jsx = jsx?.[1]; - JsxRuntimeAnnotation.jsxFrag = jsxFrag?.[1]; - JsxRuntimeAnnotation.jsxRuntime = jsxRuntime?.[1]; - JsxRuntimeAnnotation.jsxImportSource = jsxImportSource?.[1]; - } - return JsxRuntimeAnnotation; -} diff --git a/packages/utilities/kit/src/JsxRuntime/JsxRuntimeOptions.ts b/packages/utilities/kit/src/JsxRuntime/JsxRuntimeOptions.ts deleted file mode 100644 index 6b577f1732..0000000000 --- a/packages/utilities/kit/src/JsxRuntime/JsxRuntimeOptions.ts +++ /dev/null @@ -1,32 +0,0 @@ -import type { RuleContext } from "../Rule"; -import { type CompilerOptions, JsxEmit } from "typescript"; - -export type JsxRuntimeOptions = Pick< - CompilerOptions, - // Specifies the object invoked for `createElement` and `__spread` when targeting `'react'` JSX emit. - | "reactNamespace" - // Specifies what JSX code is generated. - | "jsx" - // Specifies the JSX factory function to use when targeting React JSX emit, e.g. `React.createElement` or `h`. - | "jsxFactory" - // Specifies the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. - | "jsxFragmentFactory" - // Specify module specifier used to import the JSX factory functions when using `jsx: react-jsx*`. - | "jsxImportSource" ->; - -/** - * Get JsxRuntimeOptions from RuleContext - * @param context The RuleContext - * @returns JsxRuntimeOptions - */ -export function getJsxRuntimeOptions(context: RuleContext) { - const options = context.sourceCode.parserServices?.program?.getCompilerOptions() ?? {}; - return { - jsx: options.jsx ?? JsxEmit.ReactJSX, - jsxFactory: options.jsxFactory ?? "React.createElement", - jsxFragmentFactory: options.jsxFragmentFactory ?? "React.Fragment", - jsxImportSource: options.jsxImportSource ?? "react", - reactNamespace: options.reactNamespace ?? "React", - }; -} diff --git a/packages/utilities/kit/src/JsxRuntime/index.ts b/packages/utilities/kit/src/JsxRuntime/index.ts deleted file mode 100644 index a5480357fd..0000000000 --- a/packages/utilities/kit/src/JsxRuntime/index.ts +++ /dev/null @@ -1,2 +0,0 @@ -export * from "./JsxRuntimeAnnotation"; -export * from "./JsxRuntimeOptions"; diff --git a/packages/utilities/kit/src/JsxRuntimeOptions/JsxRuntimeOptions.ts b/packages/utilities/kit/src/JsxRuntimeOptions/JsxRuntimeOptions.ts new file mode 100644 index 0000000000..7ba49cc131 --- /dev/null +++ b/packages/utilities/kit/src/JsxRuntimeOptions/JsxRuntimeOptions.ts @@ -0,0 +1,70 @@ +/* eslint-disable perfectionist/sort-variable-declarations */ +import type { RuleContext } from "../Rule"; +import { type CompilerOptions, JsxEmit } from "typescript"; +import { + RE_ANNOTATION_JSX, + RE_ANNOTATION_JSX_FRAG, + RE_ANNOTATION_JSX_IMPORT_SOURCE, + RE_ANNOTATION_JSX_RUNTIME, +} from "../RegExp"; + +export type JsxRuntimeOptions = Pick< + CompilerOptions, + // Specifies the object invoked for `createElement` and `__spread` when targeting `'react'` JSX emit. + | "reactNamespace" + // Specifies what JSX code is generated. + | "jsx" + // Specifies the JSX factory function to use when targeting React JSX emit, e.g. `React.createElement` or `h`. + | "jsxFactory" + // Specifies the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. + | "jsxFragmentFactory" + // Specify module specifier used to import the JSX factory functions when using `jsx: react-jsx*`. + | "jsxImportSource" +>; + +/** + * Create a JsxRuntimeOptions object + * @returns JsxRuntimeOptions + */ +export function make(): JsxRuntimeOptions { + return {}; +} + +/** + * Get JsxRuntimeOptions from RuleContext + * @param context The RuleContext + * @returns JsxRuntimeOptions + */ +export function getFromContext(context: RuleContext) { + const options = context.sourceCode.parserServices?.program?.getCompilerOptions() ?? {}; + return { + jsx: options.jsx ?? JsxEmit.ReactJSX, + jsxFactory: options.jsxFactory ?? "React.createElement", + jsxFragmentFactory: options.jsxFragmentFactory ?? "React.Fragment", + jsxImportSource: options.jsxImportSource ?? "react", + reactNamespace: options.reactNamespace ?? "React", + }; +} + +/** + * Get JsxRuntimeOptions from annotation + * @param context The RuleContext + * @returns JsxRuntimeOptions + */ +export function getFromAnnotation(context: RuleContext) { + if (!context.sourceCode.text.includes("@jsx")) return {}; + let jsx, jsxFrag, jsxRuntime, jsxImportSource; + for (const comment of context.sourceCode.getAllComments()) { + const value = comment.value; + jsx = value.match(RE_ANNOTATION_JSX)?.[1]; + jsxFrag = value.match(RE_ANNOTATION_JSX_FRAG)?.[1]; + jsxRuntime = value.match(RE_ANNOTATION_JSX_RUNTIME)?.[1]; + jsxImportSource = value.match(RE_ANNOTATION_JSX_IMPORT_SOURCE)?.[1]; + } + const options = make(); + if (jsx != null) options.jsxFactory = jsx; + if (jsxFrag != null) options.jsxFragmentFactory = jsxFrag; + if (jsxRuntime != null) options.jsx = jsxRuntime === "classic" ? JsxEmit.ReactJSX : JsxEmit.ReactJSXDev; + if (jsxImportSource != null) options.jsxImportSource = jsxImportSource; + return options; +} diff --git a/packages/utilities/kit/src/JsxRuntimeOptions/index.ts b/packages/utilities/kit/src/JsxRuntimeOptions/index.ts new file mode 100644 index 0000000000..dc9752cb31 --- /dev/null +++ b/packages/utilities/kit/src/JsxRuntimeOptions/index.ts @@ -0,0 +1 @@ +export * from "./JsxRuntimeOptions"; diff --git a/packages/utilities/kit/src/LanguagePreference/LanguagePreference.ts b/packages/utilities/kit/src/LanguagePreference/LanguagePreference.ts index 36bbce89b5..e7736f74f7 100644 --- a/packages/utilities/kit/src/LanguagePreference/LanguagePreference.ts +++ b/packages/utilities/kit/src/LanguagePreference/LanguagePreference.ts @@ -28,6 +28,10 @@ export function make(): LanguagePreference { }; } +export function getFromContext() { + throw new Error("getFromContext is not implemented"); +} + declare module "@typescript-eslint/utils/ts-eslint" { export interface SharedConfigurationSettings { // TODO: Add the language preference to the shared configuration settings when it is ready. diff --git a/packages/utilities/kit/src/RegExp.ts b/packages/utilities/kit/src/RegExp.ts index b9f5c14dd4..1bc3f7e1cd 100644 --- a/packages/utilities/kit/src/RegExp.ts +++ b/packages/utilities/kit/src/RegExp.ts @@ -48,6 +48,26 @@ export const RE_JS_IDENTIFIER = /^[_$a-z][\w$]*$/i; */ export const RE_REGEXP_STR = /^\/(.+)\/([A-Za-z]*)$/u; +/** + * Regular expression for matching a `@jsx` annotation comment. + */ +export const RE_ANNOTATION_JSX = /@jsx\s+(\S+)/u; + +/** + * Regular expression for matching a `@jsxFrag` annotation comment. + */ +export const RE_ANNOTATION_JSX_FRAG = /@jsxFrag\s+(\S+)/u; + +/** + * Regular expression for matching a `@jsxRuntime` annotation comment. + */ +export const RE_ANNOTATION_JSX_RUNTIME = /@jsxRuntime\s+(\S+)/u; + +/** + * Regular expression for matching a `@jsxImportSource` annotation comment. + */ +export const RE_ANNOTATION_JSX_IMPORT_SOURCE = /@jsxImportSource\s+(\S+)/u; + /** * Convert a string to the `RegExp`. * Normal strings (e.g. `"foo"`) is converted to `/^foo$/` of `RegExp`. diff --git a/packages/utilities/kit/src/index.ts b/packages/utilities/kit/src/index.ts index a115d448a3..8524b62460 100644 --- a/packages/utilities/kit/src/index.ts +++ b/packages/utilities/kit/src/index.ts @@ -1,4 +1,4 @@ -export * as JsxRuntime from "./JsxRuntime"; +export * as JsxRuntimeOptions from "./JsxRuntimeOptions"; export * as LanguagePreference from "./LanguagePreference"; export * from "./RegExp"; export * from "./Rule";