Skip to content

this in class fields is misinterpreted #2775

@coreyfarrell

Description

@coreyfarrell
  • Rollup Version: 1.7.0
  • Operating System (or Browser): Fedora building for Chrome 72+
  • Node Version: 10.15.0

How Do We Reproduce?

Minimum reproduction is at https://github.com/coreyfarrell/rollup-class-fields (I was not able to figure out how to add an acorn parser with REPL). rollup.config.js demonstrates the issue (produces broken.js), rollup.workaround.js shows how I'm dealing with this for now, produces workaround.js. Rollup can be run against both configs using npm install && npm test.

Expected Behavior

The this keyword found anyplace within a class body should not be considered top-level. For static class fields this is the class, for public class fields it refers to the instance.

Actual Behavior

this in the following code gets replaced with undefined:

class Example {
  a = 1;
  b = this.a + 1;
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions