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
@@ -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