Skip to content

Commit 1c2f7fc

Browse files
praxxisadidahiya
authored andcommitted
Fixes for translation lint where punctuation and HTML entities are side by side (#142)
This case came up running lint across our larger code base, where strings like "{var1}: {var2}" were incorrectly flagged when both punctuation and HTML entities were allowed.
1 parent e540543 commit 1c2f7fc

File tree

5 files changed

+110
-10
lines changed

5 files changed

+110
-10
lines changed

src/rules/jsxUseTranslationFunctionRule.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ interface IOptions {
2424
allowHtmlEntities: boolean;
2525
}
2626

27+
const htmlEntityRegex = /(&(?:#[0-9]+|[a-zA-Z]+);)/;
28+
2729
export class Rule extends Lint.Rules.AbstractRule {
2830
/* tslint:disable:object-literal-sort-keys */
2931
public static metadata: Lint.IRuleMetadata = {
@@ -99,17 +101,23 @@ function isInvalidText(text: string, options: Readonly<IOptions>) {
99101
return false;
100102
}
101103

102-
let invalid = true;
103-
104-
if (options.allowPunctuation) {
105-
invalid = /\w/.test(t);
104+
if (options.allowPunctuation && t.indexOf("&") === -1) {
105+
// fast path: any punctuation that is not potentially an HTML entity
106+
return /\w/.test(t);
106107
}
107108

108-
if (options.allowHtmlEntities && t.indexOf("&") !== -1) {
109-
invalid = t.split("&")
110-
.filter((entity) => entity !== "")
111-
.some((entity) => /^&(?:#[0-9]+|[a-zA-Z]+);$/.test(`&${entity}`) !== true);
112-
}
109+
// split the text into HTML entities and everything else so we can test each part of the string individually
110+
const parts = t.split(htmlEntityRegex).filter((entity) => entity !== "");
111+
112+
return parts.some((entity) => {
113+
if (options.allowHtmlEntities && htmlEntityRegex.test(entity)) {
114+
return false;
115+
}
116+
117+
if (options.allowPunctuation) {
118+
return /\w/.test(entity);
119+
}
113120

114-
return invalid;
121+
return true;
122+
});
115123
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<div>Hello world!</div>
2+
~~~~~~~~~~~~ [0]
3+
4+
<div>{'Hello world!'}</div>
5+
~~~~~~~~~~~~~~~~ [0]
6+
7+
<div>{translate('hello-world')}</div>
8+
9+
<input placeholder={translate('name')} />
10+
<input placeholder="Name" />
11+
~~~~~~ [1]
12+
13+
<input placeholder={translate('name')} />
14+
<input placeholder={"Name"} />
15+
~~~~~~~~ [1]
16+
17+
<div>
18+
<div>{translate('hi')}</div>
19+
</div>
20+
21+
<div>
22+
<span>{translate('this')}</span>is bad<span>
23+
~~~~~~ [0]
24+
</div>
25+
26+
<div>{`foo`}</div>
27+
~~~~~~~ [0]
28+
29+
<div>{`foo ${1}`}</div>
30+
31+
<ul>
32+
<li>{translate('one')}</li>
33+
Two
34+
~~~
35+
<li>Three</li>
36+
~~~~ [0]
37+
~~~~~ [0]
38+
</ul>
39+
40+
<div> - </div>
41+
42+
<div> & </div>
43+
44+
<div>:&nbsp;</div>
45+
46+
<div>:&nbsp</div>
47+
~~~~~~ [0]
48+
49+
<div>{' - '}</div>
50+
51+
<input placeholder="-" />
52+
53+
<div>&nbsp;</div>
54+
55+
<div>{'&nbsp;'}</div>
56+
57+
<input placeholder="&nbsp;" />
58+
59+
<div>hello - world</div>
60+
~~~~~~~~~~~~~ [0]
61+
62+
<div>hello&nbsp;world</div>
63+
~~~~~~~~~~~~~~~~ [0]
64+
65+
[0]: String literals are disallowed as JSX. Use a translation function
66+
[1]: String literal is not allowed for value of placeholder. Use a translation function
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"rules": {
3+
"jsx-use-translation-function": {
4+
"options": ["allow-punctuation", "allow-htmlentities"]
5+
}
6+
}
7+
}

test/rules/jsx-use-translation-function/allow-htmlentities/test.tsx.lint

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,17 @@
4848

4949
<div>&nbsp;</div>
5050

51+
<div>&nbsp;&nbsp;</div>
52+
5153
<div>{'&nbsp;'}</div>
5254

5355
<input placeholder="&nbsp;" />
5456

57+
<div>hello - world</div>
58+
~~~~~~~~~~~~~ [0]
59+
60+
<div>hello&nbsp;world</div>
61+
~~~~~~~~~~~~~~~~ [0]
62+
5563
[0]: String literals are disallowed as JSX. Use a translation function
5664
[1]: String literal is not allowed for value of placeholder. Use a translation function

test/rules/jsx-use-translation-function/allow-punctuation/test.tsx.lint

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@
3939

4040
<div> - </div>
4141

42+
<div> & </div>
43+
44+
<div>:&nbsp;</div>
45+
~~~~~~~ [0]
46+
4247
<div>{' - '}</div>
4348

4449
<input placeholder="-" />
@@ -52,5 +57,11 @@
5257
<input placeholder="&nbsp;" />
5358
~~~~~~~~ [1]
5459

60+
<div>hello - world</div>
61+
~~~~~~~~~~~~~ [0]
62+
63+
<div>hello&nbsp;world</div>
64+
~~~~~~~~~~~~~~~~ [0]
65+
5566
[0]: String literals are disallowed as JSX. Use a translation function
5667
[1]: String literal is not allowed for value of placeholder. Use a translation function

0 commit comments

Comments
 (0)