Closure-compiler: Extending computed class name identifiers throws:

Created on 4 Dec 2016  路  20Comments  路  Source: google/closure-compiler

Input:

var a = {
  foo: class {}
}

class bar extends a['foo']{

}

CC throws:

JSC_DYNAMIC_EXTENDS_TYPE: The class in an extends clause must be a qualified name. at line 6 character 18
class bar extends a['foo']{
                  ^

Is this by design? The JS code is valid.

P2

Most helpful comment

I ran into this issue and decided to take a stab at fixing it (despite never having touched the Closure Compiler codebase before...). As far as I can tell, the most elegant solution is to allow CC to consider terms like a.b['c'] as a qualified name, similar (but not equivalent!) to a.b.c.

I don't want to submit a PR for this as my fix isn't perfect -- it fails some tests -- but perhaps somebody more familiar with the codebase can build upon it and complete it: https://github.com/Treeki/closure-compiler/tree/allow-constant-getelem-in-qualified-names

All 20 comments

I think CC should not treat bracket property accessor with string value as "dynamic". In my example class bar extends a.foo works but class bar extends a['foo'] doesn't while both are the same.

TypeScript compiler can follow the code:

screen shot 2016-12-04 at 10 40 08 am

Can you elaborate a bit on what your use case is? i.e. why you don't want
to write 'a.foo' instead?

Webpack outputs code like this:

class APIClientError extends __WEBPACK_IMPORTED_MODULE_3_helpers__["e" /* CustomError */] {

And I am using CC to minify Webpack output.

Not a duplicate - it's different. Fixing this would be a pretty simple change to the transpilation. Should be non-controversial as well.

Just ran into this as well trying to replace the ever-bloating Babel with CC. Sadly I'm getting otherwise great results, but this error pretty much prevents us from adopting CC.

Webpack outputs code like this:

class APIClientError extends __WEBPACK_IMPORTED_MODULE_3_helpers__["e" /* CustomError */] {

@mohsen1 webpack would only replace that if you are extending a namespace / import / etc. that you have imported. Can you please share the original source?

I don't exactly remember the original code anymore but this is close enough:

// Errors.ts
export class CustomError {
// ....
}
// SomeFile.ts
import { CustomError } from './Errors.ts';

class APIClientError extends CustomError {
// ...
}

As workaround:

// SomeFile.ts
import { CustomError } from './Errors.ts';

const CustomErrorTemp = CustomError;

class APIClientError extends CustomErrorTemp {
// ...
}

The problem is from cases like this from typescript in ES6 mode:

class APIClientError extends CustomError<API> {
// ...
}

we cant do:

const CustomErrorTemp = CustomError<API>;

You should be able to do that with type:

type CustomErrorTemp = CustomError<API>;

class APIClientError extends CustomErrorTemp {
// ...
}

Hmm error:

error TS2693: 'CustomErrorTemp' only refers to a type, but is being used as a value here.

oh right... sorry for misleading comment

I ran into this issue and decided to take a stab at fixing it (despite never having touched the Closure Compiler codebase before...). As far as I can tell, the most elegant solution is to allow CC to consider terms like a.b['c'] as a qualified name, similar (but not equivalent!) to a.b.c.

I don't want to submit a PR for this as my fix isn't perfect -- it fails some tests -- but perhaps somebody more familiar with the codebase can build upon it and complete it: https://github.com/Treeki/closure-compiler/tree/allow-constant-getelem-in-qualified-names

I deal with this limitation by manually extracting the base into a variable. I wonder if applying such a transform mechanically could be a correct resolution for this issue.

I.e. for the code in the first comment, the rewriting could be:

var a = {
  foo: class {}
}

var barBase0 = a['foo'];
class bar extends barBase0 {
  // ...
}

Will this be typed as expected?

Hmm, I see I unassigned MatrixFrog here, but I didn't intend to; I was just posting a comment. I don't think I even have a permission to assign or unassign anyone here. Please disregard and re-assign the issue back if you are an owner.

I'm also experiencing this issue with webpack + closure compiler. Is there any ETA on a fix?

See #2997

Closed by #2997

Was this page helpful?
0 / 5 - 0 ratings

Related issues

JohannBlake picture JohannBlake  路  3Comments

rongjiecomputer picture rongjiecomputer  路  3Comments

Muhammad-Salman-Sheikh picture Muhammad-Salman-Sheikh  路  6Comments

agrafix picture agrafix  路  5Comments

tSte picture tSte  路  5Comments