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,17 @@ 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 = {
1821 allowOutsideHalfParentheses : true
1922} ;
2023function reporter ( context , options ) {
2124 let { Syntax, RuleError, report, fixer, getSource } = context ;
2225 const allowOutsideHalfParentheses =
2326 options . allowOutsideHalfParentheses ?? defaultOptions . allowOutsideHalfParentheses ;
27+ const requireOutsideHalfParentheses =
28+ options . requireOutsideHalfParentheses ?? defaultOptions . requireOutsideHalfParentheses ;
2429 return {
2530 [ Syntax . Str ] ( node ) {
2631 if ( ! isUserWrittenNode ( node , context ) ) {
@@ -59,6 +64,30 @@ function reporter(context, options) {
5964 ) ;
6065 } ) ;
6166 } ) ;
67+ if ( requireOutsideHalfParentheses ) {
68+ // 左にスペース必須
69+ matchCaptureGroupAll ( text , leftHalfParentheses ) . forEach ( ( match ) => {
70+ const { index } = match ;
71+ report (
72+ node ,
73+ new RuleError ( "半角かっこの外側に半角スペースが必要です。" , {
74+ index,
75+ fix : fixer . replaceTextRange ( [ index , index + 1 ] , " " + match . text )
76+ } )
77+ ) ;
78+ } ) ;
79+ // 右にスペース必須
80+ matchCaptureGroupAll ( text , rightHalfParentheses ) . forEach ( ( match ) => {
81+ const { index } = match ;
82+ report (
83+ node ,
84+ new RuleError ( "半角かっこの外側に半角スペースが必要です。" , {
85+ index,
86+ fix : fixer . replaceTextRange ( [ index , index + 1 ] , match . text + " " )
87+ } )
88+ ) ;
89+ } ) ;
90+ }
6291 }
6392 } ;
6493}
0 commit comments