Repository navigation
[patch] Honour IsStatic and IsConstant on JavaScript fields - #157
Merged
Merged
Conversation
The JavaScript generator wrote every FieldDeclaration as `name = value;`. Inside a class, a static or constant field became an ordinary instance field, so `Counter.MAX` was undefined. At file or namespace scope, the declaration was a bare assignment to an undeclared name, which throws ReferenceError when an ES module loads and leaks a global in a script. A class field is now written `static` when it is static or constant, matching the VariableDeclaration path. A field outside a class is declared `const` when it is a constant with an initializer, and `let` otherwise, the same way locals are. Fixes #104 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01N47Hdpq8iSMutiKdDS81pa
…atic-fields # Conflicts: # Coder.Test/Languages/JavaScriptGeneratorTests.cs
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Fixes #104
Problem
JavaScriptGenerator.GenerateFieldDeclarationwrote everyFieldDeclarationasname = value;and never readIsStaticorIsConstant. This broke in two places:Counter.MAXwasundefined.ReferenceErrorwhen an ES module loads, and creates a global in a classic script.Change
FieldDeclarationto a newGenerateClassField. It writesstaticwhen the field isIsStatic || IsConstant, matching the existingVariableDeclaration-as-field path.GenerateFieldDeclarationwritesconstfor a constant with an initializer andletotherwise. This is the ruleGenerateVariableDeclarationalready uses for locals, sinceconstneeds an initializer.WriteFieldhelper.Tests
New tests in
JavaScriptGeneratorTests:static, and that an instance field is not.const Limit = 10;,let total = 0;, andlet Pending;for an uninitialized constant.Counter.MAX === 99, thatMAXis not on instances, and thatLimitdoes not leak ontoglobalThis. It is inconclusive when node is not on the path.With the generator change reverted, all three tests fail. The full suite passes: 1057/1057.
This branch is independent of #156 (the
super()fix for #103). Both touch the class-bodyswitchinGenerateClassDeclaration, on adjacent lines. Whichever merges second may need a trivial rebase.🤖 Generated with Claude Code
https://claude.ai/code/session_01N47Hdpq8iSMutiKdDS81pa
Generated by Claude Code