Skip to content

Commit

Permalink
fix: support multiple words
Browse files Browse the repository at this point in the history
  • Loading branch information
azu committed Oct 22, 2021
1 parent 1b7392c commit fc563e8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/textlint-rule-no-duplicate-abbr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,15 @@ const reporter: TextlintRuleReporter<Options> = (context, options) => {
const source = getSource(node);
const words = source.split(/\b/);
words.forEach((word, index) => {
const noSuffix = words[index + 1] == undefined;
if (noSuffix) {
return;
}
// Skip the word
if (allowAbbrList.includes(word)) {
return;
}
const hasSpace = /\s/.test(words[index + 1]);
const hasSpace = /^\s+$/.test(words[index + 1]);
const nextWord = hasSpace ? words[index + 2] : words[index + 1];
if (!nextWord) {
return;
Expand All @@ -33,8 +37,13 @@ const reporter: TextlintRuleReporter<Options> = (context, options) => {
if (!matchData) {
return;
}
// ABC プロトコル
// ^^^|^^^^^^^^^
// [0] [1]
// [1] to be " プロトコル"
const normalizedNextWord = nextWord.trimStart();
const useDuplicatedSuffixWord = matchData.suffixes.find((suffixWord) => {
return nextWord.startsWith(suffixWord);
return normalizedNextWord.startsWith(suffixWord);
});
if (useDuplicatedSuffixWord) {
const spacer = hasSpace ? words[index + 1] : "";
Expand Down
19 changes: 19 additions & 0 deletions test/textlint-rule-no-duplicate-abbr.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import TextLintTester from "textlint-tester";
import rule from "../src/textlint-rule-no-duplicate-abbr";

const tester = new TextLintTester();
// @ts-ignore
tester.run("textlint-rule-no-duplicate-abbr", rule, {
valid: ["BGP is Border Gateway Protocol.", "npm module is installed by Node package manager"],
invalid: [
Expand Down Expand Up @@ -49,6 +50,24 @@ tester.run("textlint-rule-no-duplicate-abbr", rule, {
index: 3
}
]
},
// multiple
{
text: "TCP protocol. UDP protocol. BGP プロトコル",
errors: [
{
message: `"TCP protocol" has duplicated suffix word. "TCP" stands for "Transmission Control Protocol".`,
index: 0
},
{
message: `"UDP protocol" has duplicated suffix word. "UDP" stands for "User Datagram Protocol".`,
index: 14
},
{
message: `"BGP プロトコル" has duplicated suffix word. "BGP" stands for "Border Gateway Protocol".`,
index: 28
}
]
}
]
});

0 comments on commit fc563e8

Please sign in to comment.