Skip to content

Commit acefbe0

Browse files
chore(mosaic): lint raw buttons, svgs and inputs in features and blocks (#10805)
1 parent 7443113 commit acefbe0

7 files changed

Lines changed: 190 additions & 42 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
---

‎packages/mosaic/eslint/__tests__/boundaries.test.mjs‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ ruleTester.run('dependencies', boundaries.rules.dependencies, {
251251
import { useUser as user } from '@clerk/shared/react';
252252
import { useMosaicRouter as router } from '../../hooks/use-mosaic-router';
253253
`,
254-
errors: [/useMessages\(\)/, /model/, /model/],
254+
errors: [/^Read copy with `useMessages\('<namespace>'\)` from src\/localization/, /model/, /model/],
255255
},
256256
{
257257
name: 'sample.view.tsx rejects catalog values and Clerk hooks',
@@ -261,7 +261,7 @@ ruleTester.run('dependencies', boundaries.rules.dependencies, {
261261
import { useUser as user } from '@clerk/shared/react';
262262
import { useMosaicRouter as router } from '../../hooks/use-mosaic-router';
263263
`,
264-
errors: [/useMessages\(\)/, /model/, /model/],
264+
errors: [/^Read copy with `useMessages\('<namespace>'\)` from src\/localization/, /model/, /model/],
265265
},
266266
{
267267
name: 'sample.controller.ts rejects catalog values and Clerk hooks',
@@ -271,7 +271,7 @@ ruleTester.run('dependencies', boundaries.rules.dependencies, {
271271
import { useUser as user } from '@clerk/shared/react';
272272
import { useMosaicRouter as router } from '../../hooks/use-mosaic-router';
273273
`,
274-
errors: [/useMessages\(\)/, /model/, /model/],
274+
errors: [/^Read copy with `useMessages\('<namespace>'\)` from src\/localization/, /model/, /model/],
275275
},
276276
{
277277
name: 'sample.controller.tsx rejects catalog values and Clerk hooks',
@@ -281,7 +281,7 @@ ruleTester.run('dependencies', boundaries.rules.dependencies, {
281281
import { useUser as user } from '@clerk/shared/react';
282282
import { useMosaicRouter as router } from '../../hooks/use-mosaic-router';
283283
`,
284-
errors: [/useMessages\(\)/, /model/, /model/],
284+
errors: [/^Read copy with `useMessages\('<namespace>'\)` from src\/localization/, /model/, /model/],
285285
},
286286
{
287287
name: 'rejects catalog values and environment hooks but allows catalog types',
@@ -292,7 +292,7 @@ ruleTester.run('dependencies', boundaries.rules.dependencies, {
292292
import { useMosaicEnvironment } from '../../hooks/use-mosaic-environment';
293293
import { useMosaicSupportEmail } from '../../hooks/use-mosaic-support-email';
294294
`,
295-
errors: [/useMessages\(\)/, /model/, /model/],
295+
errors: [/^Read copy with `useMessages\('<namespace>'\)` from src\/localization/, /model/, /model/],
296296
},
297297
{
298298
name: 'rejects aliased useClerk in a controller',

‎packages/mosaic/eslint/__tests__/config.test.mjs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,14 +111,16 @@ describe('Mosaic rule configuration', () => {
111111
const overrides = config.overrides.filter(override => override.rules['eslint-compat/no-restricted-syntax']);
112112
expect(overrides.map(override => override.files)).toEqual([
113113
['src/**/*'],
114+
['src/features/**/*', 'src/blocks/**/*'],
114115
['src/**/*.styles.ts'],
115116
['src/primitives/**/*'],
116117
]);
117118
for (const override of overrides) {
118119
expect(override.rules['eslint-compat/no-restricted-syntax'][0]).toBe('error');
119120
}
120121
expect(overrides[0].excludeFiles).toEqual(['src/__tests__/**', 'src/**/*.test.{ts,tsx}']);
121-
expect(overrides[2].excludeFiles).toEqual(['src/**/*.test.{ts,tsx}']);
122+
expect(overrides[1].excludeFiles).toEqual(testFiles);
123+
expect(overrides[3].excludeFiles).toEqual(['src/**/*.test.{ts,tsx}']);
122124
});
123125

124126
test('inherits repository import restrictions and keeps Mosaic guards local', () => {

‎packages/mosaic/eslint/__tests__/rules.test.mjs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ ruleTester.run('no-dynamic-message-catalogs', plugin.rules['no-dynamic-message-c
4343
"import React from 'react'; React.useState(0);",
4444
],
4545
invalid: [
46-
{ code: "import('./reverification.messages');", errors: [{ messageId: 'useMessages' }] },
46+
{
47+
code: "import('./reverification.messages');",
48+
errors: [{ message: /^Read copy with `useMessages\('<namespace>'\)` from src\/localization/ }],
49+
},
4750
{ code: 'import(`./reverification.messages.ts`);', errors: [{ messageId: 'useMessages' }] },
4851
],
4952
});

‎packages/mosaic/eslint/__tests__/syntax.test.mjs‎

Lines changed: 97 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -288,19 +288,21 @@ describe('JSX guards', () => {
288288
name: 'keeps JSX restrictions: export const Sample = () => <form />;',
289289
filename: 'src/components/sample.tsx',
290290
code: 'export const Sample = () => <form />;',
291-
errors: [/Use `Form` from mosaic\/primitives\/form/],
291+
errors: [/^Use `Form` from src\/primitives\/form, driven by `useForm` from src\/components\/form/],
292292
},
293293
{
294294
name: "keeps JSX restrictions: export const Sample = ({ Part }: { Part: 'div' }) => <Part className='x' />;",
295295
filename: 'src/components/sample.tsx',
296296
code: "export const Sample = ({ Part }: { Part: 'div' }) => <Part className='x' />;",
297-
errors: [/Mosaic parts take `xstyle`/],
297+
errors: [
298+
/^Mosaic parts take `xstyle` instead of `className`\/`style`\. Define the atoms with `stylex\.create` in the sibling `\*\.styles\.ts` file/,
299+
],
298300
},
299301
{
300302
name: 'rejects native forms in views',
301303
filename: 'src/features/reverification/sample.view.tsx',
302304
code: 'export function View() { return <form />; }',
303-
errors: [/Use `Form` from mosaic\/primitives\/form/],
305+
errors: [/^Use `Form` from src\/primitives\/form, driven by `useForm` from src\/components\/form/],
304306
},
305307
{
306308
name: 'rejects logical AND in conditional JSX',
@@ -312,14 +314,104 @@ describe('JSX guards', () => {
312314
name: `rejects ${attribute} on member components`,
313315
filename: 'src/components/sample.tsx',
314316
code: `export const Sample = () => <Card.Root ${attribute} />;`,
315-
errors: [/Mosaic parts take `xstyle`/],
317+
errors: [
318+
/^Mosaic parts take `xstyle` instead of `className`\/`style`\. Define the atoms with `stylex\.create` in the sibling `\*\.styles\.ts` file/,
319+
],
316320
})),
317321
...['Part', 'Card.Root'].map(component => ({
318322
name: `rejects stylex.props spreads on ${component}`,
319323
filename: 'src/components/sample.tsx',
320324
code: `export const Sample = () => <${component} {...stylex.props(styles.root)} />;`,
321-
errors: [/Mosaic parts take the atoms as `xstyle`/],
325+
errors: [
326+
/^Mosaic parts take the atoms as `xstyle`, not a `stylex\.props\(\.\.\.\)` spread\. Define them with `stylex\.create` in the sibling `\*\.styles\.ts` file/,
327+
],
322328
})),
323329
].map(testCase => ({ ...testCase, options })),
324330
});
325331
});
332+
333+
describe('raw element guards in features and blocks', () => {
334+
const override = config.overrides.find(
335+
override => override.files.includes('src/features/**/*') && override.rules['eslint-compat/no-restricted-syntax'],
336+
);
337+
const options = override.rules['eslint-compat/no-restricted-syntax'].slice(1);
338+
ruleTester.run('no-restricted-syntax', syntaxPlugin.rules['no-restricted-syntax'], {
339+
valid: [
340+
"export const View = () => <Menu.Item render={<button type='button' onClick={onPick} />} />;",
341+
'export const View = () => <Part render={<svg aria-hidden />} />;',
342+
"export const View = () => <input type='hidden' name='id' value={id} />;",
343+
"export const View = () => <input readOnly hidden name='identifier' value={identifier} />;",
344+
"export const View = () => <input type='password' hidden name='password' />;",
345+
'export const View = () => <Menu.Item render={props => <button {...props} />} />;',
346+
'export const View = () => <Part render={function render(props) { return <svg {...props} />; }} />;',
347+
'export const View = () => <Part render={(props, state) => { return <input {...props} />; }} />;',
348+
"export const View = () => <input hidden={true} name='identifier' />;",
349+
"export const View = () => <Button type='button' />;",
350+
"export const View = () => <Icon name='check' />;",
351+
].flatMap(code =>
352+
['src/features/sample/sample.view.tsx', 'src/blocks/sample/sample.tsx'].map(filename => ({
353+
code,
354+
filename,
355+
options,
356+
})),
357+
),
358+
invalid: [
359+
{
360+
name: 'rejects a raw button',
361+
code: "export const View = () => <button type='button' />;",
362+
errors: [/^Use `Button` from src\/components\/button, or `SubmitButton` when the click runs an async action/],
363+
},
364+
{
365+
name: 'rejects a raw button nested in a render prop element',
366+
code: "export const View = () => <Part render={<div><button type='button' /></div>} />;",
367+
errors: [/^Use `Button` from src\/components\/button/],
368+
},
369+
{
370+
name: 'rejects a raw button nested in a render callback element',
371+
code: 'export const View = () => <Part render={props => <div {...props}><button type="button" /></div>} />;',
372+
errors: [/^Use `Button` from src\/components\/button/],
373+
},
374+
{
375+
name: 'rejects a visible input with hidden={false}',
376+
code: "export const View = () => <input hidden={false} name='field' />;",
377+
errors: [/^Use `Input` from src\/components\/input inside `Field`/],
378+
},
379+
{
380+
name: 'rejects a visible password input with hidden={false}',
381+
code: "export const View = () => <input type='password' hidden={false} name='field' />;",
382+
errors: [/^Use `PasswordInput` from src\/components\/password-input/],
383+
},
384+
{
385+
name: 'rejects a raw svg',
386+
code: 'export const View = () => <svg viewBox="0 0 16 16"><path d="M0 0" /></svg>;',
387+
errors: [/^Use an icon from src\/icons through `Icon`.*stop and ask the user/],
388+
},
389+
...[
390+
["type='password'", /^Use `PasswordInput` from src\/components\/password-input inside `Field`/],
391+
["type='tel'", /^Use `PhoneInput` from src\/components\/phone-input inside `Field`/],
392+
["type='checkbox'", /^Use `Checkbox` from src\/components\/checkbox, or `Switch`/],
393+
["type='radio'", /^Use `Radio` from src\/components\/radio/],
394+
["type='file'", /^Use `FileUpload` from src\/primitives\/file-upload, or `AvatarUpload`/],
395+
["type='email'", /^Use `Input` from src\/components\/input inside `Field`/],
396+
["type='text'", /^Use `Input` from src\/components\/input inside `Field`/],
397+
['', /^Use `Input` from src\/components\/input inside `Field`/],
398+
].map(([type, message]) => ({
399+
name: `rejects a visible input: <input ${type} />`,
400+
code: `export const View = () => <input ${type} name='field' />;`,
401+
errors: [message],
402+
})),
403+
{
404+
name: 'keeps the shared JSX guards',
405+
code: 'export const View = () => <form />;',
406+
errors: [/^Use `Form` from src\/primitives\/form, driven by `useForm` from src\/components\/form/],
407+
},
408+
].flatMap(testCase =>
409+
['src/features/sample/sample.view.tsx', 'src/blocks/sample/sample.tsx'].map(filename => ({
410+
...testCase,
411+
name: `${testCase.name} in ${filename}`,
412+
filename,
413+
options,
414+
})),
415+
),
416+
});
417+
});

‎packages/mosaic/eslint/rules.mjs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ export const noDynamicMessageCatalogs = {
5757
type: 'problem',
5858
schema: [],
5959
messages: {
60-
useMessages: 'Use useMessages() for localized and overridden copy instead of importing catalog values.',
60+
useMessages:
61+
"Read copy with `useMessages('<namespace>')` from src/localization, so the customer's locale and overrides apply. Do not import a `*.messages.ts` value.",
6162
},
6263
},
6364
create(context) {

‎packages/mosaic/oxlint.config.mjs‎

Lines changed: 77 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,73 @@ const noLeakedConditionalJsx = {
2323
'Use a ternary (`condition ? <Element /> : null`) for conditional JSX; `&&` renders a falsy left-hand value such as `0`.',
2424
};
2525

26+
const jsxGuards = [
27+
noLeakedConditionalJsx,
28+
{
29+
selector: "JSXOpeningElement[name.name='form']",
30+
message:
31+
'Use `Form` from src/primitives/form, driven by `useForm` from src/components/form for values, validation and submit.',
32+
},
33+
{
34+
selector:
35+
"JSXOpeningElement[name.type='JSXIdentifier'][name.name=/^[A-Z]/] > JSXAttribute[name.name=/^(className|style)$/]",
36+
message:
37+
'Mosaic parts take `xstyle` instead of `className`/`style`. Define the atoms with `stylex.create` in the sibling `*.styles.ts` file and pass `xstyle={styles.root}`.',
38+
},
39+
{
40+
selector: "JSXOpeningElement[name.type='JSXMemberExpression'] > JSXAttribute[name.name=/^(className|style)$/]",
41+
message:
42+
'Mosaic parts take `xstyle` instead of `className`/`style`. Define the atoms with `stylex.create` in the sibling `*.styles.ts` file and pass `xstyle={styles.root}`.',
43+
},
44+
{
45+
selector:
46+
"JSXOpeningElement[name.type='JSXIdentifier'][name.name=/^[A-Z]/] > JSXSpreadAttribute > CallExpression[callee.object.name='stylex'][callee.property.name='props']",
47+
message:
48+
'Mosaic parts take the atoms as `xstyle`, not a `stylex.props(...)` spread. Define them with `stylex.create` in the sibling `*.styles.ts` file and pass `xstyle={styles.root}`.',
49+
},
50+
{
51+
selector:
52+
"JSXOpeningElement[name.type='JSXMemberExpression'] > JSXSpreadAttribute > CallExpression[callee.object.name='stylex'][callee.property.name='props']",
53+
message:
54+
'Mosaic parts take the atoms as `xstyle`, not a `stylex.props(...)` spread. Define them with `stylex.create` in the sibling `*.styles.ts` file and pass `xstyle={styles.root}`.',
55+
},
56+
];
57+
const renderProp = "JSXAttribute[name.name='render'] > JSXExpressionContainer";
58+
const renderCallback = `${renderProp} > :matches(ArrowFunctionExpression, FunctionExpression)`;
59+
const notInRenderProp = `:not(${renderProp} > JSXElement > JSXOpeningElement, ${renderCallback} > JSXElement > JSXOpeningElement, ${renderCallback} > BlockStatement > ReturnStatement > JSXElement > JSXOpeningElement)`;
60+
const visibleInput = `JSXOpeningElement[name.name='input']${notInRenderProp}:not(:has(> JSXAttribute[name.name='hidden']:matches([value=null], [value.expression.value=true])))`;
61+
const inputOfType = type => `${visibleInput}:has(> JSXAttribute[name.name='type'][value.value='${type}'])`;
62+
const inputTypeGuards = [
63+
['password', 'Use `PasswordInput` from src/components/password-input inside `Field` from src/components/field.'],
64+
['tel', 'Use `PhoneInput` from src/components/phone-input inside `Field` from src/components/field.'],
65+
[
66+
'checkbox',
67+
'Use `Checkbox` from src/components/checkbox, or `Switch` from src/components/switch for an on/off setting.',
68+
],
69+
['radio', 'Use `Radio` from src/components/radio.'],
70+
[
71+
'file',
72+
'Use `FileUpload` from src/primitives/file-upload, or `AvatarUpload` from src/blocks/avatar-upload for an avatar or logo.',
73+
],
74+
];
75+
const rawElementGuards = [
76+
{
77+
selector: `JSXOpeningElement[name.name='button']${notInRenderProp}`,
78+
message:
79+
'Use `Button` from src/components/button, or `SubmitButton` when the click runs an async action (it shows the pending state for you).',
80+
},
81+
{
82+
selector: `JSXOpeningElement[name.name='svg']${notInRenderProp}`,
83+
message:
84+
'Use an icon from src/icons through `Icon` from src/components/icon. If no icon fits, stop and ask the user; do not add a glyph or draw an `<svg>`.',
85+
},
86+
...inputTypeGuards.map(([type, message]) => ({ selector: inputOfType(type), message })),
87+
{
88+
selector: `${visibleInput}:not(:has(> JSXAttribute[name.name='type'][value.value=/^(hidden|${inputTypeGuards.map(([type]) => type).join('|')})$/]))`,
89+
message: 'Use `Input` from src/components/input inside `Field` from src/components/field.',
90+
},
91+
];
92+
2693
export default defineConfig({
2794
...rootConfig,
2895
plugins: [...rootConfig.plugins, 'vitest'],
@@ -136,34 +203,14 @@ export default defineConfig({
136203
'@stylexjs/sort-keys': 'error',
137204
'@stylexjs/valid-shorthands': 'error',
138205
'@stylexjs/valid-styles': 'error',
139-
'eslint-compat/no-restricted-syntax': [
140-
'error',
141-
noLeakedConditionalJsx,
142-
{
143-
selector: "JSXOpeningElement[name.name='form']",
144-
message: 'Use `Form` from mosaic/primitives/form, which turns off the browser validation popup.',
145-
},
146-
{
147-
selector:
148-
"JSXOpeningElement[name.type='JSXIdentifier'][name.name=/^[A-Z]/] > JSXAttribute[name.name=/^(className|style)$/]",
149-
message: 'Mosaic parts take `xstyle` (StyleX atoms) instead of `className`/`style`.',
150-
},
151-
{
152-
selector:
153-
"JSXOpeningElement[name.type='JSXMemberExpression'] > JSXAttribute[name.name=/^(className|style)$/]",
154-
message: 'Mosaic parts take `xstyle` (StyleX atoms) instead of `className`/`style`.',
155-
},
156-
{
157-
selector:
158-
"JSXOpeningElement[name.type='JSXIdentifier'][name.name=/^[A-Z]/] > JSXSpreadAttribute > CallExpression[callee.object.name='stylex'][callee.property.name='props']",
159-
message: 'Mosaic parts take the atoms as `xstyle`, not a `stylex.props(...)` spread.',
160-
},
161-
{
162-
selector:
163-
"JSXOpeningElement[name.type='JSXMemberExpression'] > JSXSpreadAttribute > CallExpression[callee.object.name='stylex'][callee.property.name='props']",
164-
message: 'Mosaic parts take the atoms as `xstyle`, not a `stylex.props(...)` spread.',
165-
},
166-
],
206+
'eslint-compat/no-restricted-syntax': ['error', ...jsxGuards],
207+
},
208+
},
209+
{
210+
files: ['src/features/**/*', 'src/blocks/**/*'],
211+
excludeFiles: ['src/**/__tests__/**', 'src/**/*.test.{ts,tsx}'],
212+
rules: {
213+
'eslint-compat/no-restricted-syntax': ['error', ...jsxGuards, ...rawElementGuards],
167214
},
168215
},
169216
{
@@ -327,7 +374,8 @@ export default defineConfig({
327374
to: { file: { categories: 'messages' } },
328375
dependency: { kind: 'value', nodeKind: ['import', 'export'] },
329376
},
330-
message: 'Use useMessages() for localized and overridden copy instead of importing catalog values.',
377+
message:
378+
"Read copy with `useMessages('<namespace>')` from src/localization, so the customer's locale and overrides apply. Do not import a `*.messages.ts` value.",
331379
},
332380
{
333381
from: FLOW_UI,

0 commit comments

Comments
 (0)