eslint-plugin-code-style 1.0.4

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.
Files changed (4) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +323 -0
  3. package/index.js +6869 -0
  4. package/package.json +45 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Mohamed Elhawary
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,323 @@
1
+ # ESLint Plugin Code Style
2
+
3
+ A custom ESLint plugin for enforcing consistent code formatting and style rules in React/JSX projects.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install eslint-plugin-code-style --save-dev
9
+ # or
10
+ pnpm add eslint-plugin-code-style -D
11
+ # or
12
+ yarn add eslint-plugin-code-style -D
13
+ ```
14
+
15
+ ## Configuration
16
+
17
+ ### ESLint Flat Config (eslint.config.js)
18
+
19
+ ```javascript
20
+ import codeStyle from "eslint-plugin-code-style";
21
+
22
+ export default [
23
+ {
24
+ plugins: {
25
+ "code-style": codeStyle,
26
+ },
27
+ rules: {
28
+ "code-style/rule-name": "error",
29
+ // ... other rules
30
+ },
31
+ },
32
+ ];
33
+ ```
34
+
35
+ ### Enable All Rules
36
+
37
+ ```javascript
38
+ rules: {
39
+ "code-style/absolute-imports-only": "error",
40
+ "code-style/array-items-per-line": "error",
41
+ "code-style/array-objects-on-new-lines": "error",
42
+ "code-style/arrow-function-block-body": "error",
43
+ "code-style/arrow-function-simple-jsx": "error",
44
+ "code-style/arrow-function-simplify": "error",
45
+ "code-style/assignment-value-same-line": "error",
46
+ "code-style/block-statement-newlines": "error",
47
+ "code-style/comment-spacing": "error",
48
+ "code-style/curried-arrow-same-line": "error",
49
+ "code-style/export-format": "error",
50
+ "code-style/function-call-spacing": "error",
51
+ "code-style/function-naming-convention": "error",
52
+ "code-style/function-params-per-line": "error",
53
+ "code-style/hook-callback-format": "error",
54
+ "code-style/hook-deps-per-line": "error",
55
+ "code-style/if-statement-format": "error",
56
+ "code-style/import-format": "error",
57
+ "code-style/import-source-spacing": "error",
58
+ "code-style/jsx-children-on-new-line": "error",
59
+ "code-style/jsx-closing-bracket-spacing": "error",
60
+ "code-style/jsx-element-child-new-line": "error",
61
+ "code-style/jsx-logical-expression-simplify": "error",
62
+ "code-style/jsx-parentheses-position": "error",
63
+ "code-style/jsx-prop-naming-convention": "error",
64
+ "code-style/jsx-simple-element-one-line": "error",
65
+ "code-style/jsx-string-value-trim": "error",
66
+ "code-style/jsx-ternary-format": "error",
67
+ "code-style/member-expression-bracket-spacing": "error",
68
+ "code-style/module-index-exports": "error",
69
+ "code-style/multiline-argument-newline": "error",
70
+ "code-style/multiline-if-conditions": "error",
71
+ "code-style/multiple-arguments-per-line": "error",
72
+ "code-style/nested-call-closing-brackets": "error",
73
+ "code-style/no-empty-lines-in-function-calls": "error",
74
+ "code-style/no-empty-lines-in-function-params": "error",
75
+ "code-style/no-empty-lines-in-jsx": "error",
76
+ "code-style/no-empty-lines-in-objects": "error",
77
+ "code-style/no-empty-lines-in-switch-cases": "error",
78
+ "code-style/object-property-per-line": "error",
79
+ "code-style/object-property-value-brace": "error",
80
+ "code-style/object-property-value-format": "error",
81
+ "code-style/opening-brackets-same-line": "error",
82
+ "code-style/simple-call-single-line": "error",
83
+ "code-style/single-argument-on-line": "error",
84
+ "code-style/string-property-spacing": "error",
85
+ "code-style/variable-naming-convention": "error",
86
+ }
87
+ ```
88
+
89
+ ## Rules Reference
90
+
91
+ All rules in this plugin are **auto-fixable** using `eslint --fix`.
92
+
93
+ ### Import/Export Rules
94
+
95
+ | Rule | Description | Example |
96
+ |------|-------------|---------|
97
+ | `absolute-imports-only` | Enforce absolute imports using `@/` alias instead of relative paths | `import { foo } from "@/utils"` not `"../../utils"` |
98
+ | `import-format` | Format imports: `import {` on same line, `} from` on same line, collapse 1-3 specifiers to single line | See examples below |
99
+ | `import-source-spacing` | Enforce no extra spaces inside import path quotes | `from "@/utils"` not `from " @/utils "` |
100
+ | `export-format` | Format exports: `export {` on same line, collapse 1-3 specifiers, multiline for 4+ | Similar to import-format |
101
+ | `module-index-exports` | Enforce proper exports in index files | `export { Foo } from "./foo"` |
102
+
103
+ ### Function Rules
104
+
105
+ | Rule | Description | Example |
106
+ |------|-------------|---------|
107
+ | `arrow-function-block-body` | Enforce parentheses for arrow functions with multiline expressions (preserves implicit return) | `() => (\n <Component />\n)` |
108
+ | `arrow-function-simplify` | Simplify arrow functions returning simple JSX to single line | `() => <Simple />` |
109
+ | `arrow-function-simple-jsx` | Simplify arrow functions in JSX props with single statement block body | `onClick={() => doSomething()}` |
110
+ | `function-call-spacing` | Enforce no space between function name and opening parenthesis | `fn()` not `fn ()` |
111
+ | `function-naming-convention` | Enforce function naming conventions (camelCase) | `const myFunction = () => {}` |
112
+ | `function-params-per-line` | Enforce function parameters on separate lines when more than 2 | See examples below |
113
+ | `curried-arrow-same-line` | Enforce curried arrow function to start on same line as `=>` | `=> () =>` not `=>\n() =>` |
114
+
115
+ ### Object Rules
116
+
117
+ | Rule | Description | Example |
118
+ |------|-------------|---------|
119
+ | `object-property-per-line` | Enforce each property on its own line when object has 2+ properties | `{\n a: 1,\n b: 2,\n}` |
120
+ | `object-property-value-format` | Enforce property value on same line as colon with proper spacing | `key: value` |
121
+ | `object-property-value-brace` | Enforce opening brace on same line as colon for object property values | `key: {` not `key:\n{` |
122
+ | `no-empty-lines-in-objects` | Disallow empty lines inside objects | No blank lines between properties |
123
+ | `string-property-spacing` | Enforce no extra spaces inside string property keys | `["key"]` not `[ "key" ]` |
124
+
125
+ ### Array Rules
126
+
127
+ | Rule | Description | Example |
128
+ |------|-------------|---------|
129
+ | `array-items-per-line` | Enforce array formatting: 3 or less items on one line, more than 3 each on new line | `[1, 2, 3]` or multiline for 4+ |
130
+ | `array-objects-on-new-lines` | Enforce array of objects to have each object on a new line | `[\n { a: 1 },\n { b: 2 },\n]` |
131
+
132
+ ### JSX Rules
133
+
134
+ | Rule | Description | Example |
135
+ |------|-------------|---------|
136
+ | `jsx-children-on-new-line` | Enforce JSX children on separate lines from parent tags | `<Parent>\n child\n</Parent>` |
137
+ | `jsx-closing-bracket-spacing` | No space before `>` or `/>` in JSX tags | `<Component />` not `<Component / >` |
138
+ | `jsx-element-child-new-line` | JSX children that are JSX elements must be on new lines | Each child element on its own line |
139
+ | `jsx-logical-expression-simplify` | Simplify logical expressions in JSX when right side is single-line | `{condition && <Simple />}` |
140
+ | `jsx-parentheses-position` | Enforce opening parenthesis position for JSX in arrow functions | `=> (\n <JSX />` |
141
+ | `jsx-prop-naming-convention` | Enforce JSX prop naming conventions | camelCase for props |
142
+ | `jsx-simple-element-one-line` | Simple JSX elements with only text/expression children should be on one line | `<Text>Hello</Text>` |
143
+ | `jsx-string-value-trim` | Disallow leading/trailing whitespace in JSX string values | `<Text>Hello</Text>` not `<Text> Hello </Text>` |
144
+ | `jsx-ternary-format` | Enforce consistent formatting for JSX ternary expressions | See examples below |
145
+ | `no-empty-lines-in-jsx` | Disallow empty lines inside JSX elements | No blank lines between JSX children |
146
+
147
+ ### Call Expression Rules
148
+
149
+ | Rule | Description | Example |
150
+ |------|-------------|---------|
151
+ | `simple-call-single-line` | Simplify simple function calls with arrow function to single line | `fn(() => value)` |
152
+ | `single-argument-on-line` | Enforce single simple argument calls to be on one line | `fn(arg)` not `fn(\n arg\n)` |
153
+ | `multiple-arguments-per-line` | Enforce multiple arguments to each be on their own line | `fn(\n arg1,\n arg2,\n)` |
154
+ | `multiline-argument-newline` | Enforce newlines for function calls with multiline arguments | Proper formatting for complex calls |
155
+ | `nested-call-closing-brackets` | Enforce nested function call closing brackets on same line | `}));` not `}\n)\n);` |
156
+ | `no-empty-lines-in-function-calls` | Disallow empty lines in function calls | No blank lines between arguments |
157
+ | `opening-brackets-same-line` | Enforce opening brackets on same line for function calls | `fn({` not `fn(\n{` |
158
+
159
+ ### React Hooks Rules
160
+
161
+ | Rule | Description | Example |
162
+ |------|-------------|---------|
163
+ | `hook-callback-format` | Enforce consistent formatting for React hooks (useEffect, useCallback, etc.) | See examples below |
164
+ | `hook-deps-per-line` | Enforce each hook dependency on its own line when more than 2 dependencies | `[\n dep1,\n dep2,\n dep3,\n]` |
165
+
166
+ ### Control Flow Rules
167
+
168
+ | Rule | Description | Example |
169
+ |------|-------------|---------|
170
+ | `if-statement-format` | Ensure if statement has proper formatting | `if (...) {` |
171
+ | `multiline-if-conditions` | Enforce multiline if conditions when there are more than 3 operands | See examples below |
172
+ | `no-empty-lines-in-switch-cases` | Prevent empty lines at the beginning of switch case logic or between cases | Clean switch statements |
173
+
174
+ ### Spacing & Formatting Rules
175
+
176
+ | Rule | Description | Example |
177
+ |------|-------------|---------|
178
+ | `assignment-value-same-line` | Enforce assignment value on same line as equals sign | `const x = value` not `const x =\nvalue` |
179
+ | `block-statement-newlines` | Enforce newlines after opening brace and before closing brace in blocks | `{\n code\n}` |
180
+ | `comment-spacing` | Enforce comment spacing and formatting | Proper spacing around comments |
181
+ | `member-expression-bracket-spacing` | Enforce no spaces inside brackets for member expressions | `obj[key]` not `obj[ key ]` |
182
+ | `no-empty-lines-in-function-params` | Disallow empty lines in function parameters | No blank lines between params |
183
+ | `variable-naming-convention` | Enforce variable naming conventions | camelCase for variables |
184
+
185
+ ## Examples
186
+
187
+ ### import-format
188
+
189
+ ```javascript
190
+ // Good - 3 or fewer specifiers on one line
191
+ import { Box, Button, Grid } from "@mui/material";
192
+
193
+ // Good - 4+ specifiers on multiple lines
194
+ import {
195
+ Box,
196
+ Button,
197
+ Grid,
198
+ Typography,
199
+ } from "@mui/material";
200
+
201
+ // Bad
202
+ import {
203
+ Box, Button, Grid } from "@mui/material";
204
+ ```
205
+
206
+ ### function-params-per-line
207
+
208
+ ```javascript
209
+ // Good - 2 or fewer params on one line
210
+ const fn = (a, b) => {};
211
+
212
+ // Good - 3+ params on separate lines
213
+ const fn = (
214
+ param1,
215
+ param2,
216
+ param3,
217
+ ) => {};
218
+
219
+ // Bad
220
+ const fn = (param1, param2, param3) => {};
221
+ ```
222
+
223
+ ### jsx-ternary-format
224
+
225
+ ```javascript
226
+ // Good - Simple ternary on one line
227
+ {condition ? <Simple /> : <Other />}
228
+
229
+ // Good - Complex ternary with proper formatting
230
+ {condition ? (
231
+ <Complex>
232
+ <Children />
233
+ </Complex>
234
+ ) : (
235
+ <Alternative>
236
+ <Content />
237
+ </Alternative>
238
+ )}
239
+
240
+ // Bad - Empty lines inside ternary
241
+ {condition ? (
242
+
243
+ <Button />
244
+
245
+ ) : (
246
+
247
+ <Link />
248
+
249
+ )}
250
+ ```
251
+
252
+ ### hook-callback-format
253
+
254
+ ```javascript
255
+ // Good
256
+ useEffect(
257
+ () => {
258
+ doSomething();
259
+ },
260
+ [dependency],
261
+ );
262
+
263
+ // Good - Simple callback
264
+ useEffect(() => doSomething(), []);
265
+
266
+ // Bad
267
+ useEffect(() => {
268
+ doSomething();
269
+ }, [dependency]);
270
+ ```
271
+
272
+ ### multiline-if-conditions
273
+
274
+ ```javascript
275
+ // Good - 3 or fewer operands on one line
276
+ if (a && b && c) {}
277
+
278
+ // Good - 4+ operands on multiple lines
279
+ if (
280
+ condition1
281
+ && condition2
282
+ && condition3
283
+ && condition4
284
+ ) {}
285
+
286
+ // Bad
287
+ if (condition1 && condition2 && condition3 && condition4) {}
288
+ ```
289
+
290
+ ## Auto-fixing
291
+
292
+ All rules support auto-fixing. Run ESLint with the `--fix` flag:
293
+
294
+ ```bash
295
+ # Fix all files
296
+ pnpm eslint src/ --fix
297
+
298
+ # Fix specific file
299
+ pnpm eslint src/components/MyComponent.jsx --fix
300
+ ```
301
+
302
+ ## Disabling Rules
303
+
304
+ To disable a rule for a specific line:
305
+
306
+ ```javascript
307
+ // eslint-disable-next-line code-style/rule-name
308
+ const code = "violates rule";
309
+ ```
310
+
311
+ To disable a rule for an entire file, add at the top:
312
+
313
+ ```javascript
314
+ /* eslint-disable code-style/rule-name */
315
+ ```
316
+
317
+ To disable in eslint.config.js:
318
+
319
+ ```javascript
320
+ rules: {
321
+ "code-style/rule-name": "off",
322
+ }
323
+ ```