Skip to content

Commit cc63a52

Browse files
authored
feat: make the rule fixable (#4)
1 parent 1a853a1 commit cc63a52

File tree

3 files changed

+44
-4
lines changed

3 files changed

+44
-4
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,18 @@ Via CLI
3636
textlint --rule no-hankaku-kana README.md
3737
```
3838

39+
## Fixable
40+
41+
[![Textlint fixable](https://img.shields.io/badge/textlint-fixable-green.svg?style=social)](https://textlint.github.io/)
42+
43+
`textlint --fix` での自動修正に対応しています。
44+
45+
* NFCで表現できる濁点・半濁点はNFCに修正
46+
* 例:パンダはパンダに修正
47+
* NFCで表現できない濁点・半濁点はNFDに修正
48+
* 例:ア゙[゙](https://ja.wikipedia.org/wiki/%E3%81%82%E3%82%99)に修正
49+
* 例:ツ゚[゚](https://ja.wikipedia.org/wiki/%E3%83%84%E3%82%9C)に修正
50+
3951
## Changelog
4052

4153
See [Releases page](https://github.com/textlint-ja/textlint-rule-no-hankaku-kana/releases).

src/textlint-rule-no-hankaku-kana.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { TextlintRuleModule } from "@textlint/types";
66
const HankakuRegExp = /([\uff61-\uff9f]+)/g;
77
export type Options = {};
88
export const report: TextlintRuleModule<Options> = function reporter(context) {
9-
const { Syntax, RuleError, report, locator, getSource } = context;
9+
const { Syntax, RuleError, fixer, report, locator, getSource } = context;
1010
const helper = new RuleHelper(context);
1111
return {
1212
[Syntax.Str](node) {
@@ -17,12 +17,17 @@ export const report: TextlintRuleModule<Options> = function reporter(context) {
1717
for (const match of nodeText.matchAll(HankakuRegExp)) {
1818
const text = match[0];
1919
const index = match.index ?? 0;
20+
const range = [index, index + text.length] as const;
2021
const ruleError = new RuleError(`Disallow to use 半角カタカナ: "${text}"`, {
21-
padding: locator.range([index, index + text.length])
22+
padding: locator.range(range),
23+
fix: fixer.replaceTextRange(range, text.normalize('NFKC')),
2224
});
2325
report(node, ruleError);
2426
}
2527
}
2628
}
2729
};
28-
export default report;
30+
export default {
31+
linter: report,
32+
fixer: report,
33+
};

test/textlint-rule-no-hankaku-kana-test.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ tester.run("no-hankaku-kana", rule, {
1616
invalid: [
1717
{
1818
text: "カタカナ",
19+
output: "カタカナ",
1920
errors: [
2021
{
2122
message: `Disallow to use 半角カタカナ: "カタカナ"`,
@@ -26,6 +27,7 @@ tester.run("no-hankaku-kana", rule, {
2627
// multiple hit items in a line
2728
{
2829
text: `カタカナはゼンカクとハンカクがある`,
30+
output: `カタカナはゼンカクとハンカクがある`,
2931
errors: [
3032
{
3133
message: `Disallow to use 半角カタカナ: "カタカナ"`,
@@ -42,6 +44,7 @@ tester.run("no-hankaku-kana", rule, {
4244
// multiple match in multiple lines
4345
{
4446
text: "カタカナ\nハンカク",
47+
output: "カタカナ\nハンカク",
4548
errors: [
4649
{
4750
message: `Disallow to use 半角カタカナ: "カタカナ"`,
@@ -71,6 +74,26 @@ tester.run("no-hankaku-kana", rule, {
7174
}
7275
}
7376
]
74-
}
77+
},
78+
{
79+
text: `NFCで表現できる濁点・半濁点はNFCに修正する:パンダ`,
80+
output: `NFCで表現できる濁点・半濁点はNFCに修正する:パンダ`,
81+
errors: [
82+
{
83+
message: `Disallow to use 半角カタカナ: "パンダ"`,
84+
range: [25, 30]
85+
},
86+
]
87+
},
88+
{
89+
text: `NFCで表現できない濁点・半濁点はNFDに修正する:ア\u{ff9e}ツ\u{ff9f}`,
90+
output: `NFCで表現できない濁点・半濁点はNFDに修正する:ア\u{3099}ツ\u{309a}`,
91+
errors: [
92+
{
93+
message: `Disallow to use 半角カタカナ: "ア\u{ff9e}ツ\u{ff9f}"`,
94+
range: [26, 30]
95+
},
96+
]
97+
},
7598
]
7699
});

0 commit comments

Comments
 (0)