Skip to content

Commit

Permalink
Flow
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Jul 20, 2022
1 parent 9971813 commit 23418cd
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 13 deletions.
82 changes: 82 additions & 0 deletions lib/babel-packages.js.flow
Expand Up @@ -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,
};
}
10 changes: 5 additions & 5 deletions packages/babel-helper-string-parser/src/index.ts
Expand Up @@ -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),
Expand Down Expand Up @@ -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,
Expand Down
17 changes: 9 additions & 8 deletions packages/babel-parser/src/tokenizer/index.js
Expand Up @@ -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([
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions tsconfig.json
Expand Up @@ -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",
Expand Down Expand Up @@ -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"
],
Expand Down

0 comments on commit 23418cd

Please sign in to comment.