Skip to content

Commit

Permalink
Merge pull request #2 from textlint-ja/feature/1
Browse files Browse the repository at this point in the history
feat: add allowNumber options
  • Loading branch information
azu authored Jun 20, 2020
2 parents ac4d7dd + 01e6748 commit b70055e
Show file tree
Hide file tree
Showing 7 changed files with 1,709 additions and 1,795 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ textlint --rule @textlint-ja/no-synonyms README.md
* Default: true
*/
allowAlphabet?: boolean;
/**
* 同じ語形の語の中での漢数字と数字の表記ゆれを許可するかどうか
* trueの場合は漢数字と数字の表記ゆれを許可します
* 例) 「1」と「一」
* Default: true
*/
allowNumber?: boolean;
}
```

Expand All @@ -73,7 +80,8 @@ textlint --rule @textlint-ja/no-synonyms README.md
"rules": {
"@textlint-ja/no-synonyms": {
"allows": ["ウェブアプリ", "ウェブアプリケーション"],
"allowAlphabet": false
"allowAlphabet": false,
"allowNumber": false
}
}
}
Expand Down
18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,16 @@
"tiny-segmenter": "^0.2.0"
},
"devDependencies": {
"@textlint/types": "^1.2.3",
"@types/node": "^12.12.12",
"husky": "^3.1.0",
"lint-staged": "^9.4.3",
"prettier": "^1.19.1",
"sudachi-synonyms-dictionary": "^4.0.1",
"@textlint/types": "^1.3.1",
"@types/node": "^14.0.13",
"husky": "^4.2.5",
"lint-staged": "^10.2.11",
"prettier": "^2.0.5",
"sudachi-synonyms-dictionary": "^5.0.1",
"textlint-scripts": "^3.0.0",
"textlint-tester": "^5.1.11",
"ts-node": "^8.5.2",
"typescript": "^3.7.2"
"textlint-tester": "^5.1.15",
"ts-node": "^8.10.2",
"typescript": "^3.9.5"
},
"peerDependencies": {
"sudachi-synonyms-dictionary": ">=4.0.1"
Expand Down
9 changes: 7 additions & 2 deletions src/create-index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { fetchDictionary, SudachiSynonyms } from "sudachi-synonyms-dictionary";
import { isNumberString } from "./is-number";

export type Midashi = string;

Expand All @@ -18,7 +19,7 @@ export class ItemGroup {

}

usedItems(usedItemSet: Set<SudachiSynonyms>, { allowAlphabet, allows }: { allowAlphabet: boolean, allows: string[] }): SudachiSynonyms[] {
usedItems(usedItemSet: Set<SudachiSynonyms>, { allowAlphabet, allowNumber, allows }: { allowAlphabet: boolean; allowNumber: boolean; allows: string[] }): SudachiSynonyms[] {
// sort by used
return Array.from(usedItemSet.values()).filter(item => {
if (allowAlphabet && (item.hyoukiYure === "アルファベット表記" || item.ryakusyou === "略語・略称/アルファベット")) {
Expand All @@ -28,6 +29,10 @@ export class ItemGroup {
// OS <-> オペレーションシステム
return false;
}
// 数値の違いは無視する
if (allowNumber && isNumberString(item.midashi)) {
return false;
}
if (allows.includes(item.midashi)) {
return false;
}
Expand All @@ -49,7 +54,7 @@ const assertInstallationSudachiSynonymsDictionary = () => {
$ npm install sudachi-synonyms-dictionary
`)
`);
}
};
export type IndexType = { keyItemGroupMap: Map<Midashi, ItemGroup[]>; SudachiSynonymsItemGroup: Map<SudachiSynonyms, ItemGroup>; };
Expand Down
4 changes: 4 additions & 0 deletions src/is-number.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const kanNumbers = ["〇", "一", "二", "三", "四", "五", "六", "七", "八", "九"];
export const isNumberString = (str: string): boolean => {
return /\d/.test(str) || kanNumbers.includes(str);
};
18 changes: 14 additions & 4 deletions src/textlint-rule-no-synonyms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,25 @@ export interface Options {
* Default: true
*/
allowAlphabet?: boolean;
/**
* 同じ語形の語の中での漢数字と数字の表記ゆれを許可するかどうか
* trueの場合は漢数字と数字の表記ゆれを許可します
* 例) 「1」と「一」
* Default: true
*/
allowNumber?: boolean;
}


export const DefaultOptions: Required<Options> = {
allows: [],
allowAlphabet: true
allowAlphabet: true,
allowNumber: true
};

const report: TextlintRuleReporter<Options> = (context, options = {}) => {
const allowAlphabet = options.allowAlphabet !== undefined ? options.allowAlphabet : DefaultOptions.allowAlphabet;
const allowAlphabet = options.allowAlphabet ?? DefaultOptions.allowAlphabet;
const allowNumber = options.allowNumber ?? DefaultOptions.allowNumber;
const allows = options.allows !== undefined ? options.allows : DefaultOptions.allows;
const { Syntax, getSource, RuleError } = context;
const usedSudachiSynonyms: Set<SudachiSynonyms> = new Set();
Expand Down Expand Up @@ -75,14 +84,15 @@ const report: TextlintRuleReporter<Options> = (context, options = {}) => {
for (const itemGroup of usedItemGroup.values()) {
const items = itemGroup.usedItems(usedSudachiSynonyms, {
allows,
allowAlphabet
allowAlphabet,
allowNumber
});
if (items.length >= 2) {
const 同義の見出しList = items.map(item => item.midashi);
// select last used
const matchSegment = locationMap.get(items[items.length - 1]);
const index = matchSegment ? matchSegment.index : 0;
const message = `同義語である「${同義の見出しList.join("」「")}」が利用されています`;
const message = `同義語である「${同義の見出しList.join("」「")}」が利用されています`;
report(node, new RuleError(message, {
index
}));
Expand Down
18 changes: 15 additions & 3 deletions test/textlint-rule-no-synonyms.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ tester.run("textlint-rule-no-synonyms", rule, {
// allowAlphabet: true
// item.hyoukiYure === "アルファベット表記"
"blogはブログです",
// allowNumber: true
"1は数字の一種です",
// item.ryakusyou === "略語・略称/アルファベット"
"「データベース」「DB」",
// allow links by default
Expand All @@ -27,13 +29,13 @@ tester.run("textlint-rule-no-synonyms", rule, {
invalid: [{
text: "サーバとサーバーの表記揺れがある",
errors: [{
message: "同義語である「サーバ」「サーバー」が利用されています",
message: "同義語である「サーバ」「サーバー」が利用されています",
index: 4
}]
}, {
text: "この雇入と雇入れの違いは難しい問題だ",
errors: [{
message: "同義語である「雇入」「雇入れ」が利用されています",
message: "同義語である「雇入」「雇入れ」が利用されています",
index: 5
}]
},
Expand All @@ -43,7 +45,17 @@ tester.run("textlint-rule-no-synonyms", rule, {
allowAlphabet: false
},
errors: [{
message: "同義語である「blog」「ブログ」が利用されています",
message: "同義語である「blog」と「ブログ」が利用されています",
index: 5
}]
},
{
text: "1は数字の一種です",
options: {
allowNumber: false
},
errors: [{
message: "同義語である「1」と「一」が利用されています",
index: 5
}]
}]
Expand Down
Loading

0 comments on commit b70055e

Please sign in to comment.