angular / tsickle

Tsickle — TypeScript to Closure Translator
MIT License
896 stars 111 forks source link

String enums are converted to externs with empty strings enums #1412

Open zuhairtaha opened 1 year ago

zuhairtaha commented 1 year ago

If we have this enum

declare enum IOption {
  Read = 'read',
  Write = 'write'
}

The generated extern is

/** @enum {string} */
var IOption = {
  Read: '',
  Write: '',
};

It should be

/** @enum {string} */
var IOption = {
  Read: 'read',
  Write: 'write',
};
mprobst commented 1 year ago

Can you expand on why you think the value should be there? The value of the enum field in the externs should never matter to Closure Compiler - the compiler doesn't do anything with it.

zuhairtaha commented 1 year ago

I'm converting chrome types to closure compiler externs using tsickle. There are some enums like chrome.declarativeNetRequest.RuleActionType.BLOCK which should be a string, but the issue is I'm getting empty string at the compiled code

mprobst commented 1 year ago

But what piece of the toolchain would be using that string? Closure Compiler doesn't, and by definition the externs are not present at runtime.

zuhairtaha commented 1 year ago

Here is an use case After generating externs for chrome types the RuleActionType enum will be

/** @enum {string} */
chrome.declarativeNetRequest.RuleActionType = {
  BLOCK: '',
  REDIRECT: '',
  ALLOW: '',
  UPGRADE_SCHEME: '',
  MODIFY_HEADERS: '',
  ALLOW_ALL_REQUESTS: '',
};

Then I run closure compiler to compile this javascript code

void chrome.declarativeNetRequest.updateDynamicRules({
  addRules: [
    {
      id: 1,
      priority: 1,
      action: {
        type: chrome.declarativeNetRequest.RuleActionType.MODIFY_HEADERS,
        responseHeaders: [
          {
            header: "Access-Control-Allow-Origin",
            operation: chrome.declarativeNetRequest.HeaderOperation.SET,
            value: "*"
          }
        ]
      },
      condition: {
        urlFilter: "<all_urls>",
        resourceTypes: [chrome.declarativeNetRequest.ResourceType.MAIN_FRAME]
      }
    }
  ],
  removeRuleIds: [1]
});

The issue is that closure compiler will replace chrome.declarativeNetRequest.RuleActionType.MODIFY_HEADERS with an empty string

mprobst commented 1 year ago

You need to make sure closure compiler understands that the externs file is in fact an externs file, either by putting @.***" on top of it or by explicitly passing it as an externs file on the command line.

Closure Compiler should then leave references to these external symbols as they were in the original source.

Message ID: @.***>