Skip to content

Commit 5d0beb1

Browse files
authored
Merge branch 'main' into jm-nested-consts1
2 parents ebf9aa1 + 36f66ff commit 5d0beb1

File tree

2 files changed

+383
-24
lines changed

2 files changed

+383
-24
lines changed

packages/@stylexjs/eslint-plugin/__tests__/stylex-enforce-extension-test.js

Lines changed: 286 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ const invalidFilenameWithoutRestrictedExports = (suggestedExtension) =>
2020
`Only variables from \`stylex.defineVars()\` or \`stylex.defineConsts()\` can be exported from a file with a \`${suggestedExtension}\` extension.`;
2121
const invalidExportFromThemeFiles =
2222
'Files that export variables from `stylex.defineVars()` or `stylex.defineConsts()` must not export anything else.';
23+
const invalidConstsFilenameWithRestrictedExports = (suggestedExtension) =>
24+
`Files that export variables from \`stylex.defineConsts()\` must end with a \`${suggestedExtension}\` extension.`;
25+
const invalidConstsFilenameWithoutRestrictedExports = (suggestedExtension) =>
26+
`Only variables from \`stylex.defineConsts()\` can be exported from a file with a \`${suggestedExtension}\` extension.`;
27+
const invalidExportFromConstsFiles =
28+
'Files that export variables from `stylex.defineConsts()` must not export anything else.';
2329

2430
ruleTester.run('stylex-enforce-extension', rule.default, {
2531
valid: [
@@ -198,6 +204,54 @@ ruleTester.run('stylex-enforce-extension', rule.default, {
198204
filename: 'myComponent.stylex.tsx',
199205
options: [{ legacyAllowMixedExports: true }],
200206
},
207+
{
208+
code: `
209+
import * as stylex from '@stylexjs/stylex';
210+
export const consts = stylex.defineConsts({ color: 'red' });
211+
`,
212+
filename: 'myComponent.stylex.const.jsx',
213+
options: [{ enforceDefineConstsExtension: true }],
214+
},
215+
{
216+
code: `
217+
import * as stylex from '@stylexjs/stylex';
218+
export const consts = stylex.defineConsts({ color: 'red' });
219+
`,
220+
filename: 'myComponent.stylex.const.js',
221+
options: [{ enforceDefineConstsExtension: true }],
222+
},
223+
{
224+
code: `
225+
import * as stylex from '@stylexjs/stylex';
226+
export const consts = stylex.defineConsts({ color: 'red' });
227+
`,
228+
filename: 'myComponent.stylex.const.tsx',
229+
options: [{ enforceDefineConstsExtension: true }],
230+
},
231+
{
232+
code: `
233+
import * as stylex from '@stylexjs/stylex';
234+
export const consts = stylex.defineConsts({ color: 'red' });
235+
`,
236+
filename: 'myComponent.stylex.const.ts',
237+
options: [{ enforceDefineConstsExtension: true }],
238+
},
239+
{
240+
code: `
241+
import * as stylex from '@stylexjs/stylex';
242+
export const consts = stylex.defineConsts({ color: 'red' });
243+
`,
244+
filename: 'myComponent.stylex.const.cjs',
245+
options: [{ enforceDefineConstsExtension: true }],
246+
},
247+
{
248+
code: `
249+
import * as stylex from '@stylexjs/stylex';
250+
export const consts = stylex.defineConsts({ color: 'red' });
251+
`,
252+
filename: 'myComponent.stylex.const.mjs',
253+
options: [{ enforceDefineConstsExtension: true }],
254+
},
201255
{
202256
code: `
203257
import * as stylex from '@stylexjs/stylex';
@@ -514,5 +568,237 @@ ruleTester.run('stylex-enforce-extension', rule.default, {
514568
options: [{ legacyAllowMixedExports: false }],
515569
errors: [{ message: invalidExportFromThemeFiles }],
516570
},
571+
{
572+
code: `
573+
import * as stylex from '@stylexjs/stylex';
574+
export const consts = stylex.defineConsts({ color: 'red' });
575+
`,
576+
filename: 'myComponent.jsx',
577+
options: [{ enforceDefineConstsExtension: true }],
578+
errors: [
579+
{
580+
message:
581+
invalidConstsFilenameWithRestrictedExports('.stylex.const.jsx'),
582+
},
583+
],
584+
},
585+
{
586+
code: `
587+
import * as stylex from '@stylexjs/stylex';
588+
export const consts = stylex.defineConsts({ color: 'red' });
589+
`,
590+
filename: 'myComponent.tsx',
591+
options: [{ enforceDefineConstsExtension: true }],
592+
errors: [
593+
{
594+
message:
595+
invalidConstsFilenameWithRestrictedExports('.stylex.const.tsx'),
596+
},
597+
],
598+
},
599+
{
600+
code: `
601+
import * as stylex from '@stylexjs/stylex';
602+
export const consts = stylex.defineConsts({ color: 'red' });
603+
`,
604+
filename: 'myComponent.js',
605+
options: [{ enforceDefineConstsExtension: true }],
606+
errors: [
607+
{
608+
message:
609+
invalidConstsFilenameWithRestrictedExports('.stylex.const.js'),
610+
},
611+
],
612+
},
613+
{
614+
code: `
615+
import * as stylex from '@stylexjs/stylex';
616+
export const consts = stylex.defineConsts({ color: 'red' });
617+
`,
618+
filename: 'myComponent.ts',
619+
options: [{ enforceDefineConstsExtension: true }],
620+
errors: [
621+
{
622+
message:
623+
invalidConstsFilenameWithRestrictedExports('.stylex.const.ts'),
624+
},
625+
],
626+
},
627+
{
628+
code: `
629+
import * as stylex from '@stylexjs/stylex';
630+
export const consts = stylex.defineConsts({ color: 'red' });
631+
`,
632+
filename: 'myComponent.jsx',
633+
options: [
634+
{ enforceDefineConstsExtension: true, themeFileExtension: '.custom' },
635+
],
636+
errors: [
637+
{
638+
message:
639+
invalidConstsFilenameWithRestrictedExports('.custom.const.jsx'),
640+
},
641+
],
642+
},
643+
{
644+
code: 'export const somethingElse = {};',
645+
filename: 'myComponent.stylex.const.jsx',
646+
options: [{ enforceDefineConstsExtension: true }],
647+
errors: [
648+
{
649+
message:
650+
invalidConstsFilenameWithoutRestrictedExports('.stylex.const.jsx'),
651+
},
652+
],
653+
},
654+
{
655+
code: 'export const somethingElse = {};',
656+
filename: 'myComponent.stylex.const.tsx',
657+
options: [{ enforceDefineConstsExtension: true }],
658+
errors: [
659+
{
660+
message:
661+
invalidConstsFilenameWithoutRestrictedExports('.stylex.const.tsx'),
662+
},
663+
],
664+
},
665+
{
666+
code: 'export const somethingElse = {};',
667+
filename: 'myComponent.stylex.const.js',
668+
options: [{ enforceDefineConstsExtension: true }],
669+
errors: [
670+
{
671+
message:
672+
invalidConstsFilenameWithoutRestrictedExports('.stylex.const.js'),
673+
},
674+
],
675+
},
676+
{
677+
code: 'export const somethingElse = {};',
678+
filename: 'myComponent.stylex.const.ts',
679+
options: [{ enforceDefineConstsExtension: true }],
680+
errors: [
681+
{
682+
message:
683+
invalidConstsFilenameWithoutRestrictedExports('.stylex.const.ts'),
684+
},
685+
],
686+
},
687+
{
688+
code: 'export const vars = stylex.defineVars({});',
689+
filename: 'myComponent.stylex.const.cjs',
690+
options: [{ enforceDefineConstsExtension: true }],
691+
errors: [
692+
{
693+
message:
694+
invalidConstsFilenameWithoutRestrictedExports('.stylex.const.cjs'),
695+
},
696+
],
697+
},
698+
{
699+
code: 'export const somethingElse = {};',
700+
filename: 'myComponent.stylex.const.mjs',
701+
options: [{ enforceDefineConstsExtension: true }],
702+
errors: [
703+
{
704+
message:
705+
invalidConstsFilenameWithoutRestrictedExports('.stylex.const.mjs'),
706+
},
707+
],
708+
},
709+
{
710+
code: 'export const vars = stylex.defineVars({});',
711+
filename: 'myComponent.custom.const.jsx',
712+
options: [
713+
{ enforceDefineConstsExtension: true, themeFileExtension: '.custom' },
714+
],
715+
errors: [
716+
{
717+
message:
718+
invalidConstsFilenameWithoutRestrictedExports('.custom.const.jsx'),
719+
},
720+
],
721+
},
722+
{
723+
code: `
724+
import * as stylex from '@stylexjs/stylex';
725+
export const consts = stylex.defineConsts({ color: 'red' });
726+
export const consts_2 = stylex.defineConsts({ color: 'red' });
727+
export const somethingElse = someFunction();
728+
`,
729+
filename: 'myComponent.stylex.const.jsx',
730+
options: [{ enforceDefineConstsExtension: true }],
731+
errors: [{ message: invalidExportFromConstsFiles }],
732+
},
733+
{
734+
code: `
735+
import * as stylex from '@stylexjs/stylex';
736+
export const consts = stylex.defineConsts({ color: 'red' });
737+
export const somethingElse = someFunction();
738+
`,
739+
filename: 'myComponent.stylex.const.tsx',
740+
options: [{ enforceDefineConstsExtension: true }],
741+
errors: [{ message: invalidExportFromConstsFiles }],
742+
},
743+
{
744+
code: `
745+
import * as stylex from '@stylexjs/stylex';
746+
export const consts = stylex.defineConsts({ color: 'red' });
747+
export const somethingElse = stylex.defineVars({ color: 'red' });
748+
`,
749+
filename: 'myComponent.stylex.const.js',
750+
options: [{ enforceDefineConstsExtension: true }],
751+
errors: [
752+
{
753+
message: invalidExportFromConstsFiles,
754+
},
755+
{
756+
message:
757+
'Files that export variables from `stylex.defineVars()` must end with a `.stylex.js` extension.',
758+
},
759+
],
760+
},
761+
{
762+
code: `
763+
import * as stylex from '@stylexjs/stylex';
764+
export const consts = stylex.defineConsts({ color: 'red' });
765+
export const somethingElse = someFunction();
766+
`,
767+
filename: 'myComponent.stylex.const.ts',
768+
options: [{ enforceDefineConstsExtension: true }],
769+
errors: [{ message: invalidExportFromConstsFiles }],
770+
},
771+
{
772+
code: `
773+
import * as stylex from '@stylexjs/stylex';
774+
export const consts = stylex.defineConsts({ color: 'red' });
775+
export const somethingElse = someFunction();
776+
`,
777+
filename: 'myComponent.stylex.const.cjs',
778+
options: [{ enforceDefineConstsExtension: true }],
779+
errors: [{ message: invalidExportFromConstsFiles }],
780+
},
781+
{
782+
code: `
783+
import * as stylex from '@stylexjs/stylex';
784+
export const consts = stylex.defineConsts({ color: 'red' });
785+
export const somethingElse = someFunction();
786+
`,
787+
filename: 'myComponent.stylex.const.mjs',
788+
options: [{ enforceDefineConstsExtension: true }],
789+
errors: [{ message: invalidExportFromConstsFiles }],
790+
},
791+
{
792+
code: `
793+
import * as stylex from '@stylexjs/stylex';
794+
export const consts = stylex.defineConsts({ color: 'red' });
795+
export const somethingElse = someFunction();
796+
`,
797+
filename: 'myComponent.custom.const.jsx',
798+
options: [
799+
{ enforceDefineConstsExtension: true, themeFileExtension: '.custom' },
800+
],
801+
errors: [{ message: invalidExportFromConstsFiles }],
802+
},
517803
],
518804
});

0 commit comments

Comments
 (0)