Skip to content

Commit 96532ed

Browse files
committed
style: prettierを追加
1 parent de06a63 commit 96532ed

File tree

4 files changed

+56
-28
lines changed

4 files changed

+56
-28
lines changed

package.json

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
"build": "textlint-scripts build",
2626
"watch": "textlint-scripts build --watch",
2727
"prepublish": "npm run --if-present build",
28-
"test": "textlint-scripts test"
28+
"test": "textlint-scripts test",
29+
"prettier": "prettier --write \"**/*.{js,jsx,ts,tsx,css}\""
2930
},
3031
"keywords": [
3132
"rule",
@@ -35,6 +36,9 @@
3536
"devDependencies": {
3637
"@textlint/types": "^1.2.3",
3738
"@types/node": "^12.12.14",
39+
"husky": "^3.1.0",
40+
"lint-staged": "^9.5.0",
41+
"prettier": "^1.19.1",
3842
"textlint-scripts": "^3.0.0",
3943
"ts-node": "^8.5.4",
4044
"typescript": "^3.7.3"
@@ -44,5 +48,21 @@
4448
"sentence-splitter": "^3.2.0",
4549
"textlint-rule-helper": "^2.1.1",
4650
"textlint-util-to-string": "^3.0.0"
51+
},
52+
"prettier": {
53+
"singleQuote": false,
54+
"printWidth": 120,
55+
"tabWidth": 4
56+
},
57+
"husky": {
58+
"hooks": {
59+
"precommit": "lint-staged"
60+
}
61+
},
62+
"lint-staged": {
63+
"*.{js,jsx,ts,tsx,css}": [
64+
"prettier --write",
65+
"git add"
66+
]
4767
}
48-
}
68+
}

src/no-doubled-joshi.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ function createSurfaceKeyMap(tokens: KuromojiToken[]): { [index: string]: Kuromo
3636
}
3737

3838
function matchExceptionRule(tokens: KuromojiToken[]) {
39-
let token = tokens[0];
39+
const token = tokens[0];
4040
// "の" の重なりは例外
4141
if (token.pos_detail_1 === "連体化") {
4242
return true;
@@ -51,7 +51,7 @@ function matchExceptionRule(tokens: KuromojiToken[]) {
5151
}
5252
// 並立助詞は例外
5353
// 登ったり降りたり
54-
if(tokens.length === 2 && tokens[0].pos_detail_1 === "並立助詞" && tokens[1].pos_detail_1 === "並立助詞"){
54+
if (tokens.length === 2 && tokens[0].pos_detail_1 === "並立助詞" && tokens[1].pos_detail_1 === "並立助詞") {
5555
return true;
5656
}
5757
return false;
@@ -113,7 +113,7 @@ export interface Options {
113113
114114
TODO: need abstraction
115115
*/
116-
const report: TextlintRuleModule<Options> = function (context, options = {}) {
116+
const report: TextlintRuleModule<Options> = function(context, options = {}) {
117117
const helper = new RuleHelper(context);
118118
// 最低間隔値
119119
const minInterval = options.min_interval !== undefined ? options.min_interval : defaultOptions.min_interval;
@@ -124,7 +124,7 @@ const report: TextlintRuleModule<Options> = function (context, options = {}) {
124124
const allow = options.allow || defaultOptions.allow;
125125
const separatorCharacters = options.separatorCharacters || defaultOptions.separatorCharacters;
126126
const commaCharacters = options.commaCharacters || defaultOptions.commaCharacters;
127-
const {Syntax, report, RuleError} = context;
127+
const { Syntax, report, RuleError } = context;
128128
const is読点Token = create読点Matcher(commaCharacters);
129129
return {
130130
[Syntax.Paragraph](node) {
@@ -183,7 +183,7 @@ const report: TextlintRuleModule<Options> = function (context, options = {}) {
183183
}
184184
}
185185
if (tokens.length <= 1) {
186-
return;// no duplicated token
186+
return; // no duplicated token
187187
}
188188
// if found differenceIndex less than
189189
// tokes are sorted ascending order
@@ -195,17 +195,23 @@ const report: TextlintRuleModule<Options> = function (context, options = {}) {
195195
if (differenceIndex <= minInterval) {
196196
// padding positionを計算する
197197
const originalIndex = sentenceSource.originalIndexFromIndex(current.word_position - 1);
198-
report(sentence, new RuleError(`一文に二回以上利用されている助詞 "${joshiName}" がみつかりました。`, {
199-
index: originalIndex
200-
}));
198+
report(
199+
sentence,
200+
new RuleError(
201+
`一文に二回以上利用されている助詞 "${joshiName}" がみつかりました。`,
202+
{
203+
index: originalIndex
204+
}
205+
)
206+
);
201207
}
202208
return current;
203209
});
204210
});
205211
};
206-
sentences.forEach(node => checkSentence(node))
212+
sentences.forEach(node => checkSentence(node));
207213
});
208214
}
209-
}
215+
};
210216
};
211217
export default report;

src/token-utils.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@ export const is助詞Token = (token: KuromojiToken) => {
1616
*/
1717
export const create読点Matcher = (commaCharacters: string[]) => {
1818
return function is読点Token(token: KuromojiToken) {
19-
return commaCharacters.includes(token.surface_form) && (
19+
return (
20+
commaCharacters.includes(token.surface_form) &&
2021
// 、や, は名詞扱い
21-
token.pos === "名詞" ||
22-
// ,は記号 && 読点となる(surface_formを優先するために pos_detail_1/読点 のチェックを省く)
23-
token.pos === "記号"
22+
(token.pos === "名詞" ||
23+
// ,は記号 && 読点となる(surface_formを優先するために pos_detail_1/読点 のチェックを省く)
24+
token.pos === "記号")
2425
);
25-
}
26+
};
2627
};
2728
/**
2829
* aTokenの_extraKeyに結合したkeyを追加する
@@ -43,7 +44,7 @@ const concatToken = (aToken: KuromojiToken, bToken: KuromojiToken) => {
4344
*/
4445
export const concatJoishiTokens = (tokens: KuromojiToken[]) => {
4546
const newTokens: KuromojiToken[] = [];
46-
tokens.forEach((token) => {
47+
tokens.forEach(token => {
4748
const prevToken = newTokens[newTokens.length - 1];
4849
if (is助詞Token(token) && is助詞Token(prevToken)) {
4950
newTokens[newTokens.length - 1] = concatToken(prevToken, token);

test/no-doubled-joshi-test.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,18 @@ tester.run("no-double-joshi", rule, {
2727
// "プロパティを削除しようとするとエラーが発生します。",
2828
{
2929
text: "太字も強調も同じように無視されます。",
30-
options: {allow: ["も"]}
30+
options: { allow: ["も"] }
3131
},
3232
// 区切り文字をカスタムする
3333
// ♪を区切り文字としたので、次の文は2つのセンテンスになる
3434
{
3535
text: "これはペンです♪これは鉛筆です♪",
36-
options: {separatorCharacters: ["♪"]},
36+
options: { separatorCharacters: ["♪"] }
3737
},
3838
// ,を読点とみなす
3939
{
4040
text: "これがiPhone,これがAndroidです。",
41-
options: {commaCharacters: [","]},
41+
options: { commaCharacters: [","] }
4242
}
4343
],
4444
invalid: [
@@ -77,7 +77,7 @@ tester.run("no-double-joshi", rule, {
7777
{
7878
text: "洋服をドラム式洗濯機でお湯と洗剤で洗い、乾燥機で素早く乾燥させる。",
7979
options: {
80-
"min_interval": 2
80+
min_interval: 2
8181
},
8282
errors: [
8383
{
@@ -119,7 +119,7 @@ tester.run("no-double-joshi", rule, {
119119
{
120120
text: "白装束で重力のない足どりでやってくる",
121121
options: {
122-
"min_interval": 2
122+
min_interval: 2
123123
},
124124
errors: [
125125
{
@@ -151,7 +151,8 @@ tester.run("no-double-joshi", rule, {
151151
column: 16
152152
}
153153
]
154-
}, {
154+
},
155+
{
155156
text: "これとあれとそれを持ってきて。",
156157
errors: [
157158
{
@@ -160,7 +161,8 @@ tester.run("no-double-joshi", rule, {
160161
column: 6
161162
}
162163
]
163-
}, {
164+
},
165+
{
164166
text: `この行にはtextlintによる警告は出ない。
165167
この行にはtextlintにより警告が発せられる。この行に何かしようとすると起きるという
166168
この行にはtextlintによる警告は出ない。
@@ -205,7 +207,7 @@ tester.run("no-double-joshi", rule, {
205207
// 次のtextは1つのセンテンスとして認識されるので、"は"が重複する
206208
{
207209
text: "これはペンです.これは鉛筆です.",
208-
options: {separatorCharacters: ["。"]},
210+
options: { separatorCharacters: ["。"] },
209211
errors: [
210212
{
211213
message: `一文に二回以上利用されている助詞 "は" がみつかりました。`,
@@ -216,10 +218,9 @@ tester.run("no-double-joshi", rule, {
216218
// 、を読点と認識させなくする
217219
{
218220
text: "これがiPhone、これがAndroidです。",
219-
options: {commaCharacters: []},
221+
options: { commaCharacters: [] },
220222
errors: [
221223
{
222-
223224
message: `一文に二回以上利用されている助詞 "が" がみつかりました。`,
224225
index: 12
225226
}

0 commit comments

Comments
 (0)