Skip to content

Commit d127267

Browse files
committed
refactor(4.2.6): fix to base global regexp
1 parent 5c3d8e3 commit d127267

File tree

7 files changed

+44
-33
lines changed

7 files changed

+44
-33
lines changed

src/1.2.1.js

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import regx from 'regx';
44
import {japaneseRegExp} from "./util/regexp";
55
import {matchCaptureGroupAll} from "./util/match-index";
6+
import mergeMatches from "./util/merge-matches";
67
const rx = regx("g");
78
/*
89
1.2.1. 句点(。)と読点(、)
@@ -27,23 +28,6 @@ const replaceSymbol = {
2728
",": "、"
2829
};
2930

30-
31-
function mergeMatches(...aMatches) {
32-
const results = [];
33-
aMatches.forEach(matches => {
34-
matches.forEach(targetMatch => {
35-
const alreadyHave = results.some(match => {
36-
const {text, index} = match;
37-
return targetMatch.index === index && targetMatch.text === text;
38-
});
39-
if (!alreadyHave) {
40-
results.push(targetMatch);
41-
}
42-
});
43-
});
44-
return results;
45-
}
46-
4731
const reporter = (context) => {
4832
let {Syntax, report, fixer, getSource} = context;
4933
return {

src/4.2.4.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
*/
1111
import {isUserWrittenNode} from "./util/node-util";
1212
import {matchCaptureGroupAll} from "./util/match-index";
13+
import regx from 'regx';
14+
import {japaneseRegExp} from "./util/regexp";
15+
const rx = regx("g");
1316
function reporter(context) {
1417
let {Syntax, RuleError, report, fixer, getSource} = context;
1518
return {
@@ -19,7 +22,7 @@ function reporter(context) {
1922
}
2023
const text = getSource(node);
2124
// 和文で半角の・は利用しない
22-
const matchHanNakaguro = /()/g;
25+
const matchHanNakaguro = rx`(?:${japaneseRegExp}|[a-zA-Z])(・)(?:${japaneseRegExp}|[a-zA-Z])`;
2326
matchCaptureGroupAll(text, matchHanNakaguro).forEach(match => {
2427
const {index} = match;
2528
report(node, new RuleError("カタカナ複合語を区切る場合または同格の語句を並列する場合には全角の中黒(・)を使用します。", {

src/4.2.5.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function reporter(context) {
1515
return;
1616
}
1717
const text = getSource(node);
18-
// 和文で半角の?は利用しない
18+
// 数値の区切りに半角の~は利用しない
1919
const matchHanQuestion = /\d(~)\d/g;
2020
matchCaptureGroupAll(text, matchHanQuestion).forEach(match => {
2121
const {index} = match;

src/4.2.6.js

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
例外は、住所や電話番号の区切りに使う場合です。
88
*/
99
import {isUserWrittenNode} from "./util/node-util";
10+
import {matchCaptureGroupAll} from "./util/match-index";
11+
import regx from 'regx';
12+
import {japaneseRegExp} from "./util/regexp";
13+
import mergeMatches from "./util/merge-matches";
14+
const rx = regx("g");
1015
export default function (context) {
1116
let {Syntax, RuleError, report, getSource} = context;
1217
return {
@@ -15,18 +20,19 @@ export default function (context) {
1520
return;
1621
}
1722
let text = getSource(node);
18-
// 基本的にハイフン(-)を使用しません
19-
var index = text.indexOf("-");
20-
if (index !== -1) {
21-
// 例外として、住所や電話番号の区切りにはハイフン(-)を使用できる
22-
// ?-? を取り出して \d-\d ならOK、そうでないならダメ
23-
var bufOfIndex = text.slice(index - 1, index + 2);
24-
// [^A-Za-z0-9_] = \w
25-
if (!/^\w\-\w$/.test(bufOfIndex)) {
26-
report(node, new RuleError(`原則として和文ではハイフン(-)を使用しません。
27-
例外は、住所や電話番号の区切りに使う場合です。`, index));
28-
}
29-
}
23+
// 和文ではハイフン(-)を使用しません
24+
// right
25+
const rightMatches = matchCaptureGroupAll(text, rx`${japaneseRegExp}(\-)`);
26+
// left
27+
const leftMatches = matchCaptureGroupAll(text, rx`(\-)${japaneseRegExp}`);
28+
const matches = mergeMatches(leftMatches, rightMatches);
29+
matches.forEach(match => {
30+
const {index} = match;
31+
report(node, new RuleError(`原則として和文ではハイフン(-)を使用しません。
32+
例外は、住所や電話番号の区切りに使う場合です。`, {
33+
column: index
34+
}))
35+
});
3036
}
3137
};
3238
}

src/util/match-index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"use strict";
33
const flagsGetter = require('regexp.prototype.flags');
44
const assert = require("assert");
5+
56
/**
67
* @typedef {Object} MatchCaptureGroup
78
* @property {string} text - text is matched texts

src/util/merge-matches.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// LICENSE : MIT
2+
"use strict";
3+
export default function mergeMatches(...aMatches) {
4+
const results = [];
5+
aMatches.forEach(matches => {
6+
matches.forEach(targetMatch => {
7+
const alreadyHave = results.some(match => {
8+
const {text, index} = match;
9+
return targetMatch.index === index && targetMatch.text === text;
10+
});
11+
if (!alreadyHave) {
12+
results.push(targetMatch);
13+
}
14+
});
15+
});
16+
return results;
17+
}

test/4.2.6-test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ tester.run("4.2.6.ハイフン(-)", rule, {
2121
]
2222
},
2323
{
24-
text: "090 - 1234 - 5678",
24+
text: "A市-B市",
2525
errors: [
2626
{
2727
message: "原則として和文ではハイフン(-)を使用しません。\n例外は、住所や電話番号の区切りに使う場合です。",
28-
column: 5
28+
column: 3
2929
}
3030
]
3131
}

0 commit comments

Comments
 (0)