Skip to content

add textChars parameter to allow search for other non-latin alphabets #12

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use case, you may want to disable this.
* Numbers are ignored (they're the same in every language)
* Trailing whitespace is not allowed (this can be disabled)
* Non-alphanumeric values are ignored (such as `-`)
* Can change what parser considers as alphabet characters. Default is `a-zA-Z`
* Can enable enforcing label, aria-label attributes being translated (`<track label="subtitles" />`)
* Can enable enforcing alt attributes being translated (`<img alt="description" />`)
* Can enable enforcing inputProps being translated (`<Checkbox value="checkedA" inputProps={{ 'aria-label': 'Checkbox A' }} />`)
Expand Down Expand Up @@ -88,7 +89,8 @@ The rules (with their default settings) are listed below.
"ignoreLinks": true,
"enforceLabels": false,
"enforceImageAlts": false,
"enforceInputProps": false
"enforceInputProps": false,
"textChars": "a-zA-Z"
}
],
"@calm/react-intl/missing-attribute": [2,
Expand All @@ -98,7 +100,7 @@ The rules (with their default settings) are listed below.
"requireDescription": false,
"formatDefineMessages": false,
"requireIdAsString": true,
"requireDefaultMessage": true,
"requireDefaultMessage": true
}
],
"@calm/react-intl/missing-values": 2
Expand Down
9 changes: 7 additions & 2 deletions lib/rules/missing-formatted-message.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ module.exports = {
},
"enforceInputProps": {
"type": "boolean"
},
"textChars": {
"type": "string"
}
},
"additionalProperties": false
Expand All @@ -57,6 +60,8 @@ module.exports = {
const enforceImageAlts = getDefault(options.enforceImageAlts, false);
const enforceLabels = getDefault(options.enforceLabels, false);
const enforceInputProps = getDefault(options.enforceInputProps, false);
const textChars = typeof(options.textChars) !== 'undefined' ? options.textChars : "a-zA-Z";
const textCharsRegexp = new RegExp(`[${textChars}]`);

function hasChildText(node) {
if (ignoreLinks && node.openingElement.name.name === 'a') {
Expand All @@ -71,7 +76,7 @@ module.exports = {
childNode = null;
} else {
// Child contains any text
if (child.value && /[a-zA-Z]/.test(child.value)) {
if (child.value && textCharsRegexp.test(child.value)) {
childNode = child;
}
// Child is whitespace only (no newlines)
Expand Down Expand Up @@ -130,7 +135,7 @@ module.exports = {
const errorNode = hasChildText(node);
if (errorNode) {
// String is not just whitespace
if (/[a-zA-Z]/.test(errorNode.value)) {
if (textCharsRegexp.test(errorNode.value)) {
context.report({
node: errorNode,
message: `text may need translation: "${errorNode.value}"`,
Expand Down
13 changes: 12 additions & 1 deletion tests/lib/rules/missing-formatted-message.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ ruleTester.run("missing-formatted-message", rule, {
{
code: `<Checkbox value="checkedA" inputProps={{ 'aria-label': formatMessage(messages.checkboxLabel) }} />`,
options: [{ enforceInputProps: true, enforceImageAlts: true, }],
},
{
code: "<span>Русский текст</span>",
}
],

Expand Down Expand Up @@ -176,6 +179,14 @@ ruleTester.run("missing-formatted-message", rule, {
type: 'Property',
}
]
}
},
{
code: "<span>Русский текст</span>",
options: [{ textChars: "a-zA-Zа-яА-Я"}],
errors: [{
message: 'text may need translation: "Русский текст"',
type: 'Literal',
}]
},
]
});