-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rule): add require-translation-ja
- Loading branch information
Showing
4 changed files
with
98 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
"use strict"; | ||
module.exports = { | ||
rules: { | ||
"no-raw-value": require("./rules/no-raw-value") | ||
"no-raw-value": require("./rules/no-raw-value"), | ||
"require-translation": require("./rules/require-translation") | ||
}, | ||
rulesConfig: { | ||
"simple-i18n-text/no-raw-value": "on" | ||
"simple-i18n-text/no-raw-value": "on", | ||
"simple-i18n-text/require-translation": "on" | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
"use strict"; | ||
const isStringLiteral = (node) => { | ||
return node.type === "Literal" && typeof node.value === "string"; | ||
}; | ||
export const japaneseRegExp = /(?:[々〇〻\u3400-\u4DBF\u4E00-\u9FFF\uF900-\uFAFF]|[\uD840-\uD87F][\uDC00-\uDFFF]|[ぁ-んァ-ヶ])/; | ||
/** | ||
* 日本語のみを対象にした no-raw-value | ||
* Check raw string literal in JSXElement, but allow non-japanese characters. | ||
*/ | ||
module.exports = function(context) { | ||
return { | ||
JSXElement(node){ | ||
if (!node.children) { | ||
return; | ||
} | ||
const children = node.children; | ||
children.filter(isStringLiteral).forEach(literal => { | ||
const value = literal.value.trim(); | ||
if (!value || value.length === 0) { | ||
return; | ||
} | ||
if (!japaneseRegExp.test(value)) { | ||
return; | ||
} | ||
const message = `"${value}"を t() または pt() で囲んでください。`; | ||
context.report({node: literal, message: message}); | ||
}); | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
"use strict"; | ||
const RuleTester = require("eslint").RuleTester; | ||
const tester = new RuleTester(); | ||
import rule from "../src/rules/require-translation-ja"; | ||
const parserOptions = { | ||
ecmaVersion: 6, | ||
ecmaFeatures: { | ||
jsx: true | ||
} | ||
}; | ||
tester.run("require-translation-ja", rule, { | ||
valid: [ | ||
{code: `<p>{"OK"}</p>`, parserOptions}, | ||
{code: `<p>{t("OK")}</p>`, parserOptions}, | ||
{code: `<p>{true}</p>`, parserOptions}, | ||
{code: `<p key="ok"></p>`, parserOptions}, | ||
{code: `const str = "OK"; <p>{ok}</p>`, parserOptions}, | ||
{code: `<p>English is OK</p>`, parserOptions}, | ||
{code: `<p>▼</p>`, parserOptions}, | ||
{code: `<p>123</p>`, parserOptions}, | ||
], | ||
invalid: [ | ||
{ | ||
code: `<p>日本語</p>`, | ||
errors: [`"日本語"を t() または pt() で囲んでください。`], | ||
parserOptions | ||
}, { | ||
code: ` | ||
class DetailButton extends React.Component { | ||
render() { | ||
return <div className="DetailButton"> | ||
<button>ボタン</button> | ||
</div>; | ||
} | ||
}`, | ||
errors: [`"ボタン"を t() または pt() で囲んでください。`], | ||
parserOptions | ||
} | ||
] | ||
}); |