Skip to content

Commit

Permalink
feat: support allows
Browse files Browse the repository at this point in the history
  • Loading branch information
azu committed Oct 17, 2021
1 parent 7d6995b commit 0e49644
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 7 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,31 @@ Via CLI
textlint --rule @textlint-rule/no-duplicate-abbr README.md
```

## Options

```ts
export type Options = {
/**
* A list for ignoring Acronyms or Abbreviations.
* e.g. you can allow "NPO organization" by following setting.
* "allowAbbrList": ["NPO"]
*/
allowAbbrList: string[];
}
```
Example setting:
```json5
{
"rules": {
"@textlint-rule/no-duplicate-abbr": {
"allowAbbrList": ["NPO"] // Allow to use "NPO organization"
}
}
}
```
## Changelog
See [Releases page](https://github.com/textlint-rule/textlint-rule-no-duplicate-abbr/releases).
Expand Down
17 changes: 15 additions & 2 deletions src/textlint-rule-no-duplicate-abbr.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,29 @@
import { TextlintRuleReporter } from "@textlint/types";
import dataset from "./dataset.json";

const reporter: TextlintRuleReporter = (context) => {
export type Options = {
/**
* A list for ignoring Acronyms or Abbreviations.
* e.g. you can allow "NPO organization" by following setting.
* "allowAbbrList": ["NPO"]
*/
allowAbbrList: string[];
};
const reporter: TextlintRuleReporter<Options> = (context, options) => {
const { Syntax, getSource, report, RuleError } = context;
const abbrDataSet = new Map<string, { suffixes: string[]; definition: string }>(
dataset as [string, { suffixes: string[]; definition: string }][]
);
const allowAbbrList = options?.allowAbbrList ?? [];
return {
[Syntax.Str](node) {
const source = getSource(node);
const words = source.split(/\b/);
words.forEach((word, index) => {
// Skip the word
if (allowAbbrList.includes(word)) {
return;
}
const hasSpace = /\s/.test(words[index + 1]);
const nextWord = hasSpace ? words[index + 2] : words[index + 1];
if (!nextWord) {
Expand All @@ -31,7 +44,7 @@ const reporter: TextlintRuleReporter = (context) => {
report(
node,
new RuleError(
`"${word}${spacer}${nextWord}" uses duplicated suffix word. "${word}" stands for "${matchData.definition}".`,
`"${word}${spacer}${nextWord}" has duplicated suffix word. "${word}" stands for "${matchData.definition}".`,
{
index: startIndexOfWord
}
Expand Down
20 changes: 15 additions & 5 deletions test/textlint-rule-no-duplicate-abbr.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
import TextLintTester from "textlint-tester";
import rule from "../src/textlint-rule-no-duplicate-abbr";

const tester = new TextLintTester();
tester.run("textlint-rule-no-duplicate-abbr", rule, {
valid: ["BGP is Border Gateway Protocol."],
valid: ["BGP is Border Gateway Protocol.", "npm module is installed by Node package manager"],
invalid: [
{
text: "NPO organization is duplicated",
errors: [
{
message: `"NPO organization" has duplicated suffix word. "NPO" stands for "Non-Profit Organization".`,
index: 0
}
]
},
{
text: "DAO object is Data Access Object",
errors: [
{
message: `"DAO object" uses duplicated suffix word. "DAO" stands for "Data Access Object".`,
message: `"DAO object" has duplicated suffix word. "DAO" stands for "Data Access Object".`,
index: 0
}
]
Expand All @@ -17,7 +27,7 @@ tester.run("textlint-rule-no-duplicate-abbr", rule, {
text: "This is BGP protocol",
errors: [
{
message: `"BGP protocol" uses duplicated suffix word. "BGP" stands for "Border Gateway Protocol".`,
message: `"BGP protocol" has duplicated suffix word. "BGP" stands for "Border Gateway Protocol".`,
index: 8
}
]
Expand All @@ -26,7 +36,7 @@ tester.run("textlint-rule-no-duplicate-abbr", rule, {
text: "This is BGP protocol",
errors: [
{
message: `"BGP protocol" uses duplicated suffix word. "BGP" stands for "Border Gateway Protocol".`,
message: `"BGP protocol" has duplicated suffix word. "BGP" stands for "Border Gateway Protocol".`,
index: 8
}
]
Expand All @@ -35,7 +45,7 @@ tester.run("textlint-rule-no-duplicate-abbr", rule, {
text: "これはBGPプロトコルです。",
errors: [
{
message: `"BGPプロトコルです。" uses duplicated suffix word. "BGP" stands for "Border Gateway Protocol".`,
message: `"BGPプロトコルです。" has duplicated suffix word. "BGP" stands for "Border Gateway Protocol".`,
index: 3
}
]
Expand Down

0 comments on commit 0e49644

Please sign in to comment.