Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(ja-space-between-half-and-full-width): replace match-index with String.prototype.matchAll #76

Merged
merged 2 commits into from
Apr 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
},
"dependencies": {
"@textlint/regexp-string-matcher": "^2.0.2",
"match-index": "^1.0.1",
"textlint-rule-helper": "^2.2.4"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const assert = require("assert");
全角文字と半角文字の間にスペースを入れるかどうか
*/
import { RuleHelper } from "textlint-rule-helper";
import { matchCaptureGroupAll } from "match-index";
import { matchPatterns } from "@textlint/regexp-string-matcher";

const PunctuationRegExp = /[。、]/;
Expand Down Expand Up @@ -71,7 +70,7 @@ function reporter(context, options = {}) {
/**
* `text`を対象に例外オプションを取り除くfilter関数を返す
* @param {string} text テスト対象のテキスト全体
* @param {number} padding +1 or -1
* @param {number} padding 例外文字までのオフセット
* @returns {function(*, *)}
*/
const createFilter = (text, padding) => {
Expand Down Expand Up @@ -101,20 +100,20 @@ function reporter(context, options = {}) {
};
// Never: アルファベットと全角の間はスペースを入れない
const noSpaceBetween = (node, text) => {
const betweenHanAndZen = matchCaptureGroupAll(text, new RegExp(`[A-Za-z0-9]([  ])(?:${ZenRegExpStr})`));
const betweenZenAndHan = matchCaptureGroupAll(text, new RegExp(`(?:${ZenRegExpStr})([  ])[A-Za-z0-9]`));
const betweenHanAndZen = text.matchAll(new RegExp(`[A-Za-z0-9]([  ])(?:${ZenRegExpStr})`, "g"));
const betweenZenAndHan = text.matchAll(new RegExp(`(?:${ZenRegExpStr})([  ])[A-Za-z0-9]`, "g"));
const reportMatch = (match) => {
const { index } = match;
const indexOneBased = match.index + 1;
report(
node,
new RuleError("原則として、全角文字と半角文字の間にスペースを入れません。", {
index: match.index,
fix: fixer.replaceTextRange([index, index + 1], "")
index: indexOneBased,
fix: fixer.replaceTextRange([indexOneBased, indexOneBased + 1], "")
})
);
};
betweenHanAndZen.filter(createFilter(text, 1)).forEach(reportMatch);
betweenZenAndHan.filter(createFilter(text, -1)).forEach(reportMatch);
Array.from(betweenHanAndZen).filter(createFilter(text, 2)).forEach(reportMatch);
Array.from(betweenZenAndHan).filter(createFilter(text, 0)).forEach(reportMatch);
};

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

return new RegExp(expStr);
return new RegExp(expStr, "g");
};

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

const betweenHanAndZen = matchCaptureGroupAll(text, betweenHanAndZenRegExp);
const betweenZenAndHan = matchCaptureGroupAll(text, betweenZenAndHanRegExp);
const betweenHanAndZen = text.matchAll(betweenHanAndZenRegExp);
const betweenZenAndHan = text.matchAll(betweenZenAndHanRegExp);
const reportMatch = (match) => {
const { index } = match;
report(
node,
new RuleError(errorMsg, {
index: match.index,
index: index,
fix: fixer.replaceTextRange([index + 1, index + 1], " ")
})
);
};
betweenHanAndZen.filter(createFilter(text, 1)).forEach(reportMatch);
betweenZenAndHan.filter(createFilter(text, 0)).forEach(reportMatch);
Array.from(betweenHanAndZen).filter(createFilter(text, 1)).forEach(reportMatch);
Array.from(betweenZenAndHan).filter(createFilter(text, 0)).forEach(reportMatch);
};
return {
[Syntax.Str](node) {
Expand Down
Loading