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