@@ -71,6 +71,7 @@ type RegExpContextBase = {
7171
7272 fixReplaceFlags : (
7373 newFlags : string | ( ( ) => string | null ) ,
74+ includePattern ?: boolean ,
7475 ) => ( fixer : Rule . RuleFixer ) => Rule . Fix [ ] | Rule . Fix | null
7576
7677 /**
@@ -118,6 +119,7 @@ type UnparsableRegExpContextBase = {
118119
119120 fixReplaceFlags : (
120121 newFlags : string | ( ( ) => string | null ) ,
122+ includePattern ?: boolean ,
121123 ) => ( fixer : Rule . RuleFixer ) => Rule . Fix [ ] | Rule . Fix | null
122124}
123125export type RegExpContextForInvalid = {
@@ -609,12 +611,13 @@ function buildRegExpContextBase({
609611 fixReplaceQuant : ( qNode , replacement ) => {
610612 return fixReplaceQuant ( patternSource , qNode , replacement )
611613 } ,
612- fixReplaceFlags : ( newFlags ) => {
614+ fixReplaceFlags : ( newFlags , includePattern ) => {
613615 return fixReplaceFlags (
614616 patternSource ,
615617 regexpNode ,
616618 flagsNode ,
617619 newFlags ,
620+ includePattern ?? true ,
618621 )
619622 } ,
620623 getUsageOfPattern : ( ) =>
@@ -665,15 +668,13 @@ function buildUnparsableRegExpContextBase({
665668 getFlagLocation : ( flag ) =>
666669 getFlagLocation ( sourceCode , regexpNode , flagsNode , flag ) ,
667670
668- fixReplaceFlags : ( newFlags ) => {
669- if ( ! patternSource ) {
670- return ( ) => null
671- }
671+ fixReplaceFlags : ( newFlags , includePattern ) => {
672672 return fixReplaceFlags (
673673 patternSource ,
674674 regexpNode ,
675675 flagsNode ,
676676 newFlags ,
677+ includePattern ?? true ,
677678 )
678679 } ,
679680 }
@@ -863,14 +864,35 @@ function fixReplaceQuant(
863864
864865/**
865866 * Returns a new fixer that replaces the current flags with the given flags.
867+ *
868+ * @param includePattern Whether the whole pattern is to be included in the fix.
869+ *
870+ * Fixes that change the pattern generally assume that the flags don't change,
871+ * so changing the flags should conflict with all pattern fixes.
866872 */
867873function fixReplaceFlags (
868- patternSource : PatternSource ,
874+ patternSource : PatternSource | null ,
869875 regexpNode : ESTree . CallExpression | ESTree . RegExpLiteral ,
870876 flagsNode : ESTree . Expression | null ,
871877 replacement : string | ( ( ) => string | null ) ,
878+ includePattern : boolean ,
872879) {
873880 return ( fixer : Rule . RuleFixer ) : Rule . Fix [ ] | Rule . Fix | null => {
881+ let patternFix = null
882+ if ( includePattern ) {
883+ if ( ! patternSource ) {
884+ return null
885+ }
886+ const patternRange = patternSource . getReplaceRange ( {
887+ start : 0 ,
888+ end : patternSource . value . length ,
889+ } )
890+ if ( patternRange == null ) {
891+ return null
892+ }
893+ patternFix = patternRange . replace ( fixer , patternSource . value )
894+ }
895+
874896 let newFlags
875897 if ( typeof replacement === "string" ) {
876898 newFlags = replacement
@@ -916,18 +938,10 @@ function fixReplaceFlags(
916938 )
917939 }
918940
919- // fixes that change the pattern generally assume that flags don't
920- // change, so we have to create conflicts.
921-
922- const patternRange = patternSource . getReplaceRange ( {
923- start : 0 ,
924- end : patternSource . value . length ,
925- } )
926- if ( patternRange == null ) {
927- return null
941+ if ( ! patternFix ) {
942+ return flagsFix
928943 }
929-
930- return [ patternRange . replace ( fixer , patternSource . value ) , flagsFix ]
944+ return [ patternFix , flagsFix ]
931945 }
932946}
933947
0 commit comments