From 23418cdcabb92ee70b8d6cc3d54c5afac0ec31e2 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 +++++++++++++++++++ .../babel-helper-string-parser/src/index.ts | 10 +-- packages/babel-parser/src/tokenizer/index.js | 17 ++-- tsconfig.json | 4 + 4 files changed, 100 insertions(+), 13 deletions(-) diff --git a/lib/babel-packages.js.flow b/lib/babel-packages.js.flow index bffca14e73d5..a4818eefc624 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-helper-string-parser/src/index.ts b/packages/babel-helper-string-parser/src/index.ts index b3bd00c5b9fa..edb89f7dc2ef 100644 --- a/packages/babel-helper-string-parser/src/index.ts +++ b/packages/babel-helper-string-parser/src/index.ts @@ -23,16 +23,16 @@ const forbiddenNumericSeparatorSiblings = { const isAllowedNumericSeparatorSibling = { // 0 - 1 - bin: ch => ch === charCodes.digit0 || ch === charCodes.digit1, + bin: (ch: number) => ch === charCodes.digit0 || ch === charCodes.digit1, // 0 - 7 - oct: ch => ch >= charCodes.digit0 && ch <= charCodes.digit7, + oct: (ch: number) => ch >= charCodes.digit0 && ch <= charCodes.digit7, // 0 - 9 - dec: ch => ch >= charCodes.digit0 && ch <= charCodes.digit9, + dec: (ch: number) => ch >= charCodes.digit0 && ch <= charCodes.digit9, // 0 - 9, A - F, a - f, - hex: ch => + hex: (ch: number) => (ch >= charCodes.digit0 && ch <= charCodes.digit9) || (ch >= charCodes.uppercaseA && ch <= charCodes.uppercaseF) || (ch >= charCodes.lowercaseA && ch <= charCodes.lowercaseF), @@ -281,7 +281,7 @@ export function readInt( input: string, pos: number, radix: number, - len: number, + len: number | undefined, forceLen: boolean, allowNumSeparator: boolean | "bail", errors: IntErrorHandlers, diff --git a/packages/babel-parser/src/tokenizer/index.js b/packages/babel-parser/src/tokenizer/index.js index 46d74479b281..0d400d2aa7b0 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([ @@ -1119,7 +1117,7 @@ export default class Tokenizer extends CommentsParser { readInt( radix: number, len?: number, - forceLen?: boolean, + forceLen: boolean = false, allowNumSeparator: boolean | "bail" = true, ): number | null { const { n, pos } = readInt( @@ -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,8 @@ export default class Tokenizer extends CommentsParser { }, }; - errorHandlers_readEscapedChar /*EscapedCharErrorHandlers*/ = { + // $FlowIgnore - flow doesn't like introducing required methods with ... + errorHandlers_readEscapedChar: EscapedCharErrorHandlers = { ...this.errorHandlers_readCodePoint, strictNumericEscape: pos => { this.state.pos = pos; @@ -1596,7 +1595,8 @@ export default class Tokenizer extends CommentsParser { }, }; - errorHandlers_readStringContents_string /*StringContentsErrorHandlers*/ = { + // $FlowIgnore - flow doesn't like introducing required methods with ... + 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 +1608,8 @@ export default class Tokenizer extends CommentsParser { }, }; - errorHandlers_readStringContents_template /*StringContentsErrorHandlers*/ = { + // $FlowIgnore - flow doesn't like introducing required methods with ... + errorHandlers_readStringContents_template: StringContentsErrorHandlers = { ...this.errorHandlers_readEscapedChar, unterminated: (initialPos, initialLineStart, initialCurLine) => { this.state.pos = initialPos; // TODO: For strings, we subtract 1 diff --git a/tsconfig.json b/tsconfig.json index d2eba9b804cf..820156ac948e 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -30,6 +30,7 @@ "./packages/babel-helper-simple-access/src/**/*.ts", "./packages/babel-helper-skip-transparent-expression-wrappers/src/**/*.ts", "./packages/babel-helper-split-export-declaration/src/**/*.ts", + "./packages/babel-helper-string-parser/src/**/*.ts", "./packages/babel-helper-transform-fixture-test-runner/src/**/*.ts", "./packages/babel-helper-validator-identifier/src/**/*.ts", "./packages/babel-helper-validator-option/src/**/*.ts", @@ -249,6 +250,9 @@ "@babel/helper-split-export-declaration": [ "./packages/babel-helper-split-export-declaration/src" ], + "@babel/helper-string-parser": [ + "./packages/babel-helper-string-parser/src" + ], "@babel/helper-transform-fixture-test-runner": [ "./packages/babel-helper-transform-fixture-test-runner/src" ],