@@ -10,12 +10,25 @@ Halfwidth Katakana variants(半角片仮名)
1010http://www.asahi-net.or.jp/~ax2s-kmtn/ref/unicode/uff00.html
1111 */
1212import { isUserWrittenNode } from "./util/node-util" ;
13+ import { hanKarakanaRegExp } from "./util/regexp" ;
1314import prh from "textlint-rule-prh" ;
1415import path from "path" ;
15- export default function ( context ) {
16- let { Syntax, RuleError, report, getSource} = context ;
16+ import { matchCaptureGroupAll } from "./util/match-index" ;
17+ import moji from "moji" ;
18+ /**
19+ * 半角カタカナを全角カタカナに変換
20+ *
21+ * @param {String } str 変換したい文字列
22+ */
23+ function toZenkaku ( string ) {
24+ return moji ( string ) . convert ( 'HK' , 'ZK' ) . toString ( ) ;
25+ }
26+
27+
28+ function reporter ( context ) {
29+ let { Syntax, fixer, report, getSource} = context ;
1730 // 辞書ベースのカタカタ表記のチェックを行う
18- let dictRule = prh ( context , {
31+ let dictRule = prh . fixer ( context , {
1932 rulePaths : [ path . join ( __dirname , ".." , "dict" , "2.1.5.yml" ) ]
2033 } ) ;
2134 let originalStrRule = dictRule [ Syntax . Str ] ;
@@ -25,12 +38,21 @@ export default function (context) {
2538 if ( ! isUserWrittenNode ( node , context ) ) {
2639 return ;
2740 }
28- let text = getSource ( node ) ;
29- let matchReg = / [ \uFF65 - \uFF9F ] / ;
30- let index = text . search ( matchReg ) ;
31- if ( index !== - 1 ) {
32- report ( node , new RuleError ( "カタカナは「全角」で表記します。" , index ) ) ;
33- }
41+ const text = getSource ( node ) ;
42+ const matches = matchCaptureGroupAll ( text , / ( [ \uFF65 - \uFF9F ] + ) / g) ;
43+ matches . forEach ( match => {
44+ const { index, text} = match ;
45+ report ( node , {
46+ message : "カタカナは「全角」で表記します。" ,
47+ column : index ,
48+ fix : fixer . replaceTextRange ( [ index , index + text . length ] , toZenkaku ( text ) )
49+ } ) ;
50+
51+ } ) ;
3452 } ;
3553 return dictRule ;
54+ }
55+ export default {
56+ linter : reporter ,
57+ fixer : reporter
3658}
0 commit comments