-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
Copy pathjsx-no-literals.js
189 lines (160 loc) · 5.35 KB
/
jsx-no-literals.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/**
* @fileoverview Prevent using string literals in React component definition
* @author Caleb Morris
* @author David Buchan-Swanson
*/
'use strict';
const docsUrl = require('../util/docsUrl');
const report = require('../util/report');
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
function trimIfString(val) {
return typeof val === 'string' ? val.trim() : val;
}
const messages = {
invalidPropValue: 'Invalid prop value: "{{text}}"',
noStringsInAttributes: 'Strings not allowed in attributes: "{{text}}"',
noStringsInJSX: 'Strings not allowed in JSX files: "{{text}}"',
literalNotInJSXExpression: 'Missing JSX expression container around literal string: "{{text}}"',
};
module.exports = {
meta: {
type: 'suggestion',
docs: {
description: 'Prevent using string literals in React component definition',
category: 'Stylistic Issues',
recommended: false,
url: docsUrl('jsx-no-literals'),
},
messages,
schema: [{
type: 'object',
properties: {
noStrings: {
type: 'boolean',
},
allowedStrings: {
type: 'array',
uniqueItems: true,
items: {
type: 'string',
},
},
ignoreProps: {
type: 'boolean',
},
noAttributeStrings: {
type: 'boolean',
},
},
additionalProperties: false,
}],
},
create(context) {
const defaults = {
noStrings: false,
allowedStrings: [],
ignoreProps: false,
noAttributeStrings: false,
};
const config = Object.assign({}, defaults, context.options[0] || {});
config.allowedStrings = new Set(config.allowedStrings.map(trimIfString));
function defaultMessageId() {
if (config.noAttributeStrings) {
return 'noStringsInAttributes';
}
if (config.noStrings) {
return 'noStringsInJSX';
}
return 'literalNotInJSXExpression';
}
function reportLiteralNode(node, messageId) {
messageId = messageId || defaultMessageId();
report(context, messages[messageId], messageId, {
node,
data: {
text: context.getSourceCode().getText(node).trim(),
},
});
}
function getParentIgnoringBinaryExpressions(node) {
let current = node;
while (current.parent.type === 'BinaryExpression') {
current = current.parent;
}
return current.parent;
}
function getValidation(node) {
if (config.allowedStrings.has(trimIfString(node.value))) {
return false;
}
const parent = getParentIgnoringBinaryExpressions(node);
function isParentNodeStandard() {
if (!/^[\s]+$/.test(node.value) && typeof node.value === 'string' && parent.type.includes('JSX')) {
if (config.noAttributeStrings) {
return parent.type === 'JSXAttribute';
}
if (!config.noAttributeStrings) {
return parent.type !== 'JSXAttribute';
}
}
return false;
}
const standard = isParentNodeStandard();
if (config.noStrings) {
return standard;
}
return standard && parent.type !== 'JSXExpressionContainer';
}
function getParentAndGrandParentType(node) {
const parent = getParentIgnoringBinaryExpressions(node);
const parentType = parent.type;
const grandParentType = parent.parent.type;
return {
parent,
parentType,
grandParentType,
grandParent: parent.parent,
};
}
function hasJSXElementParentOrGrandParent(node) {
const parents = getParentAndGrandParentType(node);
const parentType = parents.parentType;
const grandParentType = parents.grandParentType;
return parentType === 'JSXFragment' || parentType === 'JSXElement' || grandParentType === 'JSXElement';
}
// --------------------------------------------------------------------------
// Public
// --------------------------------------------------------------------------
return {
Literal(node) {
if (getValidation(node) && (hasJSXElementParentOrGrandParent(node) || !config.ignoreProps)) {
reportLiteralNode(node);
}
},
JSXAttribute(node) {
const isNodeValueString = node && node.value && node.value.type === 'Literal' && typeof node.value.value === 'string' && !config.allowedStrings.has(node.value.value);
if (config.noStrings && !config.ignoreProps && isNodeValueString) {
const messageId = 'invalidPropValue';
reportLiteralNode(node, messageId);
}
},
JSXText(node) {
if (getValidation(node)) {
reportLiteralNode(node);
}
},
TemplateLiteral(node) {
const parents = getParentAndGrandParentType(node);
const parentType = parents.parentType;
const grandParentType = parents.grandParentType;
const isParentJSXExpressionCont = parentType === 'JSXExpressionContainer';
const isParentJSXElement = parentType === 'JSXElement' || grandParentType === 'JSXElement';
if (isParentJSXExpressionCont && config.noStrings && (isParentJSXElement || !config.ignoreProps)) {
reportLiteralNode(node);
}
},
};
},
};