From fadd6dd3bfc5f14e79fd21a6e8a4f3418fa61da8 Mon Sep 17 00:00:00 2001 From: piyo Date: Sat, 27 Apr 2024 16:26:23 +0900 Subject: [PATCH] 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 --- .../package.json | 1 - .../src/index.js | 29 +++++++++---------- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/packages/textlint-rule-ja-space-between-half-and-full-width/package.json b/packages/textlint-rule-ja-space-between-half-and-full-width/package.json index 77a8c2a..bc39677 100644 --- a/packages/textlint-rule-ja-space-between-half-and-full-width/package.json +++ b/packages/textlint-rule-ja-space-between-half-and-full-width/package.json @@ -33,7 +33,6 @@ }, "dependencies": { "@textlint/regexp-string-matcher": "^2.0.2", - "match-index": "^1.0.1", "textlint-rule-helper": "^2.2.4" } } diff --git a/packages/textlint-rule-ja-space-between-half-and-full-width/src/index.js b/packages/textlint-rule-ja-space-between-half-and-full-width/src/index.js index 066c24d..dbcec5a 100644 --- a/packages/textlint-rule-ja-space-between-half-and-full-width/src/index.js +++ b/packages/textlint-rule-ja-space-between-half-and-full-width/src/index.js @@ -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 = /[。、]/; @@ -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) => { @@ -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: アルファベットと全角の間はスペースを入れる @@ -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) {