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

Function.toString() returning strange strings #3248

Open
simylein opened this issue Jun 8, 2023 · 4 comments
Open

Function.toString() returning strange strings #3248

simylein opened this issue Jun 8, 2023 · 4 comments
Assignees
Labels
bug Something isn't working

Comments

@simylein
Copy link
Contributor

simylein commented Jun 8, 2023

What version of Bun is running?

0.6.7

What platform is your computer?

Darwin 22.5.0 arm64 arm

What steps can reproduce the bug?

run the following code with bun and node

const findUser = () => void 0;
const checkTodoAccess = () => void 0;
const findTodo = () => void 0;

const handler = (userId, todoId) => {
	const user = findUser(userId);
	checkTodoAccess(user);
	return findTodo(todoId);
};

console.log(handler.toString());

What is the expected behavior?

(userId, todoId) => {
        const user = findUser(userId);
        checkTodoAccess(user);
        return findTodo(todoId);
}

What do you see instead?

(userId, todoId) => {
  const user = findUser(userId);
  return checkTodoAccess(user), findTodo(todoId);
}

Additional information

I figured if the return value of a function is not used it gets appended with a comma somehow?

@simylein simylein added the bug Something isn't working label Jun 8, 2023
@mikrosystheme
Copy link

This is very bad and in violation of the ES specs. Function.prototype.toString() is required to return the exact source text used to define the function, whitespaces included. Is it at least possible to disable the minifier and/or any source transformation performed by Bun (given a javascript source file)?

@Jarred-Sumner
Copy link
Collaborator

Bun runs the JavaScript minifier on every file by default, which means the input source is not exactly the output source. It's reasonable to suggest we modify .toString to be the sourcemap'd source code, though it would incur an additional performance hit and won't always be possible

On the other hand, if you must rely on the output of Function.toString() to be correct, you can stick // @bun at the top of the file and Bun will skip running the transpiler on it. Note that this can cause various things to break, Bun relies on the transpiler at runtime for certain features like bun:test, the bun import (it is rewritten to globalThis.Bun), JSX, TypeScript support, and a handful of other things.

@nektro
Copy link
Member

nektro commented May 24, 2024

are you still able to reproduce this? it appears to be working for me in Bun 1.1.9 (you may need to run bun upgrade)

❯ bun-debug index.js 
(userId, todoId) => {
  const user = findUser(userId);
  checkTodoAccess(user);
  return findTodo(todoId);
}

@Jarred-Sumner
Copy link
Collaborator

@nektro this should continue to occur. We don't do quite as much minification as we did before, but we still do remove comments, some conditional expressions, and we transform function statements into function expressions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants