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
Expand Up @@ -54,5 +54,24 @@ ruleTester.run(RULE_NAME, rule, {
return <div style={someStyle} />;
}
`,
{
// https://github.com/Rel1cx/eslint-react/issues/1217
code: tsx`
const a = <StatusBar style="auto" />;
`,
settings: {
"react-x": {
additionalComponents: [
{
name: "StatusBar",
attributes: [
// inform that the style attribute on StatusBar is not an intrinsic attribute but a custom one
{ name: "", as: "style" },
],
},
],
},
},
},
],
});
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { RuleContext, RuleFeature } from "@eslint-react/kit";
import type { TSESTree } from "@typescript-eslint/types";

Check warning on line 2 in packages/plugins/eslint-plugin-react-dom/src/rules/no-string-style-prop.ts

View workflow job for this annotation

GitHub Actions / check

'TSESTree' is defined but never used

Check warning on line 2 in packages/plugins/eslint-plugin-react-dom/src/rules/no-string-style-prop.ts

View workflow job for this annotation

GitHub Actions / Publish

'TSESTree' is defined but never used
import type { RuleListener } from "@typescript-eslint/utils/ts-eslint";
import type { CamelCase } from "string-ts";

import { AST_NODE_TYPES as T } from "@typescript-eslint/types";

Check warning on line 6 in packages/plugins/eslint-plugin-react-dom/src/rules/no-string-style-prop.ts

View workflow job for this annotation

GitHub Actions / check

'T' is defined but never used

Check warning on line 6 in packages/plugins/eslint-plugin-react-dom/src/rules/no-string-style-prop.ts

View workflow job for this annotation

GitHub Actions / Publish

'T' is defined but never used
import { createRule } from "../utils";
import { createJsxElementResolver, createRule, resolveAttribute } from "../utils";

export const RULE_NAME = "no-string-style-prop";

Expand All @@ -30,33 +30,23 @@
});

export function create(context: RuleContext<MessageID, []>): RuleListener {
const resolver = createJsxElementResolver(context);
return {
JSXAttribute(node) {
if (node.name.name !== "style") return;
const styleText = getAttributeValueText(context, node.value);
if (styleText == null) return;
JSXElement(node) {
const { attributes } = resolver.resolve(node);
const {
attribute,
attributeName,
attributeValue,
attributeValueString,
} = resolveAttribute(context, attributes, node, "style");
if (attributeName !== "style") return;
if (attribute == null || attributeValue?.node == null) return;
if (attributeValueString == null) return;
context.report({
messageId: "noStringStyleProp",
node,
node: attributeValue.node,
});
},
};
}

function getAttributeValueText(context: RuleContext, node: TSESTree.JSXAttribute["value"]) {
if (node == null) return null;
switch (true) {
case node.type === T.Literal
&& typeof node.value === "string":
return context.sourceCode.getText(node);
case node.type === T.JSXExpressionContainer
&& node.expression.type === T.Literal
&& typeof node.expression.value === "string":
return context.sourceCode.getText(node.expression);
case node.type === T.JSXExpressionContainer
&& node.expression.type === T.TemplateLiteral:
return context.sourceCode.getText(node.expression);
default:
return null;
}
}