@agilebot/eslint-plugin 0.3.10 → 0.3.12

Sign up to get free protection for your applications and to get access to all the features.
package/LICENSE CHANGED
@@ -1,9 +1,9 @@
1
- The MIT License (MIT)
2
-
3
- Copyright (c) 2024 Agilebot, Inc.
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
-
7
- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
-
9
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Agilebot, Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/LICENSE.tpl CHANGED
@@ -1,8 +1,8 @@
1
- /**
2
- * @license %(name)s v%(version)s
3
- *
4
- * Copyright (c) Agilebot, Inc. and its affiliates.
5
- *
6
- * This source code is licensed under the MIT license found in the
7
- * LICENSE file in the root directory of this source tree.
8
- */
1
+ /**
2
+ * @license %(name)s v%(version)s
3
+ *
4
+ * Copyright (c) Agilebot, Inc. and its affiliates.
5
+ *
6
+ * This source code is licensed under the MIT license found in the
7
+ * LICENSE file in the root directory of this source tree.
8
+ */
package/README.md CHANGED
@@ -1,9 +1,9 @@
1
- # @agilebot/eslint-plugin
2
-
3
- Agilebot extended ESLint rules. For @agilebot/eslint-config.
4
-
5
- ### Usage
6
-
7
- ```bash
8
- npm install --save-dev eslint @agilebot/eslint-plugin
9
- ```
1
+ # @agilebot/eslint-plugin
2
+
3
+ Agilebot extended ESLint rules. For @agilebot/eslint-config.
4
+
5
+ ### Usage
6
+
7
+ ```bash
8
+ npm install --save-dev eslint @agilebot/eslint-plugin
9
+ ```
package/dist/index.js CHANGED
@@ -1,11 +1,11 @@
1
- /**
2
- * @license @agilebot/eslint-plugin v0.3.10
3
- *
4
- * Copyright (c) Agilebot, Inc. and its affiliates.
5
- *
6
- * This source code is licensed under the MIT license found in the
7
- * LICENSE file in the root directory of this source tree.
8
- */
1
+ /**
2
+ * @license @agilebot/eslint-plugin v0.3.12
3
+ *
4
+ * Copyright (c) Agilebot, Inc. and its affiliates.
5
+ *
6
+ * This source code is licensed under the MIT license found in the
7
+ * LICENSE file in the root directory of this source tree.
8
+ */
9
9
  'use strict';
10
10
 
11
11
  var eslintUtils = require('@agilebot/eslint-utils');
@@ -72,127 +72,6 @@ var enforceMuiIconAlias = eslintUtils.createESLintRule({
72
72
  }
73
73
  });
74
74
 
75
- var funcNaming = eslintUtils.createESLintRule({
76
- meta: {
77
- type: 'problem',
78
- docs: {
79
- description: 'Enforce function naming convention'
80
- },
81
- schema: [{
82
- type: 'object',
83
- properties: {
84
- format: {
85
- type: 'string',
86
- "enum": ['camelCase', 'PascalCase']
87
- }
88
- }
89
- }],
90
- messages: {
91
- invalidFuncNaming: 'Invalid function naming for non-React component, expected {{format}}',
92
- invalidReactFCNaming: 'Invalid naming convention for React functional component, expected PascalCase'
93
- }
94
- },
95
- defaultOptions: [],
96
- create(context) {
97
- if (!context.options[0]) {
98
- throw new Error('Missing options');
99
- }
100
- const format = context.options[0].format;
101
- function validate({
102
- node,
103
- fnName
104
- }) {
105
- let isPass;
106
- switch (format) {
107
- case 'camelCase':
108
- if (!eslintUtils.isCamelCase(fnName)) {
109
- isPass = false;
110
- }
111
- break;
112
- case 'PascalCase':
113
- if (!eslintUtils.isPascalCase(fnName)) {
114
- isPass = false;
115
- }
116
- break;
117
- }
118
- if (isPass === false) {
119
- context.report({
120
- node: node,
121
- messageId: 'invalidFuncNaming',
122
- data: {
123
- format
124
- }
125
- });
126
- }
127
- }
128
- function checkJSXElement(node) {
129
- if (!node) {
130
- return false;
131
- }
132
- if (node.type === 'JSXElement' || node.type === 'JSXFragment') {
133
- return true;
134
- }
135
- if (node.type === 'BlockStatement') {
136
- for (const statement of node.body) {
137
- if (statement.type === 'ReturnStatement') {
138
- if (checkJSXElement(statement.argument)) {
139
- return true;
140
- }
141
- } else if (checkJSXElement(statement)) {
142
- return true;
143
- }
144
- }
145
- }
146
- if (node.type === 'ArrowFunctionExpression' || node.type === 'FunctionExpression') {
147
- return checkJSXElement(node.body);
148
- }
149
- return false;
150
- }
151
- return {
152
- MethodDefinition(node) {
153
- if (node.kind === 'method') ;
154
- },
155
- FunctionDeclaration(node) {
156
- if (node.type === 'FunctionDeclaration' && node.id) {
157
- const fnName = node.id.name;
158
- const isReactComponent = checkJSXElement(node.body);
159
- if (!isReactComponent) {
160
- validate({
161
- node,
162
- fnName
163
- });
164
- }
165
- }
166
- },
167
- VariableDeclarator(node) {
168
- if (node.id && node.init && ['FunctionExpression', 'ArrowFunctionExpression'].includes(node.init.type)) {
169
- const fnName = node.id.name;
170
- let isReactComponent = checkJSXElement(node.init.body);
171
- if (node.id.typeAnnotation && node.id.typeAnnotation.typeAnnotation) {
172
- const typeAnnotation = node.id.typeAnnotation.typeAnnotation;
173
- if (typeAnnotation.type === 'TSTypeReference' && typeAnnotation.typeName.type === 'Identifier') {
174
- const typeName = typeAnnotation.typeName.name;
175
- const typeNameLast = typeName.split('.').pop();
176
- if (['FC', 'FunctionComponent', 'VFC', 'VoidFunctionComponent'].includes(typeNameLast)) {
177
- isReactComponent = true;
178
- }
179
- }
180
- }
181
- if (!isReactComponent) {
182
- validate({
183
- node,
184
- fnName
185
- });
186
- }
187
- }
188
- },
189
- Property(node) {
190
- if (node.value.type === 'FunctionExpression') ;
191
- }
192
- };
193
- }
194
- });
195
-
196
75
  function getSetting(context, name) {
197
76
  return context.settings["agilebot/".concat(name)];
198
77
  }
@@ -2679,10 +2558,204 @@ var tssUnusedClasses = eslintUtils.createESLintRule({
2679
2558
  }
2680
2559
  });
2681
2560
 
2561
+ const reactGlobalFuncs = new Set(['createContext', 'forwardRef', 'lazy', 'memo', 'combineProviders']);
2562
+ const reactFCTypes = new Set(['FC', 'FunctionComponent', 'VFC', 'VoidFunctionComponent']);
2563
+ const otherTypes = new Set(['StoryObj', 'StoryFn']);
2564
+ const defaultExcludes = ['^(__dirname|__filename)$', '(.*)Event$'];
2565
+ var varNaming = eslintUtils.createESLintRule({
2566
+ meta: {
2567
+ type: 'problem',
2568
+ docs: {
2569
+ description: 'Enforce variable and function naming convention',
2570
+ recommended: 'recommended'
2571
+ },
2572
+ schema: [{
2573
+ type: 'object',
2574
+ properties: {
2575
+ funcFormat: {
2576
+ type: 'array',
2577
+ items: {
2578
+ "enum": ['camelCase', 'PascalCase', 'UPPER_CASE']
2579
+ }
2580
+ },
2581
+ varFormat: {
2582
+ type: 'array',
2583
+ items: {
2584
+ "enum": ['camelCase', 'PascalCase', 'UPPER_CASE']
2585
+ }
2586
+ },
2587
+ exclude: {
2588
+ type: 'array',
2589
+ items: {
2590
+ type: 'string'
2591
+ }
2592
+ }
2593
+ }
2594
+ }],
2595
+ messages: {
2596
+ invalidFuncNaming: 'Invalid function naming for non-React component, expected {{formats}}',
2597
+ invalidReactFCNaming: 'Invalid naming convention for React functional component, expected PascalCase',
2598
+ invalidVarNaming: 'Invalid variable naming, expected {{formats}}'
2599
+ }
2600
+ },
2601
+ defaultOptions: [],
2602
+ create(context) {
2603
+ if (!context.options[0]) {
2604
+ context.options[0] = {};
2605
+ }
2606
+ const funcFormat = context.options[0].funcFormat || ['camelCase'];
2607
+ const varFormat = context.options[0].varFormat || ['camelCase', 'UPPER_CASE'];
2608
+ let exclude = context.options[0].exclude || [];
2609
+ exclude = [...defaultExcludes, ...exclude];
2610
+ function validate(type, {
2611
+ node,
2612
+ name
2613
+ }) {
2614
+ let isPass = false;
2615
+ let formats;
2616
+ let messageId;
2617
+ switch (type) {
2618
+ case 'func':
2619
+ formats = funcFormat;
2620
+ messageId = 'invalidFuncNaming';
2621
+ break;
2622
+ case 'var':
2623
+ formats = varFormat;
2624
+ messageId = 'invalidVarNaming';
2625
+ break;
2626
+ }
2627
+ for (const format of formats) {
2628
+ switch (format) {
2629
+ case 'camelCase':
2630
+ if (eslintUtils.isCamelCase(name)) {
2631
+ isPass = true;
2632
+ }
2633
+ break;
2634
+ case 'PascalCase':
2635
+ if (eslintUtils.isPascalCase(name)) {
2636
+ isPass = true;
2637
+ }
2638
+ break;
2639
+ case 'UPPER_CASE':
2640
+ if (eslintUtils.isUpperCase(name)) {
2641
+ isPass = true;
2642
+ }
2643
+ break;
2644
+ }
2645
+ }
2646
+ if (!isPass) {
2647
+ context.report({
2648
+ node: node,
2649
+ messageId: messageId,
2650
+ data: {
2651
+ formats: formats.join(', ')
2652
+ }
2653
+ });
2654
+ }
2655
+ }
2656
+ function checkJSXElement(node) {
2657
+ if (!node) {
2658
+ return false;
2659
+ }
2660
+ if (node.type === 'JSXElement' || node.type === 'JSXFragment') {
2661
+ return true;
2662
+ }
2663
+ if (node.type === 'BlockStatement') {
2664
+ for (const statement of node.body) {
2665
+ if (statement.type === 'ReturnStatement') {
2666
+ if (checkJSXElement(statement.argument)) {
2667
+ return true;
2668
+ }
2669
+ } else if (checkJSXElement(statement)) {
2670
+ return true;
2671
+ }
2672
+ }
2673
+ }
2674
+ if (node.type === 'ArrowFunctionExpression' || node.type === 'FunctionExpression') {
2675
+ return checkJSXElement(node.body);
2676
+ }
2677
+ return false;
2678
+ }
2679
+ function getTypeReference(node) {
2680
+ if (node.id.typeAnnotation && node.id.typeAnnotation.typeAnnotation) {
2681
+ const typeAnnotation = node.id.typeAnnotation.typeAnnotation;
2682
+ if (typeAnnotation.type === 'TSTypeReference' && typeAnnotation.typeName.type === 'Identifier') {
2683
+ const typeName = typeAnnotation.typeName.name;
2684
+ const typeNameLast = typeName.split('.').pop();
2685
+ return typeNameLast;
2686
+ }
2687
+ }
2688
+ }
2689
+ return {
2690
+ FunctionDeclaration(node) {
2691
+ if (node.type === 'FunctionDeclaration' && node.id) {
2692
+ const fnName = node.id.name;
2693
+ const isReactComponent = checkJSXElement(node.body);
2694
+ if (!isReactComponent) {
2695
+ validate('func', {
2696
+ node,
2697
+ name: fnName
2698
+ });
2699
+ }
2700
+ }
2701
+ },
2702
+ VariableDeclarator(node) {
2703
+ if (node.id && node.init && ['FunctionExpression', 'ArrowFunctionExpression'].includes(node.init.type)) {
2704
+ const fnName = node.id.name;
2705
+ let isReactComponent = checkJSXElement(node.init.body);
2706
+ const typeName = getTypeReference(node);
2707
+ if (typeName && reactFCTypes.has(typeName)) {
2708
+ isReactComponent = true;
2709
+ }
2710
+ if (!isReactComponent) {
2711
+ validate('func', {
2712
+ node,
2713
+ name: fnName
2714
+ });
2715
+ }
2716
+ } else if (node.id) {
2717
+ const varName = node.id.name;
2718
+ for (const excludeRegex of exclude) {
2719
+ if (new RegExp(excludeRegex).test(varName)) {
2720
+ return;
2721
+ }
2722
+ }
2723
+ const typeName = getTypeReference(node);
2724
+ if (typeName && otherTypes.has(typeName)) {
2725
+ return;
2726
+ }
2727
+ if (node.id.parent && node.id.parent.init) {
2728
+ let calleeName;
2729
+ let shouldCheckReact = false;
2730
+ if (node.id.parent.init.type === 'CallExpression') {
2731
+ shouldCheckReact = true;
2732
+ calleeName = node.id.parent.init.callee.name;
2733
+ } else if (node.id.parent.init.type === 'TSAsExpression' && node.id.parent.init.expression && node.id.parent.init.expression.callee) {
2734
+ shouldCheckReact = true;
2735
+ calleeName = node.id.parent.init.expression.callee.name;
2736
+ }
2737
+ if (shouldCheckReact) {
2738
+ if (!calleeName) {
2739
+ return;
2740
+ }
2741
+ if (reactGlobalFuncs.has(calleeName) || reactGlobalFuncs.has(calleeName.split('.').pop())) {
2742
+ return;
2743
+ }
2744
+ }
2745
+ }
2746
+ validate('var', {
2747
+ node,
2748
+ name: varName
2749
+ });
2750
+ }
2751
+ }
2752
+ };
2753
+ }
2754
+ });
2755
+
2682
2756
  var ruleFiles = /*#__PURE__*/Object.freeze({
2683
2757
  __proto__: null,
2684
2758
  rules_enforce_mui_icon_alias: enforceMuiIconAlias,
2685
- rules_func_naming: funcNaming,
2686
2759
  rules_import_monorepo: importMonorepo,
2687
2760
  rules_intl_id_missing: intlIdMissing,
2688
2761
  rules_intl_id_naming: intlIdNaming,
@@ -2700,7 +2773,8 @@ var ruleFiles = /*#__PURE__*/Object.freeze({
2700
2773
  rules_tss_class_naming: tssClassNaming,
2701
2774
  rules_tss_no_color_name: tssNoColorName,
2702
2775
  rules_tss_no_color_value: tssNoColorValue,
2703
- rules_tss_unused_classes: tssUnusedClasses
2776
+ rules_tss_unused_classes: tssUnusedClasses,
2777
+ rules_var_naming: varNaming
2704
2778
  });
2705
2779
 
2706
2780
  const plugin = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agilebot/eslint-plugin",
3
- "version": "0.3.10",
3
+ "version": "0.3.12",
4
4
  "description": "Agilebot's ESLint plugin",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -18,8 +18,8 @@
18
18
  "node": "^18.18.0 || >=20.0.0"
19
19
  },
20
20
  "dependencies": {
21
- "eslint-plugin-react": "^7.34.3",
22
- "@agilebot/eslint-utils": "0.3.10"
21
+ "eslint-plugin-react": "^7.35.0",
22
+ "@agilebot/eslint-utils": "0.3.12"
23
23
  },
24
24
  "peerDependencies": {
25
25
  "eslint": "^7.0.0 || ^8.0.0"
@@ -31,7 +31,7 @@
31
31
  "@types/color-name": "^1.1.4",
32
32
  "@types/estree": "^1.0.5",
33
33
  "color-name": "^2.0.0",
34
- "eslint-vitest-rule-tester": "^0.3.2",
34
+ "eslint-vitest-rule-tester": "^0.3.3",
35
35
  "typescript-eslint": "^7.16.0"
36
36
  },
37
37
  "scripts": {