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

Referencing otherwise unused variable inside catch {} strips out the variable outside try/catch, but leaves variable in catch untouched #2815

Open
revosw opened this issue Nov 21, 2024 · 1 comment
Labels
information needed Further information is requested

Comments

@revosw
Copy link

revosw commented Nov 21, 2024

Which project does this relate to?

Start

Describe the bug

I was unsure where to file the issue, I'm guessing it's probably a bundler or other downstream process that's causing this

In the stackblitz reproduction linked below, I have this file at /routes/index.tsx:

import { createFileRoute } from "@tanstack/react-router";

export const Route = createFileRoute("/")({
	component: RouteComponent,
});

function RouteComponent() {
	let a = undefined;
	try {
		throw "";
	} catch {
		a = 5;
	}

	return "Hello /!";
}

I'm creating a variable that is unused other than inside the catch{} block. It seems like the a variable is being stripped out when rendering the page, but the a = 5 statement is still preserved, leading to ReferenceError: a is not defined

Your Example Website or App

https://stackblitz.com/edit/github-avg9mj-mxdthn?file=app%2Froutes%2F__root.tsx,app%2Froutes%2Findex.tsx,app%2Fclient.tsx

Steps to Reproduce the Bug or Issue

  1. npm run dev
  2. When the dev server is up and the root route has loaded, notice that the console has an error Error in renderToPipeableStream: ReferenceError: a is not defined

Expected behavior

The a variable should not be stripped out, but instead realize that a = 5 in the catch block relies on the a variable

Screenshots or Videos

No response

Platform

  • OS: Windows
  • Browser: Microsoft Edge
  • Version: 131.0.2903.51 (Official build) (64-bit)

Additional context

No response

@SeanCassiere
Copy link
Member

This issue seems to stem from a bug in the bable-dead-code-elimination package that we use for the removal of unused code.

Tracking for the issue on the babel-dead-code-elimination repo - pcattori/babel-dead-code-elimination#25

@SeanCassiere SeanCassiere added the information needed Further information is requested label Nov 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
information needed Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants