12
12
文中に感嘆符を使用する場合はスペースを挿入しません。下記を参考にしてください。
13
13
*/
14
14
import { isUserWrittenNode } from "./util/node-util" ;
15
- export default function ( context ) {
16
- let { Syntax, RuleError, report, getSource} = context ;
15
+ import { matchCaptureGroupAll } from "./util/match-index" ;
16
+
17
+ function reporter ( context ) {
18
+ let { Syntax, RuleError, report, fixer, getSource} = context ;
17
19
return {
18
20
[ Syntax . Str ] ( node ) {
19
21
if ( ! isUserWrittenNode ( node , context ) ) {
20
22
return ;
21
23
}
22
24
let text = getSource ( node ) ;
23
25
// 半角の!は利用しない
24
- if ( / ( [ \u3400 - \u4DBF \u4E00 - \u9FFF \uF900 - \uFAFF ] | [ \uD840 - \uD87F ] [ \uDC00 - \uDFFF ] | [ ぁ - ん ァ - ヶ ] ) ! / . test ( text ) ) {
25
- return report ( node , new RuleError ( "感嘆符(!)を使用する場合は「全角」で表記します。" ) )
26
- }
27
- // !の後ろは全角スペース
28
- // 半角スペースではない
29
- if ( / ! [ ^ \n ] / . test ( text ) ) {
30
- return report ( node , new RuleError ( "文末に感嘆符を使用し、後に別の文が続く場合は、直後に全角スペースを挿入します。" ) )
31
- }
26
+ const matchRegExp = / (?: [ \u3400 - \u4DBF \u4E00 - \u9FFF \uF900 - \uFAFF ] | [ \uD840 - \uD87F ] [ \uDC00 - \uDFFF ] | [ ぁ - ん ァ - ヶ ] ) ( ! ) / ;
27
+ matchCaptureGroupAll ( text , matchRegExp ) . forEach ( match => {
28
+ const { index} = match ;
29
+ return report ( node , new RuleError ( "感嘆符(!)を使用する場合は「全角」で表記します。" , {
30
+ column : index ,
31
+ fix : fixer . replaceTextRange ( [ index , index + 1 ] , "!" )
32
+ } ) ) ;
33
+ } ) ;
34
+ // !の後ろは全角スペースが推奨
35
+ // 半角スペースである場合
36
+ const matchAfter = / ! ( ) [ ^ \n ] / ;
37
+ matchCaptureGroupAll ( text , matchAfter ) . forEach ( match => {
38
+ const { index} = match ;
39
+ return report ( node , new RuleError ( "文末に感嘆符を使用し、後に別の文が続く場合は、直後に全角スペースを挿入します。" , {
40
+ column : index ,
41
+ fix : fixer . replaceTextRange ( [ index , index + 1 ] , " " )
42
+ } ) ) ;
43
+ } ) ;
32
44
}
33
45
} ;
46
+ }
47
+ export default {
48
+ linter : reporter ,
49
+ fixer : reporter
34
50
}
0 commit comments