Skip to content

Commit

Permalink
Change rule to allow key before spread
Browse files Browse the repository at this point in the history
  • Loading branch information
oskarscholander committed Nov 14, 2024
1 parent f5ea003 commit 626b713
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/rules/alwaysSpreadJSXPropsFirst.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,27 @@ export const alwaysSpreadJSXPropsFirst = ESLintUtils.RuleCreator.withoutDocs({
return;
}

context.report({
node,
messageId: "alwaysSpreadJSXPropsFirst",
});
const beforeSpread = attributes.slice(0, spreadAttributeIndex);

const hasOnlyKeyBeforeSpread = beforeSpread.every(
(attribute) =>
attribute.type === "JSXAttribute" &&
attribute.name.name === "key"
);

if (!hasOnlyKeyBeforeSpread) {
context.report({
node,
messageId: "alwaysSpreadJSXPropsFirst",
});
}
},
};
},
meta: {
messages: {
alwaysSpreadJSXPropsFirst:
"Always put JSX spread props first to avoid unintended behavior",
"Only 'key' prop is allowed before JSX spread props.",
},
type: "problem",
schema: [],
Expand Down
4 changes: 4 additions & 0 deletions src/tests/alwaysSpreadPropsFirst.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ const ruleTester = new RuleTester({

ruleTester.run("alwaysSpreadJSXPropsFirst", alwaysSpreadJSXPropsFirst, {
valid: [
{
code: "<Component key={key} {...props} myProp={myValue} />",
parserOptions: { ecmaFeatures: { jsx: true } },
},
{
code: "<Component {...props} myProp={myValue} />",
parserOptions: { ecmaFeatures: { jsx: true } },
Expand Down

0 comments on commit 626b713

Please sign in to comment.