Skip to content

Commit

Permalink
feat(rule): add require-translation-ja
Browse files Browse the repository at this point in the history
  • Loading branch information
azu committed Jul 12, 2016
1 parent 5fbe23c commit 08f5a7d
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 3 deletions.
25 changes: 24 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Add "simple-i18n-text" to your `.eslintrc`
"simple-i18n-text"
],
"rules":{
"simple-i18n-text/no-raw-value": 2
"simple-i18n-text/require-translation-ja":
}
}
```
Expand All @@ -54,6 +54,29 @@ Disallow to string literal in JSXElement.
<p>{pt("OK")}</p>
```

### [src/rules/require-translation-ja.js](src/rules/require-translation-ja.js)

日本語のみを対象にした `no-raw-value` のルール。

Check raw string literal in JSXElement, but allow non-japanese characters.

**NG**:
```jsx
// NG
<p>日本語はダメ</p>
```

**OK**:
```Jsx
<p>OK</p>
<p>123</p>
<p>!?</p>
<p></p>
<p>{"OK"}</p>
<p>{t("OK")}</p>
<p>{pt("OK")}</p>
```
## Changelog
See [Releases page](https://github.com/azu/eslint-plugin-simple-i18n-text/releases).
Expand Down
6 changes: 4 additions & 2 deletions src/index.js
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"
}
};
30 changes: 30 additions & 0 deletions src/rules/require-translation-ja.js
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});
});
}
}
};
40 changes: 40 additions & 0 deletions test/require-translation-ja-test.js
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
}
]
});

0 comments on commit 08f5a7d

Please sign in to comment.