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

Incorrect transform when namespace is empty #59053

Closed
Roise-yue opened this issue Jun 27, 2024 · 1 comment
Closed

Incorrect transform when namespace is empty #59053

Roise-yue opened this issue Jun 27, 2024 · 1 comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@Roise-yue
Copy link

🔎 Search Terms

namespace empty commonjs export

🕗 Version & Regression Information

  • This is the behavior in every version I tried(Including the latest version)

⏯ Playground Link

No response

💻 Code

namespace x {}
export = x;

🙁 Actual behavior

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });

🙂 Expected behavior

var x = {};
module.exports = x;

Additional information about the issue

The configuration information is as follows:
tsc V5.5.2
node.js:v20.14.0

tsconfig.json

{
  "compilerOptions": {
    "target": "ESNext",
    "module": "CommonJS",
    "moduleResolution": "Node",
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "lib": ["es2023","dom"]
  },
  "include": [
    "TScorpus/*"
  ],
  "exclude": [
    "node_modules"
  ]
}

package.json

{
  "name": "tscfuzz",
  "version": "1.0.0",
  "main": "index.js",
  "directories": {
    "lib": "lib"
  },
  "scripts": {
    "test": "nyc --reporter=text --reporter=lcov mocha "
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@babel/cli": "^7.24.7",
    "@babel/core": "^7.24.7",
    "@babel/preset-env": "^7.24.7",
    "@babel/preset-typescript": "^7.24.7",
    "@types/node": "^20.14.9",
    "jshint": "^2.13.6",
    "mocha": "^10.4.0",
    "nyc": "^15.1.0"
  },
  "description": ""
}
@DanielRosenwasser DanielRosenwasser added the Working as Intended The behavior described is the intended behavior; this is not a bug label Jun 29, 2024
@DanielRosenwasser
Copy link
Member

namespace declarations come in two forms - instantiated and uninstantiated namespaces. When a namespace has no runtime statements, it is considered uninstantiated, and it therefore has no emit.

You can get an idea of what sorts of statements cause a namespace to be emitted or not (playground link):

namespace n {
    // Empty
}

namespace n {
    // Types only
    type Foo = number;

    interface Bar {}
}

namespace n {
    // Empty statement
    ;
}

namespace n {
    // Variable declaration
    export let x = 10;
}

namespace n {
    // Expression statements
    x++;
    x = x ** 2;
}

namespace n {
    // Classes
    class C {
    }
}

Output:

"use strict";
var n;
(function (n) {
    // Empty statement
    ;
})(n || (n = {}));
(function (n) {
    // Variable declaration
    n.x = 10;
})(n || (n = {}));
(function (n) {
    // Expression statements
    n.x++;
    n.x = n.x ** 2;
})(n || (n = {}));
(function (n) {
    // Classes
    class C {
    }
})(n || (n = {}));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

2 participants