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

Un used variable is emmitted while transpiling enums. #4620

Open
madhurakhal opened this issue Sep 3, 2015 · 5 comments
Open

Un used variable is emmitted while transpiling enums. #4620

madhurakhal opened this issue Sep 3, 2015 · 5 comments
Labels
Help Wanted You can do this Suggestion An idea for TypeScript
Milestone

Comments

@madhurakhal
Copy link

TypeScript code


module Workspace {
  export enum State {
    IN_PROGRESS,
    BUFFER_RENDERED,
    SKIPPED,
    TIME_OUT
  }
}

Compled JavaScript Code.

 
var Workspace;
(function (Workspace) {
    (function (State) {
        State[State["IN_PROGRESS"] = 0] = "IN_PROGRESS";
        State[State["BUFFER_RENDERED"] = 1] = "BUFFER_RENDERED";
        State[State["SKIPPED"] = 2] = "SKIPPED";
        State[State["TIME_OUT"] = 3] = "TIME_OUT";
    })(Workspace.State || (Workspace.State = {}));
    var State = Workspace.State; // this code is use no where.
})(Workspace || (Workspace = {}));

I am using TypeScript version 1.5.3

@xLama
Copy link

xLama commented Sep 3, 2015

It´s nccessary to use State enum. Consider this example:

TS

module Workspace {
  export enum State {
    IN_PROGRESS,
    BUFFER_RENDERED,
    SKIPPED,
    TIME_OUT
  }

  class A{
      buffer:State = State.BUFFER_RENDERED;
  }

} 

JAVASCRIPT

var Workspace;
(function (Workspace) {
    (function (State) {
        State[State["IN_PROGRESS"] = 0] = "IN_PROGRESS";
        State[State["BUFFER_RENDERED"] = 1] = "BUFFER_RENDERED";
        State[State["SKIPPED"] = 2] = "SKIPPED";
        State[State["TIME_OUT"] = 3] = "TIME_OUT";
    })(Workspace.State || (Workspace.State = {}));
    var State = Workspace.State;
    var A = (function () {
        function A() {
            this.buffer = State.BUFFER_RENDERED;
        }
        return A;
    })();
})(Workspace || (Workspace = {}));

But I don´t understand this:


module Workspace {
   export enum State {
    IN_PROGRESS,
    BUFFER_RENDERED,
    SKIPPED,
    TIME_OUT
  }
} 


var Workspace;
(function (Workspace) {
    (function (State) {
        State[State["IN_PROGRESS"] = 0] = "IN_PROGRESS";
        State[State["BUFFER_RENDERED"] = 1] = "BUFFER_RENDERED";
        State[State["SKIPPED"] = 2] = "SKIPPED";
        State[State["TIME_OUT"] = 3] = "TIME_OUT";
    })(Workspace.State || (Workspace.State = {}));
    var State = Workspace.State;
})(Workspace || (Workspace = {}));


JS could be:


var Workspace;
(function (Workspace) {
    var State;
    (function (State) {
        State[State["IN_PROGRESS"] = 0] = "IN_PROGRESS";
        State[State["BUFFER_RENDERED"] = 1] = "BUFFER_RENDERED";
        State[State["SKIPPED"] = 2] = "SKIPPED";
        State[State["TIME_OUT"] = 3] = "TIME_OUT";
    })(State || (State = {}));
    Workspace.State = State;
})(Workspace || (Workspace = {}));

Instead of :

var Workspace;
(function (Workspace) {
    (function (State) {
        State[State["IN_PROGRESS"] = 0] = "IN_PROGRESS";
        State[State["BUFFER_RENDERED"] = 1] = "BUFFER_RENDERED";
        State[State["SKIPPED"] = 2] = "SKIPPED";
        State[State["TIME_OUT"] = 3] = "TIME_OUT";
    })(Workspace.State || (Workspace.State = {}));
    var State = Workspace.State;
})(Workspace || (Workspace = {}));



@mhegazy mhegazy added Suggestion An idea for TypeScript In Discussion Not yet reached consensus labels Sep 3, 2015
@madhurakhal
Copy link
Author

Yep your code looks perfectly fine and reasonable when using the State enum at the same file. But In my case I am using State enum in different files. which means the only enum related code is there. So

var State = Workspace.State;

is unused one and also I am using Visual Studio Code which also complain the same.

Use case seniorio in another file after importing the enum (I am using backbone model)

this.model.set('state', Workspace.State.BUFFER_RENDERED);

In my point of view.

var State = Workspace.State;

should be there iff the State enum is used in same file.

Thanks

@DanielRosenwasser
Copy link
Member

@madhurakhal I don't think removing unused declarations is something we're aiming to achieve - a namespace is certainly consumable by others if any other code occurs after this. This is an optimization that linters and/or optimizers could work on.


@xLama

JS could be

var Workspace;
(function (Workspace) {
    var State;
    (function (State) {
        State[State["IN_PROGRESS"] = 0] = "IN_PROGRESS";
        State[State["BUFFER_RENDERED"] = 1] = "BUFFER_RENDERED";
        State[State["SKIPPED"] = 2] = "SKIPPED";
        State[State["TIME_OUT"] = 3] = "TIME_OUT";
    })(State || (State = {}));
    Workspace.State = State;
})(Workspace || (Workspace = {}));

The idea point is that enums merge with prior declarations (hence the Workspace.State || (Workspace.State = {}).) In your example, you create a variable that is always going to start out undefined (State), then initialize it, and then overwrite Worspace.State which isn't the desired behavior.

namespace E {
    export const C = 20;
}

enum E {
    A = 10,
    B = 20,
}

@RyanCavanaugh
Copy link
Member

@rbuckton is this fixed in the transforms branch?

@RyanCavanaugh RyanCavanaugh added Help Wanted You can do this and removed In Discussion Not yet reached consensus labels Aug 22, 2016
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 2.1 milestone Aug 22, 2016
@RyanCavanaugh
Copy link
Member

RyanCavanaugh commented Aug 22, 2016

Accepting PRs for the transforms branch / 2.1 to make the emit the same as the namespace emit code (declare a var above the IIFE, then assign to the var in the IIFE arguments)

@mhegazy mhegazy modified the milestones: Community, TypeScript 2.1 Sep 29, 2016
@RyanCavanaugh RyanCavanaugh modified the milestones: Community, Backlog Mar 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Help Wanted You can do this Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

5 participants