@@ -71,6 +71,7 @@ type RegExpContextBase = {
71
71
72
72
fixReplaceFlags : (
73
73
newFlags : string | ( ( ) => string | null ) ,
74
+ includePattern ?: boolean ,
74
75
) => ( fixer : Rule . RuleFixer ) => Rule . Fix [ ] | Rule . Fix | null
75
76
76
77
/**
@@ -118,6 +119,7 @@ type UnparsableRegExpContextBase = {
118
119
119
120
fixReplaceFlags : (
120
121
newFlags : string | ( ( ) => string | null ) ,
122
+ includePattern ?: boolean ,
121
123
) => ( fixer : Rule . RuleFixer ) => Rule . Fix [ ] | Rule . Fix | null
122
124
}
123
125
export type RegExpContextForInvalid = {
@@ -609,12 +611,13 @@ function buildRegExpContextBase({
609
611
fixReplaceQuant : ( qNode , replacement ) => {
610
612
return fixReplaceQuant ( patternSource , qNode , replacement )
611
613
} ,
612
- fixReplaceFlags : ( newFlags ) => {
614
+ fixReplaceFlags : ( newFlags , includePattern ) => {
613
615
return fixReplaceFlags (
614
616
patternSource ,
615
617
regexpNode ,
616
618
flagsNode ,
617
619
newFlags ,
620
+ includePattern ?? true ,
618
621
)
619
622
} ,
620
623
getUsageOfPattern : ( ) =>
@@ -665,15 +668,13 @@ function buildUnparsableRegExpContextBase({
665
668
getFlagLocation : ( flag ) =>
666
669
getFlagLocation ( sourceCode , regexpNode , flagsNode , flag ) ,
667
670
668
- fixReplaceFlags : ( newFlags ) => {
669
- if ( ! patternSource ) {
670
- return ( ) => null
671
- }
671
+ fixReplaceFlags : ( newFlags , includePattern ) => {
672
672
return fixReplaceFlags (
673
673
patternSource ,
674
674
regexpNode ,
675
675
flagsNode ,
676
676
newFlags ,
677
+ includePattern ?? true ,
677
678
)
678
679
} ,
679
680
}
@@ -863,14 +864,35 @@ function fixReplaceQuant(
863
864
864
865
/**
865
866
* 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.
866
872
*/
867
873
function fixReplaceFlags (
868
- patternSource : PatternSource ,
874
+ patternSource : PatternSource | null ,
869
875
regexpNode : ESTree . CallExpression | ESTree . RegExpLiteral ,
870
876
flagsNode : ESTree . Expression | null ,
871
877
replacement : string | ( ( ) => string | null ) ,
878
+ includePattern : boolean ,
872
879
) {
873
880
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
+
874
896
let newFlags
875
897
if ( typeof replacement === "string" ) {
876
898
newFlags = replacement
@@ -916,18 +938,10 @@ function fixReplaceFlags(
916
938
)
917
939
}
918
940
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
928
943
}
929
-
930
- return [ patternRange . replace ( fixer , patternSource . value ) , flagsFix ]
944
+ return [ patternFix , flagsFix ]
931
945
}
932
946
}
933
947
0 commit comments