Skip to content

Commit 6395b81

Browse files
committed
refactor: minor improvements
1 parent bb2c713 commit 6395b81

File tree

9 files changed

+27
-31
lines changed

9 files changed

+27
-31
lines changed

packages/core/docs/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
## Functions
4242

4343
- [getComponentNameFromIdentifier](functions/getComponentNameFromIdentifier.md)
44-
- [getElementType](functions/getElementType.md)
44+
- [getElementRepresentName](functions/getElementRepresentName.md)
4545
- [getFunctionComponentIdentifier](functions/getFunctionComponentIdentifier.md)
4646
- [hasNoneOrValidComponentName](functions/hasNoneOrValidComponentName.md)
4747
- [isCallFromReact](functions/isCallFromReact.md)

packages/core/docs/functions/getElementType.md renamed to packages/core/docs/functions/getElementRepresentName.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
***
44

5-
[@eslint-react/core](../README.md) / getElementType
5+
[@eslint-react/core](../README.md) / getElementRepresentName
66

7-
# Function: getElementType()
7+
# Function: getElementRepresentName()
88

9-
> **getElementType**(`node`, `context`): `string`
9+
> **getElementRepresentName**(`node`, `context`): `string`
1010
1111
## Parameters
1212

packages/core/src/element/get-element-type.ts renamed to packages/core/src/element/get-element-represent-name.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@ import { F, isString, O } from "@eslint-react/tools";
44
import type { RuleContext } from "@eslint-react/types";
55
import type { TSESTree } from "@typescript-eslint/types";
66

7-
export function getElementType(node: TSESTree.JSXOpeningElement, context: RuleContext) {
8-
const elementName = JSX.getElementName(node);
9-
if (elementName === elementName.toLowerCase()) return elementName;
7+
export function getElementRepresentName(node: TSESTree.JSXOpeningElement, context: RuleContext) {
8+
const rawElementName = JSX.getElementName(node);
9+
if (rawElementName === rawElementName.toLowerCase()) return rawElementName;
1010
const { components, polymorphicPropName } = normalizeSettings(decodeSettings(context.settings));
11-
const asElementName = components.get(elementName);
11+
const asElementName = components.get(rawElementName);
1212
if (isString(asElementName)) return asElementName;
1313
return F.pipe(
1414
O.fromNullable(polymorphicPropName),
1515
O.flatMap(JSX.findPropInAttributes(node.attributes, context.sourceCode.getScope(node))),
1616
O.flatMap(attr => JSX.getPropValue(attr, context.sourceCode.getScope(attr))),
1717
O.filter(isString),
18-
O.getOrElse(() => elementName),
18+
O.getOrElse(() => rawElementName),
1919
);
2020
}

packages/core/src/element/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
export * from "./get-element-type";
1+
export * from "./get-element-represent-name";
22
export * from "./hierarchy";
33
export * from "./misc";

packages/plugins/eslint-plugin-react-dom/src/rules/no-children-in-void-dom-elements.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getElementType } from "@eslint-react/core";
1+
import { getElementRepresentName } from "@eslint-react/core";
22
import * as JSX from "@eslint-react/jsx";
33
import { O } from "@eslint-react/tools";
44
import type { CamelCase } from "string-ts";
@@ -44,14 +44,14 @@ export default createRule<[], MessageID>({
4444
create(context) {
4545
return {
4646
JSXElement(node) {
47-
const elementType = getElementType(node.openingElement, context);
48-
if (!elementType || !voidElements.has(elementType)) return;
47+
const elementName = getElementRepresentName(node.openingElement, context);
48+
if (!elementName || !voidElements.has(elementName)) return;
4949
if (node.children.length > 0) {
5050
context.report({
5151
messageId: "noChildrenInVoidDomElements",
5252
node,
5353
data: {
54-
element: elementType,
54+
element: elementName,
5555
},
5656
});
5757
}
@@ -65,7 +65,7 @@ export default createRule<[], MessageID>({
6565
messageId: "noChildrenInVoidDomElements",
6666
node,
6767
data: {
68-
element: elementType,
68+
element: elementName,
6969
},
7070
});
7171
}

packages/plugins/eslint-plugin-react-dom/src/rules/no-missing-button-type.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getElementType } from "@eslint-react/core";
1+
import { getElementRepresentName } from "@eslint-react/core";
22
import * as JSX from "@eslint-react/jsx";
33
import { O } from "@eslint-react/tools";
44
import type { CamelCase } from "string-ts";
@@ -25,8 +25,8 @@ export default createRule<[], MessageID>({
2525
create(context) {
2626
return {
2727
JSXElement(node) {
28-
const elementType = getElementType(node.openingElement, context);
29-
if (elementType !== "button") return;
28+
const elementName = getElementRepresentName(node.openingElement, context);
29+
if (elementName !== "button") return;
3030
const { attributes } = node.openingElement;
3131
const initialScope = context.sourceCode.getScope(node);
3232
const maybeTypeAttribute = JSX.findPropInAttributes(attributes, initialScope)("type");

packages/plugins/eslint-plugin-react-dom/src/rules/no-missing-iframe-sandbox.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import * as JSX from "@eslint-react/jsx";
2-
import { decodeSettings, normalizeSettings } from "@eslint-react/shared";
32
import { F, isString, O } from "@eslint-react/tools";
4-
import type { TSESTree } from "@typescript-eslint/utils";
53
import type { CamelCase } from "string-ts";
64

75
import { createRule } from "../utils";
8-
import { getElementType } from "@eslint-react/core";
6+
import { getElementRepresentName } from "@eslint-react/core";
97

108
export const RULE_NAME = "no-missing-iframe-sandbox";
119

@@ -46,8 +44,8 @@ export default createRule<[], MessageID>({
4644
create(context) {
4745
return {
4846
JSXElement(node) {
49-
const elementType = getElementType(node.openingElement, context);
50-
if (elementType !== "iframe") return;
47+
const elementName = getElementRepresentName(node.openingElement, context);
48+
if (elementName !== "iframe") return;
5149
const { attributes } = node.openingElement;
5250
const initialScope = context.sourceCode.getScope(node);
5351
const maybeSandboxAttribute = JSX.findPropInAttributes(attributes, initialScope)("sandbox");

packages/plugins/eslint-plugin-react-dom/src/rules/no-unsafe-iframe-sandbox.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import * as JSX from "@eslint-react/jsx";
2-
import { decodeSettings, normalizeSettings } from "@eslint-react/shared";
32
import { F, isString, O } from "@eslint-react/tools";
4-
import type { TSESTree } from "@typescript-eslint/utils";
53
import type { CamelCase } from "string-ts";
64
import { match, P } from "ts-pattern";
75

86
import { createRule } from "../utils";
9-
import { getElementType } from "@eslint-react/core";
7+
import { getElementRepresentName } from "@eslint-react/core";
108

119
export const RULE_NAME = "no-unsafe-iframe-sandbox";
1210

@@ -33,8 +31,8 @@ export default createRule<[], MessageID>({
3331
create(context) {
3432
return {
3533
JSXElement(node) {
36-
const elementType = getElementType(node.openingElement, context);
37-
if (elementType !== "iframe") return;
34+
const elementName = getElementRepresentName(node.openingElement, context);
35+
if (elementName !== "iframe") return;
3836
const { attributes } = node.openingElement;
3937
const initialScope = context.sourceCode.getScope(node);
4038
const maybeSandboxAttribute = JSX.findPropInAttributes(attributes, initialScope)("sandbox");

packages/plugins/eslint-plugin-react-dom/src/rules/no-unsafe-target-blank.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getElementType } from "@eslint-react/core";
1+
import { getElementRepresentName } from "@eslint-react/core";
22
import * as JSX from "@eslint-react/jsx";
33
import { decodeSettings, normalizeSettings } from "@eslint-react/shared";
44
import { F, isString, O } from "@eslint-react/tools";
@@ -42,8 +42,8 @@ export default createRule<[], MessageID>({
4242
const additionalComponents = settings.additionalComponents.filter(c => c.as === "a");
4343
function getReportDescriptor(node: TSESTree.JSXElement): O.Option<ReportDescriptor<MessageID>> {
4444
const name = JSX.getElementName(node.openingElement);
45-
const elementType = getElementType(node.openingElement, context);
46-
if (elementType !== "a" && !additionalComponents.some(c => c.re.test(name))) return O.none();
45+
const elementName = getElementRepresentName(node.openingElement, context);
46+
if (elementName !== "a" && !additionalComponents.some(c => c.re.test(name))) return O.none();
4747
const { attributes } = node.openingElement;
4848
const initialScope = context.sourceCode.getScope(node);
4949
const additionalAttributes = additionalComponents

0 commit comments

Comments
 (0)