zeiger-eslint-plugin 0.1.0 → 0.1.1
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 +32 -1
- package/dist/index.cjs +22 -6
- package/dist/index.js +22 -6
- package/dist/rules/zeiger-deps.d.ts.map +1 -1
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
# zeiger-eslint-plugin
|
|
2
2
|
|
|
3
|
-
ESLint plugin for [Zeiger](https://
|
|
3
|
+
ESLint plugin for [Zeiger](https://www.npmjs.com/package/zeiger) - ensures proper usage of Zeiger hooks with dependency arrays.
|
|
4
|
+
|
|
5
|
+
Zeiger is a library for creating memoized collection item selectors for [Zustand](https://github.com/pmndrs/zustand) that eliminates unnecessary re-renders. This ESLint plugin helps you use Zeiger correctly by catching common mistakes with dependency arrays.
|
|
4
6
|
|
|
5
7
|
## Installation
|
|
6
8
|
|
|
@@ -86,6 +88,34 @@ return (
|
|
|
86
88
|
);
|
|
87
89
|
```
|
|
88
90
|
|
|
91
|
+
## Autofix
|
|
92
|
+
|
|
93
|
+
The plugin supports automatic fixing of unused dependencies. When you run ESLint with the `--fix` flag, it will automatically remove unused properties from dependency arrays:
|
|
94
|
+
|
|
95
|
+
```bash
|
|
96
|
+
eslint --fix .
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
**Example:**
|
|
100
|
+
|
|
101
|
+
```tsx
|
|
102
|
+
// Before
|
|
103
|
+
const user = useUserPointer('1', ['firstName', 'surname', 'email']);
|
|
104
|
+
return (
|
|
105
|
+
<div>
|
|
106
|
+
{user?.firstName} - {user?.email}
|
|
107
|
+
</div>
|
|
108
|
+
);
|
|
109
|
+
|
|
110
|
+
// After autofix
|
|
111
|
+
const user = useUserPointer('1', ['firstName', 'email']);
|
|
112
|
+
return (
|
|
113
|
+
<div>
|
|
114
|
+
{user?.firstName} - {user?.email}
|
|
115
|
+
</div>
|
|
116
|
+
);
|
|
117
|
+
```
|
|
118
|
+
|
|
89
119
|
## Why This Plugin?
|
|
90
120
|
|
|
91
121
|
Zeiger hooks use dependency arrays to specify which properties to subscribe to. This plugin helps you:
|
|
@@ -93,6 +123,7 @@ Zeiger hooks use dependency arrays to specify which properties to subscribe to.
|
|
|
93
123
|
- **Avoid unnecessary subscriptions** - Catch unused properties in dependency arrays
|
|
94
124
|
- **Prevent empty arrays** - Ensure you're always subscribing to at least one property
|
|
95
125
|
- **Maintain code quality** - Keep your Zeiger hooks clean and efficient
|
|
126
|
+
- **Automatic fixes** - Remove unused dependencies with ESLint's autofix feature
|
|
96
127
|
|
|
97
128
|
## License
|
|
98
129
|
|
package/dist/index.cjs
CHANGED
|
@@ -29,6 +29,7 @@ __webpack_require__.d(__webpack_exports__, {
|
|
|
29
29
|
const rule = {
|
|
30
30
|
meta: {
|
|
31
31
|
type: 'problem',
|
|
32
|
+
fixable: 'code',
|
|
32
33
|
docs: {
|
|
33
34
|
description: 'Ensure zeiger hook dependency arrays are not empty and all dependencies are used'
|
|
34
35
|
},
|
|
@@ -145,19 +146,34 @@ const rule = {
|
|
|
145
146
|
const depsArg = callNode.arguments.length > 0 ? callNode.arguments[callNode.arguments.length - 1] : null;
|
|
146
147
|
if (depsArg && 'ArrayExpression' === depsArg.type) {
|
|
147
148
|
const arrayNode = depsArg;
|
|
149
|
+
const unusedDeps = [];
|
|
148
150
|
for(let i = 0; i < deps.length; i++){
|
|
149
151
|
const dep = deps[i];
|
|
150
152
|
if (!usedProperties.has(dep)) {
|
|
151
153
|
const depElement = arrayNode.elements[i];
|
|
152
|
-
if (depElement)
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
property: dep
|
|
157
|
-
}
|
|
154
|
+
if (depElement) unusedDeps.push({
|
|
155
|
+
element: depElement,
|
|
156
|
+
index: i,
|
|
157
|
+
property: dep
|
|
158
158
|
});
|
|
159
159
|
}
|
|
160
160
|
}
|
|
161
|
+
if (unusedDeps.length > 0) {
|
|
162
|
+
const usedDeps = deps.filter((dep)=>usedProperties.has(dep));
|
|
163
|
+
for (const { element, property } of unusedDeps)context.report({
|
|
164
|
+
node: element,
|
|
165
|
+
messageId: 'unusedDep',
|
|
166
|
+
data: {
|
|
167
|
+
property
|
|
168
|
+
},
|
|
169
|
+
fix (fixer) {
|
|
170
|
+
if (0 === usedDeps.length) return null;
|
|
171
|
+
const arrayText = usedDeps.map((dep)=>`'${dep}'`).join(', ');
|
|
172
|
+
const newArrayText = `[${arrayText}]`;
|
|
173
|
+
return fixer.replaceText(depsArg, newArrayText);
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
}
|
|
161
177
|
}
|
|
162
178
|
}
|
|
163
179
|
};
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
const rule = {
|
|
2
2
|
meta: {
|
|
3
3
|
type: 'problem',
|
|
4
|
+
fixable: 'code',
|
|
4
5
|
docs: {
|
|
5
6
|
description: 'Ensure zeiger hook dependency arrays are not empty and all dependencies are used'
|
|
6
7
|
},
|
|
@@ -117,19 +118,34 @@ const rule = {
|
|
|
117
118
|
const depsArg = callNode.arguments.length > 0 ? callNode.arguments[callNode.arguments.length - 1] : null;
|
|
118
119
|
if (depsArg && 'ArrayExpression' === depsArg.type) {
|
|
119
120
|
const arrayNode = depsArg;
|
|
121
|
+
const unusedDeps = [];
|
|
120
122
|
for(let i = 0; i < deps.length; i++){
|
|
121
123
|
const dep = deps[i];
|
|
122
124
|
if (!usedProperties.has(dep)) {
|
|
123
125
|
const depElement = arrayNode.elements[i];
|
|
124
|
-
if (depElement)
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
property: dep
|
|
129
|
-
}
|
|
126
|
+
if (depElement) unusedDeps.push({
|
|
127
|
+
element: depElement,
|
|
128
|
+
index: i,
|
|
129
|
+
property: dep
|
|
130
130
|
});
|
|
131
131
|
}
|
|
132
132
|
}
|
|
133
|
+
if (unusedDeps.length > 0) {
|
|
134
|
+
const usedDeps = deps.filter((dep)=>usedProperties.has(dep));
|
|
135
|
+
for (const { element, property } of unusedDeps)context.report({
|
|
136
|
+
node: element,
|
|
137
|
+
messageId: 'unusedDep',
|
|
138
|
+
data: {
|
|
139
|
+
property
|
|
140
|
+
},
|
|
141
|
+
fix (fixer) {
|
|
142
|
+
if (0 === usedDeps.length) return null;
|
|
143
|
+
const arrayText = usedDeps.map((dep)=>`'${dep}'`).join(', ');
|
|
144
|
+
const newArrayText = `[${arrayText}]`;
|
|
145
|
+
return fixer.replaceText(depsArg, newArrayText);
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
}
|
|
133
149
|
}
|
|
134
150
|
}
|
|
135
151
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"zeiger-deps.d.ts","sourceRoot":"","sources":["../../src/rules/zeiger-deps.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AAEnC,QAAA,MAAM,IAAI,EAAE,IAAI,CAAC,
|
|
1
|
+
{"version":3,"file":"zeiger-deps.d.ts","sourceRoot":"","sources":["../../src/rules/zeiger-deps.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AAEnC,QAAA,MAAM,IAAI,EAAE,IAAI,CAAC,UA0XhB,CAAC;AAEF,eAAe,IAAI,CAAC"}
|
package/package.json
CHANGED
|
@@ -4,8 +4,9 @@
|
|
|
4
4
|
"homepage": "https://github.com/DNepovim/zeiger",
|
|
5
5
|
"repository": "https://github.com/DNepovim/zeiger",
|
|
6
6
|
"author": "Dominik Bláha <iam@dominikblaha.cz>",
|
|
7
|
-
"version": "0.1.
|
|
7
|
+
"version": "0.1.1",
|
|
8
8
|
"type": "module",
|
|
9
|
+
"license": "MIT",
|
|
9
10
|
"exports": {
|
|
10
11
|
".": {
|
|
11
12
|
"types": "./dist/index.d.ts",
|