eslint-plugin-lube 0.2.5 → 0.3.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.
@@ -5,7 +5,7 @@ module.exports = {
5
5
  docs: {
6
6
  description: "Enforces snake_case with some symbols for variable declaration.",
7
7
  recommended: true,
8
- url: "https://github.com/sveltejs/svelte/blob/master/CONTRIBUTING.md#code-conventions"
8
+ url: "https://github.com/Artxe2/lube-series/blob/master/packages/eslint-plugin-lube/docs/svelte-naming-convention.md"
9
9
  },
10
10
  fixable: "code",
11
11
  messages: {
@@ -20,10 +20,10 @@ module.exports = {
20
20
  },
21
21
  type: "object"
22
22
  }],
23
- type: "suggestion"
23
+ type: "layout"
24
24
  },
25
25
  create(context) {
26
- const fix_same_names = /** @type {boolean} */ (context.options[0]?.fixSameNames) ?? true
26
+ const fix_same_names = /** @type {boolean} */(context.options[0]?.fixSameNames)/**/ ?? true
27
27
 
28
28
  // let name | const name | class name
29
29
  const allow_regex = /^[_$]?[_$]?(?:[\da-z]+(?:_[\da-z]+)*\$?\$?)?$|^[A-Z](?:_?[\dA-Z]+)*$|^(?:[\dA-Z][\da-z]*)+$/
@@ -31,23 +31,17 @@ module.exports = {
31
31
  const fix_regex = /([\da-z]?)([A-Z][\dA-Z]*)/g
32
32
  const camel_case_regex = /^[\da-z]+([A-Z][\da-z]*)*$/
33
33
 
34
- /**
35
- * @param {string} _
36
- * @param {string} a
37
- * @param {string} b
38
- */
39
- function fix_callback(_, a, b) {
40
- return a + (a ? "_" : "") + b.toLowerCase()
41
- }
42
-
43
34
  /** @type {Set<string>} */
44
35
  const reported_declarations = new Set()
45
- /** @type {Map<string, import("../private").ASTNode[]>} */
36
+ /** @type {Map<string, import("../private").AstNode[]>} */
46
37
  const pending_usages = new Map()
47
- /** @type {Set<import("../private").ASTNode>} */
38
+ /** @type {Set<import("../private").AstNode>} */
48
39
  const shortand_properties = new Set()
49
40
 
50
- /** @param {import("../private").ASTNode & import("estree").Identifier} node */
41
+ /**
42
+ * @param {import("../private").AstNode & import("estree").Identifier} node
43
+ * @returns {void}
44
+ */
51
45
  function defer(node) {
52
46
  if (fix_same_names) {
53
47
  const name = node.name
@@ -63,7 +57,20 @@ module.exports = {
63
57
  }
64
58
  }
65
59
 
66
- /** @param {import("../private").ASTNode & import("estree").Identifier} node */
60
+ /**
61
+ * @param {string} _
62
+ * @param {string} a
63
+ * @param {string} b
64
+ * @returns {string}
65
+ */
66
+ function fix_callback(_, a, b) {
67
+ return a + (a ? "_" : "") + b.toLowerCase()
68
+ }
69
+
70
+ /**
71
+ * @param {import("../private").AstNode & import("estree").Identifier} node
72
+ * @returns {void}
73
+ */
67
74
  function report(node) {
68
75
  const name = node.name
69
76
  context.report({
@@ -79,313 +86,270 @@ module.exports = {
79
86
  return null
80
87
  },
81
88
  messageId: "not_match",
82
- node: /** @type {import("estree").Node} */(node)
89
+ node: /** @type {import("estree").Node} */(node)/**/
83
90
  })
84
91
  if (fix_same_names) {
85
92
  const usages = pending_usages.get(name)
86
93
  while (usages?.length) {
87
- report(/** @type {import("../private").ASTNode & import("estree").Identifier} */(
88
- usages.pop()
89
- ))
94
+ report(/** @type {import("../private").AstNode & import("estree").Identifier} */(usages.pop())/**/)
90
95
  }
91
96
  reported_declarations.add(name)
92
97
  }
93
98
  }
94
99
 
95
100
  return {
96
- /** @param {import("../private").ASTNode & import("estree").Identifier} node */
101
+ /** @param {import("../private").AstNode & import("estree").Identifier} node */
97
102
  Identifier(node) {
98
103
  const name = node.name
99
104
  if (allow_regex.test(name)) return
100
105
  const parent = node.parent
101
106
  const type = parent.type
102
- // var value = [ camelCase ]
103
- if ("ArrayExpression" == type) {
107
+ switch (type) {
108
+ case "ArrayExpression":
109
+ // var value = [ camelCase ]
104
110
  defer(node)
105
- return
106
- }
107
- // var [ camelCase ] = value
108
- if ("ArrayPattern" == type) {
111
+ break
112
+ case "ArrayPattern":
113
+ // var [ camelCase ] = value
109
114
  report(node)
110
- return
111
- }
112
- // (camelCase) => {}
113
- if ("ArrowFunctionExpression" == type) {
115
+ break
116
+ case "ArrowFunctionExpression":
117
+ // (camelCase) => {}
114
118
  report(node)
115
- return
116
- }
117
- // left = right
118
- if ("AssignmentExpression" == type) {
119
+ break
120
+ case "AssignmentExpression":
121
+ // left = right
119
122
  defer(node)
120
- return
121
- }
122
- // (left = right) => {}
123
- if ("AssignmentPattern" == type) {
123
+ break
124
+ case "AssignmentPattern":
125
+ // (left = right) => {}
124
126
  if (parent.left == node) {
125
- // function func({ camelCase = true }) {}
126
127
  if (parent.parent?.type != "Property") {
128
+ // function func({ camelCase = true }) {}
127
129
  report(node)
128
130
  }
129
131
  } else {
130
132
  defer(node)
131
133
  }
132
- return
133
- }
134
- // await camelCase
135
- if ("AwaitExpression" == type) {
134
+ break
135
+ case "AwaitExpression":
136
+ // await camelCase
136
137
  defer(node)
137
- return
138
- }
139
- // left +-*/&| right
140
- if ("BinaryExpression" == type) {
138
+ break
139
+ case "BinaryExpression":
140
+ // left +-*/&| right
141
141
  defer(node)
142
- return
143
- }
144
- /* BlockStatement */
145
- /* BreakStatement */
146
- // camelCase()
147
- if ("CallExpression" == type) {
142
+ break
143
+ /* BlockStatement */
144
+ /* BreakStatement */
145
+ case "CallExpression":
146
+ // camelCase()
148
147
  defer(node)
149
- return
150
- }
148
+ break
151
149
  /* CatchClause */
152
150
  /* ChainExpression */
153
151
  /* ClassBody */
154
- // class camelCase {}
155
- if ("ClassDeclaration" == type) {
152
+ case "ClassDeclaration":
153
+ // class camelCase {}
156
154
  report(node)
157
- return
158
- }
155
+ break
159
156
  /* ClassExpression */
160
- // test ? alternate : consequent
161
- if ("ConditionalExpression" == type) {
157
+ case "ConditionalExpression":
158
+ // test ? alternate : consequent
162
159
  defer(node)
163
- return
164
- }
160
+ break
165
161
  /* ContinueStatement */
166
162
  /* DebuggerStatement */
167
163
  /* DoWhileStatement */
168
164
  /* EmptyStatement */
169
- // export * as camelCase from "module"
170
- if ("ExportAllDeclaration" == type) {
171
- return
172
- }
173
- // export default camelCase
174
- if ("ExportDefaultDeclaration" == type) {
165
+ case "ExportAllDeclaration":
166
+ // export * as camelCase from "module"
167
+ break
168
+ case "ExportDefaultDeclaration":
169
+ // export default camelCase
175
170
  defer(node)
176
- return
177
- }
171
+ break
178
172
  /* ExportNamedDeclaration */
179
- // export { default as camelCase, camelCase2 } from "module"
180
- if ("ExportSpecifier" == type) {
181
- return
182
- }
183
- // camelCase
184
- if ("ExpressionStatement" == type) {
173
+ case "ExportSpecifier":
174
+ // export { default as camelCase, camelCase2 } from "module"
175
+ break
176
+ case "ExpressionStatement":
177
+ // camelCase
185
178
  defer(node)
186
- return
187
- }
188
- // for (left in right) {}
189
- if ("ForInStatement" == type) {
179
+ break
180
+ case "ForInStatement":
181
+ // for (left in right) {}
190
182
  if (parent.left == node) {
191
183
  report(node)
192
184
  } else {
193
185
  defer(node)
194
186
  }
195
- return
196
- }
197
- // for (left of right) {}
198
- if ("ForOfStatement" == type) {
187
+ break
188
+ case "ForOfStatement":
189
+ // for (left of right) {}
199
190
  if (parent.left == node) {
200
191
  report(node)
201
192
  } else {
202
193
  defer(node)
203
194
  }
204
- return
205
- }
206
- // for (init; test; update) {}
207
- if ("ForStatement" == type) {
195
+ break
196
+ case "ForStatement":
197
+ // for (init; test; update) {}
208
198
  defer(node)
209
- return
210
- }
211
- // function camelCase() {}
212
- if ("FunctionDeclaration" == type) {
213
- if (
199
+ break
200
+ case "FunctionDeclaration":
201
+ // function camelCase() {}
202
+ if (
214
203
  parent.parent?.type != "ExportNamedDeclaration"
215
204
  || !camel_case_regex.test(name)
216
205
  ) {
217
206
  report(node)
218
207
  }
219
208
  // export function camelCase() {}
220
- return
221
- }
209
+ break
222
210
  /* FunctionExpression */
223
211
  /* Identifier */
224
- // if (camelCase) {}
225
- if ("IfStatement" == type) {
212
+ case "IfStatement":
213
+ // if (camelCase) {}
226
214
  defer(node)
227
- return
228
- }
215
+ break
229
216
  /* ImportDeclaration */
230
- // import camelCase from "module"
231
- if ("ImportDefaultSpecifier" == type) {
217
+ case "ImportDefaultSpecifier":
218
+ // import camelCase from "module"
232
219
  report(node)
233
- return
234
- }
235
- /* ImportExpression */
236
- // import * as camelCase from "module"
237
- if ("ImportNamespaceSpecifier" == type) {
220
+ break
221
+ /* ImportExpression */
222
+ case "ImportNamespaceSpecifier":
238
223
  report(node)
239
- return
240
- }
241
- // import { imported as local, local2 } from "module"
242
- if ("ImportSpecifier" == type) {
243
- // import { imported as camelCase } from "module"
224
+ // import * as camelCase from "module"
225
+ break
226
+ case "ImportSpecifier":
227
+ // import { imported as local, local2 } from "module"
244
228
  if (parent.local == node && parent.imported.name != parent.local.name) {
229
+ // import { imported as camelCase } from "module"
245
230
  report(node)
246
231
  }
247
- return
248
- }
232
+ break
249
233
  /* LabeledStatement */
250
234
  /* Literal */
251
- // left &&||?? right
252
- if ("LogicalExpression" == type) {
235
+ case "LogicalExpression":
236
+ // left &&||?? right
253
237
  defer(node)
254
- return
255
- }
256
- // object.property
257
- if ("MemberExpression" == type) {
238
+ break
239
+ case "MemberExpression":
240
+ // object.property
258
241
  if (parent.object == node) {
259
242
  defer(node)
260
243
  }
261
- // object[camelCase]
262
- // camelCase[key]
263
244
  else if (parent.computed) {
245
+ // object[camelCase]
246
+ // camelCase[key]
264
247
  defer(node)
265
248
  }
266
- return
267
- }
249
+ break
268
250
  /* MetaProperty */
269
- // class Object { camelCase() {} }
270
- if ("MethodDefinition" == type) {
271
- return
272
- }
273
- // new camelCase
274
- if ("NewExpression" == type) {
251
+ case "MethodDefinition":
252
+ // class Object { camelCase() {} }
253
+ break
254
+ case "NewExpression":
255
+ // new camelCase
275
256
  defer(node)
276
- return
277
- }
257
+ break
278
258
  /* ObjectExpression */
279
259
  /* ObjectPattern */
280
260
  /* PrivateIdentifier */
281
261
  /* Program */
282
- // var value = { key: value }
283
- // var { key: value } = value
284
- if ("Property" == type) {
262
+ case "Property":
263
+ // var value = { key: value }
264
+ // var { key: value } = value
285
265
  if (parent.value == node) {
286
266
  if (parent.key.name == parent.value.name) {
287
267
  shortand_properties.add(node)
288
268
  }
289
269
  defer(node)
290
270
  }
291
- return
292
- }
293
- // class Object { camelCase = value }
294
- if ("PropertyDefinition" == type) {
295
- return
296
- }
297
- // var [ ...camelCase ] = value
298
- // var { ...camelCase } = value
299
- if ("RestElement" == type) {
271
+ break
272
+ case "PropertyDefinition":
273
+ // class Object { camelCase = value }
274
+ break
275
+ case "RestElement":
276
+ // var [ ...camelCase ] = value
277
+ // var { ...camelCase } = value
300
278
  report(node)
301
- return
302
- }
303
- // function func() { return camelCase }
304
- if ("ReturnStatement" == type) {
279
+ break
280
+ case "ReturnStatement":
281
+ // function func() { return camelCase }
305
282
  defer(node)
306
- return
307
- }
308
- // a, b, camelCase
309
- if ("SequenceExpression" == type) {
283
+ break
284
+ case "SequenceExpression":
285
+ // a, b, camelCase
310
286
  defer(node)
311
- return
312
- }
313
- // var value = [ ...camelCase ]
314
- // var value = { ...camelCase }
315
- if ("SpreadElement" == type) {
287
+ break
288
+ case "SpreadElement":
289
+ // var value = [ ...camelCase ]
290
+ // var value = { ...camelCase }
316
291
  defer(node)
317
- return
318
- }
292
+ break
319
293
  /* StaticBlock */
320
294
  /* Super */
321
- // switch (camelCase) {}
322
- if ("SwitchCase" == type) {
295
+ case "SwitchCase":
296
+ // switch (camelCase) {}
323
297
  defer(node)
324
- return
325
- }
326
- // switch (cond) { case camelCase: break }
327
- if ("SwitchStatement" == type) {
298
+ break
299
+ case "SwitchStatement":
300
+ // switch (cond) { case camelCase: break }
328
301
  defer(node)
329
- return
330
- }
331
- // camelCase`quasis${expression}quasis`
332
- if ("TaggedTemplateExpression" == type) {
302
+ break
303
+ case "TaggedTemplateExpression":
304
+ // camelCase`quasis${expression}quasis`
333
305
  defer(node)
334
- return
335
- }
306
+ break
336
307
  /* TemplateElement */
337
- // `quasis${camelCase}quasis`
338
- if ("TemplateLiteral" == type) {
308
+ case "TemplateLiteral":
309
+ // `quasis${camelCase}quasis`
339
310
  defer(node)
340
- return
341
- }
311
+ break
342
312
  /* ThisExpression */
343
- // throw camelCase
344
- if ("ThrowStatement" == type) {
313
+ case "ThrowStatement":
314
+ // throw camelCase
345
315
  defer(node)
346
- return
347
- }
316
+ break
348
317
  /* TryStatement */
349
- // +-!~ camelCase
350
- if ("UnaryExpression" == type) {
318
+ case "UnaryExpression":
319
+ // +-!~ camelCase
351
320
  defer(node)
352
- return
353
- }
354
- // ++--camelCase++--
355
- if ("UpdateExpression" == type) {
321
+ break
322
+ case "UpdateExpression":
323
+ // ++--camelCase++--
356
324
  defer(node)
357
- return
358
- }
325
+ break
359
326
  /* VariableDeclaration */
360
- // var id = init
361
- if ("VariableDeclarator" == type) {
327
+ case "VariableDeclarator":
328
+ // var id = init
362
329
  if (parent.id == node) {
363
- if (
330
+ if (
364
331
  parent.parent?.parent?.type != "ExportNamedDeclaration"
365
332
  || !camel_case_regex.test(name)
366
333
  ) {
367
334
  report(node)
368
335
  }
369
- // export var camelCase
370
336
  } else {
337
+ // export var camelCase
371
338
  defer(node)
372
339
  }
373
- return
374
- }
375
- // while (camelCase) {}
376
- if ("WhileStatement" == type) {
340
+ break
341
+ case "WhileStatement":
342
+ // while (camelCase) {}
377
343
  defer(node)
378
- return
379
- }
380
- // with (camelCase) {}
381
- if ("WithStatement" == type) {
344
+ break
345
+ case "WithStatement":
346
+ // with (camelCase) {}
382
347
  defer(node)
383
- return
384
- }
385
- // function* func() { yield camelCase }
386
- if ("YieldExpression" == type) {
348
+ break
349
+ case "YieldExpression":
350
+ // function* func() { yield camelCase }
387
351
  defer(node)
388
- return
352
+ break
389
353
  }
390
354
  }
391
355
  }
@@ -37,13 +37,13 @@ exports.module = (valid, invalid) => {
37
37
  `,
38
38
  errors: [
39
39
  {
40
- messageId: "not_match",
40
+ messageId: "exports",
41
41
  type: "ExportNamedDeclaration",
42
42
  line: 3,
43
43
  column: 4
44
44
  },
45
45
  {
46
- messageId: "not_match",
46
+ messageId: "exports",
47
47
  type: "ExportNamedDeclaration",
48
48
  line: 4,
49
49
  column: 7
@@ -79,13 +79,13 @@ exports.module = (valid, invalid) => {
79
79
  `,
80
80
  errors: [
81
81
  {
82
- messageId: "not_match",
82
+ messageId: "exports",
83
83
  type: "ExportNamedDeclaration",
84
84
  line: 3,
85
85
  column: 4
86
86
  },
87
87
  {
88
- messageId: "not_match",
88
+ messageId: "exports",
89
89
  type: "ExportNamedDeclaration",
90
90
  line: 4,
91
91
  column: 7
@@ -39,13 +39,13 @@ import {
39
39
  `,
40
40
  errors: [
41
41
  {
42
- messageId: "not_match",
42
+ messageId: "imports",
43
43
  type: "ImportDeclaration",
44
44
  line: 3,
45
45
  column: 4
46
46
  },
47
47
  {
48
- messageId: "not_match",
48
+ messageId: "imports",
49
49
  type: "ImportDeclaration",
50
50
  line: 4,
51
51
  column: 7
@@ -81,13 +81,13 @@ import {
81
81
  `,
82
82
  errors: [
83
83
  {
84
- messageId: "not_match",
84
+ messageId: "imports",
85
85
  type: "ImportDeclaration",
86
86
  line: 3,
87
87
  column: 4
88
88
  },
89
89
  {
90
- messageId: "not_match",
90
+ messageId: "imports",
91
91
  type: "ImportDeclaration",
92
92
  line: 4,
93
93
  column: 7
@@ -6,8 +6,8 @@ const valid = []
6
6
  /** @type {RuleTester.InvalidTestCase[]} */
7
7
  const invalid = []
8
8
 
9
- require("./pretty-imports/check_exports").module(valid, invalid)
10
- require("./pretty-imports/check_imports").module(valid, invalid)
9
+ require("./pretty-imports/ExportNamedDeclaration").module(valid, invalid)
10
+ require("./pretty-imports/ImportDeclaration").module(valid, invalid)
11
11
 
12
12
  new RuleTester().run(
13
13
  "pretty-imports",