Skip to content

Fixes for no-raw-text rule #205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/rules/no-raw-text.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,7 @@ The following patterns are not considered warnings:
const text = 'some text';
<View><Text>{`${text}`}</Text></View>
```

####This rule has an object option:

- "skip" – allow to skip checking for the array of custom components
35 changes: 29 additions & 6 deletions lib/rules/no-raw-text.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
'use strict';

module.exports = (context) => {
const options = context.options[0] || {};

const elementName = node => (
node.openingElement &&
node.openingElement.name &&
Expand All @@ -16,21 +18,29 @@ module.exports = (context) => {
const report = (node) => {
const errorValue = node.type === 'TemplateLiteral'
? `TemplateLiteral: ${node.expressions[0].name}`
: node.value;
: node.value.trim();

const formattedErrorValue = errorValue.length > 0
? `Raw text (${errorValue})`
: 'Whitespace(s)';

context.report({
node,
message: `Raw text (${errorValue.trim()}) cannot be used outside of a <Text> tag`,
message: `${formattedErrorValue} cannot be used outside of a <Text> tag`,
});
};

const getValidation = node => elementName(node.parent) !== 'Text';
const skippedElements = options.skip ? options.skip : [];
const allowedElements = ['Text', 'TSpan', 'StyledText'].concat(skippedElements);

const getValidation = node => !allowedElements.includes(elementName(node.parent));

return {
Literal(node) {
const parentType = node.parent.type;
const onlyFor = ['JSXExpressionContainer', 'JSXElement'];
if (/^[\s]+$/.test(node.value) ||
typeof node.value !== 'string' ||
if (typeof node.value !== 'string' ||
/^[\r\n\t\f\v]+$/.test(node.value.replace(/ /g, '')) ||
!onlyFor.includes(parentType) ||
(node.parent.parent && node.parent.parent.type === 'JSXAttribute')
) return;
Expand Down Expand Up @@ -60,4 +70,17 @@ module.exports = (context) => {
};
};

module.exports.schema = [];
module.exports.schema = [
{
type: 'object',
properties: {
skip: {
type: 'array',
items: {
type: 'string',
},
},
},
additionalProperties: false,
},
];
69 changes: 69 additions & 0 deletions tests/lib/rules/no-raw-text.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,53 @@ const tests = {
}
`,
},
{
code: `
export default class MyComponent extends Component {
render() {
return (
<View>
<Text>some text</Text>
</View>
);
}
}
`,
},
{
code: `
export default class MyComponent extends Component {
render() {
return (
<Svg>
<Text>
<TSpan>some text</TSpan>
</Text>
</Svg>
);
}
}
`,
},
{
code: `
const StyledText = styled.Text\`
color: red;
\`
export default class MyComponent extends Component {
render() {
return (<StyledText>some text</StyledText>);
}
}
`,
},
{
code: `
const Title = ({ children }) => (<Text>{children}</Text>);
<Title>This is the title</Title>
`,
options: [{ skip: ['Title'] }],
},
],
invalid: [
{
Expand Down Expand Up @@ -89,6 +136,28 @@ const tests = {
message: 'Raw text (some text) cannot be used outside of a <Text> tag',
}],
},
{
code: `
export default class MyComponent extends Component {
render() {
return (<View> </View>);
}
}
`,
errors: [{
message: 'Whitespace(s) cannot be used outside of a <Text> tag',
}],
},
{
code: `
const Component = ({ children }) => (<Text>{children}</Text>);
<Component>some text</Component>
`,
options: [{ skip: ['Title'] }],
errors: [{
message: 'Raw text (some text) cannot be used outside of a <Text> tag',
}],
},
],
};

Expand Down