Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warn when properties are accessed through ['literal'] #23

Closed
rkirov opened this issue Jan 27, 2016 · 8 comments
Closed

Warn when properties are accessed through ['literal'] #23

rkirov opened this issue Jan 27, 2016 · 8 comments

Comments

@rkirov
Copy link
Contributor

rkirov commented Jan 27, 2016

Typescript allows property access with square brackets and string literal to access regular properties (ref https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md#4.13)

so the following is valid TS

class A {
  foo: string;
}

var a: A;
var b = a['foo'];

but this will fail spectacularly post minification with closure.

@evmar
Copy link
Contributor

evmar commented Mar 18, 2016

This feature is quite weird. If you create an intermediate variable (let foo = 'foo';) you cannot index into A with it (like A[foo]) -- it only allows string literals. This makes me wonder whether we can just rewrite A['foo'] into A.foo. I guess the former may occur for field names that aren't valid identifiers...

@mprobst
Copy link
Contributor

mprobst commented Mar 18, 2016

Does it output a quoted access, or a non quoted? If it compiles into non
quoted, it'd actually be fine.

Evan Martin notifications@github.com schrieb am Fr., 18. März 2016, 09:23:

This feature is quite weird. If you create an intermediate variable (let
foo = 'foo';) you cannot index into A with it (like A[foo]) -- it only
allows string literals.


You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub
#23 (comment)

@evmar
Copy link
Contributor

evmar commented Mar 19, 2016

It outputs quoted.

@evmar evmar self-assigned this Mar 23, 2016
@evmar
Copy link
Contributor

evmar commented Mar 23, 2016

Got a CL ready that checks this. I am pretty scared I'm accidentally warning on some other, legal thing, so I think I want to put this behind a temporary flag.

@evmar
Copy link
Contributor

evmar commented Jun 2, 2016

Another antipattern I spotted in the same vein:

let data: {[key: string]: string} = {
  foo: "bar",
};

I think you need to quote foo to prevent it from being renamed.

@evmar evmar changed the title sickle should error when properties are accessed through ['literal'] Warn when properties are accessed through ['literal'] Jun 9, 2016
@evmar
Copy link
Contributor

evmar commented Sep 13, 2016

If you have an any, perhaps we should require to use quoted accessors?

@mprobst
Copy link
Contributor

mprobst commented Sep 13, 2016 via email

@rkirov
Copy link
Contributor Author

rkirov commented Apr 16, 2019

Randomly stumbled on this bug, through our documentation. Current status is that this is handled by 'https://tsetse.info/property-renaming-safe'. In any case, we definitely don't want to add such checks into tsickle - tslint and/or tsetse is the right layer for checks.

@rkirov rkirov closed this as completed Apr 16, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants