Skip to content

Commit 95d9f7e

Browse files
committed
implement 2.1.2.漢字
1 parent 45461dd commit 95d9f7e

File tree

4 files changed

+72
-1
lines changed

4 files changed

+72
-1
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
},
4343
"dependencies": {
4444
"analyze-desumasu-dearu": "^2.0.0",
45+
"sorted-joyo-kanji": "^0.1.0",
4546
"textlint-rule-helper": "^1.1.3",
4647
"textlint-rule-prh": "^2.0.0"
4748
}

src/2.1.2.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// LICENSE : MIT
2+
"use strict";
3+
/*
4+
2.1.2.漢字
5+
漢字は「全角」で表記します。漢字の使用は、平成 22 年 11 月 30 日内閣告示第 2 号の「常用漢字表」に原則として準じます。
6+
ただし、「常用漢字表」にない漢字であっても実務翻訳で慣用的に用いられる語には漢字を使います。
7+
*/
8+
import {isUserWrittenNode} from "./util/node-util";
9+
import {isJoyo} from "sorted-joyo-kanji";
10+
11+
// http://qiita.com/YusukeHirao/items/2f0fb8d5bbb981101be0
12+
function stringToArray (value) {
13+
return value.match(/[\uD800-\uDBFF][\uDC00-\uDFFF]|[^\uD800-\uDFFF]/g) || [];
14+
}
15+
16+
// http://tama-san.com/kanji-regex/
17+
const kanjiReg = /(?:[\u3400-\u9FFF\uF900-\uFAFF]|[\uD840-\uD87F][\uDC00-\uDFFF])/;
18+
19+
export default function (context) {
20+
let {Syntax, RuleError, report, getSource} = context;
21+
return {
22+
[Syntax.Str](node){
23+
if (!isUserWrittenNode(node, context)) {
24+
return;
25+
}
26+
let text = getSource(node);
27+
let strArray = stringToArray(text);
28+
for(let index = 0; index < strArray.length; index++) {
29+
let item = strArray[index];
30+
if(kanjiReg.test(item) && !isJoyo(item)) {
31+
report(node, new RuleError("「" + item + "」は、「常用漢字表」外の漢字です。", index));
32+
}
33+
}
34+
}
35+
}
36+
}

src/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export default {
1010
"1.2.1.句点(。)と読点(、)": require("./1.2.1"),
1111
"1.2.2.ピリオド(.)とカンマ(,)": require("./1.2.2"),
1212
// TODO: 2.1.1.ひらがな
13-
// TODO: 2.1.2.漢字
13+
"2.1.2.漢字": require("./2.1.2"),
1414
// TODO: 2.1.3.漢字の送りがな
1515
// TODO: 2.1.4.複合語の送りがな
1616
"2.1.5.カタカナ": require("./2.1.5"),
@@ -53,6 +53,7 @@ export default {
5353
"1.1.5.図表のキャプション": true,
5454
"1.2.1.句点(。)と読点(、)": true,
5555
"1.2.2.ピリオド(.)とカンマ(,)": true,
56+
"2.1.2.漢字": true,
5657
"2.1.5.カタカナ": true,
5758
"2.1.6.カタカナの長音": true,
5859
"2.1.8.算用数字": true,

test/2.1.2-test.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// LICENSE : MIT
2+
"use strict";
3+
import TextLintTester from "textlint-tester";
4+
import rule from "../src/2.1.2";
5+
var tester = new TextLintTester();
6+
tester.run("2.1.2.漢字", rule, {
7+
valid: [
8+
"今日は日本語の勉強をします。"
9+
],
10+
invalid: [
11+
{
12+
text: "文章を推敲する",
13+
errors: [
14+
{
15+
message: "「敲」は、「常用漢字表」外の漢字です。",
16+
line: 1,
17+
column: 5
18+
}
19+
]
20+
},
21+
{
22+
text: "私は聡明でありたい",
23+
errors: [
24+
{
25+
message: "「聡」は、「常用漢字表」外の漢字です。",
26+
line: 1,
27+
column: 3
28+
}
29+
]
30+
}
31+
32+
]
33+
});

0 commit comments

Comments
 (0)