mirror of
https://github.com/jashkenas/coffeescript.git
synced 2022-11-09 12:23:24 -05:00
allowing any manner of indentation in the comments, by adjusting them in the lexer
This commit is contained in:
parent
942572d081
commit
a6539a030c
2 changed files with 27 additions and 0 deletions
|
@ -77,6 +77,7 @@ module CoffeeScript
|
|||
end
|
||||
puts "original stream: #{@tokens.inspect}" if ENV['VERBOSE']
|
||||
close_indentation
|
||||
adjust_comments
|
||||
remove_mid_expression_newlines
|
||||
move_commas_outside_outdents
|
||||
add_implicit_indentation
|
||||
|
@ -244,6 +245,24 @@ module CoffeeScript
|
|||
end
|
||||
end
|
||||
|
||||
# Massage newlines and indentations so that comments don't have to be
|
||||
# correctly indented, or appear on their own line.
|
||||
def adjust_comments
|
||||
scan_tokens do |prev, token, post, i|
|
||||
next unless token[0] == :COMMENT
|
||||
before, after = @tokens[i - 2], @tokens[i + 2]
|
||||
if before && after &&
|
||||
((before[0] == :INDENT && after[0] == :OUTDENT) ||
|
||||
(before[0] == :OUTDENT && after[0] == :INDENT)) &&
|
||||
before[1] == after[1]
|
||||
@tokens.delete_at(i + 2)
|
||||
@tokens.delete_at(i - 2)
|
||||
elsif !["\n", :INDENT, :OUTDENT].include?(prev[0])
|
||||
@tokens.insert(i, ["\n", Value.new("\n", token[1].line)])
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Some blocks occur in the middle of expressions -- when we're expecting
|
||||
# this, remove their trailing newlines.
|
||||
def remove_mid_expression_newlines
|
||||
|
|
8
test/fixtures/execution/test_funky_comments.coffee
vendored
Normal file
8
test/fixtures/execution/test_funky_comments.coffee
vendored
Normal file
|
@ -0,0 +1,8 @@
|
|||
func: =>
|
||||
false
|
||||
false # comment
|
||||
false
|
||||
# comment
|
||||
true
|
||||
|
||||
print(func())
|
Loading…
Reference in a new issue