Skip to content

Commit d8cafd2

Browse files
authored
refactor: code optimizations (#1140)
1 parent 7e1d87c commit d8cafd2

File tree

7 files changed

+295
-144
lines changed

7 files changed

+295
-144
lines changed

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

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import type { RuleContext, RuleFeature, RuleSuggest } from "@eslint-react/kit";
22
import type { RuleFixer, RuleListener } from "@typescript-eslint/utils/ts-eslint";
33
import type { CamelCase } from "string-ts";
4-
import * as ER from "@eslint-react/core";
5-
import { createJsxElementResolver, createRule, findCustomComponentProp } from "../utils";
4+
import { createJsxElementResolver, createRule, resolveAttribute } from "../utils";
65

76
export const RULE_NAME = "no-missing-button-type";
87

@@ -41,40 +40,26 @@ export function create(context: RuleContext<MessageID, []>): RuleListener {
4140
JSXElement(node) {
4241
const { attributes, domElementType } = resolver.resolve(node);
4342
if (domElementType !== "button") return;
44-
const customComponentProp = findCustomComponentProp("type", attributes);
45-
const propNameOnJsx = customComponentProp?.name ?? "type";
46-
const attributeNode = ER.getAttribute(
47-
context,
48-
propNameOnJsx,
49-
node.openingElement.attributes,
50-
context.sourceCode.getScope(node),
51-
);
52-
if (attributeNode != null) {
53-
const attributeValue = ER.getAttributeValue(
54-
context,
55-
attributeNode,
56-
propNameOnJsx,
57-
);
58-
if (attributeValue.kind !== "some" || typeof attributeValue.value !== "string") {
59-
context.report({
60-
messageId: "noMissingButtonType",
61-
node: attributeNode,
62-
suggest: getSuggest((type) => (fixer: RuleFixer) => {
63-
return fixer.replaceText(attributeNode, `${propNameOnJsx}="${type}"`);
64-
}),
65-
});
66-
}
67-
return;
68-
}
69-
if (typeof customComponentProp?.defaultValue !== "string") {
43+
const typeAttribute = resolveAttribute(context, attributes, node, "type");
44+
if (typeAttribute.attributeValueString != null) return;
45+
if (typeAttribute.attribute == null) {
7046
context.report({
7147
messageId: "noMissingButtonType",
72-
node,
48+
node: node.openingElement,
7349
suggest: getSuggest((type) => (fixer: RuleFixer) => {
74-
return fixer.insertTextAfter(node.openingElement.name, ` ${propNameOnJsx}="${type}"`);
50+
return fixer.insertTextAfter(node.openingElement.name, ` ${typeAttribute.attributeName}="${type}"`);
7551
}),
7652
});
53+
return;
7754
}
55+
context.report({
56+
messageId: "noMissingButtonType",
57+
node: typeAttribute.attributeValue?.node ?? typeAttribute.attribute,
58+
suggest: getSuggest((type) => (fixer: RuleFixer) => {
59+
if (typeAttribute.attribute == null) return null;
60+
return fixer.replaceText(typeAttribute.attribute, `${typeAttribute.attributeName}="${type}"`);
61+
}),
62+
});
7863
},
7964
};
8065
}
Lines changed: 24 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import type { RuleContext, RuleFeature } from "@eslint-react/kit";
22
import type { RuleListener } from "@typescript-eslint/utils/ts-eslint";
33
import type { CamelCase } from "string-ts";
4-
import * as ER from "@eslint-react/core";
54

6-
import { createJsxElementResolver, createRule, findCustomComponentProp } from "../utils";
5+
import { createJsxElementResolver, createRule, resolveAttribute } from "../utils";
76

87
export const RULE_NAME = "no-missing-iframe-sandbox";
98

@@ -41,52 +40,34 @@ export function create(context: RuleContext<MessageID, []>): RuleListener {
4140
JSXElement(node) {
4241
const { attributes, domElementType } = resolver.resolve(node);
4342
if (domElementType !== "iframe") return;
44-
const customComponentProp = findCustomComponentProp("sandbox", attributes);
45-
const propNameOnJsx = customComponentProp?.name ?? "sandbox";
46-
const attributeNode = ER.getAttribute(
47-
context,
48-
propNameOnJsx,
49-
node.openingElement.attributes,
50-
context.sourceCode.getScope(node),
51-
);
52-
if (attributeNode != null) {
53-
const attributeValue = ER.getAttributeValue(
54-
context,
55-
attributeNode,
56-
propNameOnJsx,
57-
);
58-
if (attributeValue.kind !== "some" || typeof attributeValue.value !== "string") {
59-
context.report({
60-
messageId: "noMissingIframeSandbox",
61-
node: attributeNode,
62-
suggest: [
63-
{
64-
messageId: "addIframeSandbox",
65-
data: { value: "" },
66-
fix(fixer) {
67-
return fixer.replaceText(attributeNode, `${propNameOnJsx}=""`);
68-
},
69-
},
70-
],
71-
});
72-
}
73-
return;
74-
}
75-
if (typeof customComponentProp?.defaultValue !== "string") {
43+
const sandboxAttribute = resolveAttribute(context, attributes, node, "sandbox");
44+
if (sandboxAttribute.attributeValueString != null) return;
45+
if (sandboxAttribute.attribute == null) {
7646
context.report({
7747
messageId: "noMissingIframeSandbox",
78-
node,
79-
suggest: [
80-
{
81-
messageId: "addIframeSandbox",
82-
data: { value: "" },
83-
fix(fixer) {
84-
return fixer.insertTextAfter(node.openingElement.name, ` ${propNameOnJsx}=""`);
85-
},
48+
node: node.openingElement,
49+
suggest: [{
50+
messageId: "addIframeSandbox",
51+
data: { value: "" },
52+
fix(fixer) {
53+
return fixer.insertTextAfter(node.openingElement.name, ` ${sandboxAttribute.attributeName}=""`);
8654
},
87-
],
55+
}],
8856
});
57+
return;
8958
}
59+
context.report({
60+
messageId: "noMissingIframeSandbox",
61+
node: sandboxAttribute.attributeValue?.node ?? sandboxAttribute.attribute,
62+
suggest: [{
63+
messageId: "addIframeSandbox",
64+
data: { value: "" },
65+
fix(fixer) {
66+
if (sandboxAttribute.attribute == null) return null;
67+
return fixer.replaceText(sandboxAttribute.attribute, `${sandboxAttribute.attributeName}=""`);
68+
},
69+
}],
70+
});
9071
},
9172
};
9273
}

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

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
import type { unit } from "@eslint-react/eff";
12
import type { RuleContext, RuleFeature } from "@eslint-react/kit";
3+
24
import type { RuleListener } from "@typescript-eslint/utils/ts-eslint";
35
import type { CamelCase } from "string-ts";
4-
import * as ER from "@eslint-react/core";
5-
6-
import { createJsxElementResolver, createRule, findCustomComponentProp } from "../utils";
6+
import { createJsxElementResolver, createRule, resolveAttribute } from "../utils";
77

88
export const RULE_NAME = "no-unsafe-iframe-sandbox";
99

@@ -15,7 +15,7 @@ const unsafeSandboxValues = [
1515
["allow-scripts", "allow-same-origin"],
1616
] as const;
1717

18-
function hasSafeSandbox(value: unknown) {
18+
function isSafeSandbox(value: string | unit): value is string {
1919
if (typeof value !== "string") return false;
2020
return !unsafeSandboxValues.some((values) => {
2121
return values.every((v) => value.includes(v));
@@ -45,33 +45,11 @@ export function create(context: RuleContext<MessageID, []>): RuleListener {
4545
JSXElement(node) {
4646
const { attributes, domElementType } = resolver.resolve(node);
4747
if (domElementType !== "iframe") return;
48-
const customComponentProp = findCustomComponentProp("sandbox", attributes);
49-
const propNameOnJsx = customComponentProp?.name ?? "sandbox";
50-
const attributeNode = ER.getAttribute(
51-
context,
52-
propNameOnJsx,
53-
node.openingElement.attributes,
54-
context.sourceCode.getScope(node),
55-
);
56-
if (attributeNode != null) {
57-
const attributeValue = ER.getAttributeValue(
58-
context,
59-
attributeNode,
60-
propNameOnJsx,
61-
);
62-
if (attributeValue.kind === "some" && !hasSafeSandbox(attributeValue.value)) {
63-
context.report({
64-
messageId: "noUnsafeIframeSandbox",
65-
node: attributeNode,
66-
});
67-
return;
68-
}
69-
}
70-
if (customComponentProp?.defaultValue == null) return;
71-
if (!hasSafeSandbox(customComponentProp.defaultValue)) {
48+
const sandboxAttribute = resolveAttribute(context, attributes, node, "sandbox");
49+
if (!isSafeSandbox(sandboxAttribute.attributeValueString)) {
7250
context.report({
7351
messageId: "noUnsafeIframeSandbox",
74-
node,
52+
node: sandboxAttribute.attributeValue?.node ?? sandboxAttribute.attribute ?? node,
7553
});
7654
}
7755
},

0 commit comments

Comments
 (0)