Distinct var declaration proposal: x @= 3 #5477
Draft
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.
This PR is a
+55/-0
(very small!) diff on top of #5475: see the comparison at https://github.com/cosmicexplorer/coffeescript/compare/scope-refactoring...cosmicexplorer:coffeescript:distinct-var-decl?expand=1.Problem
We would like to be able to attach JSDoc comments to variable declarations for two reasons:
.d.ts
) for our exported interfaces.As discussed in the thread at #5307 (comment), the current mechanism of generating
var
declarations implicitly and declaring them all at once in a single statement at the top of the function means users are unable to associate a block comment with a specific variable declaration (we currently coalesce all comments in assignments together on top of the singlevar
statement):We want to be able to associate that particular comments with
x
andx
alone.Solution
With #5475, this becomes much easier:
@=
) and a new node (Declaration
) to separate it fromAssign
behavior entirely.@=
is only called at the top level of avar
scope (either a function scope or the top-level scope).@=
is the first assignment in the scope, and that there is exactly one@=
in the scope for a given variable name.Result
For valid distinct declarations, block comments are correctly assigned to the output so that JSDoc syntax can be observed:
tsc
integration prototypeI have added a script
build-support/typescript-compile.coffee
which performs typechecking againsttsc
and rewrites error locations using source maps. I'm not sure how reliable this technique is since I don't really know how source maps are supposed to work, but it seems to work in extremely small examples: