66 */
77import { isUserWrittenNode } from "./util/node-util" ;
88import { matchCaptureGroupAll } from "match-index" ;
9+ import { japaneseRegExp } from "./util/regexp" ;
910
1011const brackets = [ "\\(" , "\\)" , "\\[" , "\\]" , "(" , ")" , "[" , "]" , "「" , "」" , "『" , "』" ] ;
1112const leftBrackets = brackets . map ( ( bracket ) => {
@@ -14,13 +15,18 @@ const leftBrackets = brackets.map((bracket) => {
1415const rightBrackets = brackets . map ( ( bracket ) => {
1516 return new RegExp ( bracket + "([ ])" , "g" ) ;
1617} ) ;
18+ const leftHalfParentheses = new RegExp ( `${ japaneseRegExp . source } (\\()` , "g" ) ;
19+ const rightHalfParentheses = new RegExp ( `(\\))${ japaneseRegExp . source } ` , "g" ) ;
1720const defaultOptions = {
18- allowOutsideHalfParentheses : true
21+ allowOutsideHalfParentheses : true ,
22+ requireOutsideHalfParentheses : false
1923} ;
2024function reporter ( context , options ) {
2125 let { Syntax, RuleError, report, fixer, getSource } = context ;
2226 const allowOutsideHalfParentheses =
2327 options . allowOutsideHalfParentheses ?? defaultOptions . allowOutsideHalfParentheses ;
28+ const requireOutsideHalfParentheses =
29+ options . requireOutsideHalfParentheses ?? defaultOptions . requireOutsideHalfParentheses ;
2430 return {
2531 [ Syntax . Str ] ( node ) {
2632 if ( ! isUserWrittenNode ( node , context ) ) {
@@ -59,6 +65,30 @@ function reporter(context, options) {
5965 ) ;
6066 } ) ;
6167 } ) ;
68+ if ( requireOutsideHalfParentheses ) {
69+ // 左にスペース必須
70+ matchCaptureGroupAll ( text , leftHalfParentheses ) . forEach ( ( match ) => {
71+ const { index } = match ;
72+ report (
73+ node ,
74+ new RuleError ( "半角かっこの外側に半角スペースが必要です。" , {
75+ index,
76+ fix : fixer . replaceTextRange ( [ index , index + 1 ] , " " + match . text )
77+ } )
78+ ) ;
79+ } ) ;
80+ // 右にスペース必須
81+ matchCaptureGroupAll ( text , rightHalfParentheses ) . forEach ( ( match ) => {
82+ const { index } = match ;
83+ report (
84+ node ,
85+ new RuleError ( "半角かっこの外側に半角スペースが必要です。" , {
86+ index,
87+ fix : fixer . replaceTextRange ( [ index , index + 1 ] , match . text + " " )
88+ } )
89+ ) ;
90+ } ) ;
91+ }
6292 }
6393 } ;
6494}
0 commit comments