Skip to content

Pragmas #218

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions lib/ruby2js.rb
Original file line number Diff line number Diff line change
Expand Up @@ -224,15 +224,15 @@ def self.convert(source, options={})
file,line = source.source_location
source = IO.read(file)
ast, comments = parse(source)
comments = Parser::Source::Comment.associate(ast, comments) if ast
comments = Parser::Source::Comment.associate_by_identity(ast, comments) if ast
ast = find_block( ast, line )
options[:file] ||= file
elsif Parser::AST::Node === source
ast, comments = source, {}
source = ast.loc.expression.source_buffer.source
else
ast, comments = parse( source, options[:file] )
comments = ast ? Parser::Source::Comment.associate(ast, comments) : {}
comments = ast ? Parser::Source::Comment.associate_by_identity(ast, comments) : {}
end

# check if magic comment is present
Expand Down
20 changes: 20 additions & 0 deletions lib/ruby2js/converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -203,13 +203,33 @@ def comments(ast)
end
end

def get_comments(ast)
result = []

if Parser::AST::Node === ast
ast.children.each do |child|
comment = @comments[child]
result << comment.map {|c| c.text }.first if comment&.size > 0
more = get_comments(child)
result += more if more.size > 0
end
end

return result
end

def parse(ast, state=:expression)
oldstate, @state = @state, state
oldast, @ast = @ast, ast
return unless ast

handler = @handlers[ast.type]

if state == :statement
@comment = get_comments(ast).first&.strip
@pragma = @comment =~ /#\s+Pragma:\s*([^\s]+)/i ? $1.downcase : nil
end

unless handler
raise Error.new("unknown AST type #{ ast.type }", ast)
end
Expand Down
8 changes: 7 additions & 1 deletion lib/ruby2js/converter/logical.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ class Converter
handle :and, :or do |left, right|
type = @ast.type

default_or = ' || '

if es2020
# Override '||' and use '??'
default_or = ' ?? ' if @pragma == '??'
end

if es2020 and type == :and
node = rewrite(left, right)
Expand All @@ -36,7 +42,7 @@ class Converter
rgroup = true if right.type == :begin

put '(' if lgroup; parse left; put ')' if lgroup
put (type==:and ? ' && ' : ((@or == :nullish and es2020) ? ' ?? ' : ' || '))
put (type==:and ? ' && ' : ((@or == :nullish and es2020) ? ' ?? ' : default_or))
put '(' if rgroup; parse right; put ')' if rgroup
end

Expand Down
11 changes: 9 additions & 2 deletions lib/ruby2js/converter/opasgn.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,15 @@ class Converter
vtype = :ivar if asgn.type == :ivasgn
vtype = :cvar if asgn.type == :cvasgn

if es2021
op = type == :and ? '&&' : (@or == :nullish ? '??' : '||')
if es2020
default_or = '||'

if @pragma == '??'
# Override '||' and use '??'
default_or = '??'
end

op = type == :and ? '&&' : (@or == :nullish ? '??' : default_or)
parse s(:op_asgn, asgn, op, value);
elsif vtype
parse s(asgn.type, asgn.children.first, s(type,
Expand Down
16 changes: 11 additions & 5 deletions lib/ruby2js/converter/super.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,29 @@ class Converter
end

if es2015
add_args = true

if @class_method
parse @class_parent
put '.'
put method.children[0]
add_args = method.is_method?
elsif method.children[0] == :constructor
put 'super'
else
put 'super.'
put method.children[0]
add_args = method.is_method?
end

put '('
cleaned_args = args.map do |arg| # FIX: #212
arg.type == :optarg ? s(:arg, arg.children[0]) : arg
if add_args
put '('
cleaned_args = args.map do |arg| # FIX: #212
arg.type == :optarg ? s(:arg, arg.children[0]) : arg
end
parse s(:args, *cleaned_args)
put ')'
end
parse s(:args, *cleaned_args)
put ')'
else
parse @class_parent

Expand Down
Loading