From 6b653b8d17c80b16659c5238875977f0941490c2 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Tue, 7 Apr 2026 15:06:21 -0400 Subject: [PATCH] chore: simplify parser (#18077) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit small tidy-up spurred by #17954 — rather than try-catching every `parse_expression_at` call and passing the error to `parser.acorn_error`, we can handle the error locally and get rid of that method ### Before submitting the PR, please make sure you do the following - [ ] It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs - [x] Prefix your PR title with `feat:`, `fix:`, `chore:`, or `docs:`. - [x] This message body should clearly illustrate what problems it solves. - [ ] Ideally, include a test that fails without this PR but passes with it. - [ ] If this PR changes code within `packages/svelte/src`, add a changeset (`npx changeset`). ### Tests and linting - [x] Run the tests with `pnpm test` and lint the project with `pnpm lint` --------- Co-authored-by: vercel[bot] <35613825+vercel[bot]@users.noreply.github.com> --- .../src/compiler/phases/1-parse/acorn.js | 66 ++++++++++++------- .../src/compiler/phases/1-parse/index.js | 10 --- .../compiler/phases/1-parse/read/context.js | 54 +++++++-------- .../phases/1-parse/read/expression.js | 2 +- .../compiler/phases/1-parse/read/script.js | 9 +-- 5 files changed, 69 insertions(+), 72 deletions(-) diff --git a/packages/svelte/src/compiler/phases/1-parse/acorn.js b/packages/svelte/src/compiler/phases/1-parse/acorn.js index 797ab4cea5..45a7c2a58c 100644 --- a/packages/svelte/src/compiler/phases/1-parse/acorn.js +++ b/packages/svelte/src/compiler/phases/1-parse/acorn.js @@ -4,8 +4,10 @@ import * as acorn from 'acorn'; import { walk } from 'zimmerframe'; import { tsPlugin } from '@sveltejs/acorn-typescript'; +import * as e from '../../errors.js'; -const ParserWithTS = acorn.Parser.extend(tsPlugin()); +const JSParser = acorn.Parser; +const TSParser = JSParser.extend(tsPlugin()); /** * @typedef {Comment & { @@ -21,15 +23,15 @@ const ParserWithTS = acorn.Parser.extend(tsPlugin()); * @param {boolean} [is_script] */ export function parse(source, comments, typescript, is_script) { - const parser = typescript ? ParserWithTS : acorn.Parser; + const acorn = typescript ? TSParser : JSParser; const { onComment, add_comments } = get_comment_handlers( source, /** @type {CommentWithLocation[]} */ (comments) ); - // @ts-ignore - const parse_statement = parser.prototype.parseStatement; + // @ts-expect-error + const parse_statement = acorn.prototype.parseStatement; // If we're dealing with a