-
Notifications
You must be signed in to change notification settings - Fork 656
Enum and namespace transformer #284
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
Conversation
I've finished most of the |
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.
This seems okay to me, though this PR still has the "changed emit" which I don't remember if were okay with or not compared to using the checker (I am personally okay with it for sure).
type EmitFlags uint32 | ||
|
||
const ( | ||
EFSingleLine EmitFlags = 1 << iota // The contents of this node should be emitted on a single line. |
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 think we only have one other case where we do this naming scheme, but, I can't really complain 😄
8d12a16
to
324f750
Compare
internal/binder/binder.go
Outdated
@@ -767,7 +767,7 @@ func (b *Binder) bindModuleDeclaration(node *ast.Node) { | |||
} | |||
|
|||
func (b *Binder) declareModuleSymbol(node *ast.Node) ModuleInstanceState { | |||
state := getModuleInstanceState(node, nil /*visited*/) | |||
state := GetModuleInstanceState(node, nil /*visited*/) |
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.
https://github.com/microsoft/typescript-go/pull/296/files#diff-9969a067bafd18e3dafe06418204ef65b894c09827a5c98ca142d7cb2e7be4e3R1930 actually moves this all to the AST package, which might be good to pull out into a PR before these two?
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.
Still LGTM
77c705c
to
2d46183
Compare
This introduces transformer to handle
enum
andnamespace
emit to JavaScript. This transformer does not currently supportconst enum
inlining, nor does it support unqualifiedenum
member name resolution in merged enums (i.e.,enum E { A }; enum E { B = A }
), nor unqualifiednamespace
export references in merged namespaces (i.e.,namespace N { export var x = 1; } namespace N { x; }
.In order to reduce the dependence on the type checker, this transformer pushes some of the logic for auto-incrementing
enum
members to the runtime. When possible, the transformer uses a number of heuristics to simplify theenum
emit.The following are some examples of the new emit:
Auto-numbered
enum
(matches existing emit)
Auto-numbered
enum
with explicit start(matches existing emit)
String
enum
(matches existing emit)
Auto-numbered
enum
with self reference(changed emit)
In this example, we introduce a local
A
binding that is used when evalutatingE.B
. From that point on, we use the varaibleauto
to track further auto-incrementing at runtime.Reference to external variable
(changed emit)
This emulates at runtime what we validate at check time. In the old compiler, if the checker determined that
x
ory
werestring
types, then the reverse mapping would not be applied.Auto-incrementing after external variable reference
(changed emit)
The old compiler would report an error if you continued using auto-numbered enum members after an enum member initializer that did not produce a numeric literal type. In the new emitter we assume that this check succeeded and resume auto-numbering, as an error would signify that the results were unreliable.
In the old compiler, the above emit is type-directed as it changes based on the type of
x
:However, in the new emit we produce the same output in both cases, which produces the correct result in the non-error case.