@rotki/eslint-plugin 0.7.0 → 1.1.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/dist/index.cjs DELETED
@@ -1,707 +0,0 @@
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
- const utils = require('@typescript-eslint/utils');
8
-
9
- function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
10
-
11
- function _interopNamespaceCompat(e) {
12
- if (e && typeof e === 'object' && 'default' in e) return e;
13
- const n = Object.create(null);
14
- if (e) {
15
- for (const k in e) {
16
- n[k] = e[k];
17
- }
18
- }
19
- n.default = e;
20
- return n;
21
- }
22
-
23
- const createDebug__default = /*#__PURE__*/_interopDefaultCompat(createDebug);
24
- const compat__namespace = /*#__PURE__*/_interopNamespaceCompat(compat);
25
-
26
- const version = "0.7.0";
27
- const pkg = {
28
- version: version};
29
-
30
- function getFilename(context) {
31
- return compat__namespace.getFilename(context);
32
- }
33
- function getSourceCode(context) {
34
- return compat__namespace.getSourceCode(context);
35
- }
36
-
37
- const blobUrl = "https://rotki.github.io/eslint-plugin/rules/";
38
- function RuleCreator(urlCreator) {
39
- return function createNamedRule({
40
- meta,
41
- name,
42
- ...rule
43
- }) {
44
- return createRule({
45
- meta: {
46
- ...meta,
47
- docs: {
48
- ...meta.docs,
49
- url: urlCreator(name)
50
- }
51
- },
52
- ...rule
53
- });
54
- };
55
- }
56
- function createRule({
57
- create,
58
- defaultOptions,
59
- meta
60
- }) {
61
- return {
62
- create: (context) => {
63
- const optionsWithDefault = context.options.map((options, index) => ({
64
- ...defaultOptions[index] || {},
65
- ...options || {}
66
- }));
67
- return create(context, optionsWithDefault);
68
- },
69
- defaultOptions,
70
- meta
71
- };
72
- }
73
- const createEslintRule = RuleCreator(
74
- (ruleName) => `${blobUrl}${ruleName}`
75
- );
76
-
77
- function defineTemplateBodyVisitor(context, templateBodyVisitor, scriptVisitor, options) {
78
- const sourceCode = getSourceCode(context);
79
- const parserServices = sourceCode.parserServices;
80
- if (!parserServices)
81
- throw new Error("missing parserServices");
82
- if (!("defineTemplateBodyVisitor" in parserServices) || parserServices.defineTemplateBodyVisitor == null) {
83
- const filename = getFilename(context);
84
- if (node_path.extname(filename) === ".vue") {
85
- context.report({
86
- loc: { column: 0, line: 1 },
87
- 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"
88
- });
89
- }
90
- return {};
91
- }
92
- return parserServices.defineTemplateBodyVisitor(
93
- templateBodyVisitor,
94
- scriptVisitor,
95
- options
96
- );
97
- }
98
-
99
- function getStringLiteralValue(node, stringOnly = false) {
100
- if (node.type === "Literal") {
101
- if (node.value == null) {
102
- if (!stringOnly && node.bigint != null)
103
- return node.bigint;
104
- return null;
105
- }
106
- if (typeof node.value === "string")
107
- return node.value;
108
- if (!stringOnly)
109
- return String(node.value);
110
- return null;
111
- }
112
- if (node.type === "TemplateLiteral" && node.expressions.length === 0 && node.quasis.length === 1) {
113
- return node.quasis[0].value.cooked;
114
- }
115
- return null;
116
- }
117
- function getStaticPropertyName(node) {
118
- if (node.type === "Property" || node.type === "MethodDefinition") {
119
- if (!node.computed) {
120
- const key2 = node.key;
121
- if (key2.type === "Identifier")
122
- return key2.name;
123
- }
124
- const key = node.key;
125
- return getStringLiteralValue(key);
126
- } else if (node.type === "MemberExpression") {
127
- if (!node.computed) {
128
- const property2 = node.property;
129
- if (property2.type === "Identifier")
130
- return property2.name;
131
- return null;
132
- }
133
- const property = node.property;
134
- return getStringLiteralValue(property);
135
- }
136
- return null;
137
- }
138
-
139
- function createRecommended(plugin, name, flat) {
140
- const rules = Object.fromEntries(Object.entries(plugin.rules).filter(([_key, rule]) => rule.meta.recommended === "recommended" && !rule.meta.deprecated).map(([key]) => [`${name}/${key}`, 2]));
141
- if (flat) {
142
- return {
143
- plugins: {
144
- [name]: plugin
145
- },
146
- rules
147
- };
148
- } else {
149
- return {
150
- plugins: [name],
151
- rules
152
- };
153
- }
154
- }
155
-
156
- const RULE_NAME$5 = "no-deprecated-classes";
157
- const debug$3 = createDebug__default("@rotki/eslint-plugin:no-deprecated-classes");
158
- const replacements$2 = [
159
- ["d-block", "block"],
160
- ["d-flex", "flex"],
161
- ["flex-column", "flex-col"],
162
- ["flex-grow-1", "grow"],
163
- ["flex-grow-0", "grow-0"],
164
- ["flex-shrink-1", "shrink"],
165
- ["flex-shrink-0", "shrink-0"],
166
- [
167
- /^align-(start|end|center|baseline|stretch)$/,
168
- ([align]) => `items-${align}`
169
- ],
170
- [/^justify-space-(between|around)$/, ([justify]) => `justify-${justify}`],
171
- [
172
- /^align-self-(start|end|center|baseline|auto|strech)$/,
173
- ([align]) => `self-${align}`
174
- ],
175
- [
176
- /^font-weight-(thin|extralight|light|normal|medium|semibold|bold|extrabold|black)$/,
177
- ([weight]) => `font-${weight}`
178
- ],
179
- [/^text-(capitalize|uppercase|lowercase)$/, ([casing]) => casing],
180
- ["text--secondary", "text-rui-text-secondary"],
181
- ["white--text", "text-white"],
182
- ["primary--text", "text-rui-primary"],
183
- [
184
- /^([mp])([abelr-txy]?)-n(\d)$/,
185
- ([type, position, size]) => `-${type}${position === "a" ? "" : position}-${size}`
186
- ],
187
- [
188
- /^([mp])a-(\d)$/,
189
- ([type, size]) => `${type}-${size}`
190
- ]
191
- ];
192
- function isString(replacement) {
193
- return typeof replacement[0] === "string";
194
- }
195
- function isRegex(replacement) {
196
- return replacement[0] instanceof RegExp;
197
- }
198
- function findReplacement(className) {
199
- for (const replacement of replacements$2) {
200
- if (isString(replacement) && replacement[0] === className)
201
- return replacement[1];
202
- if (isRegex(replacement)) {
203
- const matches = (replacement[0].exec(className) || []).slice(1);
204
- const replace = replacement[1];
205
- if (matches.length > 0 && typeof replace === "function")
206
- return replace(matches);
207
- }
208
- }
209
- return undefined;
210
- }
211
- function getRange(node) {
212
- if (node.type === "VAttribute" && node.value && node.value.range)
213
- return node.value.range;
214
- return node.range;
215
- }
216
- function reportReplacement(className, replacement, node, context, position = 1) {
217
- debug$3(`found replacement ${replacement} for ${className}`);
218
- const source = getSourceCode(context);
219
- const initialRange = getRange(node);
220
- const range = [
221
- initialRange[0] + position,
222
- initialRange[0] + position + className.length
223
- ];
224
- const loc = {
225
- end: source.getLocFromIndex(range[1]),
226
- start: source.getLocFromIndex(range[0])
227
- };
228
- context.report({
229
- data: {
230
- className,
231
- replacement
232
- },
233
- fix(fixer) {
234
- return fixer.replaceTextRange(range, replacement);
235
- },
236
- loc,
237
- messageId: "replacedWith"
238
- });
239
- }
240
- function* extractClassNames(node, textOnly = false) {
241
- if (node.type === "Literal") {
242
- const classNames = `${node.value}`;
243
- yield* classNames.split(/\s+/).map((className) => ({ className, position: classNames.indexOf(className) + 1, reportNode: node }));
244
- return;
245
- }
246
- if (node.type === "TemplateLiteral") {
247
- for (const templateElement of node.quasis) {
248
- const classNames = templateElement.value.cooked;
249
- if (classNames === null)
250
- continue;
251
- yield* classNames.split(/\s+/).map((className) => ({ className, position: classNames.indexOf(className) + 1, reportNode: templateElement }));
252
- }
253
- for (const expr of node.expressions)
254
- yield* extractClassNames(expr, true);
255
- return;
256
- }
257
- if (node.type === "BinaryExpression") {
258
- if (node.operator !== "+")
259
- return;
260
- yield* extractClassNames(node.left, true);
261
- yield* extractClassNames(node.right, true);
262
- return;
263
- }
264
- if (textOnly)
265
- return;
266
- if (node.type === "ObjectExpression") {
267
- for (const prop of node.properties) {
268
- if (prop.type !== "Property")
269
- continue;
270
- const classNames = getStaticPropertyName(prop);
271
- if (!classNames)
272
- continue;
273
- yield* classNames.split(/\s+/).map((className) => ({ className, position: classNames.indexOf(className) + 1, reportNode: prop.key }));
274
- }
275
- return;
276
- }
277
- if (node.type === "ArrayExpression") {
278
- for (const element of node.elements) {
279
- if (element == null)
280
- continue;
281
- if (element.type === "SpreadElement")
282
- continue;
283
- yield* extractClassNames(element);
284
- }
285
- }
286
- if (node.type === "ConditionalExpression") {
287
- yield* extractClassNames(node.consequent);
288
- yield* extractClassNames(node.alternate);
289
- }
290
- }
291
- const noDeprecatedClasses = createEslintRule({
292
- create(context) {
293
- return defineTemplateBodyVisitor(context, {
294
- 'VAttribute[directive=false][key.name="class"]': function(node) {
295
- if (!node.value || !node.value.value)
296
- return;
297
- for (const className of node.value.value.split(/\s+/).filter((s) => !!s)) {
298
- const replacement = findReplacement(className);
299
- const position = node.value.value.indexOf(className) + 1;
300
- if (!replacement)
301
- continue;
302
- reportReplacement(className, replacement, node, context, position);
303
- }
304
- },
305
- "VAttribute[directive=true][key.name.name='bind'][key.argument.name='class'] > VExpressionContainer.value": function(node) {
306
- if (!node.expression)
307
- return;
308
- for (const { className, position, reportNode } of extractClassNames(node.expression)) {
309
- const replacement = findReplacement(className);
310
- if (!replacement)
311
- continue;
312
- reportReplacement(className, replacement, reportNode, context, position);
313
- }
314
- }
315
- });
316
- },
317
- defaultOptions: [],
318
- meta: {
319
- docs: {
320
- description: "disallow the usage of vuetify css classes since they are replaced with tailwindcss",
321
- recommendation: "recommended"
322
- },
323
- fixable: "code",
324
- messages: {
325
- replacedWith: `'{{ className }}' has been replaced with '{{ replacement }}'`
326
- },
327
- schema: [],
328
- type: "problem"
329
- },
330
- name: RULE_NAME$5
331
- });
332
-
333
- const debug$2 = createDebug__default("@rotki/eslint-plugin:no-deprecated-components");
334
- const RULE_NAME$4 = "no-deprecated-components";
335
- const vuetify = {
336
- VApp: true,
337
- VAppBar: true,
338
- VAppBarNavIcon: true,
339
- VAutocomplete: true,
340
- VAvatar: true,
341
- VBottomSheet: true,
342
- VColorPicker: true,
343
- VCombobox: true,
344
- VDataFooter: true,
345
- VDataTable: true,
346
- VDialog: true,
347
- VDialogTransition: true,
348
- VExpansionPanel: true,
349
- VExpansionPanelContent: true,
350
- VExpansionPanelHeader: true,
351
- VExpansionPanels: true,
352
- VMain: true,
353
- VMenu: true,
354
- VNavigationDrawer: true,
355
- VPagination: true,
356
- VSelect: true,
357
- VSlider: true,
358
- VSnackbar: true,
359
- VSwitch: true,
360
- VTextField: true
361
- };
362
- const replacements$1 = {
363
- DataTable: true,
364
- Fragment: false,
365
- ...vuetify
366
- };
367
- const skipInLegacy = [
368
- "Fragment"
369
- ];
370
- function hasReplacement$1(tag) {
371
- return Object.prototype.hasOwnProperty.call(replacements$1, tag);
372
- }
373
- const noDeprecatedComponents = createEslintRule({
374
- create(context, optionsWithDefault) {
375
- const options = optionsWithDefault[0] || {};
376
- const legacy = options.legacy;
377
- return defineTemplateBodyVisitor(context, {
378
- VElement(element) {
379
- const tag = scule.pascalCase(element.rawName);
380
- const sourceCode = getSourceCode(context);
381
- if (sourceCode?.parserServices && !("getTemplateBodyTokenStore" in sourceCode.parserServices))
382
- throw new Error("cannot find getTemplateBodyTokenStore in parserServices");
383
- if (!hasReplacement$1(tag))
384
- return;
385
- const replacement = replacements$1[tag];
386
- if (replacement || legacy && skipInLegacy.includes(tag)) {
387
- debug$2(`${tag} has been deprecated`);
388
- context.report({
389
- data: {
390
- name: tag
391
- },
392
- messageId: "deprecated",
393
- node: element
394
- });
395
- } else {
396
- debug$2(`${tag} has will be removed`);
397
- context.report({
398
- data: {
399
- name: tag
400
- },
401
- fix(fixer) {
402
- return [
403
- fixer.remove(element.startTag),
404
- ...element.endTag ? [fixer.remove(element.endTag)] : []
405
- ];
406
- },
407
- messageId: "removed",
408
- node: element
409
- });
410
- }
411
- }
412
- });
413
- },
414
- defaultOptions: [{ legacy: false }],
415
- meta: {
416
- docs: {
417
- description: "Removes deprecated classes that do not exist anymore",
418
- recommendation: "recommended"
419
- },
420
- fixable: "code",
421
- messages: {
422
- deprecated: `'{{ name }}' has been deprecated`,
423
- removed: `'{{ name }}' has been removed`,
424
- replacedWith: `'{{ a }}' has been replaced with '{{ b }}'`
425
- },
426
- schema: [
427
- {
428
- additionalProperties: false,
429
- properties: {
430
- legacy: {
431
- type: "boolean"
432
- }
433
- },
434
- type: "object"
435
- }
436
- ],
437
- type: "problem"
438
- },
439
- name: RULE_NAME$4
440
- });
441
-
442
- const debug$1 = createDebug__default("@rotki/eslint-plugin:no-deprecated-props");
443
- const RULE_NAME$3 = "no-deprecated-props";
444
- const replacements = {
445
- RuiRadio: {
446
- internalValue: "value"
447
- }
448
- };
449
- function hasReplacement(tag) {
450
- return Object.prototype.hasOwnProperty.call(replacements, tag);
451
- }
452
- function getPropName(node) {
453
- if (node.directive) {
454
- if (node.key.argument?.type !== "VIdentifier")
455
- return undefined;
456
- return scule.kebabCase(node.key.argument.rawName);
457
- }
458
- return scule.kebabCase(node.key.rawName);
459
- }
460
- const noDeprecatedProps = createEslintRule({
461
- create(context) {
462
- return defineTemplateBodyVisitor(context, {
463
- VAttribute(node) {
464
- if (node.directive && (node.value?.type === "VExpressionContainer" && (node.key.name.name !== "bind" || !node.key.argument)))
465
- return;
466
- const tag = scule.pascalCase(node.parent.parent.rawName);
467
- if (!hasReplacement(tag))
468
- return;
469
- debug$1(`${tag} has replacement properties`);
470
- const propName = getPropName(node);
471
- const propNameNode = node.directive ? node.key.argument : node.key;
472
- if (!propName || !propNameNode) {
473
- debug$1("could not get prop name and/or node");
474
- return;
475
- }
476
- Object.entries(replacements[tag]).forEach(([prop, replacement]) => {
477
- if (scule.kebabCase(prop) === propName) {
478
- debug$1(`preparing a replacement for ${tag}:${propName} -> ${replacement}`);
479
- context.report({
480
- data: {
481
- prop,
482
- replacement
483
- },
484
- fix(fixer) {
485
- return fixer.replaceText(propNameNode, replacement);
486
- },
487
- messageId: "replacedWith",
488
- node: propNameNode
489
- });
490
- }
491
- });
492
- }
493
- });
494
- },
495
- defaultOptions: [],
496
- meta: {
497
- docs: {
498
- description: "Replaces deprecated props with their replacements",
499
- recommendation: "recommended"
500
- },
501
- fixable: "code",
502
- messages: {
503
- replacedWith: `'{{ prop }}' has been replaced with '{{ replacement }}'`
504
- },
505
- schema: [],
506
- type: "problem"
507
- },
508
- name: RULE_NAME$3
509
- });
510
-
511
- const RULE_NAME$2 = "no-legacy-library-import";
512
- const legacyLibrary = "@rotki/ui-library-compat";
513
- const newLibrary = "@rotki/ui-library";
514
- const noLegacyLibraryImport = createEslintRule({
515
- create(context) {
516
- return {
517
- ImportDeclaration(node) {
518
- if (!node.source.value.startsWith(legacyLibrary))
519
- return;
520
- const replacement = node.source.value.replace(legacyLibrary, newLibrary);
521
- context.report({
522
- fix(fixer) {
523
- return fixer.replaceText(node.source, `'${replacement}'`);
524
- },
525
- messageId: "replacedWith",
526
- node: node.source
527
- });
528
- }
529
- };
530
- },
531
- defaultOptions: [],
532
- meta: {
533
- docs: {
534
- description: `Reports and replaces imports of ${legacyLibrary} with ${newLibrary}`,
535
- recommendation: "recommended"
536
- },
537
- fixable: "code",
538
- messages: {
539
- replacedWith: `${legacyLibrary} has been replaced by ${newLibrary}`
540
- },
541
- schema: [],
542
- type: "problem"
543
- },
544
- name: RULE_NAME$2
545
- });
546
-
547
- const debug = createDebug__default("@rotki/eslint-plugin:consistent-ref-type-annotation");
548
- const RULE_NAME$1 = "consistent-ref-type-annotation";
549
- const FIXABLE_METHODS = ["ref", "computed"];
550
- function checkAssignmentDeclaration(context, node, declaration, options) {
551
- const source = getSourceCode(context);
552
- const { allowInference } = options;
553
- let declarationTypeArguments;
554
- const init = declaration.init;
555
- if (!(init && init.type === utils.TSESTree.AST_NODE_TYPES.CallExpression))
556
- return;
557
- const callee = init.callee;
558
- if (!(callee && callee.type === utils.TSESTree.AST_NODE_TYPES.Identifier))
559
- return;
560
- if (!Array.prototype.includes.call(FIXABLE_METHODS, callee.name))
561
- return;
562
- const name = callee.name;
563
- debug(`found ${name}, checking type arguments`);
564
- const initializationTypeArguments = init.typeArguments;
565
- const typeAnnotation = declaration.id.typeAnnotation;
566
- if (typeAnnotation) {
567
- const typeNode = typeAnnotation.typeAnnotation;
568
- if (typeNode && typeNode.type === utils.TSESTree.AST_NODE_TYPES.TSTypeReference)
569
- declarationTypeArguments = typeNode.typeArguments;
570
- }
571
- if (initializationTypeArguments && !declarationTypeArguments)
572
- return;
573
- debug(`generating report for ${name}`);
574
- if (!initializationTypeArguments && !declarationTypeArguments) {
575
- if (allowInference) {
576
- debug("type inference is allowed");
577
- } else {
578
- context.report({
579
- data: {
580
- name
581
- },
582
- messageId: "missingType",
583
- node
584
- });
585
- }
586
- return;
587
- }
588
- context.report({
589
- data: {
590
- name
591
- },
592
- fix(fixer) {
593
- const fixes = [];
594
- if (!initializationTypeArguments && callee)
595
- fixes.push(fixer.insertTextAfter(callee, source.getText(declarationTypeArguments)));
596
- if (typeAnnotation)
597
- fixes.push(fixer.remove(typeAnnotation));
598
- return fixes;
599
- },
600
- messageId: "inconsistent",
601
- node
602
- });
603
- }
604
- const consistentRefTypeAnnotation = createEslintRule({
605
- create(context, optionsWithDefault) {
606
- const options = optionsWithDefault[0] || {};
607
- const allowInference = options.allowInference;
608
- return {
609
- VariableDeclaration: (node) => {
610
- const declarations = node.declarations;
611
- for (const declaration of declarations) {
612
- if (declaration.type !== utils.TSESTree.AST_NODE_TYPES.VariableDeclarator)
613
- continue;
614
- checkAssignmentDeclaration(context, node, declaration, { allowInference });
615
- }
616
- }
617
- };
618
- },
619
- defaultOptions: [{ allowInference: false }],
620
- meta: {
621
- docs: {
622
- description: "Ensures consistent type annotation position for ref, computed assignments",
623
- recommendation: "recommended"
624
- },
625
- fixable: "code",
626
- messages: {
627
- inconsistent: `Generic type annotation for the {{ name }} call was not on the right side`,
628
- missingType: `variable assignment for {{ name }} is missing the type annotation`
629
- },
630
- schema: [
631
- {
632
- additionalProperties: false,
633
- properties: {
634
- allowInference: {
635
- type: "boolean"
636
- }
637
- },
638
- type: "object"
639
- }
640
- ],
641
- type: "problem"
642
- },
643
- name: RULE_NAME$1
644
- });
645
-
646
- const RULE_NAME = "no-dot-ts-imports";
647
- const noDotTsImport = createEslintRule({
648
- create(context) {
649
- return {
650
- ImportDeclaration(node) {
651
- const importDeclaration = node.source.value;
652
- if (!importDeclaration.endsWith(".ts"))
653
- return;
654
- const lastIndexOfExtension = importDeclaration.lastIndexOf(".ts");
655
- const replacement = importDeclaration.substring(0, lastIndexOfExtension);
656
- context.report({
657
- data: {
658
- import: importDeclaration
659
- },
660
- fix(fixer) {
661
- return fixer.replaceText(node.source, `'${replacement}'`);
662
- },
663
- messageId: "invalidTSExtension",
664
- node: node.source
665
- });
666
- }
667
- };
668
- },
669
- defaultOptions: [],
670
- meta: {
671
- docs: {
672
- description: "Checks and replaces .ts extension in import statements.",
673
- recommendation: "recommended"
674
- },
675
- fixable: "code",
676
- messages: {
677
- invalidTSExtension: `'{{ import }}' has a .ts extension, please remove it'`
678
- },
679
- schema: [],
680
- type: "problem"
681
- },
682
- name: RULE_NAME
683
- });
684
-
685
- const plugin = {
686
- meta: {
687
- name: "@rotki/eslint-plugin",
688
- version: pkg.version
689
- },
690
- rules: {
691
- "consistent-ref-type-annotation": consistentRefTypeAnnotation,
692
- "no-deprecated-classes": noDeprecatedClasses,
693
- "no-deprecated-components": noDeprecatedComponents,
694
- "no-deprecated-props": noDeprecatedProps,
695
- "no-dot-ts-imports": noDotTsImport,
696
- "no-legacy-library-import": noLegacyLibraryImport
697
- }
698
- };
699
-
700
- const configs = {
701
- "recommended": createRecommended(plugin, "@rotki", false),
702
- "recommended-flat": createRecommended(plugin, "@rotki", true)
703
- };
704
-
705
- const index = Object.assign(plugin, { configs });
706
-
707
- module.exports = index;