-
Notifications
You must be signed in to change notification settings - Fork 130
/
no-raw-text.js
125 lines (105 loc) · 3.01 KB
/
no-raw-text.js
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/**
* @fileoverview Detects raw text outside of Text component
* @author Alex Zhukov
*/
'use strict';
const elementName = (node) => {
const reversedIdentifiers = [];
if (
node.type === 'JSXElement'
&& node.openingElement.type === 'JSXOpeningElement'
) {
let object = node.openingElement.name;
while (object.type === 'JSXMemberExpression') {
if (object.property.type === 'JSXIdentifier') {
reversedIdentifiers.push(object.property.name);
}
object = object.object;
}
if (object.type === 'JSXIdentifier') {
reversedIdentifiers.push(object.name);
}
}
return reversedIdentifiers.reverse().join('.');
};
const hasAllowedParent = (parent, allowedElements) => {
let curNode = parent;
while (curNode) {
if (curNode.type === 'JSXElement') {
const name = elementName(curNode);
if (allowedElements.includes(name)) {
return true;
}
}
curNode = curNode.parent;
}
return false;
};
function create(context) {
const options = context.options[0] || {};
const report = (node) => {
const errorValue = node.type === 'TemplateLiteral'
? `TemplateLiteral: ${node.expressions[0].name}`
: node.value.trim();
const formattedErrorValue = errorValue.length > 0
? `Raw text (${errorValue})`
: 'Whitespace(s)';
context.report({
node,
message: `${formattedErrorValue} cannot be used outside of a <Text> tag`,
});
};
const skippedElements = options.skip ? options.skip : [];
const allowedElements = ['Text', 'TSpan', 'StyledText', 'Animated.Text'].concat(skippedElements);
const hasOnlyLineBreak = (value) => /^[\r\n\t\f\v]+$/.test(value.replace(/ /g, ''));
const getValidation = (node) => !hasAllowedParent(node.parent, allowedElements);
return {
Literal(node) {
const parentType = node.parent.type;
const onlyFor = ['JSXExpressionContainer', 'JSXElement'];
if (typeof node.value !== 'string'
|| hasOnlyLineBreak(node.value)
|| !onlyFor.includes(parentType)
|| (node.parent.parent && node.parent.parent.type === 'JSXAttribute')
) return;
const isStringLiteral = parentType === 'JSXExpressionContainer';
if (getValidation(isStringLiteral ? node.parent : node)) {
report(node);
}
},
JSXText(node) {
if (typeof node.value !== 'string' || hasOnlyLineBreak(node.value)) return;
if (getValidation(node)) {
report(node);
}
},
TemplateLiteral(node) {
if (
node.parent.type !== 'JSXExpressionContainer'
|| (node.parent.parent && node.parent.parent.type === 'JSXAttribute')
) return;
if (getValidation(node.parent)) {
report(node);
}
},
};
}
module.exports = {
meta: {
schema: [
{
type: 'object',
properties: {
skip: {
type: 'array',
items: {
type: 'string',
},
},
},
additionalProperties: false,
},
],
},
create,
};