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 700d6f0
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 7 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,
};
}
12 changes: 5 additions & 7 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 @@ -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,7 @@ export default class Tokenizer extends CommentsParser {
},
};

errorHandlers_readEscapedChar /*EscapedCharErrorHandlers*/ = {
errorHandlers_readEscapedChar: EscapedCharErrorHandlers = {
...this.errorHandlers_readCodePoint,
strictNumericEscape: pos => {
this.state.pos = pos;
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 700d6f0

Please sign in to comment.