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

fix(@stylex/eslint-plugin): Change message when a property is disallowed #283

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
9 changes: 3 additions & 6 deletions packages/eslint-plugin/__tests__/stylex-valid-styles-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1238,8 +1238,7 @@ revert`,
],
errors: [
{
message: `grid value must be one of:
grid properties disallowed for testing`,
message: 'Property grid is not allowed. grid properties disallowed for testing',
},
],
},
Expand All @@ -1265,8 +1264,7 @@ grid properties disallowed for testing`,
],
errors: [
{
message: `gridTemplateColumns value must be one of:
grid properties disallowed for testing`,
message: 'Property gridTemplateColumns is not allowed. grid properties disallowed for testing',
},
],
},
Expand All @@ -1287,8 +1285,7 @@ grid properties disallowed for testing`,
],
errors: [
{
message: `grid value must be one of:
This property is not supported in legacy StyleX resolution.`,
message: 'Property grid is not allowed. This property is not supported in legacy StyleX resolution.',
},
],
},
Expand Down
5 changes: 4 additions & 1 deletion packages/eslint-plugin/src/rules/makeUnionRule.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export default function makeUnionRule(
node: Expression | Pattern,
variables?: Variables,
prop?: Property,
key?: string,
): RuleResponse => {
const failedRules = [];
for (const _rule of rules) {
Expand All @@ -43,8 +44,10 @@ export default function makeUnionRule(
const fixable = failedRules.filter((a) => a.suggest != null);
fixable.sort((a, b) => (a.distance || Infinity) - (b.distance || Infinity));

const enumValues = failedRules.map((a) => a.message).join('\n')

return {
message: failedRules.map((a) => a.message).join('\n'),
message: key ? `${key} value must be one of:\n${enumValues}` : enumValues,
suggest: fixable[0] != null ? fixable[0].suggest : undefined,
};
};
Expand Down
7 changes: 4 additions & 3 deletions packages/eslint-plugin/src/stylex-valid-styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export type RuleCheck = (
node: $ReadOnly<Expression | Pattern>,
variables?: Variables,
prop?: $ReadOnly<Property>,
key?: string,
Copy link
Author

@tjosepo tjosepo Dec 30, 2023

Choose a reason for hiding this comment

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

I made this argument optional because I don't know how RuleCheck is being used throughout the codebase.

Should I make it required?

Copy link
Contributor

Choose a reason for hiding this comment

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

This OK to start. We can make this stricter in the future if it makes sense.

) => RuleResponse;
export type RuleResponse = void | {
message: string,
Expand All @@ -62,7 +63,7 @@ export type RuleResponse = void | {

const showError =
(message: string): RuleCheck =>
() => ({ message });
(_node, _variables, _props, key) => ({ message: key ? `Property ${key} is not allowed. ${message}` : message });

const isStringOrNumber = makeUnionRule(isString, isNumber);

Expand Down Expand Up @@ -2624,14 +2625,14 @@ const stylexValidStyles = {
}
}

const check = ruleChecker(style.value, varsWithFnArgs, style);
const check = ruleChecker(style.value, varsWithFnArgs, style, key);
if (check != null) {
const { message, suggest } = check;
return context.report(
({
node: style.value,
loc: style.value.loc,
message: `${key} value must be one of:\n${message}${
message: `${message}${
key === 'lineHeight'
? '\nBe careful when fixing: lineHeight: 10px is not the same as lineHeight: 10'
: ''
Expand Down
Loading