google / google/closure-compiler

Inlining values from an enum-getter

Open
#3,695 2 comments 0 reactions 0 assignees View on GitHub
enhancement P2
Dominant language
JavaScript
Stars
7.7k
Forks
1.2k
Avg merge
2d 12h
Merged PRs (30d)
6

Description

**summary**
I'm currently working on a [PR for AMP](https://github.com/ampproject/amphtml/pull/30304#discussion_r492403836) in which we'd like to use a `@const` to assist with inlining a variable. An issue has come up where we are required to use an enum-getter as opposed to directly using the enum, but this seems to deopt.

For example, this code inlines as expected:
```js
/** @enum {string} */
const a = { cx1: 'hello', cx2: 'world'};
console.log(a.cx1, a.cx2); // compiles down to: console.log('hello', 'world');
```

Whereas when a getter is introduced, it stops inlining:
```js
/** @enum {string} */
const a = { cx1: 'hello', cx2: 'world'};

/** @const @function(): typeof a */
const getA = () => a;

console.log(getA().cx1, getA().cx2); // compiles down to: console.log(a.cx1)
```

Is this an intentional deopt? Is there a workaround?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.