@@ -59,7 +59,7 @@ export function readStringContents(
5959 const initialCurLine = curLine ;
6060
6161 let out = "" ;
62- let containsInvalid = false ;
62+ let firstInvalidLoc = null ;
6363 let chunkStart = pos ;
6464 const { length } = input ;
6565 for ( ; ; ) {
@@ -75,25 +75,20 @@ export function readStringContents(
7575 }
7676 if ( ch === charCodes . backslash ) {
7777 out += input . slice ( chunkStart , pos ) ;
78- let escaped ;
79- ( {
80- ch : escaped ,
81- pos,
82- lineStart,
83- curLine,
84- } = readEscapedChar (
78+ const res = readEscapedChar (
8579 input ,
8680 pos ,
8781 lineStart ,
8882 curLine ,
8983 type === "template" ,
9084 errors ,
91- ) ) ;
92- if ( escaped === null ) {
93- containsInvalid = true ;
85+ ) ;
86+ if ( res . ch === null && ! firstInvalidLoc ) {
87+ firstInvalidLoc = { pos , lineStart , curLine } ;
9488 } else {
95- out += escaped ;
89+ out += res . ch ;
9690 }
91+ ( { pos, lineStart, curLine } = res ) ;
9792 chunkStart = pos ;
9893 } else if (
9994 ch === charCodes . lineSeparator ||
@@ -121,7 +116,17 @@ export function readStringContents(
121116 ++ pos ;
122117 }
123118 }
124- return { pos, str : out , containsInvalid, lineStart, curLine } ;
119+ return {
120+ pos,
121+ str : out ,
122+ firstInvalidLoc,
123+ lineStart,
124+ curLine,
125+
126+ // TODO(Babel 8): This is only needed for backwards compatibility,
127+ // we can remove it.
128+ containsInvalid : ! ! firstInvalidLoc ,
129+ } ;
125130}
126131
127132function isStringEnd (
@@ -280,6 +285,7 @@ function readHexChar(
280285 forceLen ,
281286 false ,
282287 errors ,
288+ /* bailOnError */ ! throwOnInvalid ,
283289 ) ) ;
284290 if ( n === null ) {
285291 if ( throwOnInvalid ) {
@@ -322,6 +328,7 @@ export function readInt(
322328 forceLen : boolean ,
323329 allowNumSeparator : boolean | "bail" ,
324330 errors : IntErrorHandlers ,
331+ bailOnError : boolean ,
325332) {
326333 const start = pos ;
327334 const forbiddenSiblings =
@@ -349,13 +356,15 @@ export function readInt(
349356 const next = input . charCodeAt ( pos + 1 ) ;
350357
351358 if ( ! allowNumSeparator ) {
359+ if ( bailOnError ) return { n : null , pos } ;
352360 errors . numericSeparatorInEscapeSequence ( pos , lineStart , curLine ) ;
353361 } else if (
354362 Number . isNaN ( next ) ||
355363 ! isAllowedSibling ( next ) ||
356364 forbiddenSiblings . has ( prev ) ||
357365 forbiddenSiblings . has ( next )
358366 ) {
367+ if ( bailOnError ) return { n : null , pos } ;
359368 errors . unexpectedNumericSeparator ( pos , lineStart , curLine ) ;
360369 }
361370
@@ -376,7 +385,12 @@ export function readInt(
376385 if ( val >= radix ) {
377386 // If we found a digit which is too big, errors.invalidDigit can return true to avoid
378387 // breaking the loop (this is used for error recovery).
379- if ( val <= 9 && errors . invalidDigit ( pos , lineStart , curLine , radix ) ) {
388+ if ( val <= 9 && bailOnError ) {
389+ return { n : null , pos } ;
390+ } else if (
391+ val <= 9 &&
392+ errors . invalidDigit ( pos , lineStart , curLine , radix )
393+ ) {
380394 val = 0 ;
381395 } else if ( forceLen ) {
382396 val = 0 ;
0 commit comments