Skip to content

Commit 8124850

Browse files
committed
feat(2.2.2): fixer support
1 parent 63372c8 commit 8124850

File tree

2 files changed

+35
-16
lines changed

2 files changed

+35
-16
lines changed

src/2.2.2.js

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -104,49 +104,51 @@ function _num2ja(num, opt) {
104104
return result;
105105
}
106106

107-
// 2.2.2. 算用数字と漢数字の使い分け
108-
export default function (context) {
109-
let {Syntax, RuleError, report, getSource} = context;
107+
function reporter(context) {
108+
let {Syntax, RuleError, report, fixer, getSource} = context;
110109
return {
111110
[Syntax.Str](node){
112111
if (!isUserWrittenNode(node, context)) {
113112
return;
114113
}
115-
let text = getSource(node);
114+
const text = getSource(node);
116115
// 漢数字 -> 算用数字
117-
let toNumber = (text, pattern, match) => {
118-
let matchedString = match[0];
119-
let index = match.index;
120-
var expected = matchedString.replace(pattern, function (all, match) {
116+
const toNumber = (text, pattern, match) => {
117+
const matchedString = match[0];
118+
const index = match.index;
119+
const expected = matchedString.replace(pattern, function (all, match) {
121120
let result = 0;
122121
match.split("").forEach(kanNumber => {
123122
result += numberMap[kanNumber];
124123
});
125124
return all.replace(match, result);
126125
});
127-
var ruleError = new RuleError(`${matchedString} => ${expected}
126+
const ruleError = new RuleError(`${matchedString} => ${expected}
128127
数量を表現し、数を数えられるものは算用数字を使用します。任意の数に置き換えても通用する語句がこれに該当します。`, {
129-
column: index
128+
column: index,
129+
fix: fixer.replaceTextRange([index, index + matchedString.length], expected)
130130
});
131131
report(node, ruleError);
132132
};
133133

134134

135135
// 算用数字 -> 漢数字
136136

137-
let toKanNumber = (text, pattern, match) => {
138-
var matchedString = match[0];
139-
var expected = matchedString.replace(pattern, function (all, match) {
137+
const toKanNumber = (text, pattern, match) => {
138+
const matchedString = match[0];
139+
const expected = matchedString.replace(pattern, function (all, match) {
140140
return all.replace(match, _num2ja(match, {'with_arabic': false}));
141141
});
142+
const index = match.index;
142143
report(node, new RuleError(`${matchedString} => ${expected}
143144
慣用的表現、熟語、概数、固有名詞、副詞など、漢数字を使用することが一般的な語句では漢数字を使います。`, {
144-
column: matchedString.index
145+
column: index,
146+
fix: fixer.replaceTextRange([index, index + matchedString.length], expected)
145147
}));
146148
};
147149

148150
// ignorePatternにマッチしたらmatchFnを呼ばないようにする(エラーを無視する)
149-
let ignoreWhenMatched = (ignorePattern, matchFn) => {
151+
const ignoreWhenMatched = (ignorePattern, matchFn) => {
150152
return (text, pattern, match) => {
151153
if (ignorePattern.test(text)) {
152154
return null;
@@ -185,3 +187,9 @@ export default function (context) {
185187
}
186188
}
187189
}
190+
191+
// 2.2.2. 算用数字と漢数字の使い分け
192+
export default {
193+
linter: reporter,
194+
fixer: reporter
195+
}

test/2.2.2-test.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ tester.run("2.2.2.算用数字と漢数字の使い分け", rule, {
1313
invalid: [
1414
{
1515
text: "一億百十万人",
16+
output: "1億百十万人",
1617
errors: [
1718
{
1819
message: `一億 => 1億
@@ -24,6 +25,7 @@ tester.run("2.2.2.算用数字と漢数字の使い分け", rule, {
2425
},
2526
{
2627
text: "百八つのボタン",
28+
output: "108つのボタン",
2729
errors: [
2830
{
2931
message: `百八つ => 108つ
@@ -33,6 +35,7 @@ tester.run("2.2.2.算用数字と漢数字の使い分け", rule, {
3335
},
3436
{
3537
text: "第三回大会",
38+
output: "第3回大会",
3639
errors: [
3740
{
3841
message: `三回 => 3回
@@ -42,6 +45,7 @@ tester.run("2.2.2.算用数字と漢数字の使い分け", rule, {
4245
},
4346
{
4447
text: "サンフランシスコマラソン第三回大会",
48+
output: "サンフランシスコマラソン第3回大会",
4549
errors: [
4650
{
4751
message: `三回 => 3回
@@ -77,6 +81,7 @@ tester.run("2.2.2.算用数字と漢数字の使い分け", rule, {
7781
invalid: [
7882
{
7983
text: "これは世界1",
84+
output: "これは世界一",
8085
errors: [
8186
{
8287
message: `世界1 => 世界一
@@ -86,6 +91,7 @@ tester.run("2.2.2.算用数字と漢数字の使い分け", rule, {
8691
},
8792
{
8893
text: "1部の文章",
94+
output: "一部の文章",
8995
errors: [
9096
{
9197
message: `1部の => 一部の
@@ -95,6 +101,7 @@ tester.run("2.2.2.算用数字と漢数字の使い分け", rule, {
95101
},
96102
{
97103
text: "朝1番に",
104+
output: "朝一番に",
98105
errors: [
99106
{
100107
message: `1番に => 一番に
@@ -104,6 +111,7 @@ tester.run("2.2.2.算用数字と漢数字の使い分け", rule, {
104111
},
105112
{
106113
text: "数100倍",
114+
output: "数百倍",
107115
errors: [
108116
{
109117
message: `数100倍 => 数百倍
@@ -113,6 +121,7 @@ tester.run("2.2.2.算用数字と漢数字の使い分け", rule, {
113121
},
114122
{
115123
text: "数10億",
124+
output: "数十億",
116125
errors: [
117126
{
118127
message: `数10億 => 数十億
@@ -124,17 +133,19 @@ tester.run("2.2.2.算用数字と漢数字の使い分け", rule, {
124133
},
125134
{
126135
text: "しばしば数10万行以上に",
136+
output: "しばしば数十万行以上に",
127137
errors: [
128138
{
129139
message: `数10万 => 数十万
130140
慣用的表現、熟語、概数、固有名詞、副詞など、漢数字を使用することが一般的な語句では漢数字を使います。`,
131141
line: 1,
132-
column: 1
142+
column: 5
133143
}
134144
]
135145
},
136146
{
137147
text: "数10年に一度の奇跡",
148+
output: "数十年に一度の奇跡",
138149
errors: [
139150
{
140151
message: `数10年 => 数十年

0 commit comments

Comments
 (0)