oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
73.01k stars 2.66k forks source link

Add flag to disable transpiler #5646

Open jimmywarting opened 11 months ago

jimmywarting commented 11 months ago

What version of Bun is running?

1.0.2+37edd5a6e389265738e89265bcbdf2999cb81a49

What platform is your computer?

Darwin 22.4.0 arm64 arm

What steps can reproduce the bug?

function f() {
  // program to check an Armstrong number of three digits

  let sum = 0
  const number = prompt('Enter a three-digit positive integer: ')

  // create a temporary variable
  let temp = number
  while (temp > 0) {
      // finding the one's digit
      let remainder = temp % 10

      sum += remainder * remainder * remainder

      // removing last digit from the number
      temp = parseInt(temp / 10) // convert float into integer
  }
  // check the condition
  if (sum == number) {
      console.log(`${number} is an Armstrong number`)
  }
  else {
      console.log(`${number} is not an Armstrong number.`)
  }
}

console.log(f.toString())

bun run script.js

What is the expected behavior?

To see that the source code is exactly the same, but it's not.

What do you see instead?

transformed code.

function() {
  let sum = 0;
  const number = prompt("Enter a three-digit positive integer: ");
  let temp = number;
  while (temp > 0) {
    let remainder = temp % 10;
    sum += remainder * remainder * remainder, temp = parseInt(temp / 10);
  }
  if (sum == number)
    console.log(`${number} is an Armstrong number`);
  else
    console.log(`${number} is not an Armstrong number.`);
}

Additional information

This is bad... the code are not exactly the same. i expected them to be the same, and i must rely on them being exactly the same.

code transformation should be disabled by default.

Jarred-Sumner commented 11 months ago

Bun's transpiler is what enables JSX, TypeScript, CommonJS and more to "just work". We are not going to disable the transpiler by default.

If you want to disable the transpiler for your code, you can stick // @bun as a comment at the top of the file.

jimmywarting commented 11 months ago

That's just weird... are there any other option to opt into disable it then? like using bunfig.toml?

I don't want to have to add this comment to every file.

Perhaps you can disable it for just plain vanilla .js files?

Electroid commented 11 months ago

We could add a CLI flag for this, which would be the same behaviour as if every file had // @bun at the top.

jimmywarting commented 11 months ago

both a CLI flag and a bunfig.toml config would be 👍

ayonli commented 11 months ago

@Jarred-Sumner After added // @bun to the top of the file, I got this error

SyntaxError: Unexpected token '{'. Expected 'from' before imported module name.
      at /Users/ayon/Workspace/jsext/example.ts:4

Any ideas?

This is my code:

// @bun
import util, { InspectOptions } from "node:util";
import type { Ensured } from "./index.ts";
ayonli commented 11 months ago

Is there any way not to remove the comments? I tried ts-node and deno, they both keep the comment as is, it's so odd that Bun removed them. I'd like to do some analysis based on the comment in the source code returned from the toString() function.

jimmywarting commented 11 months ago

@ayonli

you are using TypeScript specific syntax. like import type ... I have just learned that // @bun disables the transpiler. So it would expect your code to be pure vanilla javascript. hence why you get the Syntax Error "Unexpected token '{'. Expected 'from' before imported module name"

ayonli commented 11 months ago

@jimmywarting And this directive seems odd too, it is more like @no-bun instead of @bun.

jimmywarting commented 11 months ago

just the name "bun" isn't such a good word in itself. it dose not describe what the comment dose or what the meaning of it is

following typescript convention (@ts-check @ts-expect-error @ts-ignore @ts-nocheck) then i think a better name for it would be @bun-notransform or @bun-ignore