Skip to content

After parsing identifiers that start with two underscore another underscore is prepended #14013

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

Closed
soda0289 opened this issue Feb 11, 2017 · 10 comments
Labels
API Relates to the public API for TypeScript Design Limitation Constraints of the existing architecture prevent this from being fixed

Comments

@soda0289
Copy link

TypeScript Version: 2.1.5

Code

console.log(__dirname);

Expected behavior:
When the above typescript is parsed into an AST I would expect the Identifier of the function argument __dirname to start with two underscores.

Actual behavior:

AST Example here:
https://astexplorer.net/#/gist/7b4386c740b3ba88a996a6f644a215c8/857121c5ac1b3cba6edb0ee8c499cb66e9919772

Identifiers prefixed with one underscore are parsed correctly, so are identifiers prefixed with three underscores.

These bugs are probably related:
#11902
#3268

@mhegazy
Copy link
Contributor

mhegazy commented Feb 13, 2017

The internal symbol tables were represented as regular objects (though we mostlly use ES6 maps now, we still support systems with no native Map implementation). The compiler adds an extra underscore, to avoid the __proto__ being considered as a user-defined entry. The expectation is that the user will use unescapeIdentifier function when getting the name of the identifier. APIs for dealing with symbols/node already do this conversion.

@mhegazy mhegazy added Design Limitation Constraints of the existing architecture prevent this from being fixed API Relates to the public API for TypeScript labels Feb 13, 2017
@soda0289
Copy link
Author

@mhegazy Thanks for the information. I will use that call when retrieving the identifier name.

@ajafff
Copy link
Contributor

ajafff commented Feb 13, 2017

@mhegazy How are developers supposed to use unescapeIdentifier? There seems to be no declaration for this funcion in both versions 2.1.6 and 2.2.0.

@soda0289
Copy link
Author

I also wish this was documented somewhere.

@mhegazy
Copy link
Contributor

mhegazy commented Feb 13, 2017

@mhegazy How are developers supposed to use unescapeIdentifier? There seems to be no declaration for this funcion in both versions 2.1.6 and 2.2.0.

The assumption is that users will not need to do that. Can you elaborate on what you are trying to do?

We can expose the helper function on the ts namespace like other helpers.

@soda0289
Copy link
Author

@mhegazy I'd like the function to be exposed. I use the typescript-eslint-parser and it marks variables that start with two underscores as undefined since they are not matched to globals correctly.

@mhegazy
Copy link
Contributor

mhegazy commented Feb 13, 2017

sure.. feel free to send a PR to remove the /** @internal */ comment from its declaration.

@JamesHenry
Copy link
Contributor

Thanks for following up on this @soda0289!

And thanks for your support as always @mhegazy 😄 was great to have the chance to chat with you the other week.

Just to be 100% clear on what @soda0289 is saying - the typescript-eslint-parser will be the consumer of this API, because we need to make sure that all identifiers in the final ESTree AST that it outputs match up with the original source.

The behaviour he describes around actually marking variables as undefined is separate (performed within relevant ESLint rules, core or otherwise), but naturally it is all based off the ESTree AST as the source of truth, so the underlying point is valid.

I will submit a PR to make unescapeIdentifier public shortly!

@JamesHenry
Copy link
Contributor

Here is the PR #14076. I used a72abc8 as an example

@soda0289
Copy link
Author

Nice! Thanks @JamesHenry

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
API Relates to the public API for TypeScript Design Limitation Constraints of the existing architecture prevent this from being fixed
Projects
None yet
Development

No branches or pull requests

4 participants