@@ -36,7 +36,8 @@ function checkRegexInSource(source, regex) {
3636 const foundDetail = regex . exec ( source )
3737 return ! ! foundDetail
3838 ? {
39- pattern : foundDetail [ 0 ] ,
39+ pattern : foundDetail . shift ( ) ,
40+ captured : foundDetail ,
4041 patternIndex : foundDetail . index ,
4142 nextChar : regex . lastIndex
4243 }
@@ -73,7 +74,7 @@ function checkRegex(source, sourceLines, nextLine, nextChar, rootChar, pattern,
7374 pattern ,
7475 from => `Invalid regular expression ${ from } found`
7576 ) ,
76- fix : replace ( rootChar , sourceDetail . patternIndex , sourceDetail . nextChar , sourceDetail . pattern )
77+ fix : replace ( rootChar , sourceDetail )
7778 } )
7879 checkRegex ( remainingSource , sourceLines , foundDetail . nextLine , foundDetail . nextChar , rootChar + remainingSourceStart ,
7980 pattern , replace , report )
@@ -85,7 +86,7 @@ function checkRegex(source, sourceLines, nextLine, nextChar, rootChar, pattern,
8586 pattern ,
8687 from => `Invalid regular expression ${ from } found`
8788 ) ,
88- fix : replace ( rootChar , sourceDetail . patternIndex , sourceDetail . nextChar , sourceDetail . pattern )
89+ fix : replace ( rootChar , sourceDetail )
8990 } )
9091 checkRegex ( remainingSource , sourceLines , nextLine + 1 , nextChar , rootChar + remainingSourceStart , pattern , replace , report )
9192 }
@@ -96,18 +97,16 @@ function checkRegex(source, sourceLines, nextLine, nextChar, rootChar, pattern,
9697function buildReplacementFunction ( replacement ) {
9798 if ( / \b r e t u r n \b / . test ( replacement ) ) {
9899 try {
99- const replacementFunction = new Function ( 'text' , replacement ) // eslint-disable-line no-new-func
100- if ( typeof replacementFunction ( 'text' ) === 'string' ) {
101- return text => {
102- try {
103- const replacement = replacementFunction ( text )
104- if ( typeof replacement === 'string' ) {
105- return replacement
106- }
100+ const replacementFunction = new Function ( 'text' , 'captured' , replacement ) // eslint-disable-line no-new-func
101+ return ( text , captured ) => {
102+ try {
103+ const replacement = replacementFunction ( text , captured )
104+ if ( typeof replacement === 'string' ) {
105+ return replacement
107106 }
108- catch ( e ) { }
109- return text
110107 }
108+ catch ( e ) { }
109+ return text
111110 }
112111 }
113112 catch ( e ) { }
@@ -118,13 +117,16 @@ function buildReplacementFunction(replacement) {
118117function createReplacement ( replacement ) {
119118 switch ( typeof replacement ) {
120119 case 'string' :
121- return ( from , deltaStart , deltaEnd ) => fixer => fixer . replaceTextRange ( [ from + deltaStart , from + deltaEnd ] , replacement )
120+ return ( from , found ) => fixer => fixer . replaceTextRange ( [ from + found . patternIndex , from + found . nextChar ] , replacement )
122121 case 'object' :
123122 if ( typeof replacement . function === 'string' ) {
124123 const replacementFunction = buildReplacementFunction ( replacement . function )
125124 if ( typeof replacementFunction === 'function' ) {
126- return ( from , deltaStart , deltaEnd , text ) =>
127- fixer => fixer . replaceTextRange ( [ from + deltaStart , from + deltaEnd ] , replacementFunction ( text ) )
125+ return ( from , found ) =>
126+ fixer => fixer . replaceTextRange (
127+ [ from + found . patternIndex , from + found . nextChar ] ,
128+ replacementFunction ( found . pattern , found . captured )
129+ )
128130 }
129131 }
130132 }
0 commit comments