Skip to content

Commit 574512d

Browse files
authored
Merge pull request Intellicode#247 from Debens/master
feat: adding support for object notation components for no-raw-text exclusion rule
2 parents d98deec + 26576c1 commit 574512d

File tree

4 files changed

+1209
-640
lines changed

4 files changed

+1209
-640
lines changed

lib/rules/no-raw-text.js

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,27 @@
55

66
'use strict';
77

8+
const { default: traverse } = require('@babel/traverse');
9+
10+
const elementName = (node, scope) => {
11+
const identifiers = [];
12+
13+
traverse(node, {
14+
JSXOpeningElement({ node: element }) {
15+
traverse(element, {
16+
JSXIdentifier({ node: identifier }) {
17+
identifiers.push(identifier.name);
18+
},
19+
}, scope);
20+
},
21+
}, scope);
22+
23+
return identifiers.join('.');
24+
};
25+
826
module.exports = (context) => {
927
const options = context.options[0] || {};
1028

11-
const elementName = (node) => (
12-
node.openingElement
13-
&& node.openingElement.name
14-
&& node.openingElement.name.type === 'JSXIdentifier'
15-
&& node.openingElement.name.name
16-
);
17-
1829
const report = (node) => {
1930
const errorValue = node.type === 'TemplateLiteral'
2031
? `TemplateLiteral: ${node.expressions[0].name}`
@@ -35,7 +46,8 @@ module.exports = (context) => {
3546

3647
const hasOnlyLineBreak = (value) => /^[\r\n\t\f\v]+$/.test(value.replace(/ /g, ''));
3748

38-
const getValidation = (node) => !allowedElements.includes(elementName(node.parent));
49+
const scope = context.getScope();
50+
const getValidation = (node) => !allowedElements.includes(elementName(node.parent, scope));
3951

4052
return {
4153
Literal(node) {

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
],
4949
"license": "MIT",
5050
"dependencies": {
51+
"@babel/traverse": "^7.7.4",
5152
"eslint-plugin-react-native-globals": "^0.1.1"
5253
}
5354
}

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,14 @@ const tests = {
9797
`,
9898
options: [{ skip: ['Title'] }],
9999
},
100+
{
101+
code: `
102+
const Title = ({ children }) => (<Title.Text>{children}</Title.Text>);
103+
Title.Text = ({ children }) => (<Text>{children}</Text>);
104+
<Title.Text>This is the title</Title.Text>
105+
`,
106+
options: [{ skip: ['Title.Text'] }],
107+
},
100108
],
101109
invalid: [
102110
{
@@ -158,6 +166,16 @@ const tests = {
158166
message: 'Raw text (some text) cannot be used outside of a <Text> tag',
159167
}],
160168
},
169+
{
170+
code: `
171+
const Component = ({ children }) => (<Text>{children}</Text>);
172+
<Component>some text</Component>
173+
`,
174+
options: [{ skip: ['Component.Text'] }],
175+
errors: [{
176+
message: 'Raw text (some text) cannot be used outside of a <Text> tag',
177+
}],
178+
},
161179
],
162180
};
163181

0 commit comments

Comments
 (0)