mirror of
https://github.com/jashkenas/coffeescript.git
synced 2026-02-18 11:31:20 -05:00
started raising syntax errors for parens wrapped around expressions (they used to silently be ignored)
This commit is contained in:
@@ -125,13 +125,13 @@ Usage:
|
|||||||
end
|
end
|
||||||
|
|
||||||
# Compile a single source file to JavaScript.
|
# Compile a single source file to JavaScript.
|
||||||
def compile(script, source='')
|
def compile(script, source='error')
|
||||||
begin
|
begin
|
||||||
options = {}
|
options = {}
|
||||||
options[:no_wrap] = true if @options[:no_wrap]
|
options[:no_wrap] = true if @options[:no_wrap]
|
||||||
CoffeeScript.compile(script, options)
|
CoffeeScript.compile(script, options)
|
||||||
rescue CoffeeScript::ParseError => e
|
rescue CoffeeScript::ParseError, SyntaxError => e
|
||||||
STDERR.puts e.message(source)
|
STDERR.puts "#{source}: #{e.message}"
|
||||||
exit(1) unless @options[:watch]
|
exit(1) unless @options[:watch]
|
||||||
nil
|
nil
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -608,7 +608,9 @@ module CoffeeScript
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# An extra set of parenthesis, supplied by the script source.
|
# An extra set of parentheses, supplied by the script source.
|
||||||
|
# You can't wrap parentheses around bits that get compiled into JS statements,
|
||||||
|
# unfortunately.
|
||||||
class ParentheticalNode < Node
|
class ParentheticalNode < Node
|
||||||
attr_reader :expressions
|
attr_reader :expressions
|
||||||
|
|
||||||
@@ -617,7 +619,7 @@ module CoffeeScript
|
|||||||
end
|
end
|
||||||
|
|
||||||
def statement?
|
def statement?
|
||||||
@expressions.statement?
|
@expressions.unwrap.statement?
|
||||||
end
|
end
|
||||||
|
|
||||||
def custom_assign?
|
def custom_assign?
|
||||||
@@ -629,10 +631,11 @@ module CoffeeScript
|
|||||||
end
|
end
|
||||||
|
|
||||||
def compile(o={})
|
def compile(o={})
|
||||||
|
raise SyntaxError, "parentheses can't be wrapped around a statement" if statement?
|
||||||
o = super(o)
|
o = super(o)
|
||||||
compiled = @expressions.compile(o)
|
compiled = @expressions.compile(o)
|
||||||
compiled = compiled[0...-1] if compiled[-1..-1] == ';'
|
compiled = compiled[0...-1] if compiled[-1..-1] == ';'
|
||||||
write(o[:no_paren] || statement? ? compiled : "(#{compiled})")
|
write(o[:no_paren] ? compiled : "(#{compiled})")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -9,9 +9,9 @@ module CoffeeScript
|
|||||||
@token_id, @value, @stack = token_id, value, stack
|
@token_id, @value, @stack = token_id, value, stack
|
||||||
end
|
end
|
||||||
|
|
||||||
def message(source_file=nil)
|
def message
|
||||||
line = @value.respond_to?(:line) ? @value.line : "END"
|
line = @value.respond_to?(:line) ? @value.line : "END"
|
||||||
line_part = source_file ? "#{source_file}:#{line}:" : "line #{line}:"
|
line_part = "line #{line}:"
|
||||||
id_part = @token_id != @value.inspect ? ", unexpected #{@token_id.downcase}" : ""
|
id_part = @token_id != @value.inspect ? ", unexpected #{@token_id.downcase}" : ""
|
||||||
"#{line_part} syntax error for '#{@value.to_s}'#{id_part}"
|
"#{line_part} syntax error for '#{@value.to_s}'#{id_part}"
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -77,4 +77,10 @@ class ParserTest < Test::Unit::TestCase
|
|||||||
assert nodes.compile(:no_wrap => true) == File.read('test/fixtures/generation/each_no_wrap.js')
|
assert nodes.compile(:no_wrap => true) == File.read('test/fixtures/generation/each_no_wrap.js')
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_no_wrapping_parens_around_statements
|
||||||
|
assert_raises(SyntaxError) do
|
||||||
|
@par.parse("(a: 1)").compile
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user