From 6f0d90ff860d72727fdbccaed748f13b6d57f40c Mon Sep 17 00:00:00 2001 From: liuxingbaoyu <30521560+liuxingbaoyu@users.noreply.github.com> Date: Thu, 21 Jul 2022 01:50:15 +0800 Subject: [PATCH] use `@babel/helper-string-parser` --- .../babel-helper-string-parser/src/index.ts | 1 + packages/babel-types/package.json | 4 +- packages/babel-types/src/definitions/core.ts | 39 ++++++++++++++++--- yarn.lock | 9 +---- 4 files changed, 38 insertions(+), 15 deletions(-) diff --git a/packages/babel-helper-string-parser/src/index.ts b/packages/babel-helper-string-parser/src/index.ts index edb89f7dc2ef..beb7bccf8381 100644 --- a/packages/babel-helper-string-parser/src/index.ts +++ b/packages/babel-helper-string-parser/src/index.ts @@ -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); diff --git a/packages/babel-types/package.json b/packages/babel-types/package.json index 1bbd1278e862..d9dadbda21f1 100644 --- a/packages/babel-types/package.json +++ b/packages/babel-types/package.json @@ -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:^", diff --git a/packages/babel-types/src/definitions/core.ts b/packages/babel-types/src/definitions/core.ts index d2159cf34df4..66213c683c68 100644 --- a/packages/babel-types/src/definitions/core.ts +++ b/packages/babel-types/src/definitions/core.ts @@ -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, @@ -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; }, ), }, diff --git a/yarn.lock b/yarn.lock index 15b6c9931f19..715da6f14bdd 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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 @@ -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"