Skip to content

Commit fadd6dd

Browse files
authored
refactor(ja-space-between-half-and-full-width): replace match-index with String.prototype.matchAll (#76)
* refactor(ja-space-between-half-and-full-width): replace match-index with String.prototype.matchAll * deps(ja-space-between-half-and-full-width): remove match-index
1 parent 2b2f0d7 commit fadd6dd

File tree

2 files changed

+14
-16
lines changed

2 files changed

+14
-16
lines changed

packages/textlint-rule-ja-space-between-half-and-full-width/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
},
3434
"dependencies": {
3535
"@textlint/regexp-string-matcher": "^2.0.2",
36-
"match-index": "^1.0.1",
3736
"textlint-rule-helper": "^2.2.4"
3837
}
3938
}

packages/textlint-rule-ja-space-between-half-and-full-width/src/index.js

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ const assert = require("assert");
55
全角文字と半角文字の間にスペースを入れるかどうか
66
*/
77
import { RuleHelper } from "textlint-rule-helper";
8-
import { matchCaptureGroupAll } from "match-index";
98
import { matchPatterns } from "@textlint/regexp-string-matcher";
109

1110
const PunctuationRegExp = /[]/;
@@ -71,7 +70,7 @@ function reporter(context, options = {}) {
7170
/**
7271
* `text`を対象に例外オプションを取り除くfilter関数を返す
7372
* @param {string} text テスト対象のテキスト全体
74-
* @param {number} padding +1 or -1
73+
* @param {number} padding 例外文字までのオフセット
7574
* @returns {function(*, *)}
7675
*/
7776
const createFilter = (text, padding) => {
@@ -101,20 +100,20 @@ function reporter(context, options = {}) {
101100
};
102101
// Never: アルファベットと全角の間はスペースを入れない
103102
const noSpaceBetween = (node, text) => {
104-
const betweenHanAndZen = matchCaptureGroupAll(text, new RegExp(`[A-Za-z0-9]([  ])(?:${ZenRegExpStr})`));
105-
const betweenZenAndHan = matchCaptureGroupAll(text, new RegExp(`(?:${ZenRegExpStr})([  ])[A-Za-z0-9]`));
103+
const betweenHanAndZen = text.matchAll(new RegExp(`[A-Za-z0-9]([  ])(?:${ZenRegExpStr})`, "g"));
104+
const betweenZenAndHan = text.matchAll(new RegExp(`(?:${ZenRegExpStr})([  ])[A-Za-z0-9]`, "g"));
106105
const reportMatch = (match) => {
107-
const { index } = match;
106+
const indexOneBased = match.index + 1;
108107
report(
109108
node,
110109
new RuleError("原則として、全角文字と半角文字の間にスペースを入れません。", {
111-
index: match.index,
112-
fix: fixer.replaceTextRange([index, index + 1], "")
110+
index: indexOneBased,
111+
fix: fixer.replaceTextRange([indexOneBased, indexOneBased + 1], "")
113112
})
114113
);
115114
};
116-
betweenHanAndZen.filter(createFilter(text, 1)).forEach(reportMatch);
117-
betweenZenAndHan.filter(createFilter(text, -1)).forEach(reportMatch);
115+
Array.from(betweenHanAndZen).filter(createFilter(text, 2)).forEach(reportMatch);
116+
Array.from(betweenZenAndHan).filter(createFilter(text, 0)).forEach(reportMatch);
118117
};
119118

120119
// Always: アルファベットと全角の間はスペースを入れる
@@ -136,27 +135,27 @@ function reporter(context, options = {}) {
136135
expStr = `(${ZenRegExpStr})[${alphabets}${numbers}]`;
137136
}
138137

139-
return new RegExp(expStr);
138+
return new RegExp(expStr, "g");
140139
};
141140

142141
const betweenHanAndZenRegExp = generateRegExp(options);
143142
const betweenZenAndHanRegExp = generateRegExp(options, false);
144143
const errorMsg = "原則として、全角文字と半角文字の間にスペースを入れます。";
145144

146-
const betweenHanAndZen = matchCaptureGroupAll(text, betweenHanAndZenRegExp);
147-
const betweenZenAndHan = matchCaptureGroupAll(text, betweenZenAndHanRegExp);
145+
const betweenHanAndZen = text.matchAll(betweenHanAndZenRegExp);
146+
const betweenZenAndHan = text.matchAll(betweenZenAndHanRegExp);
148147
const reportMatch = (match) => {
149148
const { index } = match;
150149
report(
151150
node,
152151
new RuleError(errorMsg, {
153-
index: match.index,
152+
index: index,
154153
fix: fixer.replaceTextRange([index + 1, index + 1], " ")
155154
})
156155
);
157156
};
158-
betweenHanAndZen.filter(createFilter(text, 1)).forEach(reportMatch);
159-
betweenZenAndHan.filter(createFilter(text, 0)).forEach(reportMatch);
157+
Array.from(betweenHanAndZen).filter(createFilter(text, 1)).forEach(reportMatch);
158+
Array.from(betweenZenAndHan).filter(createFilter(text, 0)).forEach(reportMatch);
160159
};
161160
return {
162161
[Syntax.Str](node) {

0 commit comments

Comments
 (0)