Skip to content

Commit 0e1d377

Browse files
authored
Merge pull request Intellicode#220 from AleksandrZhukov/master
Fixed no-raw-text rule for typescript
2 parents 54bae56 + b8a0636 commit 0e1d377

File tree

5 files changed

+585
-552
lines changed

5 files changed

+585
-552
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,7 @@ build/Release
2727
node_modules
2828

2929
.idea
30+
31+
# OSX
32+
#
33+
.DS_Store

lib/rules/no-raw-text.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,16 @@ module.exports = (context) => {
3333
const skippedElements = options.skip ? options.skip : [];
3434
const allowedElements = ['Text', 'TSpan', 'StyledText'].concat(skippedElements);
3535

36+
const hasOnlyLineBreak = value => /^[\r\n\t\f\v]+$/.test(value.replace(/ /g, ''));
37+
3638
const getValidation = node => !allowedElements.includes(elementName(node.parent));
3739

3840
return {
3941
Literal(node) {
4042
const parentType = node.parent.type;
4143
const onlyFor = ['JSXExpressionContainer', 'JSXElement'];
4244
if (typeof node.value !== 'string' ||
43-
/^[\r\n\t\f\v]+$/.test(node.value.replace(/ /g, '')) ||
45+
hasOnlyLineBreak(node.value) ||
4446
!onlyFor.includes(parentType) ||
4547
(node.parent.parent && node.parent.parent.type === 'JSXAttribute')
4648
) return;
@@ -52,6 +54,7 @@ module.exports = (context) => {
5254
},
5355

5456
JSXText(node) {
57+
if (typeof node.value !== 'string' || hasOnlyLineBreak(node.value)) return;
5558
if (getValidation(node)) {
5659
report(node);
5760
}

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"homepage": "https://github.com/intellicode/eslint-plugin-react-native",
2424
"bugs": "https://github.com/intellicode/eslint-plugin-react-native/issues",
2525
"devDependencies": {
26+
"@typescript-eslint/parser": "^1.4.2",
2627
"babel-eslint": "7.2.3",
2728
"coveralls": "^3.0.2",
2829
"eslint": "^5.5.0",
@@ -31,7 +32,8 @@
3132
"eslint-plugin-jsx-a11y": "^5.1.1",
3233
"eslint-plugin-react": "^7.3.0",
3334
"istanbul": "0.4.5",
34-
"mocha": "^5.2.0"
35+
"mocha": "^5.2.0",
36+
"typescript": "^3.3.3333"
3537
},
3638
"peerDependencies": {
3739
"eslint": "^3.17.0 || ^4 || ^5"

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,23 @@ const config = {
171171
},
172172
};
173173

174+
const TSConfig = {
175+
parser: '@typescript-eslint/parser',
176+
parserOptions: {
177+
ecmaVersion: 2018,
178+
sourceType: 'module',
179+
ecmaFeatures: {
180+
jsx: true,
181+
},
182+
},
183+
};
184+
174185
tests.valid.forEach(t => Object.assign(t, config));
175186
tests.invalid.forEach(t => Object.assign(t, config));
176187

177188
ruleTester.run('no-raw-text', rule, tests);
189+
190+
tests.valid.forEach(t => Object.assign(t, TSConfig));
191+
tests.invalid.forEach(t => Object.assign(t, TSConfig));
192+
193+
ruleTester.run('no-raw-text', rule, tests);

0 commit comments

Comments
 (0)