From 700d6f0386de4447cf27d61b464cf3ad2d790c3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Wed, 20 Jul 2022 13:34:03 +0200 Subject: [PATCH] Flow --- lib/babel-packages.js.flow | 82 ++++++++++++++++++++ packages/babel-parser/src/tokenizer/index.js | 12 ++- 2 files changed, 87 insertions(+), 7 deletions(-) diff --git a/lib/babel-packages.js.flow b/lib/babel-packages.js.flow index bffca14e73d5..e17b434bb4f0 100644 --- a/lib/babel-packages.js.flow +++ b/lib/babel-packages.js.flow @@ -220,3 +220,85 @@ declare module "@babel/plugin-transform-classes" { declare module "@babel/helper-compilation-targets" { declare module.exports: any; } + +declare module "@babel/helper-string-parser" { + declare export type StringContentsErrorHandlers = EscapedCharErrorHandlers & { + unterminated( + initialPos: number, + initialLineStart: number, + initialCurLine: number + ): void, + }; + declare export function readStringContents( + type: "single" | "double" | "template", + input: string, + pos: number, + lineStart: number, + curLine: number, + errors: StringContentsErrorHandlers + ): { + pos: number, + str: string, + containsInvalid: boolean, + lineStart: number, + curLine: number, + }; + + declare export type EscapedCharErrorHandlers = HexCharErrorHandlers & + CodePointErrorHandlers & { + strictNumericEscape(pos: number): void, + }; + + declare export function readEscapedChar( + input: string, + pos: number, + lineStart: number, + curLine: number, + inTemplate: boolean, + errors: EscapedCharErrorHandlers + ): { + pos: number, + ch: string | null, + lineStart: number, + curLine: number, + }; + + declare type HexCharErrorHandlers = IntErrorHandlers & { + invalidEscapeSequence(pos: number, startPos: number): void, + }; + + declare export type IntErrorHandlers = { + numericSeparatorInEscapeSequence(pos: number): void, + unexpectedNumericSeparator(pos: number): void, + // It can return "true" to indicate that the error was handled + // and the int parsing should continue. + invalidDigit(pos: number, radix: number): boolean, + }; + + declare export function readInt( + input: string, + pos: number, + radix: number, + len: number, + forceLen: boolean, + allowNumSeparator: boolean | "bail", + errors: IntErrorHandlers + ): { + n: null | number, + pos: number, + }; + + declare export type CodePointErrorHandlers = HexCharErrorHandlers & { + invalidCodePoint(pos: number): void, + }; + + declare export function readCodePoint( + input: string, + pos: number, + throwOnInvalid: boolean, + errors: CodePointErrorHandlers + ): { + code: any, + pos: number, + }; +} diff --git a/packages/babel-parser/src/tokenizer/index.js b/packages/babel-parser/src/tokenizer/index.js index 46d74479b281..c0566791cd93 100644 --- a/packages/babel-parser/src/tokenizer/index.js +++ b/packages/babel-parser/src/tokenizer/index.js @@ -40,12 +40,10 @@ import { readCodePoint, readEscapedChar, readStringContents, - /* TODO: flow->ts type IntErrorHandlers, type CodePointErrorHandlers, type EscapedCharErrorHandlers, type StringContentsErrorHandlers, - */ } from "@babel/helper-string-parser"; const VALID_REGEX_FLAGS = new Set([ @@ -1542,7 +1540,7 @@ export default class Tokenizer extends CommentsParser { } } - errorHandlers_readInt /*IntErrorHandlers*/ = { + errorHandlers_readInt: IntErrorHandlers = { invalidDigit: (pos, radix) => { if (this.options.errorRecovery) { this.state.pos = pos; @@ -1569,7 +1567,7 @@ export default class Tokenizer extends CommentsParser { }, }; - errorHandlers_readCodePoint /*CodePointErrorHandlers*/ = { + errorHandlers_readCodePoint: CodePointErrorHandlers = { ...this.errorHandlers_readInt, invalidEscapeSequence: (pos, startPos) => { this.state.pos = pos; @@ -1586,7 +1584,7 @@ export default class Tokenizer extends CommentsParser { }, }; - errorHandlers_readEscapedChar /*EscapedCharErrorHandlers*/ = { + errorHandlers_readEscapedChar: EscapedCharErrorHandlers = { ...this.errorHandlers_readCodePoint, strictNumericEscape: pos => { this.state.pos = pos; @@ -1596,7 +1594,7 @@ export default class Tokenizer extends CommentsParser { }, }; - errorHandlers_readStringContents_string /*StringContentsErrorHandlers*/ = { + errorHandlers_readStringContents_string: StringContentsErrorHandlers = { ...this.errorHandlers_readEscapedChar, unterminated: (initialPos, initialLineStart, initialCurLine) => { this.state.pos = initialPos - 1; // Report the error at the string quote @@ -1608,7 +1606,7 @@ export default class Tokenizer extends CommentsParser { }, }; - errorHandlers_readStringContents_template /*StringContentsErrorHandlers*/ = { + errorHandlers_readStringContents_template: StringContentsErrorHandlers = { ...this.errorHandlers_readEscapedChar, unterminated: (initialPos, initialLineStart, initialCurLine) => { this.state.pos = initialPos; // TODO: For strings, we subtract 1