Skip to content

Commit 4e83968

Browse files
committed
chore(lint): add prettier
1 parent 4d99aee commit 4e83968

File tree

3 files changed

+92
-56
lines changed

3 files changed

+92
-56
lines changed

package.json

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,38 @@
2525
"test": "textlint-scripts test",
2626
"build": "textlint-scripts build",
2727
"watch": "textlint-scripts build --watch",
28-
"prepublish": "npm run --if-present build"
28+
"prepublish": "npm run --if-present build",
29+
"prettier": "prettier --write \"**/*.{js,jsx,ts,tsx,css}\""
2930
},
3031
"keywords": [
3132
"textlint",
3233
"textlintrule"
3334
],
3435
"devDependencies": {
36+
"husky": "^1.1.2",
37+
"lint-staged": "^7.3.0",
38+
"prettier": "^1.8.1",
3539
"textlint-plugin-review": "^0.3.3",
3640
"textlint-scripts": "^2.1.0"
3741
},
3842
"dependencies": {
3943
"check-ends-with-period": "^1.0.1",
4044
"textlint-rule-helper": "^2.0.0"
45+
},
46+
"prettier": {
47+
"singleQuote": false,
48+
"printWidth": 120,
49+
"tabWidth": 4
50+
},
51+
"husky": {
52+
"hooks": {
53+
"precommit": "lint-staged"
54+
}
55+
},
56+
"lint-staged": {
57+
"*.{js,jsx,ts,tsx,css}": [
58+
"prettier --write",
59+
"git add"
60+
]
4161
}
42-
}
62+
}

src/textlint-rule-ja-no-mixed-period.js

Lines changed: 40 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,11 @@ const reporter = (context, options = {}) => {
3434
const preferPeriodMark = options.periodMark || defaultOptions.periodMark;
3535
// 優先する句点記号は常に句点として許可される
3636
const allowPeriodMarks = (options.allowPeriodMarks || defaultOptions.allowPeriodMarks).concat(preferPeriodMark);
37-
const allowEmojiAtEnd = options.allowEmojiAtEnd !== undefined
38-
? options.allowEmojiAtEnd
39-
: defaultOptions.allowEmojiAtEnd;
40-
const forceAppendPeriod = options.forceAppendPeriod !== undefined
41-
? options.forceAppendPeriod
42-
: defaultOptions.forceAppendPeriod;
43-
const checkFootnote = options.checkFootnote !== undefined
44-
? options.checkFootnote
45-
: defaultOptions.checkFootnote;
37+
const allowEmojiAtEnd =
38+
options.allowEmojiAtEnd !== undefined ? options.allowEmojiAtEnd : defaultOptions.allowEmojiAtEnd;
39+
const forceAppendPeriod =
40+
options.forceAppendPeriod !== undefined ? options.forceAppendPeriod : defaultOptions.forceAppendPeriod;
41+
const checkFootnote = options.checkFootnote !== undefined ? options.checkFootnote : defaultOptions.checkFootnote;
4642
// 脚注のNode Typeを定義(TxtASTの定義外)
4743
const FootnoteNodes = [
4844
// https://github.com/orangain/textlint-plugin-review
@@ -52,7 +48,12 @@ const reporter = (context, options = {}) => {
5248
"Definition"
5349
];
5450
const ignoredNodeTypes = [
55-
Syntax.ListItem, Syntax.Link, Syntax.Code, Syntax.Image, Syntax.BlockQuote, Syntax.Emphasis
51+
Syntax.ListItem,
52+
Syntax.Link,
53+
Syntax.Code,
54+
Syntax.Image,
55+
Syntax.BlockQuote,
56+
Syntax.Emphasis
5657
].concat(checkFootnote ? FootnoteNodes : []);
5758
return {
5859
[Syntax.Paragraph](node) {
@@ -81,35 +82,47 @@ const reporter = (context, options = {}) => {
8182
}
8283
// 文末がスペースである場合はスペースを削除する
8384
if (/\s/.test(periodMark)) {
84-
report(lastNode, new RuleError(`文末が"${preferPeriodMark}"で終わっていません。末尾に不要なスペースがあります。`, {
85-
index,
86-
fix: fixer.replaceTextRange([index, index + periodMark.length], "")
87-
}));
88-
return
85+
report(
86+
lastNode,
87+
new RuleError(`文末が"${preferPeriodMark}"で終わっていません。末尾に不要なスペースがあります。`, {
88+
index,
89+
fix: fixer.replaceTextRange([index, index + periodMark.length], "")
90+
})
91+
);
92+
return;
8993
}
9094
// 典型的なパターンは自動的に`preferPeriodMark`に置き換える
9195
// 例) "." であるなら "。"に変換
9296
if (classicPeriodMarkPattern.test(periodMark)) {
93-
report(lastNode, new RuleError(`文末が"${preferPeriodMark}"で終わっていません。`, {
94-
index: index,
95-
fix: fixer.replaceTextRange([index, index + preferPeriodMark.length], preferPeriodMark)
96-
}));
97+
report(
98+
lastNode,
99+
new RuleError(`文末が"${preferPeriodMark}"で終わっていません。`, {
100+
index: index,
101+
fix: fixer.replaceTextRange([index, index + preferPeriodMark.length], preferPeriodMark)
102+
})
103+
);
97104
} else {
98105
// 句点を忘れているパターン
99106
if (forceAppendPeriod) {
100107
// `forceAppendPeriod`のオプションがtrueならば、自動で句点を追加する。
101-
report(lastNode, new RuleError(`文末が"${preferPeriodMark}"で終わっていません。`, {
102-
index: index,
103-
fix: fixer.replaceTextRange([index + 1, index + 1], preferPeriodMark)
104-
}));
108+
report(
109+
lastNode,
110+
new RuleError(`文末が"${preferPeriodMark}"で終わっていません。`, {
111+
index: index,
112+
fix: fixer.replaceTextRange([index + 1, index + 1], preferPeriodMark)
113+
})
114+
);
105115
} else {
106-
report(lastNode, new RuleError(`文末が"${preferPeriodMark}"で終わっていません。`, {
107-
index: index
108-
}));
116+
report(
117+
lastNode,
118+
new RuleError(`文末が"${preferPeriodMark}"で終わっていません。`, {
119+
index: index
120+
})
121+
);
109122
}
110123
}
111124
}
112-
}
125+
};
113126
};
114127

115128
module.exports = {

test/textlint-rule-ja-no-mixed-period-test.js

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,34 @@ import rule from "../src/textlint-rule-ja-no-mixed-period";
33
const TextLintTester = require("textlint-tester");
44
const reviewPlugin = require("textlint-plugin-review");
55
const tester = new TextLintTester();
6-
tester.run("Re:view + textlint-rule-ja-no-mixed-period", {
7-
plugins: [
8-
{
9-
pluginId: "review",
10-
plugin: reviewPlugin
11-
}
12-
],
13-
rules: [
14-
{
15-
ruleId: "ja-no-mixed-period",
16-
rule: rule,
17-
options: {
18-
checkFootnote: true
6+
tester.run(
7+
"Re:view + textlint-rule-ja-no-mixed-period",
8+
{
9+
plugins: [
10+
{
11+
pluginId: "review",
12+
plugin: reviewPlugin
1913
}
20-
},
21-
]
22-
}, {
23-
valid: [
24-
{
25-
text: `//footnote[test][脚注はデフォルトで無視される]`,
26-
ext: ".re"
27-
}
28-
]
29-
});
14+
],
15+
rules: [
16+
{
17+
ruleId: "ja-no-mixed-period",
18+
rule: rule,
19+
options: {
20+
checkFootnote: true
21+
}
22+
}
23+
]
24+
},
25+
{
26+
valid: [
27+
{
28+
text: `//footnote[test][脚注はデフォルトで無視される]`,
29+
ext: ".re"
30+
}
31+
]
32+
}
33+
);
3034

3135
tester.run("textlint-rule-ja-no-mixed-period", rule, {
3236
valid: [
@@ -49,13 +53,13 @@ tester.run("textlint-rule-ja-no-mixed-period", rule, {
4953
text: "絵文字が末尾にある。😆",
5054
options: {
5155
allowEmojiAtEnd: true
52-
},
56+
}
5357
},
5458
{
5559
text: "これはOK",
5660
options: {
5761
allowPeriodMarks: ["OK"]
58-
},
62+
}
5963
},
6064
{
6165
text: `次のコード:
@@ -65,7 +69,7 @@ tester.run("textlint-rule-ja-no-mixed-period", rule, {
6569
`,
6670
options: {
6771
allowPeriodMarks: [":"]
68-
},
72+
}
6973
},
7074
// 脚注はMarkdownでは常に無視される
7175
{
@@ -74,7 +78,6 @@ tester.run("textlint-rule-ja-no-mixed-period", rule, {
7478
[^1]: 脚注はデフォルトで無視される`,
7579
ext: ".md"
7680
}
77-
7881
],
7982
invalid: [
8083
// single match

0 commit comments

Comments
 (0)