google / google/closure-compiler

Compiler warnings when referencing inherited non-public property in getter/setter

Open
#2,261 0 comments 0 reactions 1 assignee Claimed by @MatrixFrog View on GitHub
ES6
Dominant language
JavaScript
Stars
7.7k
Forks
1.2k
Avg merge
2d 12h
Merged PRs (30d)
6

Description

Given:

```js
// a.js

export class A {
constructor() {
/** @protected {string} */
this.protected_ = '';
}
}
```

```js
// b.js

import {A} from './a';

class B extends A {
constructor() {
super();
/** @const {string} */
this.publicProp;
}

/**
* @return {string}
*/
get publicProp() {
return this.protected_;
}

/** @return {string} */
getPublicProp() {
return this.protected_;
}
}

const b = new B();
b.publicProp = '?';
console.log(b.publicProp);
```

Running:

```sh
java -jar ../node_modules/google-closure-compiler/compiler.jar --compilation_level ADVANCED \
--js *.js \
--language_out ECMASCRIPT5_STRICT \
--entry_point b.js \
--checks_only
```

Yields:

```
b.js:14: WARNING - Access to protected property protected_ of B not allowed here.
return this.protected_;
^^^^^^^^^^^^^^^

0 error(s), 1 warning(s), 94.0% typed
```

Note how `this.protected_` could be accessed within the regular method, but not within the getter/setter.

Discovered this while doing work for https://github.com/material-components/material-components-web/issues/134. We make heavy use of getters and setters due to our desire to align with DOM APIs for native elements, so I'd be willing to help fix this if someone could point me in the right direction :)

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.