@maltjoy/eslint-rules 0.1.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 ADDED
@@ -0,0 +1,35 @@
1
+ # @maltjoy/eslint-rules
2
+
3
+ Custom ESLint rules for Maltjoy design system usage.
4
+
5
+ ## Rules
6
+
7
+ - `no-vjoybutton-circle`: disallow `circle` prop usage on `VJoyButton`.
8
+
9
+ Message:
10
+
11
+ `VJoyButton circle property is deprecated, please use VJoyIconButton instead`
12
+
13
+ - `no-vjoyinput-type-password`: disallow `VJoyInput` with `type="password"` (or a literal `:type` bound to `password`). Use `VJoyInputPassword` instead.
14
+
15
+ ## Usage (flat config)
16
+
17
+ ```js
18
+ const maltjoyRules = require('@maltjoy/eslint-rules');
19
+ const vueParser = require('vue-eslint-parser');
20
+
21
+ module.exports = [
22
+ {
23
+ files: ['**/*.vue'],
24
+ languageOptions: {
25
+ parser: vueParser,
26
+ },
27
+ plugins: {
28
+ '@maltjoy/eslint-rules': maltjoyRules,
29
+ },
30
+ rules: {
31
+ '@maltjoy/eslint-rules/no-vjoybutton-circle': 'warn', // or 'error'
32
+ },
33
+ },
34
+ ];
35
+ ```
package/index.js ADDED
@@ -0,0 +1,11 @@
1
+ const noVJoyButtonCircle = require('./rules/no-vjoybutton-circle');
2
+ const noVJoyInputTypePassword = require('./rules/no-vjoyinput-type-password');
3
+
4
+ const rules = {
5
+ 'no-vjoybutton-circle': noVJoyButtonCircle,
6
+ 'no-vjoyinput-type-password': noVJoyInputTypePassword,
7
+ };
8
+
9
+ module.exports = {
10
+ rules,
11
+ };
package/package.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "name": "@maltjoy/eslint-rules",
3
+ "version": "0.1.0",
4
+ "description": "Custom ESLint rules for the @maltjoy/core-vue design system",
5
+ "main": "index.js",
6
+ "files": [
7
+ "index.js",
8
+ "rules",
9
+ "README.md"
10
+ ],
11
+ "scripts": {
12
+ "build": "pnpm test",
13
+ "test": "node --test tests/*.test.js"
14
+ },
15
+ "peerDependencies": {
16
+ "eslint": ">=8"
17
+ },
18
+ "devDependencies": {
19
+ "eslint": "9.39.4",
20
+ "vue-eslint-parser": "10.4.0"
21
+ },
22
+ "license": "MIT"
23
+ }
@@ -0,0 +1,74 @@
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
+ }
25
+
26
+ module.exports = {
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
+ }
69
+
70
+ visitElement(programNode.templateBody);
71
+ },
72
+ };
73
+ },
74
+ };
@@ -0,0 +1,111 @@
1
+ const ERROR_MESSAGE =
2
+ 'Do not use VJoyInput with type="password", use VJoyInputPassword instead.';
3
+
4
+ function isPasswordString(value) {
5
+ return typeof value === 'string' && value.toLowerCase() === 'password';
6
+ }
7
+
8
+ function isLiteralPasswordExpression(expr) {
9
+ if (!expr) {
10
+ return false;
11
+ }
12
+
13
+ if (expr.type === 'Literal' && isPasswordString(expr.value)) {
14
+ return true;
15
+ }
16
+
17
+ if (
18
+ expr.type === 'TemplateLiteral' &&
19
+ expr.expressions.length === 0 &&
20
+ expr.quasis.length === 1
21
+ ) {
22
+ const cooked = expr.quasis[0].value.cooked;
23
+ return isPasswordString(cooked);
24
+ }
25
+
26
+ return false;
27
+ }
28
+
29
+ function isTypePasswordAttribute(node) {
30
+ if (!node || node.type !== 'VAttribute') {
31
+ return false;
32
+ }
33
+
34
+ if (!node.directive) {
35
+ if (node.key && node.key.name === 'type' && node.value && node.value.type === 'VLiteral') {
36
+ return isPasswordString(node.value.value);
37
+ }
38
+ return false;
39
+ }
40
+
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
+
44
+ if (directiveName !== 'bind' || argumentName !== 'type') {
45
+ return false;
46
+ }
47
+
48
+ if (!node.value || node.value.type !== 'VExpressionContainer') {
49
+ return false;
50
+ }
51
+
52
+ return isLiteralPasswordExpression(node.value.expression);
53
+ }
54
+
55
+ function isVJoyInputElement(node) {
56
+ if (!node || node.type !== 'VElement' || typeof node.name !== 'string') {
57
+ return false;
58
+ }
59
+
60
+ return node.name.toLowerCase() === 'vjoyinput';
61
+ }
62
+
63
+ module.exports = {
64
+ meta: {
65
+ type: 'suggestion',
66
+ docs: {
67
+ description: 'disallow VJoyInput with type password; use VJoyInputPassword',
68
+ recommended: false,
69
+ },
70
+ schema: [],
71
+ messages: {
72
+ useVJoyInputPassword: ERROR_MESSAGE,
73
+ },
74
+ },
75
+ 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)) {
82
+ node.startTag.attributes.forEach((attribute) => {
83
+ if (isTypePasswordAttribute(attribute)) {
84
+ context.report({
85
+ node: attribute,
86
+ messageId: 'useVJoyInputPassword',
87
+ });
88
+ }
89
+ });
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
+ };
110
+ },
111
+ };