@rotki/eslint-plugin 0.0.3 → 0.2.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 CHANGED
@@ -13,4 +13,4 @@ See [here](https://rotki.github.io/eslint-plugin)
13
13
 
14
14
  ## License
15
15
 
16
- [AGPL-3.0](./LICENSE) License © 2023- [Rotki Solutions GmbH](https://github.com/rotki)
16
+ [AGPL-3.0](./LICENSE) License © 2023- [Rotki Solutions GmbH](https://github.com/rotki)
package/dist/index.cjs ADDED
@@ -0,0 +1,625 @@
1
+ 'use strict';
2
+
3
+ const createDebug = require('debug');
4
+ const node_path = require('node:path');
5
+ const compat = require('eslint-compat-utils');
6
+ const scule = require('scule');
7
+
8
+ function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
9
+
10
+ function _interopNamespaceCompat(e) {
11
+ if (e && typeof e === 'object' && 'default' in e) return e;
12
+ const n = Object.create(null);
13
+ if (e) {
14
+ for (const k in e) {
15
+ n[k] = e[k];
16
+ }
17
+ }
18
+ n.default = e;
19
+ return n;
20
+ }
21
+
22
+ const createDebug__default = /*#__PURE__*/_interopDefaultCompat(createDebug);
23
+ const compat__namespace = /*#__PURE__*/_interopNamespaceCompat(compat);
24
+
25
+ const name = "@rotki/eslint-plugin";
26
+ const version = "0.2.0";
27
+ const packageManager = "pnpm@8.14.1";
28
+ const type = "module";
29
+ const license = "AGPL-3.0";
30
+ const bugs = {
31
+ url: "https://github.com/rotki/eslint-plugin/issues"
32
+ };
33
+ const repository = {
34
+ type: "git",
35
+ url: "https://github.com/rotki/eslint-plugin.git"
36
+ };
37
+ const author = "Rotki Solutions GmbH <info@rotki.com>";
38
+ const files = [
39
+ "dist"
40
+ ];
41
+ const main = "./dist/index.mjs";
42
+ const module$1 = "./dist/index.mjs";
43
+ const types = "./dist/index.d.ts";
44
+ const exports$1 = {
45
+ ".": {
46
+ types: "./dist/index.d.ts",
47
+ require: "./dist/index.cjs",
48
+ "import": "./dist/index.mjs"
49
+ }
50
+ };
51
+ const sideEffects = false;
52
+ const scripts = {
53
+ clean: "rimraf .nyc_output coverage dist docs/.vitepress/dist",
54
+ coverage: "nyc report --reporter lcov && opener coverage/lcov-report/index.html",
55
+ generate: "node --experimental-specifier-resolution=node --loader ts-node/esm scripts/update-rule-docs.ts",
56
+ lint: "eslint .",
57
+ "lint:fix": "eslint . --fix",
58
+ build: "unbuild",
59
+ dev: "unbuild --stub",
60
+ prepublishOnly: "pnpm run build",
61
+ test: "vitest",
62
+ "new": "node --experimental-specifier-resolution=node --loader ts-node/esm ./scripts/new-rule.ts",
63
+ docs: "vitepress dev docs",
64
+ "docs:build": "pnpm run generate && vitepress build docs",
65
+ prepare: "husky install",
66
+ typecheck: "tsc --noEmit",
67
+ release: "bumpp -r --no-push"
68
+ };
69
+ const peerDependencies = {
70
+ eslint: "^8.0.0 || ^9.0.0"
71
+ };
72
+ const dependencies = {
73
+ "@typescript-eslint/utils": "6.19.0",
74
+ debug: "4.3.4",
75
+ "eslint-compat-utils": "0.4.1",
76
+ "jsonc-eslint-parser": "2.4.0",
77
+ scule: "1.2.0",
78
+ "vue-eslint-parser": "9.4.1",
79
+ "yaml-eslint-parser": "1.2.2"
80
+ };
81
+ const devDependencies = {
82
+ "@commitlint/cli": "18.5.0",
83
+ "@commitlint/config-conventional": "18.5.0",
84
+ "@rotki/eslint-config": "2.3.0",
85
+ "@types/debug": "4.1.12",
86
+ "@types/eslint": "8.56.2",
87
+ "@types/node": "20",
88
+ "@typescript-eslint/eslint-plugin": "6.19.0",
89
+ "@typescript-eslint/parser": "6.19.0",
90
+ "@typescript-eslint/rule-tester": "6.19.0",
91
+ bumpp: "9.3.0",
92
+ debug: "4.3.4",
93
+ eslint: "8.56.0",
94
+ husky: "8.0.3",
95
+ "lint-staged": "15.2.0",
96
+ rimraf: "5.0.5",
97
+ "ts-node": "10.9.2",
98
+ typescript: "5.3.3",
99
+ unbuild: "2.0.0",
100
+ vitepress: "1.0.0-rc.40",
101
+ vitest: "1.2.1"
102
+ };
103
+ const engines = {
104
+ node: ">=18.0.0",
105
+ pnpm: ">=8 <9"
106
+ };
107
+ const pkg = {
108
+ name: name,
109
+ version: version,
110
+ packageManager: packageManager,
111
+ type: type,
112
+ license: license,
113
+ bugs: bugs,
114
+ repository: repository,
115
+ author: author,
116
+ files: files,
117
+ main: main,
118
+ module: module$1,
119
+ types: types,
120
+ exports: exports$1,
121
+ sideEffects: sideEffects,
122
+ scripts: scripts,
123
+ peerDependencies: peerDependencies,
124
+ dependencies: dependencies,
125
+ devDependencies: devDependencies,
126
+ engines: engines,
127
+ "lint-staged": {
128
+ "*.{js,cjs,ts,vue,yml,json,md}": "eslint"
129
+ }
130
+ };
131
+
132
+ function getFilename(context) {
133
+ return compat__namespace.getFilename(context);
134
+ }
135
+ function getSourceCode(context) {
136
+ return compat__namespace.getSourceCode(context);
137
+ }
138
+
139
+ const blobUrl = "https://rotki.github.io/eslint-plugin/rules/";
140
+ function RuleCreator(urlCreator) {
141
+ return function createNamedRule({
142
+ meta,
143
+ name,
144
+ ...rule
145
+ }) {
146
+ return createRule({
147
+ meta: {
148
+ ...meta,
149
+ docs: {
150
+ ...meta.docs,
151
+ url: urlCreator(name)
152
+ }
153
+ },
154
+ ...rule
155
+ });
156
+ };
157
+ }
158
+ function createRule({
159
+ create,
160
+ defaultOptions,
161
+ meta
162
+ }) {
163
+ return {
164
+ create: (context) => {
165
+ const optionsWithDefault = context.options.map((options, index) => ({
166
+ ...defaultOptions[index] || {},
167
+ ...options || {}
168
+ }));
169
+ return create(context, optionsWithDefault);
170
+ },
171
+ defaultOptions,
172
+ meta
173
+ };
174
+ }
175
+ const createEslintRule = RuleCreator(
176
+ (ruleName) => `${blobUrl}${ruleName}`
177
+ );
178
+
179
+ function defineTemplateBodyVisitor(context, templateBodyVisitor, scriptVisitor, options) {
180
+ const sourceCode = getSourceCode(context);
181
+ const parserServices = sourceCode.parserServices;
182
+ if (!("defineTemplateBodyVisitor" in parserServices) || parserServices.defineTemplateBodyVisitor == null) {
183
+ const filename = getFilename(context);
184
+ if (node_path.extname(filename) === ".vue") {
185
+ context.report({
186
+ loc: { column: 0, line: 1 },
187
+ message: "Use the latest vue-eslint-parser. See also https://github.com/vuejs/eslint-plugin-vue#what-is-the-use-the-latest-vue-eslint-parser-error"
188
+ });
189
+ }
190
+ return {};
191
+ }
192
+ return parserServices.defineTemplateBodyVisitor(
193
+ templateBodyVisitor,
194
+ scriptVisitor,
195
+ options
196
+ );
197
+ }
198
+
199
+ function getStringLiteralValue(node, stringOnly = false) {
200
+ if (node.type === "Literal") {
201
+ if (node.value == null) {
202
+ if (!stringOnly && node.bigint != null)
203
+ return node.bigint;
204
+ return null;
205
+ }
206
+ if (typeof node.value === "string")
207
+ return node.value;
208
+ if (!stringOnly)
209
+ return String(node.value);
210
+ return null;
211
+ }
212
+ if (node.type === "TemplateLiteral" && node.expressions.length === 0 && node.quasis.length === 1)
213
+ return node.quasis[0].value.cooked;
214
+ return null;
215
+ }
216
+ function getStaticPropertyName(node) {
217
+ if (node.type === "Property" || node.type === "MethodDefinition") {
218
+ if (!node.computed) {
219
+ const key2 = node.key;
220
+ if (key2.type === "Identifier")
221
+ return key2.name;
222
+ }
223
+ const key = node.key;
224
+ return getStringLiteralValue(key);
225
+ } else if (node.type === "MemberExpression") {
226
+ if (!node.computed) {
227
+ const property2 = node.property;
228
+ if (property2.type === "Identifier")
229
+ return property2.name;
230
+ return null;
231
+ }
232
+ const property = node.property;
233
+ return getStringLiteralValue(property);
234
+ }
235
+ return null;
236
+ }
237
+
238
+ function createRecommended(plugin, name, flat) {
239
+ const rules = Object.fromEntries(Object.entries(plugin.rules).filter(([_key, rule]) => rule.meta.recommended === "recommended" && !rule.meta.deprecated).map(([key]) => [`${name}/${key}`, 2]));
240
+ if (flat) {
241
+ return {
242
+ plugins: {
243
+ [name]: plugin
244
+ },
245
+ rules
246
+ };
247
+ } else {
248
+ return {
249
+ plugins: [name],
250
+ rules
251
+ };
252
+ }
253
+ }
254
+
255
+ const RULE_NAME$3 = "no-deprecated-classes";
256
+ const debug$2 = createDebug__default("@rotki/eslint-plugin:no-deprecated-classes");
257
+ const replacements$2 = [
258
+ ["d-block", "block"],
259
+ ["d-flex", "flex"],
260
+ ["flex-column", "flex-col"],
261
+ ["flex-grow-1", "grow"],
262
+ ["flex-grow-0", "grow-0"],
263
+ ["flex-shrink-1", "shrink"],
264
+ ["flex-shrink-0", "shrink-0"],
265
+ [
266
+ /^align-(start|end|center|baseline|stretch)$/,
267
+ ([align]) => `items-${align}`
268
+ ],
269
+ [/^justify-space-(between|around)$/, ([justify]) => `justify-${justify}`],
270
+ [
271
+ /^align-self-(start|end|center|baseline|auto|strech)$/,
272
+ ([align]) => `self-${align}`
273
+ ],
274
+ [
275
+ /^font-weight-(thin|extralight|light|normal|medium|semibold|bold|extrabold|black)$/,
276
+ ([weight]) => `font-${weight}`
277
+ ],
278
+ [/^text-(capitalize|uppercase|lowercase)$/, ([casing]) => casing]
279
+ ];
280
+ function isString(replacement) {
281
+ return typeof replacement[0] === "string";
282
+ }
283
+ function isRegex(replacement) {
284
+ return replacement[0] instanceof RegExp;
285
+ }
286
+ function findReplacement(className) {
287
+ for (const replacement of replacements$2) {
288
+ if (isString(replacement) && replacement[0] === className)
289
+ return replacement[1];
290
+ if (isRegex(replacement)) {
291
+ const matches = (replacement[0].exec(className) || []).slice(1);
292
+ const replace = replacement[1];
293
+ if (matches.length > 0 && typeof replace === "function")
294
+ return replace(matches);
295
+ }
296
+ }
297
+ return void 0;
298
+ }
299
+ function getRange(node) {
300
+ if (node.type === "VAttribute" && node.value && node.value.range)
301
+ return node.value.range;
302
+ return node.range;
303
+ }
304
+ function reportReplacement(className, replacement, node, context, position = 1) {
305
+ debug$2(`found replacement ${replacement} for ${className}`);
306
+ const source = getSourceCode(context);
307
+ const initialRange = getRange(node);
308
+ const range = [
309
+ initialRange[0] + position,
310
+ initialRange[0] + position + className.length
311
+ ];
312
+ const loc = {
313
+ end: source.getLocFromIndex(range[1]),
314
+ start: source.getLocFromIndex(range[0])
315
+ };
316
+ context.report({
317
+ data: {
318
+ className,
319
+ replacement
320
+ },
321
+ fix(fixer) {
322
+ return fixer.replaceTextRange(range, replacement);
323
+ },
324
+ loc,
325
+ messageId: "replacedWith"
326
+ });
327
+ }
328
+ function* extractClassNames(node, textOnly = false) {
329
+ if (node.type === "Literal") {
330
+ const classNames = `${node.value}`;
331
+ yield* classNames.split(/\s+/).map((className) => ({ className, position: classNames.indexOf(className) + 1, reportNode: node }));
332
+ return;
333
+ }
334
+ if (node.type === "TemplateLiteral") {
335
+ for (const templateElement of node.quasis) {
336
+ const classNames = templateElement.value.cooked;
337
+ if (classNames === null)
338
+ continue;
339
+ yield* classNames.split(/\s+/).map((className) => ({ className, position: classNames.indexOf(className) + 1, reportNode: templateElement }));
340
+ }
341
+ for (const expr of node.expressions)
342
+ yield* extractClassNames(expr, true);
343
+ return;
344
+ }
345
+ if (node.type === "BinaryExpression") {
346
+ if (node.operator !== "+")
347
+ return;
348
+ yield* extractClassNames(node.left, true);
349
+ yield* extractClassNames(node.right, true);
350
+ return;
351
+ }
352
+ if (textOnly)
353
+ return;
354
+ if (node.type === "ObjectExpression") {
355
+ for (const prop of node.properties) {
356
+ if (prop.type !== "Property")
357
+ continue;
358
+ const classNames = getStaticPropertyName(prop);
359
+ if (!classNames)
360
+ continue;
361
+ yield* classNames.split(/\s+/).map((className) => ({ className, position: classNames.indexOf(className) + 1, reportNode: prop.key }));
362
+ }
363
+ return;
364
+ }
365
+ if (node.type === "ArrayExpression") {
366
+ for (const element of node.elements) {
367
+ if (element == null)
368
+ continue;
369
+ if (element.type === "SpreadElement")
370
+ continue;
371
+ yield* extractClassNames(element);
372
+ }
373
+ }
374
+ if (node.type === "ConditionalExpression") {
375
+ yield* extractClassNames(node.consequent);
376
+ yield* extractClassNames(node.alternate);
377
+ }
378
+ }
379
+ const noDeprecatedClasses = createEslintRule({
380
+ create(context) {
381
+ return defineTemplateBodyVisitor(context, {
382
+ 'VAttribute[directive=false][key.name="class"]': function(node) {
383
+ if (!node.value || !node.value.value)
384
+ return;
385
+ for (const className of node.value.value.split(/\s+/).filter((s) => !!s)) {
386
+ const replacement = findReplacement(className);
387
+ const position = node.value.value.indexOf(className) + 1;
388
+ if (!replacement)
389
+ continue;
390
+ reportReplacement(className, replacement, node, context, position);
391
+ }
392
+ },
393
+ "VAttribute[directive=true][key.name.name='bind'][key.argument.name='class'] > VExpressionContainer.value": function(node) {
394
+ if (!node.expression)
395
+ return;
396
+ for (const { className, position, reportNode } of extractClassNames(node.expression)) {
397
+ const replacement = findReplacement(className);
398
+ if (!replacement)
399
+ continue;
400
+ reportReplacement(className, replacement, reportNode, context, position);
401
+ }
402
+ }
403
+ });
404
+ },
405
+ defaultOptions: [],
406
+ meta: {
407
+ docs: {
408
+ description: "disallow the usage of vuetify css classes since they are replaced with tailwindcss",
409
+ recommended: "recommended"
410
+ },
411
+ fixable: "code",
412
+ messages: {
413
+ replacedWith: `'{{ className }}' has been replaced with '{{ replacement }}'`
414
+ },
415
+ schema: [],
416
+ type: "problem"
417
+ },
418
+ name: RULE_NAME$3
419
+ });
420
+
421
+ const debug$1 = createDebug__default("@rotki/eslint-plugin:no-deprecated-components");
422
+ const RULE_NAME$2 = "no-deprecated-components";
423
+ const replacements$1 = {
424
+ DataTable: true,
425
+ Fragment: false
426
+ };
427
+ const skipInLegacy = [
428
+ "Fragment"
429
+ ];
430
+ function hasReplacement$1(tag) {
431
+ return Object.prototype.hasOwnProperty.call(replacements$1, tag);
432
+ }
433
+ const noDeprecatedComponents = createEslintRule({
434
+ create(context, optionsWithDefault) {
435
+ const options = optionsWithDefault[0] || {};
436
+ const legacy = options.legacy;
437
+ return defineTemplateBodyVisitor(context, {
438
+ VElement(element) {
439
+ const tag = scule.pascalCase(element.rawName);
440
+ const sourceCode = getSourceCode(context);
441
+ if (!("getTemplateBodyTokenStore" in sourceCode.parserServices))
442
+ throw new Error("cannot find getTemplateBodyTokenStore in parserServices");
443
+ if (!hasReplacement$1(tag))
444
+ return;
445
+ const replacement = replacements$1[tag];
446
+ if (replacement || legacy && skipInLegacy.includes(tag)) {
447
+ debug$1(`${tag} has been deprecated`);
448
+ context.report({
449
+ data: {
450
+ name: scule.snakeCase(tag)
451
+ },
452
+ messageId: "deprecated",
453
+ node: element
454
+ });
455
+ } else {
456
+ debug$1(`${tag} has will be removed`);
457
+ context.report({
458
+ data: {
459
+ name: scule.snakeCase(tag)
460
+ },
461
+ fix(fixer) {
462
+ return [
463
+ fixer.remove(element.startTag),
464
+ ...element.endTag ? [fixer.remove(element.endTag)] : []
465
+ ];
466
+ },
467
+ messageId: "removed",
468
+ node: element
469
+ });
470
+ }
471
+ }
472
+ });
473
+ },
474
+ defaultOptions: [{ legacy: false }],
475
+ meta: {
476
+ docs: {
477
+ description: "Removes deprecated classes that do not exist anymore",
478
+ recommended: "recommended"
479
+ },
480
+ fixable: "code",
481
+ messages: {
482
+ deprecated: `'{{ name }}' has been deprecated`,
483
+ removed: `'{{ name }}' has been removed`,
484
+ replacedWith: `'{{ a }}' has been replaced with '{{ b }}'`
485
+ },
486
+ schema: [
487
+ {
488
+ additionalProperties: false,
489
+ properties: {
490
+ legacy: {
491
+ type: "boolean"
492
+ }
493
+ },
494
+ type: "object"
495
+ }
496
+ ],
497
+ type: "problem"
498
+ },
499
+ name: RULE_NAME$2
500
+ });
501
+
502
+ const debug = createDebug__default("@rotki/eslint-plugin:no-deprecated-props");
503
+ const RULE_NAME$1 = "no-deprecated-props";
504
+ const replacements = {
505
+ RuiRadio: {
506
+ internalValue: "value"
507
+ }
508
+ };
509
+ function hasReplacement(tag) {
510
+ return Object.prototype.hasOwnProperty.call(replacements, tag);
511
+ }
512
+ function getPropName(node) {
513
+ if (node.directive) {
514
+ if (node.key.argument?.type !== "VIdentifier")
515
+ return void 0;
516
+ return scule.kebabCase(node.key.argument.rawName);
517
+ }
518
+ return scule.kebabCase(node.key.rawName);
519
+ }
520
+ const noDeprecatedProps = createEslintRule({
521
+ create(context) {
522
+ return defineTemplateBodyVisitor(context, {
523
+ VAttribute(node) {
524
+ if (node.directive && (node.value?.type === "VExpressionContainer" && (node.key.name.name !== "bind" || !node.key.argument)))
525
+ return;
526
+ const tag = scule.pascalCase(node.parent.parent.rawName);
527
+ if (!hasReplacement(tag))
528
+ return;
529
+ debug(`${tag} has replacement properties`);
530
+ const propName = getPropName(node);
531
+ const propNameNode = node.directive ? node.key.argument : node.key;
532
+ if (!propName || !propNameNode) {
533
+ debug("could not get prop name and/or node");
534
+ return;
535
+ }
536
+ Object.entries(replacements[tag]).forEach(([prop, replacement]) => {
537
+ if (scule.kebabCase(prop) === propName) {
538
+ debug(`preparing a replacement for ${tag}:${propName} -> ${replacement}`);
539
+ context.report({
540
+ data: {
541
+ prop,
542
+ replacement
543
+ },
544
+ fix(fixer) {
545
+ return fixer.replaceText(propNameNode, replacement);
546
+ },
547
+ messageId: "replacedWith",
548
+ node: propNameNode
549
+ });
550
+ }
551
+ });
552
+ }
553
+ });
554
+ },
555
+ defaultOptions: [],
556
+ meta: {
557
+ docs: {
558
+ description: "..."
559
+ },
560
+ fixable: "code",
561
+ messages: {
562
+ replacedWith: `'{{ prop }}' has been replaced with '{{ replacement }}'`
563
+ },
564
+ schema: [],
565
+ type: "problem"
566
+ },
567
+ name: RULE_NAME$1
568
+ });
569
+
570
+ const RULE_NAME = "no-legacy-library-import";
571
+ const legacyLibrary = "@rotki/ui-library-compat";
572
+ const newLibrary = "@rotki/ui-library";
573
+ const noLegacyLibraryImport = createEslintRule({
574
+ create(context) {
575
+ return {
576
+ ImportDeclaration(node) {
577
+ if (node.source.value !== legacyLibrary)
578
+ return;
579
+ context.report({
580
+ fix(fixer) {
581
+ return fixer.replaceText(node.source, `'${newLibrary}'`);
582
+ },
583
+ messageId: "replacedWith",
584
+ node: node.source
585
+ });
586
+ }
587
+ };
588
+ },
589
+ defaultOptions: [],
590
+ meta: {
591
+ docs: {
592
+ description: `Reports and replaces imports of ${legacyLibrary} with ${newLibrary}`
593
+ },
594
+ fixable: "code",
595
+ messages: {
596
+ replacedWith: `${legacyLibrary} has been replaced by ${newLibrary}`
597
+ },
598
+ schema: [],
599
+ type: "problem"
600
+ },
601
+ name: RULE_NAME
602
+ });
603
+
604
+ const plugin = {
605
+ meta: {
606
+ name: "@rotki/eslint-plugin",
607
+ version: pkg.version
608
+ },
609
+ rules: {
610
+ "no-deprecated-classes": noDeprecatedClasses,
611
+ "no-deprecated-components": noDeprecatedComponents,
612
+ "no-deprecated-props": noDeprecatedProps,
613
+ "no-legacy-library-import": noLegacyLibraryImport
614
+ }
615
+ };
616
+ const plugin$1 = plugin;
617
+
618
+ const configs = {
619
+ "recommended": createRecommended(plugin$1, "@rotki", false),
620
+ "recommended-flat": createRecommended(plugin$1, "@rotki", true)
621
+ };
622
+
623
+ const index = Object.assign(plugin$1, { configs });
624
+
625
+ module.exports = index;