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

🐛 Typescripts "Module" syntax is invalid for typescript-enabled common js #2114

Closed
1 task done
JacobLey opened this issue Mar 17, 2024 · 0 comments · Fixed by #2116
Closed
1 task done

🐛 Typescripts "Module" syntax is invalid for typescript-enabled common js #2114

JacobLey opened this issue Mar 17, 2024 · 0 comments · Fixed by #2116

Comments

@JacobLey
Copy link

Environment information

CLI:
  Version:                      1.6.1
  Color support:                true

Platform:
  CPU Architecture:             aarch64
  OS:                           linux

Environment:
  BIOME_LOG_DIR:                unset
  NO_COLOR:                     unset
  TERM:                         "xterm-256color"
  JS_RUNTIME_VERSION:           "v20.11.1"
  JS_RUNTIME_NAME:              "node"
  NODE_PACKAGE_MANAGER:         unset

Biome Configuration:
  Status:                       Loaded successfully
  Formatter disabled:           false
  Linter disabled:              true
  Organize imports disabled:    false
  VCS disabled:                 false

Workspace:
  Open Documents:               0

What happened?

  1. Create a file called index.cts (this is a forced commonjs file, but it is typescript so we are allowed to used use ESM-like syntax: https://www.typescriptlang.org/play?module=1#code/KYDwDg9gTgLgBAE2AMwIYFcA28DecDGAVgM4BccMU6wcAvgNxA).
  2. Add an export to the file using ESM-like syntax (e.g. export default { cjs: true };)
  3. Run biome format .
  4. Get parsing error:
./src/tests/data/cjs.cts:1:1 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

  × Illegal use of an export declaration outside of a module
  
  > 1 │ export default { cjs: true };
      │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    2 │ 
  
  i not allowed inside scripts

See playground: https://biomejs.dev/playground/?code=ZQB4AHAAbwByAHQAIABkAGUAZgBhAHUAbAB0ACAAewAgAGMAagBzADoAIAB0AHIAdQBlACAAfQA7AAoA&typescript=false&jsx=false&script=true

Expected result

Biome should recognize that this is a typescript file (.cts not .cjs), and therefore ESM-like syntax is allowed.

Perhaps more generally, maybe it should recognize that some other transpiler will be touching this code before execution, so seemingly invalid code is actually allowed and formatting should proceed as usual.

To be clear, the intention of this error is correct. If I were using import/export in a .cjs file (or a .js file with { "type": "commonjs" }) I would expect this error to occur.

In the Biome playground I needed to set the source type to Script to get the failure, which explicitly disables the ability to enable typescript. I suspect there is some failure to recognize .cts as module+typescript, rather than commonjs.

Code of Conduct

  • I agree to follow Biome's Code of Conduct
tylerbutler added a commit to microsoft/FluidFramework that referenced this issue Mar 22, 2024
[Version
1.6.2](https://github.com/biomejs/biome/releases/tag/cli%2Fv1.6.2)
includes a fix we'll want for formatting client code, which is [support
for module syntax in .cts
files](biomejs/biome#2114).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant