Skip to content

Commit

Permalink
feat(2.2.2): fixer support
Browse files Browse the repository at this point in the history
  • Loading branch information
azu committed Mar 2, 2016
1 parent 63372c8 commit 8124850
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 16 deletions.
38 changes: 23 additions & 15 deletions src/2.2.2.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,49 +104,51 @@ function _num2ja(num, opt) {
return result;
}

// 2.2.2. 算用数字と漢数字の使い分け
export default function (context) {
let {Syntax, RuleError, report, getSource} = context;
function reporter(context) {
let {Syntax, RuleError, report, fixer, getSource} = context;
return {
[Syntax.Str](node){
if (!isUserWrittenNode(node, context)) {
return;
}
let text = getSource(node);
const text = getSource(node);
// 漢数字 -> 算用数字
let toNumber = (text, pattern, match) => {
let matchedString = match[0];
let index = match.index;
var expected = matchedString.replace(pattern, function (all, match) {
const toNumber = (text, pattern, match) => {
const matchedString = match[0];
const index = match.index;
const expected = matchedString.replace(pattern, function (all, match) {
let result = 0;
match.split("").forEach(kanNumber => {
result += numberMap[kanNumber];
});
return all.replace(match, result);
});
var ruleError = new RuleError(`${matchedString} => ${expected}
const ruleError = new RuleError(`${matchedString} => ${expected}
数量を表現し、数を数えられるものは算用数字を使用します。任意の数に置き換えても通用する語句がこれに該当します。`, {
column: index
column: index,
fix: fixer.replaceTextRange([index, index + matchedString.length], expected)
});
report(node, ruleError);
};


// 算用数字 -> 漢数字

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

// ignorePatternにマッチしたらmatchFnを呼ばないようにする(エラーを無視する)
let ignoreWhenMatched = (ignorePattern, matchFn) => {
const ignoreWhenMatched = (ignorePattern, matchFn) => {
return (text, pattern, match) => {
if (ignorePattern.test(text)) {
return null;
Expand Down Expand Up @@ -185,3 +187,9 @@ export default function (context) {
}
}
}

// 2.2.2. 算用数字と漢数字の使い分け
export default {
linter: reporter,
fixer: reporter
}
13 changes: 12 additions & 1 deletion test/2.2.2-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ tester.run("2.2.2.算用数字と漢数字の使い分け", rule, {
invalid: [
{
text: "一億百十万人",
output: "1億百十万人",
errors: [
{
message: `一億 => 1億
Expand All @@ -24,6 +25,7 @@ tester.run("2.2.2.算用数字と漢数字の使い分け", rule, {
},
{
text: "百八つのボタン",
output: "108つのボタン",
errors: [
{
message: `百八つ => 108つ
Expand All @@ -33,6 +35,7 @@ tester.run("2.2.2.算用数字と漢数字の使い分け", rule, {
},
{
text: "第三回大会",
output: "第3回大会",
errors: [
{
message: `三回 => 3回
Expand All @@ -42,6 +45,7 @@ tester.run("2.2.2.算用数字と漢数字の使い分け", rule, {
},
{
text: "サンフランシスコマラソン第三回大会",
output: "サンフランシスコマラソン第3回大会",
errors: [
{
message: `三回 => 3回
Expand Down Expand Up @@ -77,6 +81,7 @@ tester.run("2.2.2.算用数字と漢数字の使い分け", rule, {
invalid: [
{
text: "これは世界1",
output: "これは世界一",
errors: [
{
message: `世界1 => 世界一
Expand All @@ -86,6 +91,7 @@ tester.run("2.2.2.算用数字と漢数字の使い分け", rule, {
},
{
text: "1部の文章",
output: "一部の文章",
errors: [
{
message: `1部の => 一部の
Expand All @@ -95,6 +101,7 @@ tester.run("2.2.2.算用数字と漢数字の使い分け", rule, {
},
{
text: "朝1番に",
output: "朝一番に",
errors: [
{
message: `1番に => 一番に
Expand All @@ -104,6 +111,7 @@ tester.run("2.2.2.算用数字と漢数字の使い分け", rule, {
},
{
text: "数100倍",
output: "数百倍",
errors: [
{
message: `数100倍 => 数百倍
Expand All @@ -113,6 +121,7 @@ tester.run("2.2.2.算用数字と漢数字の使い分け", rule, {
},
{
text: "数10億",
output: "数十億",
errors: [
{
message: `数10億 => 数十億
Expand All @@ -124,17 +133,19 @@ tester.run("2.2.2.算用数字と漢数字の使い分け", rule, {
},
{
text: "しばしば数10万行以上に",
output: "しばしば数十万行以上に",
errors: [
{
message: `数10万 => 数十万
慣用的表現、熟語、概数、固有名詞、副詞など、漢数字を使用することが一般的な語句では漢数字を使います。`,
line: 1,
column: 1
column: 5
}
]
},
{
text: "数10年に一度の奇跡",
output: "数十年に一度の奇跡",
errors: [
{
message: `数10年 => 数十年
Expand Down

0 comments on commit 8124850

Please sign in to comment.