Skip to content

[Bug]: functions defined inside a switch case block will be trasformed uncorrectly #14960

@aleen42

Description

@aleen42

💻

  • Would you like to work on a fix?

How are you using Babel?

babel-loader (webpack)

Input code

switch ('what') {
default:
    console.log(a())
    function a() { return 1; }
}

Configuration file name

No response

Configuration

{
    "loose": true, 
    "forceAllTransforms": true,
    "exclude": ["@babel/transform-function-name"]
}

Current and expected behaviour

Current output which results in exceptions:

"use strict";

switch ('what') {
default:
    console.log(a());

    var a = function a() {
      return 1;
    };

}

We had some problems inside IE8 according to #1087 (comment), so we excluded @babel/transform-function-name, and we got output :

"use strict";

switch ('what') {
default:
    console.log(a());

    var a = function () {
      return 1;
    };

}

Expected output:

"use strict";

switch ('what') {
default:
    console.log(a());

    function a() {
      return 1;
    };

}

Environment

  • @babel/core: 7.18.13
  • @babel/preset-env: 7.18.10

Possible solution

Even in the switch case block, babel should analysis whether the function is called before doing the tranformation.

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions