swc icon indicating copy to clipboard operation
swc copied to clipboard

Class fields named 'export' not allowed when using decorators

Open maclockard opened this issue 2 years ago • 0 comments

Describe the bug

When using the option "decorators": true, using the name export for class fields and methods is no longer allowed, returning this error:

  × `export` is not allowed here
   ╭─[file.js:3:3]
 3 │ export = true;
   · ──────
   ╰────


Caused by:
    0: failed to process input file
    1: Syntax Error
Error: Failed to compile 1 file with swc.
    at Object.assertCompilationResult (/Users/maclockard/workspace/hex-inc/hex/node_modules/@swc/cli/lib/swc/util.js:113:15)
    at files (/Users/maclockard/workspace/hex-inc/hex/node_modules/@swc/cli/lib/swc/file.js:173:18)
    at async _default (/Users/maclockard/workspace/hex-inc/hex/node_modules/@swc/cli/lib/swc/file.js:192:9)

This error occurs for both syntax "ecmascript" and "typescript". Also worth noting with the same config, the name export is allowed if there is not a decorator on the field.

Input code

export class MyClass {
  @MyDecorator()
  export = true;
}

Config

{
  "$schema": "https://json.schemastore.org/swcrc",
  "jsc": {
    "parser": {
      "syntax": "ecmascript",
      "decorators": true
    }
  }
}

Playground link

https://play.swc.rs/?version=1.2.218&code=H4sIAAAAAAAAA0utKMgvKlFIzkksLlbwrXQG09VcCgoOvpUuqcn5RYkl%2BUUamkCBVIhKW4WSotJUa65aAJrlKr86AAAA&config=H4sIAAAAAAAAA0WOQQoDIQxF75K123bhHXqI4GQGy2gkyUBFvHu1WGYX%2Fn%2F%2FkQZvDeAbFBQlmZfWbPgBDxQSapBYDBxsFFjQWBS8yUVuDAe046nUHRjKQTZH%2Bhj0yay0Wgcp5rjX6Q6cipDqXWE%2Bzj%2FZhyjxds2ggdVCP%2BET%2Bu1Yu6ivBc5n%2BhejSMoXxwAAAA%3D%3D

Expected behavior

I would expect swc to be able to compile a class that uses the word export as a field name or method name when using decorators.

Actual behavior

No response

Version

1.2.218

Additional context

Here's an example of typescript compiling the exact same code: playground link (ignore the type error for now, its just because I'm not defining the decorator to keep the code snippet short).

maclockard avatar Jul 22 '22 18:07 maclockard