@maltjoy/eslint-rules 0.4.0 → 0.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,12 +6,30 @@ This package is **ESM-only** (`"type": "module"`). Use `import` from `eslint.con
6
6
 
7
7
  ## Rules
8
8
 
9
- - `no-vjoybutton-circle`: disallow `circle` prop usage on `VJoyButton`.
9
+ - `no-vjoyavatar-fullname-totalnumber`: disallow using `fullName` and `totalNumber` together on `VJoyAvatar`. This rule is a warning in the recommended preset.
10
+
11
+ Message:
12
+
13
+ `VJoyAvatar fullName and totalNumber props are mutually exclusive.`
14
+
15
+ - `no-vjoyavatar-photourl-totalnumber`: disallow using `photoUrl` and `totalNumber` together on `VJoyAvatar`. This rule is a warning in the recommended preset.
16
+
17
+ Message:
18
+
19
+ `VJoyAvatar photoUrl and totalNumber props are mutually exclusive.`
20
+
21
+ - `no-vjoybutton-circle`: disallow `circle` prop usage on `VJoyButton` / `v-joy-button`.
10
22
 
11
23
  Message:
12
24
 
13
25
  `VJoyButton circle property is deprecated, please use VJoyIconButton instead`
14
26
 
27
+ - `no-vjoyformgroup-vjoyradio`: disallow `VJoyRadio` inside `VJoyFormGroup`. Use `VJoyRadioGroup` to group radios instead.
28
+
29
+ Message:
30
+
31
+ `VJoyFormGroup shouldn't be used around a VJoyRadio. Use VJoyRadioGroup instead.`
32
+
15
33
  - `no-vjoyinput-type-password`: disallow `VJoyInput` with `type="password"` (or a literal `:type` bound to `password`). Use `VJoyInputPassword` instead.
16
34
 
17
35
  - `no-vjoyinput-unit`: disallow `unit` prop usage on `VJoyInput`. Use `VJoyInputNumber` instead.
@@ -20,6 +38,30 @@ Message:
20
38
 
21
39
  `Do not use VJoyInput with unit, use VJoyInputNumber instead.`
22
40
 
41
+ - `no-vjoymenu-vjoypanel`: disallow `VJoyMenu` inside `VJoyPanel`.
42
+
43
+ Message:
44
+
45
+ `Never use a VJoyMenu inside a VJoyPanel.`
46
+
47
+ - `require-vjoymenu-template-sidebar`: require `VJoyMenu` to be inside `VJoyTemplate`'s `#template-sidebar` slot. This rule is a warning in the recommended preset.
48
+
49
+ Message:
50
+
51
+ `VJoyMenu must be used inside the VJoyTemplate #template-sidebar slot.`
52
+
53
+ - `require-vjoytemplate-template-title`: require `VJoyTemplate` to provide a `#template-title` slot. This rule is a warning in the recommended preset.
54
+
55
+ Message:
56
+
57
+ `VJoyTemplate must have a template-title slot.`
58
+
59
+ - `require-vjoytemplate-template-title-heading`: require the `VJoyTemplate` `#template-title` slot to contain an `h1`–`h6` element, preferably `h1`. This rule is a warning in the recommended preset.
60
+
61
+ Message:
62
+
63
+ `VJoyTemplate template-title slot should use a heading tag (h1-h6), preferably h1.`
64
+
23
65
  - `no-vjoyusercard-rating-stars-slot`: disallow `user-card-rating-stars` slot usage on `VJoyUserCard`. Use the default slot instead.
24
66
 
25
67
  Message:
package/index.js CHANGED
@@ -1,13 +1,27 @@
1
1
  import noVJoyButtonCircle from './rules/no-vjoybutton-circle.js';
2
+ import noVJoyAvatarFullNameTotalNumber from './rules/no-vjoyavatar-fullname-totalnumber.js';
3
+ import noVJoyAvatarPhotoUrlTotalNumber from './rules/no-vjoyavatar-photourl-totalnumber.js';
4
+ import noVJoyFormGroupVJoyRadio from './rules/no-vjoyformgroup-vjoyradio.js';
2
5
  import noVJoyInputTypePassword from './rules/no-vjoyinput-type-password.js';
3
6
  import noVJoyInputUnit from './rules/no-vjoyinput-unit.js';
7
+ import noVJoyMenuVJoyPanel from './rules/no-vjoymenu-vjoypanel.js';
4
8
  import noVJoyUserCardRatingStarsSlot from './rules/no-vjoyusercard-rating-stars-slot.js';
9
+ import requireVJoyMenuTemplateSidebar from './rules/require-vjoymenu-template-sidebar.js';
10
+ import requireVJoyTemplateTemplateTitle from './rules/require-vjoytemplate-template-title.js';
11
+ import requireVJoyTemplateTemplateTitleHeading from './rules/require-vjoytemplate-template-title-heading.js';
5
12
 
6
13
  const rules = {
7
14
  'no-vjoybutton-circle': noVJoyButtonCircle,
15
+ 'no-vjoyavatar-fullname-totalnumber': noVJoyAvatarFullNameTotalNumber,
16
+ 'no-vjoyavatar-photourl-totalnumber': noVJoyAvatarPhotoUrlTotalNumber,
17
+ 'no-vjoyformgroup-vjoyradio': noVJoyFormGroupVJoyRadio,
8
18
  'no-vjoyinput-type-password': noVJoyInputTypePassword,
9
19
  'no-vjoyinput-unit': noVJoyInputUnit,
20
+ 'no-vjoymenu-vjoypanel': noVJoyMenuVJoyPanel,
10
21
  'no-vjoyusercard-rating-stars-slot': noVJoyUserCardRatingStarsSlot,
22
+ 'require-vjoymenu-template-sidebar': requireVJoyMenuTemplateSidebar,
23
+ 'require-vjoytemplate-template-title': requireVJoyTemplateTemplateTitle,
24
+ 'require-vjoytemplate-template-title-heading': requireVJoyTemplateTemplateTitleHeading,
11
25
  };
12
26
 
13
27
  const plugin = {
@@ -23,9 +37,16 @@ plugin.configs.recommended = [
23
37
  },
24
38
  rules: {
25
39
  '@maltjoy/eslint-rules/no-vjoybutton-circle': 'error',
40
+ '@maltjoy/eslint-rules/no-vjoyavatar-fullname-totalnumber': 'warn',
41
+ '@maltjoy/eslint-rules/no-vjoyavatar-photourl-totalnumber': 'warn',
42
+ '@maltjoy/eslint-rules/no-vjoyformgroup-vjoyradio': 'error',
26
43
  '@maltjoy/eslint-rules/no-vjoyinput-type-password': 'error',
27
44
  '@maltjoy/eslint-rules/no-vjoyinput-unit': 'error',
45
+ '@maltjoy/eslint-rules/no-vjoymenu-vjoypanel': 'error',
28
46
  '@maltjoy/eslint-rules/no-vjoyusercard-rating-stars-slot': 'error',
47
+ '@maltjoy/eslint-rules/require-vjoymenu-template-sidebar': 'warn',
48
+ '@maltjoy/eslint-rules/require-vjoytemplate-template-title': 'warn',
49
+ '@maltjoy/eslint-rules/require-vjoytemplate-template-title-heading': 'warn',
29
50
  },
30
51
  },
31
52
  ];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@maltjoy/eslint-rules",
3
- "version": "0.4.0",
3
+ "version": "0.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,14 @@
1
+ import {createVJoyAvatarConflictingPropsRule} from './utils/vjoyavatar.js';
2
+
3
+ export default createVJoyAvatarConflictingPropsRule({
4
+ description: 'disallow VJoyAvatar fullName and totalNumber props used together',
5
+ details:
6
+ 'fullName and totalNumber are mutually exclusive on VJoyAvatar. Choose either a person avatar or a total-count avatar.',
7
+ examples: {
8
+ incorrect: '<VJoyAvatar full-name="Ada Lovelace" :total-number="4" />',
9
+ correct: '<VJoyAvatar full-name="Ada Lovelace" />',
10
+ },
11
+ message: 'VJoyAvatar fullName and totalNumber props are mutually exclusive.',
12
+ messageId: 'conflictingProps',
13
+ propNames: ['fullname', 'totalnumber'],
14
+ });
@@ -0,0 +1,14 @@
1
+ import {createVJoyAvatarConflictingPropsRule} from './utils/vjoyavatar.js';
2
+
3
+ export default createVJoyAvatarConflictingPropsRule({
4
+ description: 'disallow VJoyAvatar photoUrl and totalNumber props used together',
5
+ details:
6
+ 'photoUrl and totalNumber are mutually exclusive on VJoyAvatar. A photo avatar cannot also represent a total count.',
7
+ examples: {
8
+ incorrect: '<VJoyAvatar photo-url="/ada.jpg" :total-number="4" />',
9
+ correct: '<VJoyAvatar photo-url="/ada.jpg" />',
10
+ },
11
+ message: 'VJoyAvatar photoUrl and totalNumber props are mutually exclusive.',
12
+ messageId: 'conflictingProps',
13
+ propNames: ['photourl', 'totalnumber'],
14
+ });
@@ -1,74 +1,17 @@
1
- const ERROR_MESSAGE = 'VJoyButton circle property is deprecated, please use VJoyIconButton instead';
2
-
3
- function isCircleAttribute(node) {
4
- if (!node || node.type !== 'VAttribute') {
5
- return false;
6
- }
7
-
8
- if (!node.directive) {
9
- return node.key && node.key.name === 'circle';
10
- }
11
-
12
- const directiveName = node.key && node.key.name && node.key.name.name;
13
- const argumentName = node.key && node.key.argument && node.key.argument.name;
14
-
15
- return directiveName === 'bind' && argumentName === 'circle';
16
- }
17
-
18
- function isVJoyButtonElement(node) {
19
- if (!node || node.type !== 'VElement' || typeof node.name !== 'string') {
20
- return false;
21
- }
22
-
23
- return node.name.toLowerCase() === 'vjoybutton';
24
- }
1
+ import { createForbiddenAttributeRule } from './utils/forbidden-attribute.js';
25
2
 
26
- export default {
27
- meta: {
28
- type: 'suggestion',
29
- docs: {
30
- description: 'disallow deprecated circle prop on VJoyButton',
31
- recommended: false,
32
- },
33
- schema: [],
34
- messages: {
35
- deprecatedCircle: ERROR_MESSAGE,
36
- },
37
- },
38
- create(context) {
39
- function visitElement(node) {
40
- if (!node || node.type !== 'VElement') {
41
- return;
42
- }
43
-
44
- if (isVJoyButtonElement(node) && node.startTag && Array.isArray(node.startTag.attributes)) {
45
- node.startTag.attributes.forEach((attribute) => {
46
- if (isCircleAttribute(attribute)) {
47
- context.report({
48
- node: attribute,
49
- messageId: 'deprecatedCircle',
50
- });
51
- }
52
- });
53
- }
54
-
55
- if (!Array.isArray(node.children)) {
56
- return;
57
- }
58
-
59
- node.children.forEach((child) => {
60
- visitElement(child);
61
- });
62
- }
63
-
64
- return {
65
- Program(programNode) {
66
- if (!programNode || !programNode.templateBody) {
67
- return;
68
- }
3
+ const ERROR_MESSAGE = 'VJoyButton circle property is deprecated, please use VJoyIconButton instead';
69
4
 
70
- visitElement(programNode.templateBody);
71
- },
72
- };
5
+ export default createForbiddenAttributeRule({
6
+ attributeName: 'circle',
7
+ description: 'disallow deprecated circle prop on VJoyButton',
8
+ details:
9
+ 'The circle prop on VJoyButton is deprecated. Use VJoyIconButton for a circular icon-only action.',
10
+ elementNames: ['vjoybutton', 'v-joy-button'],
11
+ examples: {
12
+ incorrect: '<VJoyButton circle icon="edit" />',
13
+ correct: '<VJoyIconButton icon="edit" />',
73
14
  },
74
- };
15
+ message: ERROR_MESSAGE,
16
+ messageId: 'deprecatedCircle',
17
+ });
@@ -0,0 +1,54 @@
1
+ import {
2
+ containsElement,
3
+ createTemplateBodyVisitor,
4
+ isElementNamed,
5
+ } from './utils/vue-ast.js';
6
+
7
+ const ERROR_MESSAGE =
8
+ "VJoyFormGroup shouldn't be used around a VJoyRadio. Use VJoyRadioGroup instead.";
9
+
10
+ function isVJoyFormGroupElement(node) {
11
+ return isElementNamed(node, ['vjoyformgroup', 'v-joy-form-group']);
12
+ }
13
+
14
+ function isVJoyRadioElement(node) {
15
+ return isElementNamed(node, ['vjoyradio', 'v-joy-radio']);
16
+ }
17
+
18
+ function isVJoyRadioGroupElement(node) {
19
+ return isElementNamed(node, ['vjoyradiogroup', 'v-joy-radio-group']);
20
+ }
21
+
22
+ function containsRadioOutsideRadioGroup(node) {
23
+ return containsElement(node, isVJoyRadioElement, isVJoyRadioGroupElement);
24
+ }
25
+
26
+ export default {
27
+ meta: {
28
+ type: 'suggestion',
29
+ docs: {
30
+ description: 'disallow VJoyFormGroup around VJoyRadio; use VJoyRadioGroup instead',
31
+ details:
32
+ 'VJoyFormGroup must not wrap VJoyRadio. Use VJoyRadioGroup to provide the expected radio-group semantics and behavior.',
33
+ examples: {
34
+ incorrect: '<VJoyFormGroup>\n <VJoyRadio value="daily" />\n</VJoyFormGroup>',
35
+ correct: '<VJoyRadioGroup>\n <VJoyRadio value="daily" />\n</VJoyRadioGroup>',
36
+ },
37
+ recommended: true,
38
+ },
39
+ schema: [],
40
+ messages: {
41
+ useVJoyRadioGroup: ERROR_MESSAGE,
42
+ },
43
+ },
44
+ create(context) {
45
+ return createTemplateBodyVisitor((node) => {
46
+ if (isVJoyFormGroupElement(node) && containsRadioOutsideRadioGroup(node)) {
47
+ context.report({
48
+ node,
49
+ messageId: 'useVJoyRadioGroup',
50
+ });
51
+ }
52
+ });
53
+ },
54
+ };
@@ -1,3 +1,5 @@
1
+ import { createTemplateBodyVisitor, isElementNamed } from './utils/vue-ast.js';
2
+
1
3
  const ERROR_MESSAGE =
2
4
  'Do not use VJoyInput with type="password", use VJoyInputPassword instead.';
3
5
 
@@ -27,25 +29,25 @@ function isLiteralPasswordExpression(expr) {
27
29
  }
28
30
 
29
31
  function isTypePasswordAttribute(node) {
30
- if (!node || node.type !== 'VAttribute') {
32
+ if (node?.type !== 'VAttribute') {
31
33
  return false;
32
34
  }
33
35
 
34
36
  if (!node.directive) {
35
- if (node.key && node.key.name === 'type' && node.value && node.value.type === 'VLiteral') {
37
+ if (node.key?.name === 'type' && node.value?.type === 'VLiteral') {
36
38
  return isPasswordString(node.value.value);
37
39
  }
38
40
  return false;
39
41
  }
40
42
 
41
- const directiveName = node.key && node.key.name && node.key.name.name;
42
- const argumentName = node.key && node.key.argument && node.key.argument.name;
43
+ const directiveName = node.key?.name?.name;
44
+ const argumentName = node.key?.argument?.name;
43
45
 
44
46
  if (directiveName !== 'bind' || argumentName !== 'type') {
45
47
  return false;
46
48
  }
47
49
 
48
- if (!node.value || node.value.type !== 'VExpressionContainer') {
50
+ if (node.value?.type !== 'VExpressionContainer') {
49
51
  return false;
50
52
  }
51
53
 
@@ -53,11 +55,7 @@ function isTypePasswordAttribute(node) {
53
55
  }
54
56
 
55
57
  function isVJoyInputElement(node) {
56
- if (!node || node.type !== 'VElement' || typeof node.name !== 'string') {
57
- return false;
58
- }
59
-
60
- return node.name.toLowerCase() === 'vjoyinput';
58
+ return isElementNamed(node, ['vjoyinput']);
61
59
  }
62
60
 
63
61
  export default {
@@ -65,7 +63,13 @@ export default {
65
63
  type: 'suggestion',
66
64
  docs: {
67
65
  description: 'disallow VJoyInput with type password; use VJoyInputPassword',
68
- recommended: false,
66
+ details:
67
+ 'Do not turn VJoyInput into a password field with type="password" or a literal bound value. VJoyInputPassword provides the supported password experience.',
68
+ examples: {
69
+ incorrect: '<VJoyInput type="password" />',
70
+ correct: '<VJoyInputPassword />',
71
+ },
72
+ recommended: true,
69
73
  },
70
74
  schema: [],
71
75
  messages: {
@@ -73,12 +77,8 @@ export default {
73
77
  },
74
78
  },
75
79
  create(context) {
76
- function visitElement(node) {
77
- if (!node || node.type !== 'VElement') {
78
- return;
79
- }
80
-
81
- if (isVJoyInputElement(node) && node.startTag && Array.isArray(node.startTag.attributes)) {
80
+ return createTemplateBodyVisitor((node) => {
81
+ if (isVJoyInputElement(node) && Array.isArray(node.startTag?.attributes)) {
82
82
  node.startTag.attributes.forEach((attribute) => {
83
83
  if (isTypePasswordAttribute(attribute)) {
84
84
  context.report({
@@ -88,24 +88,6 @@ export default {
88
88
  }
89
89
  });
90
90
  }
91
-
92
- if (!Array.isArray(node.children)) {
93
- return;
94
- }
95
-
96
- node.children.forEach((child) => {
97
- visitElement(child);
98
- });
99
- }
100
-
101
- return {
102
- Program(programNode) {
103
- if (!programNode || !programNode.templateBody) {
104
- return;
105
- }
106
-
107
- visitElement(programNode.templateBody);
108
- },
109
- };
91
+ });
110
92
  },
111
93
  };
@@ -1,75 +1,17 @@
1
+ import { createForbiddenAttributeRule } from './utils/forbidden-attribute.js';
2
+
1
3
  const ERROR_MESSAGE =
2
4
  'Do not use VJoyInput with unit property, use VJoyInputNumber instead.';
3
5
 
4
- function isUnitAttribute(node) {
5
- if (!node || node.type !== 'VAttribute') {
6
- return false;
7
- }
8
-
9
- if (!node.directive) {
10
- return node.key && node.key.name === 'unit';
11
- }
12
-
13
- const directiveName = node.key && node.key.name && node.key.name.name;
14
- const argumentName = node.key && node.key.argument && node.key.argument.name;
15
-
16
- return directiveName === 'bind' && argumentName === 'unit';
17
- }
18
-
19
- function isVJoyInputElement(node) {
20
- if (!node || node.type !== 'VElement' || typeof node.name !== 'string') {
21
- return false;
22
- }
23
-
24
- return node.name.toLowerCase() === 'vjoyinput';
25
- }
26
-
27
- export default {
28
- meta: {
29
- type: 'suggestion',
30
- docs: {
31
- description: 'disallow VJoyInput with unit prop; use VJoyInputNumber',
32
- recommended: false,
33
- },
34
- schema: [],
35
- messages: {
36
- useVJoyInputNumber: ERROR_MESSAGE,
37
- },
38
- },
39
- create(context) {
40
- function visitElement(node) {
41
- if (!node || node.type !== 'VElement') {
42
- return;
43
- }
44
-
45
- if (isVJoyInputElement(node) && node.startTag && Array.isArray(node.startTag.attributes)) {
46
- node.startTag.attributes.forEach((attribute) => {
47
- if (isUnitAttribute(attribute)) {
48
- context.report({
49
- node: attribute,
50
- messageId: 'useVJoyInputNumber',
51
- });
52
- }
53
- });
54
- }
55
-
56
- if (!Array.isArray(node.children)) {
57
- return;
58
- }
59
-
60
- node.children.forEach((child) => {
61
- visitElement(child);
62
- });
63
- }
64
-
65
- return {
66
- Program(programNode) {
67
- if (!programNode || !programNode.templateBody) {
68
- return;
69
- }
70
-
71
- visitElement(programNode.templateBody);
72
- },
73
- };
6
+ export default createForbiddenAttributeRule({
7
+ attributeName: 'unit',
8
+ description: 'disallow VJoyInput with unit prop; use VJoyInputNumber',
9
+ details: 'The unit prop belongs to VJoyInputNumber, not VJoyInput.',
10
+ elementNames: ['vjoyinput'],
11
+ examples: {
12
+ incorrect: '<VJoyInput unit="€" />',
13
+ correct: '<VJoyInputNumber unit="€" />',
74
14
  },
75
- };
15
+ message: ERROR_MESSAGE,
16
+ messageId: 'useVJoyInputNumber',
17
+ });
@@ -0,0 +1,49 @@
1
+ import {
2
+ containsElement,
3
+ createTemplateBodyVisitor,
4
+ isElementNamed,
5
+ } from './utils/vue-ast.js';
6
+
7
+ const ERROR_MESSAGE = 'Never use a VJoyMenu inside a VJoyPanel.';
8
+
9
+ function isVJoyMenuElement(node) {
10
+ return isElementNamed(node, ['vjoymenu', 'v-joy-menu']);
11
+ }
12
+
13
+ function isVJoyPanelElement(node) {
14
+ return isElementNamed(node, ['vjoypanel', 'v-joy-panel']);
15
+ }
16
+
17
+ function containsVJoyMenu(node) {
18
+ return containsElement(node, isVJoyMenuElement);
19
+ }
20
+
21
+ export default {
22
+ meta: {
23
+ type: 'suggestion',
24
+ docs: {
25
+ description: 'disallow VJoyMenu inside VJoyPanel',
26
+ details:
27
+ 'VJoyMenu and VJoyPanel represent different navigation and content patterns and must not be nested.',
28
+ examples: {
29
+ incorrect: '<VJoyPanel>\n <VJoyMenu />\n</VJoyPanel>',
30
+ correct: '<VJoyMenu />\n<VJoyPanel />',
31
+ },
32
+ recommended: true,
33
+ },
34
+ schema: [],
35
+ messages: {
36
+ noVJoyMenuInsideVJoyPanel: ERROR_MESSAGE,
37
+ },
38
+ },
39
+ create(context) {
40
+ return createTemplateBodyVisitor((node) => {
41
+ if (isVJoyPanelElement(node) && containsVJoyMenu(node)) {
42
+ context.report({
43
+ node,
44
+ messageId: 'noVJoyMenuInsideVJoyPanel',
45
+ });
46
+ }
47
+ });
48
+ },
49
+ };
@@ -1,54 +1,38 @@
1
+ import {
2
+ createTemplateBodyVisitor,
3
+ getStaticName,
4
+ isElementNamed,
5
+ } from './utils/vue-ast.js';
6
+
1
7
  const SLOT_NAME = 'user-card-rating-stars';
2
8
  const ERROR_MESSAGE =
3
9
  'Do not use VJoyUserCard user-card-rating-stars slot, use the default slot instead.';
4
10
 
5
- function getStaticName(node) {
6
- if (!node) {
7
- return undefined;
8
- }
9
-
10
- if (typeof node.name === 'string') {
11
- return node.name;
12
- }
13
-
14
- if (typeof node.value === 'string') {
15
- return node.value;
16
- }
17
-
18
- return undefined;
19
- }
20
-
21
11
  function isVJoyUserCardElement(node) {
22
- if (!node || node.type !== 'VElement' || typeof node.name !== 'string') {
23
- return false;
24
- }
25
-
26
- return node.name.toLowerCase() === 'vjoyusercard';
12
+ return isElementNamed(node, ['vjoyusercard']);
27
13
  }
28
14
 
29
15
  function isDeprecatedSlotAttribute(node) {
30
- if (!node || node.type !== 'VAttribute') {
16
+ if (node?.type !== 'VAttribute') {
31
17
  return false;
32
18
  }
33
19
 
34
20
  if (!node.directive) {
35
21
  return (
36
- node.key &&
37
- node.key.name === 'slot' &&
38
- node.value &&
39
- node.value.type === 'VLiteral' &&
22
+ node.key?.name === 'slot' &&
23
+ node.value?.type === 'VLiteral' &&
40
24
  node.value.value === SLOT_NAME
41
25
  );
42
26
  }
43
27
 
44
- const directiveName = node.key && node.key.name && node.key.name.name;
45
- const argumentName = getStaticName(node.key && node.key.argument);
28
+ const directiveName = node.key?.name?.name;
29
+ const argumentName = getStaticName(node.key?.argument);
46
30
 
47
31
  return directiveName === 'slot' && argumentName === SLOT_NAME;
48
32
  }
49
33
 
50
34
  function reportDeprecatedSlotAttribute(context, node) {
51
- if (!node || !node.startTag || !Array.isArray(node.startTag.attributes)) {
35
+ if (!Array.isArray(node?.startTag?.attributes)) {
52
36
  return;
53
37
  }
54
38
 
@@ -67,7 +51,14 @@ export default {
67
51
  type: 'suggestion',
68
52
  docs: {
69
53
  description: 'disallow deprecated user-card-rating-stars slot on VJoyUserCard',
70
- recommended: false,
54
+ details:
55
+ 'The user-card-rating-stars slot on VJoyUserCard is deprecated. Put the rating content in the default slot.',
56
+ examples: {
57
+ incorrect:
58
+ '<VJoyUserCard>\n <template #user-card-rating-stars>\n <RatingStars />\n </template>\n</VJoyUserCard>',
59
+ correct: '<VJoyUserCard>\n <RatingStars />\n</VJoyUserCard>',
60
+ },
61
+ recommended: true,
71
62
  },
72
63
  schema: [],
73
64
  messages: {
@@ -75,11 +66,7 @@ export default {
75
66
  },
76
67
  },
77
68
  create(context) {
78
- function visitElement(node, parentElement) {
79
- if (!node || node.type !== 'VElement') {
80
- return;
81
- }
82
-
69
+ return createTemplateBodyVisitor((node, parentElement) => {
83
70
  if (isVJoyUserCardElement(node)) {
84
71
  reportDeprecatedSlotAttribute(context, node);
85
72
  }
@@ -87,24 +74,6 @@ export default {
87
74
  if (parentElement && isVJoyUserCardElement(parentElement) && node.name === 'template') {
88
75
  reportDeprecatedSlotAttribute(context, node);
89
76
  }
90
-
91
- if (!Array.isArray(node.children)) {
92
- return;
93
- }
94
-
95
- node.children.forEach((child) => {
96
- visitElement(child, node);
97
- });
98
- }
99
-
100
- return {
101
- Program(programNode) {
102
- if (!programNode || !programNode.templateBody) {
103
- return;
104
- }
105
-
106
- visitElement(programNode.templateBody);
107
- },
108
- };
77
+ });
109
78
  },
110
79
  };
@@ -0,0 +1,62 @@
1
+ import {
2
+ createTemplateBodyVisitor,
3
+ isElementNamed,
4
+ isNamedSlotTemplate,
5
+ } from './utils/vue-ast.js';
6
+
7
+ const ERROR_MESSAGE =
8
+ 'VJoyMenu must be used inside the VJoyTemplate #template-sidebar slot.';
9
+ const TEMPLATE_SIDEBAR_SLOT = 'template-sidebar';
10
+
11
+ function isVJoyMenuElement(node) {
12
+ return isElementNamed(node, ['vjoymenu', 'v-joy-menu']);
13
+ }
14
+
15
+ function isVJoyTemplateElement(node) {
16
+ return isElementNamed(node, ['vjoytemplate', 'v-joy-template']);
17
+ }
18
+
19
+ function isTemplateSidebarSlot(node) {
20
+ return isNamedSlotTemplate(node, TEMPLATE_SIDEBAR_SLOT);
21
+ }
22
+
23
+ function isInsideTemplateSidebar(ancestors) {
24
+ return ancestors.some((ancestor, index) => {
25
+ return (
26
+ isVJoyTemplateElement(ancestor) &&
27
+ isTemplateSidebarSlot(ancestors[index + 1])
28
+ );
29
+ });
30
+ }
31
+
32
+ export default {
33
+ meta: {
34
+ type: 'suggestion',
35
+ docs: {
36
+ description: 'require VJoyMenu to be inside the VJoyTemplate template-sidebar slot',
37
+ details:
38
+ 'VJoyMenu must be placed in the direct template-sidebar slot of VJoyTemplate.',
39
+ examples: {
40
+ incorrect:
41
+ '<VJoyTemplate>\n <template #template-main>\n <VJoyMenu />\n </template>\n</VJoyTemplate>',
42
+ correct:
43
+ '<VJoyTemplate>\n <template #template-sidebar>\n <VJoyMenu />\n </template>\n</VJoyTemplate>',
44
+ },
45
+ recommended: true,
46
+ },
47
+ schema: [],
48
+ messages: {
49
+ useTemplateSidebar: ERROR_MESSAGE,
50
+ },
51
+ },
52
+ create(context) {
53
+ return createTemplateBodyVisitor((node, _parentElement, ancestors) => {
54
+ if (isVJoyMenuElement(node) && !isInsideTemplateSidebar(ancestors)) {
55
+ context.report({
56
+ node,
57
+ messageId: 'useTemplateSidebar',
58
+ });
59
+ }
60
+ });
61
+ },
62
+ };
@@ -0,0 +1,60 @@
1
+ import {
2
+ containsElement,
3
+ createTemplateBodyVisitor,
4
+ isElementNamed,
5
+ isNamedSlotTemplate,
6
+ } from './utils/vue-ast.js';
7
+
8
+ const ERROR_MESSAGE =
9
+ 'VJoyTemplate template-title slot should use a heading tag (h1-h6), preferably h1.';
10
+ const TEMPLATE_TITLE_SLOT = 'template-title';
11
+
12
+ function isVJoyTemplateElement(node) {
13
+ return isElementNamed(node, ['vjoytemplate', 'v-joy-template']);
14
+ }
15
+
16
+ function isTemplateTitleSlot(node) {
17
+ return isNamedSlotTemplate(node, TEMPLATE_TITLE_SLOT);
18
+ }
19
+
20
+ function containsHeadingElement(node) {
21
+ return containsElement(node, (element) => /^h[1-6]$/.test(element.name));
22
+ }
23
+
24
+ export default {
25
+ meta: {
26
+ type: 'suggestion',
27
+ docs: {
28
+ description:
29
+ 'require VJoyTemplate template-title slots to contain a heading element',
30
+ details:
31
+ 'The template-title slot must contain an h1 to h6 element, preferably an h1, to preserve the page heading hierarchy.',
32
+ examples: {
33
+ incorrect:
34
+ '<VJoyTemplate>\n <template #template-title>\n <div>Page title</div>\n </template>\n</VJoyTemplate>',
35
+ correct:
36
+ '<VJoyTemplate>\n <template #template-title>\n <h1>Page title</h1>\n </template>\n</VJoyTemplate>',
37
+ },
38
+ recommended: true,
39
+ },
40
+ schema: [],
41
+ messages: {
42
+ requireHeading: ERROR_MESSAGE,
43
+ },
44
+ },
45
+ create(context) {
46
+ return createTemplateBodyVisitor((node, parentElement) => {
47
+ if (
48
+ parentElement &&
49
+ isVJoyTemplateElement(parentElement) &&
50
+ isTemplateTitleSlot(node) &&
51
+ !containsHeadingElement(node)
52
+ ) {
53
+ context.report({
54
+ node,
55
+ messageId: 'requireHeading',
56
+ });
57
+ }
58
+ });
59
+ },
60
+ };
@@ -0,0 +1,52 @@
1
+ import {
2
+ createTemplateBodyVisitor,
3
+ isElementNamed,
4
+ isNamedSlotTemplate,
5
+ } from './utils/vue-ast.js';
6
+
7
+ const ERROR_MESSAGE = 'VJoyTemplate must have a template-title slot.';
8
+ const TEMPLATE_TITLE_SLOT = 'template-title';
9
+
10
+ function isVJoyTemplateElement(node) {
11
+ return isElementNamed(node, ['vjoytemplate', 'v-joy-template']);
12
+ }
13
+
14
+ function isTemplateTitleSlot(node) {
15
+ return isNamedSlotTemplate(node, TEMPLATE_TITLE_SLOT);
16
+ }
17
+
18
+ function hasTemplateTitleSlot(node) {
19
+ return Array.isArray(node.children) && node.children.some(isTemplateTitleSlot);
20
+ }
21
+
22
+ export default {
23
+ meta: {
24
+ type: 'suggestion',
25
+ docs: {
26
+ description: 'require VJoyTemplate to provide the template-title slot',
27
+ details:
28
+ 'Every VJoyTemplate must provide a direct template-title slot so pages have a consistent title region.',
29
+ examples: {
30
+ incorrect:
31
+ '<VJoyTemplate>\n <template #template-main>Content</template>\n</VJoyTemplate>',
32
+ correct:
33
+ '<VJoyTemplate>\n <template #template-title>\n <h1>Page title</h1>\n </template>\n <template #template-main>Content</template>\n</VJoyTemplate>',
34
+ },
35
+ recommended: true,
36
+ },
37
+ schema: [],
38
+ messages: {
39
+ requireTemplateTitle: ERROR_MESSAGE,
40
+ },
41
+ },
42
+ create(context) {
43
+ return createTemplateBodyVisitor((node) => {
44
+ if (isVJoyTemplateElement(node) && !hasTemplateTitleSlot(node)) {
45
+ context.report({
46
+ node,
47
+ messageId: 'requireTemplateTitle',
48
+ });
49
+ }
50
+ });
51
+ },
52
+ };
@@ -0,0 +1,58 @@
1
+ import { createTemplateBodyVisitor, isElementNamed } from './vue-ast.js';
2
+
3
+ function isStaticOrBoundAttributeNamed(node, attributeName) {
4
+ if (node?.type !== 'VAttribute') {
5
+ return false;
6
+ }
7
+
8
+ if (!node.directive) {
9
+ return node.key?.name === attributeName;
10
+ }
11
+
12
+ return (
13
+ node.key?.name?.name === 'bind' &&
14
+ node.key?.argument?.name === attributeName
15
+ );
16
+ }
17
+
18
+ export function createForbiddenAttributeRule({
19
+ attributeName,
20
+ description,
21
+ details,
22
+ elementNames,
23
+ examples,
24
+ message,
25
+ messageId,
26
+ }) {
27
+ return {
28
+ meta: {
29
+ type: 'suggestion',
30
+ docs: {
31
+ description,
32
+ details,
33
+ examples,
34
+ recommended: true,
35
+ },
36
+ schema: [],
37
+ messages: {
38
+ [messageId]: message,
39
+ },
40
+ },
41
+ create(context) {
42
+ return createTemplateBodyVisitor((node) => {
43
+ if (!isElementNamed(node, elementNames) || !Array.isArray(node.startTag?.attributes)) {
44
+ return;
45
+ }
46
+
47
+ node.startTag.attributes.forEach((attribute) => {
48
+ if (isStaticOrBoundAttributeNamed(attribute, attributeName)) {
49
+ context.report({
50
+ node: attribute,
51
+ messageId,
52
+ });
53
+ }
54
+ });
55
+ });
56
+ },
57
+ };
58
+ }
@@ -0,0 +1,69 @@
1
+ import { createTemplateBodyVisitor, getStaticName, isElementNamed } from './vue-ast.js';
2
+
3
+ const VJOY_AVATAR_NAMES = ['vjoyavatar', 'v-joy-avatar'];
4
+
5
+ function getPropName(attribute) {
6
+ if (attribute?.type !== 'VAttribute' || !attribute.key) {
7
+ return undefined;
8
+ }
9
+
10
+ if (!attribute.directive) {
11
+ return attribute.key.name;
12
+ }
13
+
14
+ if (attribute.key.name?.name !== 'bind') {
15
+ return undefined;
16
+ }
17
+
18
+ return getStaticName(attribute.key.argument);
19
+ }
20
+
21
+ function hasProps(node, propNames) {
22
+ if (!node.startTag || !Array.isArray(node.startTag.attributes)) {
23
+ return false;
24
+ }
25
+
26
+ const normalizedPropNames = new Set(
27
+ node.startTag.attributes
28
+ .map(getPropName)
29
+ .filter((propName) => typeof propName === 'string')
30
+ .map((propName) => propName.toLowerCase().replaceAll('-', '')),
31
+ );
32
+
33
+ return propNames.every((propName) => normalizedPropNames.has(propName));
34
+ }
35
+
36
+ export function createVJoyAvatarConflictingPropsRule({
37
+ description,
38
+ details,
39
+ examples,
40
+ message,
41
+ messageId,
42
+ propNames,
43
+ }) {
44
+ return {
45
+ meta: {
46
+ type: 'suggestion',
47
+ docs: {
48
+ description,
49
+ details,
50
+ examples,
51
+ recommended: true,
52
+ },
53
+ schema: [],
54
+ messages: {
55
+ [messageId]: message,
56
+ },
57
+ },
58
+ create(context) {
59
+ return createTemplateBodyVisitor((node) => {
60
+ if (isElementNamed(node, VJOY_AVATAR_NAMES) && hasProps(node, propNames)) {
61
+ context.report({
62
+ node,
63
+ messageId,
64
+ });
65
+ }
66
+ });
67
+ },
68
+ };
69
+ }
@@ -0,0 +1,81 @@
1
+ export function getStaticName(node) {
2
+ if (!node) {
3
+ return undefined;
4
+ }
5
+
6
+ if (typeof node.name === 'string') {
7
+ return node.name;
8
+ }
9
+
10
+ if (typeof node.value === 'string') {
11
+ return node.value;
12
+ }
13
+
14
+ return undefined;
15
+ }
16
+
17
+ export function isElementNamed(node, names) {
18
+ return (
19
+ node?.type === 'VElement' &&
20
+ typeof node.name === 'string' &&
21
+ names.includes(node.name.toLowerCase())
22
+ );
23
+ }
24
+
25
+ export function isNamedSlotTemplate(node, slotName) {
26
+ if (node?.type !== 'VElement' || node.name !== 'template' || !node.startTag) {
27
+ return false;
28
+ }
29
+
30
+ return node.startTag.attributes.some((attribute) => {
31
+ if (attribute?.type !== 'VAttribute' || !attribute.directive) {
32
+ return false;
33
+ }
34
+
35
+ return (
36
+ attribute.key?.name?.name === 'slot' &&
37
+ getStaticName(attribute.key?.argument) === slotName
38
+ );
39
+ });
40
+ }
41
+
42
+ export function containsElement(node, predicate, shouldSkip = () => false) {
43
+ if (node?.type !== 'VElement' || shouldSkip(node)) {
44
+ return false;
45
+ }
46
+
47
+ if (predicate(node)) {
48
+ return true;
49
+ }
50
+
51
+ return Array.isArray(node.children) && node.children.some((child) =>
52
+ containsElement(child, predicate, shouldSkip),
53
+ );
54
+ }
55
+
56
+ export function createTemplateBodyVisitor(visitElement) {
57
+ function visit(node, parentElement = null, ancestors = []) {
58
+ if (node?.type !== 'VElement') {
59
+ return;
60
+ }
61
+
62
+ visitElement(node, parentElement, ancestors);
63
+
64
+ if (!Array.isArray(node.children)) {
65
+ return;
66
+ }
67
+
68
+ const childAncestors = [...ancestors, node];
69
+ node.children.forEach((child) => {
70
+ visit(child, node, childAncestors);
71
+ });
72
+ }
73
+
74
+ return {
75
+ Program(programNode) {
76
+ if (programNode?.templateBody) {
77
+ visit(programNode.templateBody);
78
+ }
79
+ },
80
+ };
81
+ }