MCPcopy Create free account
hub / github.com/babel/babel / readCodePoint

Function readCodePoint

packages/babel-helper-string-parser/src/index.ts:403–457  ·  view source on GitHub ↗
(
  input: string,
  pos: number,
  lineStart: number,
  curLine: number,
  throwOnInvalid: boolean,
  errors: CodePointErrorHandlers,
)

Source from the content-addressed store, hash-verified

401};
402
403export function readCodePoint(
404 input: string,
405 pos: number,
406 lineStart: number,
407 curLine: number,
408 throwOnInvalid: boolean,
409 errors: CodePointErrorHandlers,
410) {
411 const ch = input.charCodeAt(pos);
412 let code;
413
414 if (ch === charCodes.leftCurlyBrace) {
415 ++pos;
416 const rightCurlyBracePos = input.indexOf("}", pos);
417 if (rightCurlyBracePos === -1) {
418 // `\u{` without a closing `}`: there is no `}` to skip, so stop here
419 // instead of advancing `pos` past the end of the input
420 if (throwOnInvalid) {
421 errors.invalidEscapeSequence(pos, lineStart, curLine);
422 }
423 return { code: null, pos };
424 }
425 ({ code, pos } = readHexChar(
426 input,
427 pos,
428 lineStart,
429 curLine,
430 rightCurlyBracePos - pos,
431 true,
432 throwOnInvalid,
433 errors,
434 ));
435 ++pos;
436 if (code !== null && code > 0x10ffff) {
437 if (throwOnInvalid) {
438 errors.invalidCodePoint(pos, lineStart, curLine);
439 }
440 // In errorRecovery mode `invalidCodePoint` only records the error, so
441 // never hand an out-of-range code point back to `String.fromCodePoint`.
442 return { code: null, pos };
443 }
444 } else {
445 ({ code, pos } = readHexChar(
446 input,
447 pos,
448 lineStart,
449 curLine,
450 4,
451 false,
452 throwOnInvalid,
453 errors,
454 ));
455 }
456 return { code, pos };
457}

Callers 2

readCodePointFunction · 0.90
readEscapedCharFunction · 0.70

Calls 1

readHexCharFunction · 0.85

Tested by

no test coverage detected