consistent-data-testid.js
3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RULE_NAME = void 0;
var utils_1 = require("../utils");
var experimental_utils_1 = require("@typescript-eslint/experimental-utils");
var node_utils_1 = require("../node-utils");
exports.RULE_NAME = 'consistent-data-testid';
var FILENAME_PLACEHOLDER = '{fileName}';
exports.default = experimental_utils_1.ESLintUtils.RuleCreator(utils_1.getDocsUrl)({
name: exports.RULE_NAME,
meta: {
type: 'suggestion',
docs: {
description: 'Ensures consistent usage of `data-testid`',
category: 'Best Practices',
recommended: false,
},
messages: {
invalidTestId: '`{{attr}}` "{{value}}" should match `{{regex}}`',
},
fixable: null,
schema: [
{
type: 'object',
default: {},
additionalProperties: false,
required: ['testIdPattern'],
properties: {
testIdPattern: {
type: 'string',
},
testIdAttribute: {
default: 'data-testid',
oneOf: [
{
type: 'string',
},
{
type: 'array',
items: {
type: 'string',
},
},
],
},
},
},
],
},
defaultOptions: [
{
testIdPattern: '',
testIdAttribute: 'data-testid',
},
],
create: function (context, _a) {
var _b;
var options = _a[0];
var getFilename = context.getFilename;
var testIdPattern = options.testIdPattern, attr = options.testIdAttribute;
function getFileNameData() {
var splitPath = getFilename().split('/');
var fileNameWithExtension = splitPath.pop();
var parent = splitPath.pop();
var fileName = fileNameWithExtension.split('.').shift();
return {
fileName: fileName === 'index' ? parent : fileName,
};
}
function getTestIdValidator(fileName) {
return new RegExp(testIdPattern.replace(FILENAME_PLACEHOLDER, fileName));
}
function isTestIdAttribute(name) {
if (typeof attr === 'string') {
return attr === name;
}
else {
return attr.includes(name);
}
}
return _b = {},
_b["JSXIdentifier"] = function (node) {
if (!node_utils_1.isJSXAttribute(node.parent) ||
!node_utils_1.isLiteral(node.parent.value) ||
!isTestIdAttribute(node.name)) {
return;
}
var value = node.parent.value.value;
var fileName = getFileNameData().fileName;
var regex = getTestIdValidator(fileName);
if (value && typeof value === 'string' && !regex.test(value)) {
context.report({
node: node,
messageId: 'invalidTestId',
data: {
attr: node.name,
value: value,
regex: regex,
},
});
}
},
_b;
},
});