Skip to content

Commit

Permalink
Extract string parsing to a separate package (#14772)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Jul 20, 2022
1 parent b807206 commit ea5ff29
Show file tree
Hide file tree
Showing 21 changed files with 722 additions and 354 deletions.
1 change: 1 addition & 0 deletions babel.config.js
Expand Up @@ -209,6 +209,7 @@ module.exports = function (api) {
test: [
"packages/babel-generator",
"packages/babel-plugin-proposal-decorators",
"packages/babel-helper-string-parser",
].map(normalize),
plugins: ["babel-plugin-transform-charcodes"],
},
Expand Down
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,
};
}
3 changes: 3 additions & 0 deletions packages/babel-helper-string-parser/.npmignore
@@ -0,0 +1,3 @@
src
test
*.log
19 changes: 19 additions & 0 deletions packages/babel-helper-string-parser/README.md
@@ -0,0 +1,19 @@
# @babel/helper-string-parser

> A utility package to parse strings
See our website [@babel/helper-string-parser](https://babeljs.io/docs/en/babel-helper-string-parser) for more information.

## Install

Using npm:

```sh
npm install --save @babel/helper-string-parser
```

or using yarn:

```sh
yarn add @babel/helper-string-parser
```
36 changes: 36 additions & 0 deletions packages/babel-helper-string-parser/package.json
@@ -0,0 +1,36 @@
{
"name": "@babel/helper-string-parser",
"version": "7.18.6",
"description": "A utility package to parse strings",
"repository": {
"type": "git",
"url": "https://github.com/babel/babel.git",
"directory": "packages/babel-helper-string-parser"
},
"homepage": "https://babel.dev/docs/en/next/babel-helper-string-parser",
"license": "MIT",
"publishConfig": {
"access": "public"
},
"main": "./lib/index.js",
"devDependencies": {
"charcodes": "^0.2.0"
},
"engines": {
"node": ">=6.9.0"
},
"author": "The Babel Team (https://babel.dev/team)",
"conditions": {
"USE_ESM": [
{
"type": "module"
},
null
]
},
"exports": {
".": "./lib/index.js",
"./package.json": "./package.json"
},
"type": "commonjs"
}

0 comments on commit ea5ff29

Please sign in to comment.