Skip to content

Commit

Permalink
use @babel/helper-string-parser
Browse files Browse the repository at this point in the history
  • Loading branch information
liuxingbaoyu committed Jul 20, 2022
1 parent 1cbaf65 commit 6f0d90f
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 15 deletions.
1 change: 1 addition & 0 deletions packages/babel-helper-string-parser/src/index.ts
Expand Up @@ -65,6 +65,7 @@ export function readStringContents(
for (;;) {
if (pos >= length) {
errors.unterminated(initialPos, initialLineStart, initialCurLine);
out += input.slice(chunkStart, pos);
break;
}
const ch = input.charCodeAt(pos);
Expand Down
4 changes: 2 additions & 2 deletions packages/babel-types/package.json
Expand Up @@ -24,9 +24,9 @@
}
},
"dependencies": {
"@babel/helper-string-parser": "workspace:^",
"@babel/helper-validator-identifier": "workspace:^",
"to-fast-properties": "condition:BABEL_8_BREAKING ? ^3.0.0 : ^2.0.0",
"unraw": "^2.0.1"
"to-fast-properties": "condition:BABEL_8_BREAKING ? ^3.0.0 : ^2.0.0"
},
"devDependencies": {
"@babel/generator": "workspace:^",
Expand Down
39 changes: 34 additions & 5 deletions packages/babel-types/src/definitions/core.ts
Expand Up @@ -2,7 +2,7 @@ import is from "../validators/is";
import isValidIdentifier from "../validators/isValidIdentifier";
import { isKeyword, isReservedWord } from "@babel/helper-validator-identifier";
import type * as t from "..";
import { unraw } from "unraw";
import { readStringContents } from "@babel/helper-string-parser";

import {
BINARY_OPERATORS,
Expand Down Expand Up @@ -1979,11 +1979,40 @@ defineType("TemplateElement", {
function templateElementCookedValidator(node: t.TemplateElement) {
const raw = node.value.raw;

let cooked = null;
let str,
containsInvalid,
unterminatedCalled = false;
try {
cooked = unraw(raw);
} catch (error) {}
node.value.cooked = cooked;
const error = () => {
throw new Error();
};
({ str, containsInvalid } = readStringContents(
"template",
raw,
0,
0,
0,
{
unterminated() {
unterminatedCalled = true;
},
strictNumericEscape: error,
invalidEscapeSequence: error,
numericSeparatorInEscapeSequence: error,
unexpectedNumericSeparator: error,
invalidDigit: error,
invalidCodePoint: error,
},
));
} catch {
// TODO: When https://github.com/babel/babel/issues/14775 is fixed
// we can remove the try/catch block.
unterminatedCalled = true;
containsInvalid = true;
}
if (!unterminatedCalled) throw new Error("Invalid raw");

node.value.cooked = containsInvalid ? null : str;
},
),
},
Expand Down
9 changes: 1 addition & 8 deletions yarn.lock
Expand Up @@ -3748,12 +3748,12 @@ __metadata:
resolution: "@babel/types@workspace:packages/babel-types"
dependencies:
"@babel/generator": "workspace:^"
"@babel/helper-string-parser": "workspace:^"
"@babel/helper-validator-identifier": "workspace:^"
"@babel/parser": "workspace:^"
chalk: ^4.1.0
glob: ^7.1.7
to-fast-properties: "condition:BABEL_8_BREAKING ? ^3.0.0 : ^2.0.0"
unraw: ^2.0.1
languageName: unknown
linkType: soft

Expand Down Expand Up @@ -15052,13 +15052,6 @@ fsevents@^1.2.7:
languageName: node
linkType: hard

"unraw@npm:^2.0.1":
version: 2.0.1
resolution: "unraw@npm:2.0.1"
checksum: af9a9d2f6e420cb4f52fe2f1f5982e6b0be95da640d6ae8d6d9ff631d864771793cb9fe7e2a16ef1ce631b94065f4438e7bd7f1701076fc69296edc4e704d42f
languageName: node
linkType: hard

"unset-value@npm:^1.0.0":
version: 1.0.0
resolution: "unset-value@npm:1.0.0"
Expand Down

0 comments on commit 6f0d90f

Please sign in to comment.