Skip to content

Fix #4747: Flow local variables #4753

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

Merged
merged 12 commits into from
Oct 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 33 additions & 5 deletions lib/coffeescript/nodes.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion lib/coffeescript/scope.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 23 additions & 2 deletions src/nodes.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ exports.Base = class Base
# `compileToFragments` method has logic for outputting comments.
unshiftCommentFragment commentFragment
else
fragments.push @makeCode '' if fragments.length is 0
if commentFragment.unshift
fragments[0].precedingComments ?= []
fragments[0].precedingComments.push commentFragment
Expand Down Expand Up @@ -570,7 +571,13 @@ exports.Block = class Block extends Base
fragments.push @makeCode '\n' if i
fragments.push @makeCode "#{@tab}var "
if declars
fragments.push @makeCode scope.declaredVariables().join(', ')
declaredVariables = scope.declaredVariables()
for declaredVariable, declaredVariablesIndex in declaredVariables
fragments.push @makeCode declaredVariable
if Object::hasOwnProperty.call o.scope.comments, declaredVariable
fragments.push o.scope.comments[declaredVariable]...
if declaredVariablesIndex isnt declaredVariables.length - 1
fragments.push @makeCode ', '
if assigns
fragments.push @makeCode ",\n#{@tab + TAB}" if declars
fragments.push @makeCode scope.assignedVariables().join(",\n#{@tab + TAB}")
Expand Down Expand Up @@ -2155,7 +2162,7 @@ exports.Assign = class Assign extends Base
message = isUnassignable name.value
name.error message if message

# `moduleDeclaration` can be `'import'` or `'export'`
# `moduleDeclaration` can be `'import'` or `'export'`.
@checkAssignability o, name
if @moduleDeclaration
o.scope.add name.value, @moduleDeclaration
Expand All @@ -2167,6 +2174,20 @@ exports.Assign = class Assign extends Base
'param'
else
o.scope.find name.value
# If this assignment identifier has one or more herecomments
# attached, output them as part of the declarations line (unless
# other herecomments are already staged there) for compatibility
# with Flow typing. Don’t do this if this assignment is for a
# class, e.g. `ClassName = class ClassName {`, as Flow requires
# the comment to be between the class name and the `{`.
if name.comments and not o.scope.comments[name.value] and
@value not instanceof Class and
name.comments.every((comment) -> comment.here and not comment.multiline)
commentsNode = new IdentifierLiteral name.value
commentsNode.comments = name.comments
commentFragments = []
@compileCommentFragments o, commentsNode, commentFragments
o.scope.comments[name.value] = commentFragments

if @value instanceof Code
if @value.isStatic
Expand Down
4 changes: 3 additions & 1 deletion src/scope.litcoffee
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ Initialize a scope with its parent, for lookups up the chain,
as well as a reference to the **Block** node it belongs to, which is
where it should declare its variables, a reference to the function that
it belongs to, and a list of variables referenced in the source code
and therefore should be avoided when generating variables.
and therefore should be avoided when generating variables. Also track comments
that should be output as part of variable declarations.

constructor: (@parent, @expressions, @method, @referencedVars) ->
@variables = [{name: 'arguments', type: 'arguments'}]
@comments = {}
@positions = {}
@utilities = {} unless @parent

Expand Down
34 changes: 34 additions & 0 deletions test/comments.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -1073,3 +1073,37 @@ test "#4706: Flow comments for function spread", ->
var method;

method = (...rest/*: Array<string> */) => {};'''

test "#4747: Flow comments for local variable declaration", ->
eqJS 'a ###: number ### = 1', '''
var a/*: number */;

a = 1;
'''

test "#4747: Flow comments for local variable declarations", ->
eqJS '''
a ###: number ### = 1
b ###: string ### = 'c'
''', '''
var a/*: number */, b/*: string */;

a = 1;

b = 'c';
'''

test "#4747: Flow comments for local variable declarations with reassignment", ->
eqJS '''
a ###: number ### = 1
b ###: string ### = 'c'
a ### some other comment ### = 2
''', '''
var a/*: number */, b/*: string */;

a = 1;

b = 'c';

a/* some other comment */ = 2;
'''