Skip to content
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

feat: add autofix for logical to inline properties in valid-styles rule #805

Merged
merged 3 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
81 changes: 81 additions & 0 deletions packages/eslint-plugin/__tests__/stylex-valid-styles-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,21 @@ eslintTester.run('stylex-valid-styles', rule.default, {
}
});
`,
`
import stylex from "stylex";
const styles = stylex.create({
validStyle: {
marginInlineStart: "10px",
marginInlineEnd: "5px",
marginInline: "15px",
marginBlock: "20px",
paddingInlineStart: "8px",
paddingInlineEnd: "12px",
paddingInline: "10px",
paddingBlock: "16px",
},
});
`,
`
import stylex from 'stylex';
const start = 'start';
Expand Down Expand Up @@ -583,6 +598,72 @@ eslintTester.run('stylex-valid-styles', rule.default, {
},
],
},
{
code: `
import stylex from 'stylex';
const styles = stylex.create({
invalidStyle: {
marginStart: '10px',
marginEnd: '5px',
marginHorizontal: '15px',
marginVertical: '15px',
paddingStart: '10px',
paddingEnd: '5px',
paddingHorizontal: '5px',
paddingVertical: '15px',
},
});
`,
errors: [
{
message:
'The key "marginStart" is not allowed by stylex. Use "marginInlineStart" instead.',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe something like this:

"The key 'marginStart' is not a standard CSS property. Did you mean 'marginInlineStart'?"

Communicating that it's non-standard is useful context for people

},
{
message:
'The key "marginEnd" is not allowed by stylex. Use "marginInlineEnd" instead.',
},
{
message:
'The key "marginHorizontal" is not allowed by stylex. Use "marginInline" instead.',
},
{
message:
'The key "marginVertical" is not allowed by stylex. Use "marginBlock" instead.',
},
{
message:
'The key "paddingStart" is not allowed by stylex. Use "paddingInlineStart" instead.',
},
{
message:
'The key "paddingEnd" is not allowed by stylex. Use "paddingInlineEnd" instead.',
},
{
message:
'The key "paddingHorizontal" is not allowed by stylex. Use "paddingInline" instead.',
},
{
message:
'The key "paddingVertical" is not allowed by stylex. Use "paddingBlock" instead.',
},
],
output: `
import stylex from 'stylex';
const styles = stylex.create({
invalidStyle: {
marginInlineStart: '10px',
marginInlineEnd: '5px',
marginInline: '15px',
marginBlock: '15px',
paddingInlineStart: '10px',
paddingInlineEnd: '5px',
paddingInline: '5px',
paddingBlock: '15px',
},
});
`,
},
{
code: "import stylex from 'stylex'; stylex.create({default: {textAlign: 'lfet'}});",
errors: [
Expand Down
45 changes: 44 additions & 1 deletion packages/eslint-plugin/src/stylex-valid-styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -1595,6 +1595,17 @@ const SupportedVendorSpecificCSSProperties = {
),
};

const validStyleMapping: $ReadOnly<{ [key: string]: ?string }> = {
marginStart: 'marginInlineStart',
marginEnd: 'marginInlineEnd',
marginHorizontal: 'marginInline',
marginVertical: 'marginBlock',
paddingStart: 'paddingInlineStart',
paddingEnd: 'paddingInlineEnd',
paddingHorizontal: 'paddingInline',
paddingVertical: 'paddingBlock',
};

const SVGProperties = {
colorInterpolation: makeUnionRule('auto', 'sRGB', 'linearRGB'),
// colorRendering: color,
Expand Down Expand Up @@ -2292,6 +2303,7 @@ const stylexValidStyles = {
meta: {
type: 'problem',
hasSuggestions: true,
fixable: 'code',
docs: {
descriptions: 'Enforce that you create valid stylex styles',
category: 'Possible Errors',
Expand Down Expand Up @@ -2635,10 +2647,41 @@ const stylexValidStyles = {
const distance = getDistance(key, cssProp, 2);
return distance <= 2;
});

const replacementKey =
style.key.type === 'Identifier' && validStyleMapping[style.key.name]
? validStyleMapping[style.key.name]
: style.key.type === 'Literal' &&
typeof style.key.value === 'string' &&
validStyleMapping[style.key.value]
? validStyleMapping[style.key.value]
: null;

let originalKey = '';

if (style.key.type === 'Identifier') {
originalKey = style.key.name;
} else if (
style.key.type === 'Literal' &&
typeof style.key.value === 'string'
) {
originalKey = style.key.value;
}

return context.report({
node: style.key,
loc: style.key.loc,
message: 'This is not a key that is allowed by stylex',
message:
replacementKey &&
(style.key.type === 'Identifier' || style.key.type === 'Literal')
? `The key "${originalKey}" is not allowed by stylex. Use "${replacementKey}" instead.`
: 'This is not a key that is allowed by stylex',
fix: (fixer) => {
if (replacementKey) {
return fixer.replaceText(style.key, replacementKey);
}
return null;
},
suggest:
closestKey != null
? [
Expand Down
Loading