@maltjoy/eslint-rules 6.3.0 → 6.5.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.
package/README.md CHANGED
@@ -6,6 +6,12 @@ This package is **ESM-only** (`"type": "module"`). Use `import` from `eslint.con
6
6
 
7
7
  ## Rules
8
8
 
9
+ - `no-html-button`: disallow native HTML `<button>` elements. Use `VJoyButton` or `VJoyIconButton` instead.
10
+
11
+ Message:
12
+
13
+ `Do not use native HTML button elements, use VJoyButton or VJoyIconButton instead.`
14
+
9
15
  - `no-vjoyavatar-fullname-totalnumber`: disallow using `fullName` and `totalNumber` together on `VJoyAvatar`. This rule is a warning in the recommended preset.
10
16
 
11
17
  Message:
package/index.js CHANGED
@@ -1,3 +1,4 @@
1
+ import noHtmlButton from './rules/components/no-html-button.js';
1
2
  import noVJoyButtonCircle from './rules/components/no-vjoybutton-circle.js';
2
3
  import noVJoyButtonIconOnly from './rules/components/no-vjoybutton-icon-only.js';
3
4
  import noVJoyAvatarFullNameTotalNumber from './rules/components/no-vjoyavatar-fullname-totalnumber.js';
@@ -35,6 +36,7 @@ import noInlineStyleColors from './rules/tokens/no-inline-style-colors.js';
35
36
  import preferJoyColorClasses from './rules/tokens/prefer-joy-color-classes.js';
36
37
 
37
38
  const rules = {
39
+ 'no-html-button': noHtmlButton,
38
40
  'no-vjoybutton-circle': noVJoyButtonCircle,
39
41
  'no-vjoybutton-icon-only': noVJoyButtonIconOnly,
40
42
  'no-vjoyavatar-fullname-totalnumber': noVJoyAvatarFullNameTotalNumber,
@@ -84,6 +86,7 @@ plugin.configs.recommended = [
84
86
  '@maltjoy/eslint-rules': plugin,
85
87
  },
86
88
  rules: {
89
+ '@maltjoy/eslint-rules/no-html-button': 'error',
87
90
  '@maltjoy/eslint-rules/no-vjoybutton-circle': 'error',
88
91
  '@maltjoy/eslint-rules/no-vjoybutton-icon-only': 'error',
89
92
  '@maltjoy/eslint-rules/no-vjoyavatar-fullname-totalnumber': 'warn',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@maltjoy/eslint-rules",
3
- "version": "6.3.0",
3
+ "version": "6.5.0",
4
4
  "description": "Custom ESLint rules for the @maltjoy/core-vue design system",
5
5
  "type": "module",
6
6
  "main": "./index.js",
@@ -0,0 +1,17 @@
1
+ import { createForbiddenElementRule } from '../utils/forbidden-element.js';
2
+
3
+ const ERROR_MESSAGE =
4
+ 'Do not use native HTML button elements, use VJoyButton or VJoyIconButton instead.';
5
+
6
+ export default createForbiddenElementRule({
7
+ elementNames: ['button'],
8
+ description: 'disallow native HTML button elements',
9
+ details:
10
+ 'Native HTML button elements bypass Joy styles, variants, and accessibility defaults. Use VJoyButton for labeled actions and VJoyIconButton for icon-only actions.',
11
+ examples: {
12
+ incorrect: '<button type="button">Save</button>',
13
+ correct: '<VJoyButton>Save</VJoyButton>',
14
+ },
15
+ message: ERROR_MESSAGE,
16
+ messageId: 'useVJoyButton',
17
+ });
@@ -10,12 +10,12 @@ const ERROR_MESSAGE =
10
10
  export default createVueStyleInspectionRule({
11
11
  description: 'disallow arbitrary font-size values in Vue templates and style blocks',
12
12
  details:
13
- 'Font sizes should come from the Joy typography scale. Prefer VJoyText with classes such as joy-font-primary-400, utility classes such as joy-font-size-primary-400, or CSS variables such as var(--joy-font-size-primary-400) instead of px, rem, or em literals.',
13
+ 'Font sizes should come from the Joy typography scale. Prefer VJoyText with classes such as joy-font-primary-400, utility classes such as joy-font-size-primary-400, or CSS variables such as var(--joy-font-size-primary-400) instead of px, rem, or em literals. Aliasing a token on a local custom property (`--title-font-size: var(--joy-font-size-primary-400)` then `font-size: var(--title-font-size)`) is allowed.',
14
14
  examples: {
15
15
  incorrect:
16
16
  '<div style="font-size: 14px" />\n\n<style scoped>\n.title {\n font-size: 16px;\n}\n</style>',
17
17
  correct:
18
- '<VJoyText class="joy-font-primary-400" />\n\n<style scoped>\n.title {\n font-size: var(--joy-font-size-primary-400);\n}\n</style>',
18
+ '<VJoyText class="joy-font-primary-400" />\n\n<style scoped>\n.title {\n --title-font-size: var(--joy-font-size-primary-400);\n font-size: var(--title-font-size);\n}\n</style>',
19
19
  },
20
20
  messageId: 'arbitraryFontSize',
21
21
  message: ERROR_MESSAGE,
@@ -9,12 +9,12 @@ const ERROR_MESSAGE =
9
9
  export default createVueStyleInspectionRule({
10
10
  description: 'disallow hardcoded CSS color names in Vue templates and style blocks',
11
11
  details:
12
- 'Named CSS colors such as red or white bypass the Joy design token system. Prefer CSS variables from @maltjoy/themes or Joy utility classes.',
12
+ 'Named CSS colors such as red or white bypass the Joy design token system. Prefer CSS variables from @maltjoy/themes or Joy utility classes. Aliasing a token on a local custom property (`--title-color: var(--joy-color-primary-10)` then `color: var(--title-color)`) is allowed.',
13
13
  examples: {
14
14
  incorrect:
15
15
  '<div style="color: red" />\n\n<style scoped>\n.title {\n background-color: white;\n}\n</style>',
16
16
  correct:
17
- '<div class="joy-color-error-50" />\n\n<style scoped>\n.title {\n background-color: var(--joy-color-neutral-0);\n}\n</style>',
17
+ '<div class="joy-color-error-50" />\n\n<style scoped>\n.title {\n --title-color: var(--joy-color-primary-10);\n color: var(--title-color);\n}\n</style>',
18
18
  },
19
19
  messageId: 'hardcodedColorName',
20
20
  message: ERROR_MESSAGE,
@@ -9,7 +9,7 @@ const ERROR_MESSAGE =
9
9
  export default createVueStyleInspectionRule({
10
10
  description: 'disallow hardcoded hex colors in Vue templates and style blocks',
11
11
  details:
12
- 'Hardcoded hex colors bypass the Joy design token system. Prefer CSS variables from @maltjoy/themes, for example var(--joy-color-primary-30).',
12
+ 'Hardcoded hex colors bypass the Joy design token system. Prefer CSS variables from @maltjoy/themes, for example var(--joy-color-primary-30). Aliasing a token on a local custom property (`--title-color: var(--joy-color-primary-10)` then `color: var(--title-color)`) is allowed.',
13
13
  examples: {
14
14
  incorrect:
15
15
  '<div style="color: #0F6378" />\n\n<style scoped>\n.title {\n color: #fff;\n}\n</style>',
@@ -9,12 +9,12 @@ const ERROR_MESSAGE =
9
9
  export default createVueStyleInspectionRule({
10
10
  description: 'disallow hardcoded rgb/rgba colors in Vue templates and style blocks',
11
11
  details:
12
- 'Hardcoded rgb/rgba colors bypass the Joy design token system. Prefer CSS variables from @maltjoy/themes, for example var(--joy-color-primary-30).',
12
+ 'Hardcoded rgb/rgba colors bypass the Joy design token system. Prefer CSS variables from @maltjoy/themes, for example var(--joy-color-primary-30). Aliasing a token on a local custom property (`--title-color: var(--joy-color-primary-10)` then `color: var(--title-color)`) is allowed.',
13
13
  examples: {
14
14
  incorrect:
15
15
  '<div style="color: rgb(15, 99, 120)" />\n\n<style scoped>\n.title {\n background-color: rgba(0, 0, 0, 0.5);\n}\n</style>',
16
16
  correct:
17
- '<div class="joy-color-primary-30" />\n\n<style scoped>\n.title {\n background-color: var(--joy-color-neutral-60);\n}\n</style>',
17
+ '<div class="joy-color-primary-30" />\n\n<style scoped>\n.title {\n --title-color: var(--joy-color-primary-10);\n color: var(--title-color);\n}\n</style>',
18
18
  },
19
19
  messageId: 'hardcodedRgb',
20
20
  message: ERROR_MESSAGE,
@@ -6,7 +6,6 @@ import {
6
6
  isStaticStyleAttribute,
7
7
  iterateStyleBlockContents,
8
8
  objectExpressionHasColorProperty,
9
- objectExpressionHasFontSizeProperty,
10
9
  reportArbitraryFontSizesInText,
11
10
  reportHexColorsInText,
12
11
  reportNamedColorsInText,
@@ -150,13 +149,6 @@ export function inspectStyleExpressionForInlineColors(context, expression, attri
150
149
  }
151
150
 
152
151
  export function inspectStyleExpressionForFontSize(context, expression, attribute, messageId) {
153
- if (objectExpressionHasFontSizeProperty(expression)) {
154
- context.report({
155
- node: attribute,
156
- messageId,
157
- });
158
- }
159
-
160
152
  if (expression?.type === 'ObjectExpression') {
161
153
  expression.properties.forEach((property) => {
162
154
  if (property.type !== 'Property') {
@@ -54,10 +54,9 @@ const FONT_SIZE_KEYWORDS = new Set([
54
54
  'larger',
55
55
  ]);
56
56
 
57
- const FONT_SIZE_DECLARATION_PATTERN = /font-size\s*:\s*([^;}\n]+)/gi;
58
57
  const STYLE_BLOCK_PATTERN = /<style(?:\s[^>]*)?>([\s\S]*?)<\/style>/gi;
59
- const JOY_FONT_SIZE_VAR_PATTERN =
60
- /^var\(\s*--joy-(?:font-size-(?:primary|secondary)-\d+|text-size-[a-z0-9-]+)/i;
58
+ const JOY_FONT_SIZE_VAR_NAME_PATTERN =
59
+ /^--joy-(?:font-size-(?:primary|secondary)-\d+|text-size-[a-z0-9-]+)$/i;
61
60
 
62
61
  function normalizePropertyName(name) {
63
62
  return name.replace(/[\s_-]+/g, '').toLowerCase();
@@ -71,6 +70,40 @@ export function isColorPropertyName(name) {
71
70
  return COLOR_PROPERTY_NAMES.has(normalizePropertyName(name));
72
71
  }
73
72
 
73
+ export function isColorCustomProperty(name) {
74
+ return typeof name === 'string' && name.startsWith('--') && /color/i.test(name);
75
+ }
76
+
77
+ export function isColorDeclarationProperty(name) {
78
+ return isColorPropertyName(name) || isColorCustomProperty(name);
79
+ }
80
+
81
+ export function isFontSizeCustomProperty(name) {
82
+ return typeof name === 'string' && name.startsWith('--') && /font-size|text-size|fontsize/i.test(name);
83
+ }
84
+
85
+ export function isFontSizeDeclarationProperty(name) {
86
+ return normalizePropertyName(name) === 'fontsize' || isFontSizeCustomProperty(name);
87
+ }
88
+
89
+ function parseCssVar(value) {
90
+ if (!/^var\(/i.test(value) || !value.endsWith(')')) {
91
+ return null;
92
+ }
93
+
94
+ const inner = value.slice(value.indexOf('(') + 1, -1).trim();
95
+ const nameMatch = inner.match(/^(--[\w-]+)\s*(?:,\s*([\s\S]*))?$/);
96
+
97
+ if (!nameMatch) {
98
+ return null;
99
+ }
100
+
101
+ return {
102
+ name: nameMatch[1],
103
+ fallback: nameMatch[2]?.trim() ?? '',
104
+ };
105
+ }
106
+
74
107
  export function styleStringHasColorDeclaration(styleValue) {
75
108
  if (typeof styleValue !== 'string' || !styleValue.trim()) {
76
109
  return false;
@@ -88,19 +121,36 @@ export function styleStringHasColorDeclaration(styleValue) {
88
121
  }
89
122
 
90
123
  export function isArbitraryFontSizeValue(value) {
91
- const trimmed = value.trim();
124
+ const trimmed = value.replace(/\s*!important\s*$/i, '').trim();
92
125
  if (!trimmed) {
93
126
  return false;
94
127
  }
95
128
 
96
- if (JOY_FONT_SIZE_VAR_PATTERN.test(trimmed)) {
129
+ if (FONT_SIZE_KEYWORDS.has(trimmed.toLowerCase())) {
130
+ return false;
131
+ }
132
+
133
+ if (/^\$[\w-]+(?:\s+\$[\w-]+)?$/.test(trimmed)) {
97
134
  return false;
98
135
  }
99
136
 
100
- if (FONT_SIZE_KEYWORDS.has(trimmed.toLowerCase())) {
137
+ if (/^#{[\s\S]+}$/.test(trimmed)) {
101
138
  return false;
102
139
  }
103
140
 
141
+ const cssVar = parseCssVar(trimmed);
142
+
143
+ if (cssVar) {
144
+ if (
145
+ /^--joy-(?:font-size|text-size)/i.test(cssVar.name) &&
146
+ !JOY_FONT_SIZE_VAR_NAME_PATTERN.test(cssVar.name)
147
+ ) {
148
+ return true;
149
+ }
150
+
151
+ return cssVar.fallback ? isArbitraryFontSizeValue(cssVar.fallback) : false;
152
+ }
153
+
104
154
  return true;
105
155
  }
106
156
 
@@ -160,7 +210,7 @@ export function* iterateNamedColorDeclarations(text) {
160
210
  const propertyName = match[1]?.trim() ?? '';
161
211
  const value = match[2]?.trim() ?? '';
162
212
 
163
- if (isColorPropertyName(propertyName) && isHardcodedNamedColorValue(value)) {
213
+ if (isColorDeclarationProperty(propertyName) && isHardcodedNamedColorValue(value)) {
164
214
  yield {
165
215
  value,
166
216
  index: match.index + match[0].indexOf(value),
@@ -250,17 +300,18 @@ export function* iterateArbitraryFontSizeDeclarations(text) {
250
300
  return;
251
301
  }
252
302
 
253
- const pattern = new RegExp(FONT_SIZE_DECLARATION_PATTERN.source, 'gi');
303
+ const pattern = new RegExp(COLOR_DECLARATION_PATTERN.source, 'g');
254
304
  let match = pattern.exec(text);
255
305
 
256
306
  while (match) {
257
- const value = match[1]?.trim() ?? '';
307
+ const propertyName = match[1]?.trim() ?? '';
308
+ const value = match[2]?.trim() ?? '';
258
309
 
259
- if (isArbitraryFontSizeValue(value)) {
310
+ if (isFontSizeDeclarationProperty(propertyName) && isArbitraryFontSizeValue(value)) {
260
311
  yield {
261
312
  value,
262
- index: match.index,
263
- length: match[0].length,
313
+ index: match.index + match[0].indexOf(value),
314
+ length: value.length,
264
315
  };
265
316
  }
266
317