Skip to content
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

Fixes for translation lint where punctuation and HTML entities are side by side #142

Merged
merged 1 commit into from
Feb 13, 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
28 changes: 18 additions & 10 deletions src/rules/jsxUseTranslationFunctionRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ interface IOptions {
allowHtmlEntities: boolean;
}

const htmlEntityRegex = /(&(?:#[0-9]+|[a-zA-Z]+);)/;

export class Rule extends Lint.Rules.AbstractRule {
/* tslint:disable:object-literal-sort-keys */
public static metadata: Lint.IRuleMetadata = {
Expand Down Expand Up @@ -99,17 +101,23 @@ function isInvalidText(text: string, options: Readonly<IOptions>) {
return false;
}

let invalid = true;

if (options.allowPunctuation) {
invalid = /\w/.test(t);
if (options.allowPunctuation && t.indexOf("&") === -1) {
// fast path: any punctuation that is not potentially an HTML entity
return /\w/.test(t);
}

if (options.allowHtmlEntities && t.indexOf("&") !== -1) {
invalid = t.split("&")
.filter((entity) => entity !== "")
.some((entity) => /^&(?:#[0-9]+|[a-zA-Z]+);$/.test(`&${entity}`) !== true);
}
// split the text into HTML entities and everything else so we can test each part of the string individually
const parts = t.split(htmlEntityRegex).filter((entity) => entity !== "");

return parts.some((entity) => {
if (options.allowHtmlEntities && htmlEntityRegex.test(entity)) {
return false;
}

if (options.allowPunctuation) {
return /\w/.test(entity);
}

return invalid;
return true;
});
}
66 changes: 66 additions & 0 deletions test/rules/jsx-use-translation-function/allow-both/test.tsx.lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<div>Hello world!</div>
~~~~~~~~~~~~ [0]

<div>{'Hello world!'}</div>
~~~~~~~~~~~~~~~~ [0]

<div>{translate('hello-world')}</div>

<input placeholder={translate('name')} />
<input placeholder="Name" />
~~~~~~ [1]

<input placeholder={translate('name')} />
<input placeholder={"Name"} />
~~~~~~~~ [1]

<div>
<div>{translate('hi')}</div>
</div>

<div>
<span>{translate('this')}</span>is bad<span>
~~~~~~ [0]
</div>

<div>{`foo`}</div>
~~~~~~~ [0]

<div>{`foo ${1}`}</div>

<ul>
<li>{translate('one')}</li>
Two
~~~
<li>Three</li>
~~~~ [0]
~~~~~ [0]
</ul>

<div> - </div>

<div> & </div>

<div>:&nbsp;</div>

<div>:&nbsp</div>
~~~~~~ [0]

<div>{' - '}</div>

<input placeholder="-" />

<div>&nbsp;</div>

<div>{'&nbsp;'}</div>

<input placeholder="&nbsp;" />

<div>hello - world</div>
~~~~~~~~~~~~~ [0]

<div>hello&nbsp;world</div>
~~~~~~~~~~~~~~~~ [0]

[0]: String literals are disallowed as JSX. Use a translation function
[1]: String literal is not allowed for value of placeholder. Use a translation function
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"rules": {
"jsx-use-translation-function": {
"options": ["allow-punctuation", "allow-htmlentities"]
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,17 @@

<div>&nbsp;</div>

<div>&nbsp;&nbsp;</div>

<div>{'&nbsp;'}</div>

<input placeholder="&nbsp;" />

<div>hello - world</div>
~~~~~~~~~~~~~ [0]

<div>hello&nbsp;world</div>
~~~~~~~~~~~~~~~~ [0]

[0]: String literals are disallowed as JSX. Use a translation function
[1]: String literal is not allowed for value of placeholder. Use a translation function
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@

<div> - </div>

<div> & </div>

<div>:&nbsp;</div>
~~~~~~~ [0]

<div>{' - '}</div>

<input placeholder="-" />
Expand All @@ -52,5 +57,11 @@
<input placeholder="&nbsp;" />
~~~~~~~~ [1]

<div>hello - world</div>
~~~~~~~~~~~~~ [0]

<div>hello&nbsp;world</div>
~~~~~~~~~~~~~~~~ [0]

[0]: String literals are disallowed as JSX. Use a translation function
[1]: String literal is not allowed for value of placeholder. Use a translation function