@antebudimir/eslint-plugin-vanilla-extract 1.13.0 → 1.15.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/README.md +150 -4
  2. package/dist/css-rules/prefer-logical-properties/_tests_/prefer-logical-properties.test.d.ts +1 -0
  3. package/dist/css-rules/prefer-logical-properties/_tests_/prefer-logical-properties.test.js +572 -0
  4. package/dist/css-rules/prefer-logical-properties/index.d.ts +2 -0
  5. package/dist/css-rules/prefer-logical-properties/index.js +2 -0
  6. package/dist/css-rules/prefer-logical-properties/logical-properties-processor.d.ts +19 -0
  7. package/dist/css-rules/prefer-logical-properties/logical-properties-processor.js +202 -0
  8. package/dist/css-rules/prefer-logical-properties/logical-properties-visitor-creator.d.ts +14 -0
  9. package/dist/css-rules/prefer-logical-properties/logical-properties-visitor-creator.js +67 -0
  10. package/dist/css-rules/prefer-logical-properties/property-mappings.d.ts +44 -0
  11. package/dist/css-rules/prefer-logical-properties/property-mappings.js +179 -0
  12. package/dist/css-rules/prefer-logical-properties/rule-definition.d.ts +3 -0
  13. package/dist/css-rules/prefer-logical-properties/rule-definition.js +35 -0
  14. package/dist/css-rules/prefer-theme-tokens/index.d.ts +2 -0
  15. package/dist/css-rules/prefer-theme-tokens/index.js +2 -0
  16. package/dist/css-rules/prefer-theme-tokens/rule-definition.d.ts +3 -0
  17. package/dist/css-rules/prefer-theme-tokens/rule-definition.js +119 -0
  18. package/dist/css-rules/prefer-theme-tokens/theme-contract-analyzer.d.ts +60 -0
  19. package/dist/css-rules/prefer-theme-tokens/theme-contract-analyzer.js +322 -0
  20. package/dist/css-rules/prefer-theme-tokens/theme-token-processor.d.ts +26 -0
  21. package/dist/css-rules/prefer-theme-tokens/theme-token-processor.js +562 -0
  22. package/dist/css-rules/prefer-theme-tokens/theme-token-visitor-creator.d.ts +6 -0
  23. package/dist/css-rules/prefer-theme-tokens/theme-token-visitor-creator.js +81 -0
  24. package/dist/css-rules/prefer-theme-tokens/value-evaluator.d.ts +43 -0
  25. package/dist/css-rules/prefer-theme-tokens/value-evaluator.js +190 -0
  26. package/dist/demo-bad-example.css.d.ts +4 -0
  27. package/dist/demo-bad-example.css.js +45 -0
  28. package/dist/demo-good-example.css.d.ts +5 -0
  29. package/dist/demo-good-example.css.js +48 -0
  30. package/dist/demo-theme.css.d.ts +37 -0
  31. package/dist/demo-theme.css.js +39 -0
  32. package/dist/index.d.ts +2 -0
  33. package/dist/index.js +5 -1
  34. package/dist/sample-bad.css.d.ts +26 -0
  35. package/dist/sample-bad.css.js +85 -0
  36. package/dist/sample-good.css.d.ts +27 -0
  37. package/dist/sample-good.css.js +94 -0
  38. package/dist/sample-theme.css.d.ts +37 -0
  39. package/dist/sample-theme.css.js +38 -0
  40. package/dist/sample.css.d.ts +75 -0
  41. package/dist/sample.css.js +530 -0
  42. package/package.json +5 -1
package/README.md CHANGED
@@ -264,6 +264,8 @@ The recommended configuration enables the following rules with error severity:
264
264
  - `vanilla-extract/alphabetical-order`: Alternative ordering rule (alphabetical sorting)
265
265
  - `vanilla-extract/custom-order`: Alternative ordering rule (custom group-based sorting)
266
266
  - `vanilla-extract/no-px-unit`: Disallows px units with an optional allowlist
267
+ - `vanilla-extract/prefer-logical-properties`: Enforces logical CSS properties over physical directional properties
268
+ - `vanilla-extract/prefer-theme-tokens`: Enforces theme tokens instead of hard-coded values for colors, spacing, font sizes, border radius, border widths, shadows, z-index, opacity, font weights, and transitions/animations (optionally evaluates helper functions and template literals)
267
269
 
268
270
  You can use the recommended configuration as a starting point and override rules as needed for your project. See the configuration examples above for how to switch between ordering rules.
269
271
 
@@ -602,6 +604,150 @@ export const myStyle = style({
602
604
  });
603
605
  ```
604
606
 
607
+ ### vanilla-extract/prefer-logical-properties
608
+
609
+ This rule enforces the use of CSS logical properties instead of physical (directional) properties in vanilla-extract style declarations. Logical properties adapt to different writing directions (LTR/RTL) and writing modes, making your styles more internationalization-friendly. Supports 140+ property mappings across margin, padding, border, inset, size, overflow, and scroll properties. Configurable allowlist lets you permit specific properties via the `allow` option (supports both camelCase and kebab-case).
610
+
611
+ Configuration with an allowlist:
612
+
613
+ ```json
614
+ {
615
+ "rules": {
616
+ "vanilla-extract/prefer-logical-properties": ["error", { "allow": ["top", "left"] }]
617
+ }
618
+ }
619
+ ```
620
+
621
+ ```typescript
622
+ // ❌ Incorrect
623
+ import { style } from '@vanilla-extract/css';
624
+
625
+ export const box = style({
626
+ marginLeft: '1rem',
627
+ paddingTop: '2rem',
628
+ width: '100%',
629
+ borderRight: '1px solid',
630
+ textAlign: 'left',
631
+ });
632
+
633
+ // ✅ Correct
634
+ import { style } from '@vanilla-extract/css';
635
+
636
+ export const box = style({
637
+ marginInlineStart: '1rem',
638
+ paddingBlockStart: '2rem',
639
+ inlineSize: '100%',
640
+ borderInlineEnd: '1px solid',
641
+ textAlign: 'start',
642
+ });
643
+ ```
644
+
645
+ ### vanilla-extract/prefer-theme-tokens
646
+
647
+ Enforces theme tokens instead of hard-coded CSS values. Analyzes your theme contract files and suggests **specific tokens** when matches are found.
648
+
649
+ **Options:**
650
+
651
+ | Option | Type | Default | Description |
652
+ |--------|------|---------|-------------|
653
+ | `themeContracts` | `string[]` | `[]` | Theme contract file paths (relative to project root or absolute) |
654
+ | `checkColors` | `boolean` | `true` | Check colors (hex, rgb, hsl, named) |
655
+ | `checkSpacing` | `boolean` | `true` | Check spacing (margin, padding, gap, width, height) |
656
+ | `checkFontSizes` | `boolean` | `true` | Check font sizes (fontSize, lineHeight) |
657
+ | `checkBorderRadius` | `boolean` | `true` | Check border radius values |
658
+ | `checkBorderWidths` | `boolean` | `true` | Check border widths (including `border` shorthand) |
659
+ | `checkShadows` | `boolean` | `true` | Check shadows (boxShadow, textShadow, filter) |
660
+ | `checkZIndex` | `boolean` | `true` | Check z-index values |
661
+ | `checkOpacity` | `boolean` | `true` | Check opacity values |
662
+ | `checkFontWeights` | `boolean` | `true` | Check font weights (numeric and named) |
663
+ | `checkTransitions` | `boolean` | `true` | Check transitions and animations |
664
+ | `allowedValues` | `string[]` | `[]` | Whitelist specific values (e.g., `["0", "auto", "100vh"]`) |
665
+ | `allowedProperties` | `string[]` | `[]` | Skip checking specific properties |
666
+ | `autoFix` | `boolean` | `false` | Auto-fix when exactly one token matches |
667
+ | `remBase` | `number` | `16` | Base font size for `rem()` calculations |
668
+ | `checkHelperFunctions` | `boolean` | `false` | Check helper calls like `rem(48)`, `` `${rem(4)}` `` |
669
+
670
+ #### Dependency note
671
+
672
+ This rule uses a safe expression evaluator to optionally analyze helper calls when `checkHelperFunctions` is enabled. For this, the plugin internally relies on `@babel/parser` and `@babel/types` to parse small expression snippets (e.g., template literals, `rem()` calls). These are shipped as plugin dependencies, so users don't need to install them manually. They're only exercised when `checkHelperFunctions` is turned on.
673
+
674
+ **Example:**
675
+
676
+ ```json
677
+ {
678
+ "rules": {
679
+ "vanilla-extract/prefer-theme-tokens": ["error", {
680
+ "themeContracts": ["./src/theme.css.ts"],
681
+ "checkColors": true,
682
+ "checkSpacing": true,
683
+ "allowedValues": ["0", "auto", "100%"],
684
+ "allowedProperties": ["borderWidth"],
685
+ "autoFix": false,
686
+ "checkHelperFunctions": false
687
+ }]
688
+ }
689
+ }
690
+ ```
691
+
692
+ **How it works:**
693
+
694
+ 1. **Analyzes theme contracts** - Reads your theme files and evaluates computed values:
695
+ - `rem(16)` → `"1rem"`
696
+ - `` `${rem(4)} ${rem(8)}` `` → `"0.25rem 0.5rem"`
697
+ - Arithmetic expressions
698
+
699
+ 2. **Detects hard-coded values** - Checks literals, numbers, and (optionally) helper functions:
700
+
701
+ ```typescript
702
+ color: '#0055FF' // ❌ Always flagged
703
+ padding: '16px' // ❌ Always flagged
704
+ opacity: 0.5 // ❌ Always flagged (numeric literal)
705
+ margin: rem(48) // ❌ Only with checkHelperFunctions: true
706
+ boxShadow: `${rem(4)}...` // ❌ Only with checkHelperFunctions: true
707
+ ```
708
+
709
+ 3. **Suggests specific tokens** - Matches values to theme tokens:
710
+
711
+ ```text
712
+ ❌ Hard-coded color '#0055FF'. Use theme token: vars.colors.brand
713
+ ❌ Hard-coded padding '16px'. Use theme token: vars.spacing.md
714
+ ```
715
+
716
+ - **Single match**: Shows one suggestion + auto-fix (if enabled)
717
+ - **Multiple matches**: Shows all as quick-fix options
718
+
719
+ **Theme contract example:**
720
+
721
+ ```typescript
722
+ // theme.css.ts
723
+ export const [themeClass, vars] = createTheme({
724
+ colors: { brand: '#0055FF', text: '#1f2937' },
725
+ spacing: { sm: '8px', md: '16px' },
726
+ });
727
+
728
+ // styles.css.ts
729
+ export const button = style({
730
+ backgroundColor: '#0055FF', // ❌ Use vars.colors.brand
731
+ padding: '8px', // ❌ Use vars.spacing.sm
732
+ });
733
+ ```
734
+
735
+ **Helper function detection:**
736
+
737
+ By default, only checks **literals**. Enable `checkHelperFunctions: true` to also check computed values:
738
+
739
+ ```typescript
740
+ // checkHelperFunctions: false (default)
741
+ padding: rem(48) // ✅ Not flagged
742
+ padding: '3rem' // ❌ Flagged
743
+
744
+ // checkHelperFunctions: true
745
+ padding: rem(48) // ❌ Flagged if theme has matching token
746
+ padding: '3rem' // ❌ Flagged if theme has matching token
747
+ ```
748
+
749
+ **Note:** Opt-in rule (not in recommended config). Enable when ready to enforce design tokens.
750
+
605
751
  ## Font Face Declarations
606
752
 
607
753
  For `fontFace` and `globalFontFace` API calls, all three ordering rules (alphabetical, concentric, and custom) enforce
@@ -686,16 +832,16 @@ The roadmap outlines the project's current status and future plans:
686
832
  - Support for using the plugin's recommended config via the extends field (as discussed in
687
833
  [issue #3](https://github.com/antebudimir/eslint-plugin-vanilla-extract/issues/3))
688
834
  - Comprehensive rule testing.
835
+ - `no-px-unit` rule to disallow use of `px` units with configurable whitelist.
836
+ - `prefer-logical-properties` rule to enforce use of logical properties.
837
+ - `prefer-theme-tokens` rule to enforce theme tokens instead of hard-coded values for colors, spacing, font sizes, border radius, border widths, shadows, z-index, opacity, font weights, and transitions/animations (optionally evaluates helper functions and template literals).
689
838
 
690
839
  ### Current Work
691
840
 
692
- - `no-px-unit` rule to disallow use of `px` units with configurable whitelist.
841
+ - `no-unitless-values` rule that disallows numeric literals for CSS properties that are not unitless in CSS.
693
842
 
694
843
  ### Upcoming Features
695
844
 
696
- - `prefer-logical-properties` rule to enforce use of logical properties.
697
- - `prefer-theme-tokens` rule to enforce use of theme tokens instead of hard-coded values when available.
698
- - `no-global-style` rule to disallow use of `globalStyle` function.
699
845
  - `property-unit-match` rule to enforce valid units per CSS property specs. **Note**: This feature will only be
700
846
  implemented if there's sufficient interest from the community.
701
847
  - Option to sort properties within user-defined concentric groups alphabetically instead of following the concentric