swc-project / swc

Rust-based platform for the Web
https://swc.rs
Apache License 2.0
31.02k stars 1.22k forks source link

swc can't handle unescaped `\0` #2853

Closed SukkaW closed 2 years ago

SukkaW commented 2 years ago

Describe the bug

I am developing a Rollup plugin based on swc (using swc as a transformer).

Rollup uses \0 as its virtual module identifier, e.g. rollup's commonjs module uses \0commonjsHelpers.js as the name of its virtual module, which would emit lines like this:

import * as commonjsHelpers from "\0commonjsHelpers.js";

However, swc would throw Syntax Error on \0:

     Error: error: Unterminated string constant

  |
1 | import * as commonjsHelpers from "
  |                                  ^

Caused by:
    0: failed to process js file
    1: Syntax Error

Input code

// Minimum reproduction
// Check on repl.it: https://replit.com/@SukkaW/SweetSmallModulus

const { transformSync: swcTransform } = require('@swc/core');
const { transformSync: esbuildTransform } = require('esbuild');

const code = `
import * as commonjsHelpers from "\0commonjsHelpers.js";
`;

try {
  console.log(esbuildTransform(code, {}));
} catch (e) {
  console.trace(e);
}

try {
  console.log(swcTransform(code), {});
} catch (e) {
  console.trace(e);
}

// This is the minimum repro, kaboom!
try {
  console.log(swcTransform(`const a = "\0a"`), {});
} catch (e) {
  console.trace(e);
}

Config

{}

Playground link

No response

Expected behavior

Shouldn't throw Syntax Error on \0, either escape it (no matter the given target) silently or leave it as it is.

Version

1.2.111

Additional context

No response

swc-bot commented 1 year ago

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.