Skip to content

Commit 9e4d9ee

Browse files
authored
feat(options): allowオプション追加 (#7)
* allowオプション追加 * requireを使った形式に変更
1 parent 087fa47 commit 9e4d9ee

File tree

5 files changed

+95
-3
lines changed

5 files changed

+95
-3
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,11 @@ textlint --rule ja-no-successive-word README.md
5959
// 制限: オノマトペを判定する方法がないため、同じカタカナの語が連続したものをオノマトペとして扱う
6060
// 例) カクカク、ドキドキ、ビリビリ
6161
// https://ja.wikipedia.org/wiki/%E6%93%AC%E5%A3%B0%E8%AA%9E
62-
allowOnomatopee: true
62+
allowOnomatopee: true,
63+
64+
// 許可する単語
65+
// RegExp-like Stringを使用可能
66+
allow: []
6367
}
6468
}
6569
}
@@ -68,6 +72,10 @@ textlint --rule ja-no-successive-word README.md
6872
-`allowOnomatopee: boolean`
6973
- Default: `true`
7074
- **カクカク**などの[オノマトペ](https://ja.wikipedia.org/wiki/%E6%93%AC%E5%A3%B0%E8%AA%9E)を許可するかのオプションです。
75+
- `allow`: `string[]`
76+
- Default: `[]`
77+
- 許可する単語を指定するオプションです。
78+
- [RegExp-like String](https://github.com/textlint/textlint-filter-rule-allowlist#regexp-like-string)を使用できます。
7179

7280
## Changelog
7381

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"textlint-scripts": "^3.0.0"
3737
},
3838
"dependencies": {
39+
"@textlint/regexp-string-matcher": "^1.1.0",
3940
"kuromojin": "^2.0.0"
4041
}
4142
}

src/textlint-rule-ja-no-successive-word.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
// LICENSE : MIT
22
"use strict";
3+
const matchPatterns = require("@textlint/regexp-string-matcher").matchPatterns;
34
const tokenize = require("kuromojin").tokenize;
45
const DefaultOptions = {
56
// オノマトペを許可する
67
// 制限: オノマトペを判定する方法がないため、同じカタカナの語が連続したものをオノマトペとして扱う
78
// 例) カクカク、ドキドキ、ビリビリ
89
// https://ja.wikipedia.org/wiki/%E6%93%AC%E5%A3%B0%E8%AA%9E
9-
allowOnomatopee: true
10+
allowOnomatopee: true,
11+
12+
// 許可する単語
13+
// RegExp-like Stringを使用可能
14+
allow: []
1015
};
1116

1217
function isOnomatopee(str) {
@@ -16,6 +21,7 @@ function isOnomatopee(str) {
1621
module.exports = function(context, options = {}) {
1722
const allowOnomatopee = options.allowOnomatopee !== undefined ? options.allowOnomatopee
1823
: DefaultOptions.allowOnomatopee;
24+
const allow = options.allow || DefaultOptions.allow;
1925
const { Syntax, RuleError, report, getSource } = context;
2026
return {
2127
[Syntax.Str](node) {
@@ -25,6 +31,9 @@ module.exports = function(context, options = {}) {
2531
const reportIfMatch = (prevToken, nextToken) => {
2632
const prevWord = prevToken.surface_form;
2733
const currentWord = nextToken.surface_form;
34+
if (0 < allow.length && 0 < matchPatterns(currentWord, allow).length) {
35+
return;
36+
}
2837
if (prevWord !== currentWord) {
2938
return;
3039
}

test/textlint-rule-ja-no-successive-word-test.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@ tester.run("ja-no-successive-word", rule, {
99
"すもももももももものうち",
1010
"111回目の問題",
1111
"11,11回目の問題",
12-
"フレームレートが落ちて動作がカクカクしてきた"
12+
"フレームレートが落ちて動作がカクカクしてきた",
13+
{
14+
text: "_人人人人人人_",
15+
options: {
16+
allow: ["人人"]
17+
}
18+
}
1319
],
1420
invalid: [
1521
// single match
@@ -53,6 +59,22 @@ tester.run("ja-no-successive-word", rule, {
5359
column: 17
5460
}
5561
]
62+
},
63+
{
64+
text: "_人人人人人人_",
65+
errors: [
66+
{
67+
message: "\"人人\" が連続して2回使われています。",
68+
line: 1,
69+
column: 4
70+
},
71+
{
72+
message: "\"人人\" が連続して2回使われています。",
73+
line: 1,
74+
column: 6
75+
}
76+
77+
]
5678
}
5779
]
5880
});

yarn.lock

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -949,6 +949,18 @@
949949
resolved "https://registry.yarnpkg.com/@textlint/module-interop/-/module-interop-1.1.4.tgz#d8cb10e5bd1d4d31998c95fb0a52ad8100d6e18a"
950950
integrity sha512-9M3kYG5nBoD2lhp05sqi6fieNU6rBcf+A8Trp+4d8o5uJ4RRsWeRtAQMWM7Tv15onqIITRq7fm3la7xovVB9KA==
951951

952+
"@textlint/regexp-string-matcher@^1.1.0":
953+
version "1.1.0"
954+
resolved "https://registry.yarnpkg.com/@textlint/regexp-string-matcher/-/regexp-string-matcher-1.1.0.tgz#e19975029ce228a214d50c6a7e9dbcbef29ad8cd"
955+
integrity sha512-uTPnE1Dw1j+9clXPn61ZUdtg+WyhbgeXHwCTfBev7quHjeCP9PS8NdRkR6wEgmjuLg+xZlI4r/e1r6Bd0xyusQ==
956+
dependencies:
957+
escape-string-regexp "^1.0.5"
958+
execall "^1.0.0"
959+
lodash.sortby "^4.7.0"
960+
lodash.uniq "^4.5.0"
961+
lodash.uniqwith "^4.5.0"
962+
to-regex "^3.0.2"
963+
952964
"@textlint/text-to-ast@^3.2.4":
953965
version "3.2.4"
954966
resolved "https://registry.yarnpkg.com/@textlint/text-to-ast/-/text-to-ast-3.2.4.tgz#a1ccb1ad2a561d93291c9f73d9c2376ddb701a5b"
@@ -1319,6 +1331,14 @@ cliui@^5.0.0:
13191331
strip-ansi "^5.2.0"
13201332
wrap-ansi "^5.1.0"
13211333

1334+
clone-regexp@^1.0.0:
1335+
version "1.0.1"
1336+
resolved "https://registry.yarnpkg.com/clone-regexp/-/clone-regexp-1.0.1.tgz#051805cd33173375d82118fc0918606da39fd60f"
1337+
integrity sha512-Fcij9IwRW27XedRIJnSOEupS7RVcXtObJXbcUOX93UCLqqOdRpkvzKywOOSizmEK/Is3S/RHX9dLdfo6R1Q1mw==
1338+
dependencies:
1339+
is-regexp "^1.0.0"
1340+
is-supported-regexp-flag "^1.0.0"
1341+
13221342
co@^4.6.0:
13231343
version "4.6.0"
13241344
resolved "https://registry.yarnpkg.com/co/-/co-4.6.0.tgz#6ea6bdf3d853ae54ccb8e47bfa0bf3f9031fb184"
@@ -1600,6 +1620,13 @@ events@^1.1.0:
16001620
resolved "https://registry.yarnpkg.com/events/-/events-1.1.1.tgz#9ebdb7635ad099c70dcc4c2a1f5004288e8bd924"
16011621
integrity sha1-nr23Y1rQmccNzEwqH1AEKI6L2SQ=
16021622

1623+
execall@^1.0.0:
1624+
version "1.0.0"
1625+
resolved "https://registry.yarnpkg.com/execall/-/execall-1.0.0.tgz#73d0904e395b3cab0658b08d09ec25307f29bb73"
1626+
integrity sha1-c9CQTjlbPKsGWLCNCewlMH8pu3M=
1627+
dependencies:
1628+
clone-regexp "^1.0.0"
1629+
16031630
expand-brackets@^2.1.4:
16041631
version "2.1.4"
16051632
resolved "https://registry.yarnpkg.com/expand-brackets/-/expand-brackets-2.1.4.tgz#b77735e315ce30f6b6eff0f83b04151a22449622"
@@ -2122,6 +2149,16 @@ is-regex@^1.0.4, is-regex@^1.1.0:
21222149
dependencies:
21232150
has-symbols "^1.0.1"
21242151

2152+
is-regexp@^1.0.0:
2153+
version "1.0.0"
2154+
resolved "https://registry.yarnpkg.com/is-regexp/-/is-regexp-1.0.0.tgz#fd2d883545c46bac5a633e7b9a09e87fa2cb5069"
2155+
integrity sha1-/S2INUXEa6xaYz57mgnof6LLUGk=
2156+
2157+
is-supported-regexp-flag@^1.0.0:
2158+
version "1.0.1"
2159+
resolved "https://registry.yarnpkg.com/is-supported-regexp-flag/-/is-supported-regexp-flag-1.0.1.tgz#21ee16518d2c1dd3edd3e9a0d57e50207ac364ca"
2160+
integrity sha512-3vcJecUUrpgCqc/ca0aWeNu64UGgxcvO60K/Fkr1N6RSvfGCTU60UKN68JDmKokgba0rFFJs12EnzOQa14ubKQ==
2161+
21252162
is-symbol@^1.0.2:
21262163
version "1.0.3"
21272164
resolved "https://registry.yarnpkg.com/is-symbol/-/is-symbol-1.0.3.tgz#38e1014b9e6329be0de9d24a414fd7441ec61937"
@@ -2323,6 +2360,21 @@ locate-path@^3.0.0:
23232360
p-locate "^3.0.0"
23242361
path-exists "^3.0.0"
23252362

2363+
lodash.sortby@^4.7.0:
2364+
version "4.7.0"
2365+
resolved "https://registry.yarnpkg.com/lodash.sortby/-/lodash.sortby-4.7.0.tgz#edd14c824e2cc9c1e0b0a1b42bb5210516a42438"
2366+
integrity sha1-7dFMgk4sycHgsKG0K7UhBRakJDg=
2367+
2368+
lodash.uniq@^4.5.0:
2369+
version "4.5.0"
2370+
resolved "https://registry.yarnpkg.com/lodash.uniq/-/lodash.uniq-4.5.0.tgz#d0225373aeb652adc1bc82e4945339a842754773"
2371+
integrity sha1-0CJTc662Uq3BvILklFM5qEJ1R3M=
2372+
2373+
lodash.uniqwith@^4.5.0:
2374+
version "4.5.0"
2375+
resolved "https://registry.yarnpkg.com/lodash.uniqwith/-/lodash.uniqwith-4.5.0.tgz#7a0cbf65f43b5928625a9d4d0dc54b18cadc7ef3"
2376+
integrity sha1-egy/ZfQ7WShiWp1NDcVLGMrcfvM=
2377+
23262378
lodash@^4.0.0, lodash@^4.17.14, lodash@^4.17.15, lodash@^4.17.19:
23272379
version "4.17.19"
23282380
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.19.tgz#e48ddedbe30b3321783c5b4301fbd353bc1e4a4b"

0 commit comments

Comments
 (0)