Skip to content

refactor: code optimizations #1140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 21, 2025
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,8 +1,7 @@
import type { RuleContext, RuleFeature, RuleSuggest } from "@eslint-react/kit";
import type { RuleFixer, RuleListener } from "@typescript-eslint/utils/ts-eslint";
import type { CamelCase } from "string-ts";
import * as ER from "@eslint-react/core";
import { createJsxElementResolver, createRule, findCustomComponentProp } from "../utils";
import { createJsxElementResolver, createRule, resolveAttribute } from "../utils";

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

Expand Down Expand Up @@ -41,40 +40,26 @@ export function create(context: RuleContext<MessageID, []>): RuleListener {
JSXElement(node) {
const { attributes, domElementType } = resolver.resolve(node);
if (domElementType !== "button") return;
const customComponentProp = findCustomComponentProp("type", attributes);
const propNameOnJsx = customComponentProp?.name ?? "type";
const attributeNode = ER.getAttribute(
context,
propNameOnJsx,
node.openingElement.attributes,
context.sourceCode.getScope(node),
);
if (attributeNode != null) {
const attributeValue = ER.getAttributeValue(
context,
attributeNode,
propNameOnJsx,
);
if (attributeValue.kind !== "some" || typeof attributeValue.value !== "string") {
context.report({
messageId: "noMissingButtonType",
node: attributeNode,
suggest: getSuggest((type) => (fixer: RuleFixer) => {
return fixer.replaceText(attributeNode, `${propNameOnJsx}="${type}"`);
}),
});
}
return;
}
if (typeof customComponentProp?.defaultValue !== "string") {
const typeAttribute = resolveAttribute(context, attributes, node, "type");
if (typeAttribute.attributeValueString != null) return;
if (typeAttribute.attribute == null) {
context.report({
messageId: "noMissingButtonType",
node,
node: node.openingElement,
suggest: getSuggest((type) => (fixer: RuleFixer) => {
return fixer.insertTextAfter(node.openingElement.name, ` ${propNameOnJsx}="${type}"`);
return fixer.insertTextAfter(node.openingElement.name, ` ${typeAttribute.attributeName}="${type}"`);
}),
});
return;
}
context.report({
messageId: "noMissingButtonType",
node: typeAttribute.attributeValue?.node ?? typeAttribute.attribute,
suggest: getSuggest((type) => (fixer: RuleFixer) => {
if (typeAttribute.attribute == null) return null;
return fixer.replaceText(typeAttribute.attribute, `${typeAttribute.attributeName}="${type}"`);
}),
});
},
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import type { RuleContext, RuleFeature } from "@eslint-react/kit";
import type { RuleListener } from "@typescript-eslint/utils/ts-eslint";
import type { CamelCase } from "string-ts";
import * as ER from "@eslint-react/core";

import { createJsxElementResolver, createRule, findCustomComponentProp } from "../utils";
import { createJsxElementResolver, createRule, resolveAttribute } from "../utils";

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

Expand Down Expand Up @@ -41,52 +40,34 @@ export function create(context: RuleContext<MessageID, []>): RuleListener {
JSXElement(node) {
const { attributes, domElementType } = resolver.resolve(node);
if (domElementType !== "iframe") return;
const customComponentProp = findCustomComponentProp("sandbox", attributes);
const propNameOnJsx = customComponentProp?.name ?? "sandbox";
const attributeNode = ER.getAttribute(
context,
propNameOnJsx,
node.openingElement.attributes,
context.sourceCode.getScope(node),
);
if (attributeNode != null) {
const attributeValue = ER.getAttributeValue(
context,
attributeNode,
propNameOnJsx,
);
if (attributeValue.kind !== "some" || typeof attributeValue.value !== "string") {
context.report({
messageId: "noMissingIframeSandbox",
node: attributeNode,
suggest: [
{
messageId: "addIframeSandbox",
data: { value: "" },
fix(fixer) {
return fixer.replaceText(attributeNode, `${propNameOnJsx}=""`);
},
},
],
});
}
return;
}
if (typeof customComponentProp?.defaultValue !== "string") {
const sandboxAttribute = resolveAttribute(context, attributes, node, "sandbox");
if (sandboxAttribute.attributeValueString != null) return;
if (sandboxAttribute.attribute == null) {
context.report({
messageId: "noMissingIframeSandbox",
node,
suggest: [
{
messageId: "addIframeSandbox",
data: { value: "" },
fix(fixer) {
return fixer.insertTextAfter(node.openingElement.name, ` ${propNameOnJsx}=""`);
},
node: node.openingElement,
suggest: [{
messageId: "addIframeSandbox",
data: { value: "" },
fix(fixer) {
return fixer.insertTextAfter(node.openingElement.name, ` ${sandboxAttribute.attributeName}=""`);
},
],
}],
});
return;
}
context.report({
messageId: "noMissingIframeSandbox",
node: sandboxAttribute.attributeValue?.node ?? sandboxAttribute.attribute,
suggest: [{
messageId: "addIframeSandbox",
data: { value: "" },
fix(fixer) {
if (sandboxAttribute.attribute == null) return null;
return fixer.replaceText(sandboxAttribute.attribute, `${sandboxAttribute.attributeName}=""`);
},
}],
});
},
};
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { unit } from "@eslint-react/eff";
import type { RuleContext, RuleFeature } from "@eslint-react/kit";

import type { RuleListener } from "@typescript-eslint/utils/ts-eslint";
import type { CamelCase } from "string-ts";
import * as ER from "@eslint-react/core";

import { createJsxElementResolver, createRule, findCustomComponentProp } from "../utils";
import { createJsxElementResolver, createRule, resolveAttribute } from "../utils";

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

Expand All @@ -15,7 +15,7 @@ const unsafeSandboxValues = [
["allow-scripts", "allow-same-origin"],
] as const;

function hasSafeSandbox(value: unknown) {
function isSafeSandbox(value: string | unit): value is string {
if (typeof value !== "string") return false;
return !unsafeSandboxValues.some((values) => {
return values.every((v) => value.includes(v));
Expand Down Expand Up @@ -45,33 +45,11 @@ export function create(context: RuleContext<MessageID, []>): RuleListener {
JSXElement(node) {
const { attributes, domElementType } = resolver.resolve(node);
if (domElementType !== "iframe") return;
const customComponentProp = findCustomComponentProp("sandbox", attributes);
const propNameOnJsx = customComponentProp?.name ?? "sandbox";
const attributeNode = ER.getAttribute(
context,
propNameOnJsx,
node.openingElement.attributes,
context.sourceCode.getScope(node),
);
if (attributeNode != null) {
const attributeValue = ER.getAttributeValue(
context,
attributeNode,
propNameOnJsx,
);
if (attributeValue.kind === "some" && !hasSafeSandbox(attributeValue.value)) {
context.report({
messageId: "noUnsafeIframeSandbox",
node: attributeNode,
});
return;
}
}
if (customComponentProp?.defaultValue == null) return;
if (!hasSafeSandbox(customComponentProp.defaultValue)) {
const sandboxAttribute = resolveAttribute(context, attributes, node, "sandbox");
if (!isSafeSandbox(sandboxAttribute.attributeValueString)) {
context.report({
messageId: "noUnsafeIframeSandbox",
node,
node: sandboxAttribute.attributeValue?.node ?? sandboxAttribute.attribute ?? node,
});
}
},
Expand Down
Loading
Loading