Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract string parsing to a separate package #14772

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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"
},
Comment on lines +16 to +18
Copy link
Contributor

@merceyz merceyz Jul 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if @babel/helper-string-parser will be bundled so asking to be on the safe side; shouldn't this be a normal dependency?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a plugin that inlines charcodes.*** into the corresponding codepoints: https://github.com/xtuc/charcodes/blob/master/packages/babel-plugin-transform-charcodes/README.md

"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"
}