Skip to content

undefined, NaN, and Infinity ident in decl is rewritten #8336

@lucacasonato

Description

@lucacasonato

Describe the bug

This is a follow up to #8317.

Defining a const with ident undefined / NaN / Infinity is syntactically and semantically valid in modules and function bodies. It is also syntactically valid in scripts, but because globalThis.undefined, globalThis.NaN, and globalThis.Infinity are not writable or configurable it fails at runtime in a script.

Input code

const undefined = 0;
console.log(undefined);

const NaN = 0;
console.log(NaN);

const Infinity = 0;
console.log(Infinity);

Config

{
  "jsc": {
    "parser": {
      "syntax": "typescript",
      "tsx": true
    },
    "target": "es2022",
    "loose": false,
    "minify": {
      "compress": false,
      "mangle": false
    },
    "transform": {
      "react": {
        "importSource": "npm:react",
        "runtime": "automatic"
      }
    }
  },
  "module": {
    "type": "es6"
  },
  "minify": false,
  "isModule": true
}

Playground link (or link to the minimal reproduction)

https://play.swc.rs/?version=1.3.98-nightly-20231118.1&code=H4sIAAAAAAAAA0vOzysuUSjNS0lNy8xLTVGwVTCw5uVKBorm56Tq5eSna8DlNIESEKkSBb9EPyxKgaLIijzzgPoySyqxqIRJaVoDAKcqOpmBAAAA&config=H4sIAAAAAAAAA1WQPQ7CMAyFd05ReWZAHRi4AxMnsIJbBTU%2Fsh2JCvXuJOn%2FEiXfe8%2BO%2Fbs0DXzEwKP55Wt%2BRGQh3t6ZyOgVv5mAjpHEsI0K11VVKZJyokqmWQBF7klLiKS9te0SgCEEoYw7HIQW5qy33XhsaYKLTCJnY7Gi74ctf%2B7I6KUL7I6FmNDoAWRkXQysr5DYlELgo3vMtutu4uTVuqpj0uBQrYFFni7rWVuDC%2B9U%2F7RssGxpHvwOu2mdcZsHrDzXZF3f9Ae8XWptjQEAAA%3D%3D

SWC Info output

No response

Expected behavior

const undefined = 0;
console.log(undefined);

const NaN = 0;
console.log(NaN);

const Infinity = 0;
console.log(Infinity);

Actual behavior

const undefined1 = 0;
console.log(undefined);

const NaN1 = 0;
console.log(NaN);

const Infinity1 = 0;
console.log(Infinity);

Version

1.3.98-nightly-20231118.1

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions