66 */
77import { isUserWrittenNode } from "./util/node-util" ;
88import { matchCaptureGroupAll } from "match-index" ;
9+ import { japaneseRegExp } from "./util/regexp" ;
910
10- const brackets = [ "\\[" , "\\]" , "(" , ")" , "[" , "]" , "「" , "」" , "『" , "』" ] ;
11-
11+ const brackets = [ "\\(" , "\\)" , "\\[" , "\\]" , "(" , ")" , "[" , "]" , "「" , "」" , "『" , "』" ] ;
1212const leftBrackets = brackets . map ( ( bracket ) => {
1313 return new RegExp ( "([ ])" + bracket , "g" ) ;
1414} ) ;
1515const rightBrackets = brackets . map ( ( bracket ) => {
1616 return new RegExp ( bracket + "([ ])" , "g" ) ;
1717} ) ;
18- function reporter ( context ) {
18+ const leftHalfParentheses = new RegExp ( `${ japaneseRegExp . source } (\\()` , "g" ) ;
19+ const rightHalfParentheses = new RegExp ( `(\\))${ japaneseRegExp . source } ` , "g" ) ;
20+ const defaultOptions = {
21+ allowOutsideHalfParentheses : true ,
22+ requireOutsideHalfParentheses : false
23+ } ;
24+ function reporter ( context , options ) {
1925 let { Syntax, RuleError, report, fixer, getSource } = context ;
26+ const allowOutsideHalfParentheses =
27+ options . allowOutsideHalfParentheses ?? defaultOptions . allowOutsideHalfParentheses ;
28+ const requireOutsideHalfParentheses =
29+ options . requireOutsideHalfParentheses ?? defaultOptions . requireOutsideHalfParentheses ;
2030 return {
2131 [ Syntax . Str ] ( node ) {
2232 if ( ! isUserWrittenNode ( node , context ) ) {
@@ -27,6 +37,9 @@ function reporter(context) {
2737 leftBrackets . forEach ( ( pattern ) => {
2838 matchCaptureGroupAll ( text , pattern ) . forEach ( ( match ) => {
2939 const { index } = match ;
40+ if ( allowOutsideHalfParentheses && text . substring ( index , index + 2 ) === " (" ) {
41+ return ;
42+ }
3043 report (
3144 node ,
3245 new RuleError ( "かっこの外側、内側ともにスペースを入れません。" , {
@@ -39,7 +52,10 @@ function reporter(context) {
3952 // 右にスペース
4053 rightBrackets . forEach ( ( pattern ) => {
4154 matchCaptureGroupAll ( text , pattern ) . forEach ( ( match ) => {
42- const { index, text } = match ;
55+ const { index } = match ;
56+ if ( allowOutsideHalfParentheses && text . substring ( index - 1 , index + 1 ) === ") " ) {
57+ return ;
58+ }
4359 report (
4460 node ,
4561 new RuleError ( "かっこの外側、内側ともにスペースを入れません。" , {
@@ -49,6 +65,30 @@ function reporter(context) {
4965 ) ;
5066 } ) ;
5167 } ) ;
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+ }
5292 }
5393 } ;
5494}
0 commit comments