-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Alias for module.exports.x = x
#40228
Conversation
This fixes #40155 in a surprisingly small amount of code.
const m = function () { | ||
// I have no idea what to put here |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added m
here because otherwise the generated code was export { unknown as methods }
, which is correct, and better than before, but caused an error in the emitted .d.ts that I didn't want.
Once it's bound as an alias, once In the case of a lot of JS stuff, I added implementations for some things to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You'll probably also wanna add some module.exports.x = a.b.c
and module.exports = class Foo {}
style tests, too (since those should make aliases, too!)
src/compiler/binder.ts
Outdated
SymbolFlags.Property | SymbolFlags.ExportValue | SymbolFlags.Class : | ||
SymbolFlags.Property | SymbolFlags.ExportValue; | ||
declareSymbol(symbol.exports!, symbol, node.left, flags, SymbolFlags.None); | ||
const flags = isIdentifier(node.right) ? SymbolFlags.Alias |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While isIdentifier
is very narrow and works, we should probably reuse the isAliasableExpression
condition we use for exportAssignmentIsAlias
which is used for bindExportAssignment
, so the rules for what gets an alias vs not is consistent for all possibly-an-alias expressions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that isAliasableExpression
is isEntityNameExpression || isClassExpression
; previously module.x = class { }
was not handled as aan alias, but doing so is more consistent. I'm going to see whether it breaks any of our tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated tests, it was an improvement overall. (But it does not, on its own, make module.exports.x = NS.C
work; I'm looking to see what else needs to happen)
OK, the newest commit treats any aliasable expression as an alias. This applies to classes, too, so I removed the ad-hoc code that was in the binder before. I think this is an improvment. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks pretty good! It might be worth testing having some references to K
inside K
's body's types, just to check that local names of aliased things are getting serialized correctly in declaration emit.
|
||
|
||
//// [mod1.d.ts] | ||
export var K: { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm - can we have an issue/update to get this to emit as
declare namespace NS {
class _K {
values(): void;
}
export {_K as K};
}
import _K = NS.K;
export {_K as K};
? That'd be more precise, and is reasonable enough given the (alias) symbol shape of the input, IMO. I imagine the serializer just isn't handling the NS nesting well right now, and it is falling back to a var
-based emit, even though it all uses alias symbols.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding Edit: Works in the non-nested case, though. |
…tPropertyOfType` e350c35 (microsoft#40228) introduced a subtle bug: it switched the flags to an alias, dropping `SymbolFlags.Property` --- and that makes `symbolIsValue()` get to the `resolveAlias(symbol)` call, which leads to `getPropertyOfType()` with`resolved.callSignatures`+`constructSignatures` being `undefined`. So initialize them in `setStructuredTypeMembers` before calling `getNamedMembers()`. Fixes microsoft#42350
…tPropertyOfType` e350c35 (microsoft#40228) introduced a subtle bug: it switched the flags to an alias, dropping `SymbolFlags.Property` --- and that makes `symbolIsValue()` get to the `resolveAlias(symbol)` call, which leads to `getPropertyOfType()` with`resolved.callSignatures`+`constructSignatures` being `undefined`. So initialize them in `setStructuredTypeMembers` before calling `getNamedMembers()`. Fixes microsoft#42350
…tPropertyOfType` e350c35 (#40228) introduced a subtle bug: it switched the flags to an alias, dropping `SymbolFlags.Property` --- and that makes `symbolIsValue()` get to the `resolveAlias(symbol)` call, which leads to `getPropertyOfType()` with`resolved.callSignatures`+`constructSignatures` being `undefined`. So initialize them in `setStructuredTypeMembers` before calling `getNamedMembers()`. Fixes #42350
Component commits: ed26816 Avoid getting undefined `callSignatures`/`constructSignatures` in `getPropertyOfType` e350c35 (microsoft#40228) introduced a subtle bug: it switched the flags to an alias, dropping `SymbolFlags.Property` --- and that makes `symbolIsValue()` get to the `resolveAlias(symbol)` call, which leads to `getPropertyOfType()` with`resolved.callSignatures`+`constructSignatures` being `undefined`. So initialize them in `setStructuredTypeMembers` before calling `getNamedMembers()`. Fixes microsoft#42350
Component commits: ed26816 Avoid getting undefined `callSignatures`/`constructSignatures` in `getPropertyOfType` e350c35 (#40228) introduced a subtle bug: it switched the flags to an alias, dropping `SymbolFlags.Property` --- and that makes `symbolIsValue()` get to the `resolveAlias(symbol)` call, which leads to `getPropertyOfType()` with`resolved.callSignatures`+`constructSignatures` being `undefined`. So initialize them in `setStructuredTypeMembers` before calling `getNamedMembers()`. Fixes #42350 Co-authored-by: Eli Barzilay <eli@barzilay.org>
This fixes #40155 in a surprisingly small amount of code.
@weswigham any idea how alias resolution Just Works in the rest of the compiler?