Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 485b91b

Browse files
authoredOct 15, 2018
Merge pull request #205 from AleksandrZhukov/master
Fixes for no-raw-text rule
2 parents 1e1e9f7 + 6291dde commit 485b91b

File tree

3 files changed

+102
-6
lines changed

3 files changed

+102
-6
lines changed
 

‎docs/rules/no-raw-text.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,7 @@ The following patterns are not considered warnings:
2424
const text = 'some text';
2525
<View><Text>{`${text}`}</Text></View>
2626
```
27+
28+
####This rule has an object option:
29+
30+
- "skip" – allow to skip checking for the array of custom components

‎lib/rules/no-raw-text.js

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
'use strict';
77

88
module.exports = (context) => {
9+
const options = context.options[0] || {};
10+
911
const elementName = node => (
1012
node.openingElement &&
1113
node.openingElement.name &&
@@ -16,21 +18,29 @@ module.exports = (context) => {
1618
const report = (node) => {
1719
const errorValue = node.type === 'TemplateLiteral'
1820
? `TemplateLiteral: ${node.expressions[0].name}`
19-
: node.value;
21+
: node.value.trim();
22+
23+
const formattedErrorValue = errorValue.length > 0
24+
? `Raw text (${errorValue})`
25+
: 'Whitespace(s)';
26+
2027
context.report({
2128
node,
22-
message: `Raw text (${errorValue.trim()}) cannot be used outside of a <Text> tag`,
29+
message: `${formattedErrorValue} cannot be used outside of a <Text> tag`,
2330
});
2431
};
2532

26-
const getValidation = node => elementName(node.parent) !== 'Text';
33+
const skippedElements = options.skip ? options.skip : [];
34+
const allowedElements = ['Text', 'TSpan', 'StyledText'].concat(skippedElements);
35+
36+
const getValidation = node => !allowedElements.includes(elementName(node.parent));
2737

2838
return {
2939
Literal(node) {
3040
const parentType = node.parent.type;
3141
const onlyFor = ['JSXExpressionContainer', 'JSXElement'];
32-
if (/^[\s]+$/.test(node.value) ||
33-
typeof node.value !== 'string' ||
42+
if (typeof node.value !== 'string' ||
43+
/^[\r\n\t\f\v]+$/.test(node.value.replace(/ /g, '')) ||
3444
!onlyFor.includes(parentType) ||
3545
(node.parent.parent && node.parent.parent.type === 'JSXAttribute')
3646
) return;
@@ -60,4 +70,17 @@ module.exports = (context) => {
6070
};
6171
};
6272

63-
module.exports.schema = [];
73+
module.exports.schema = [
74+
{
75+
type: 'object',
76+
properties: {
77+
skip: {
78+
type: 'array',
79+
items: {
80+
type: 'string',
81+
},
82+
},
83+
},
84+
additionalProperties: false,
85+
},
86+
];

‎tests/lib/rules/no-raw-text.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,53 @@ const tests = {
5050
}
5151
`,
5252
},
53+
{
54+
code: `
55+
export default class MyComponent extends Component {
56+
render() {
57+
return (
58+
<View>
59+
<Text>some text</Text>
60+
</View>
61+
);
62+
}
63+
}
64+
`,
65+
},
66+
{
67+
code: `
68+
export default class MyComponent extends Component {
69+
render() {
70+
return (
71+
<Svg>
72+
<Text>
73+
<TSpan>some text</TSpan>
74+
</Text>
75+
</Svg>
76+
);
77+
}
78+
}
79+
`,
80+
},
81+
{
82+
code: `
83+
const StyledText = styled.Text\`
84+
color: red;
85+
\`
86+
export default class MyComponent extends Component {
87+
render() {
88+
return (<StyledText>some text</StyledText>);
89+
}
90+
}
91+
`,
92+
},
93+
{
94+
code: `
95+
const Title = ({ children }) => (<Text>{children}</Text>);
96+
<Title>This is the title</Title>
97+
`,
98+
options: [{ skip: ['Title'] }],
99+
},
53100
],
54101
invalid: [
55102
{
@@ -89,6 +136,28 @@ const tests = {
89136
message: 'Raw text (some text) cannot be used outside of a <Text> tag',
90137
}],
91138
},
139+
{
140+
code: `
141+
export default class MyComponent extends Component {
142+
render() {
143+
return (<View> </View>);
144+
}
145+
}
146+
`,
147+
errors: [{
148+
message: 'Whitespace(s) cannot be used outside of a <Text> tag',
149+
}],
150+
},
151+
{
152+
code: `
153+
const Component = ({ children }) => (<Text>{children}</Text>);
154+
<Component>some text</Component>
155+
`,
156+
options: [{ skip: ['Title'] }],
157+
errors: [{
158+
message: 'Raw text (some text) cannot be used outside of a <Text> tag',
159+
}],
160+
},
92161
],
93162
};
94163

0 commit comments

Comments
 (0)
Please sign in to comment.