lib/ruby2js.rb in ruby2js-3.5.3 vs lib/ruby2js.rb in ruby2js-3.6.0

- old
+ new

@@ -59,17 +59,22 @@ class Processor < Parser::AST::Processor include Ruby2JS::Filter BINARY_OPERATORS = Converter::OPERATORS[2..-1].flatten - attr_accessor :prepend_list + attr_accessor :prepend_list, :disable_autoimports def initialize(comments) @comments = comments + + # check if magic comment is present: + first_comment = @comments.values.first&.map(&:text)&.first + @disable_autoimports = first_comment&.include?(" autoimports: false") + @disable_autoexports = first_comment&.include?(" autoexports: false") + @ast = nil @exclude_methods = [] - @esm = false @prepend_list = Set.new end def options=(options) @options = options @@ -139,10 +144,11 @@ def on_for_of(node); on_for(node); end def on_in?(node); on_send(node); end def on_method(node); on_send(node); end def on_prop(node); on_array(node); end def on_prototype(node); on_begin(node); end + def on_send!(node); on_send(node); end def on_sendw(node); on_send(node); end def on_undefined?(node); on_defined?(node); end def on_nil(node); end def on_xnode(node); end def on_export(node); end @@ -186,11 +192,11 @@ elsif Parser::AST::Node === source ast, comments = source, {} source = ast.loc.expression.source_buffer.source else ast, comments = parse( source, options[:file] ) - comments = Parser::Source::Comment.associate(ast, comments) if ast + comments = ast ? Parser::Source::Comment.associate(ast, comments) : {} end filters = (options[:filters] || Filter::DEFAULTS) unless filters.empty? @@ -205,13 +211,14 @@ filter = filter.new(comments) filter.options = options ast = filter.process(ast) - if filter.prepend_list + unless filter.prepend_list.empty? prepend = filter.prepend_list.sort_by {|ast| ast.type == :import ? 0 : 1} - ast = Parser::AST::Node.new(:begin, [*prepend, ast], location: ast.location) + prepend.reject! {|ast| ast.type == :import} if filter.disable_autoimports + ast = Parser::AST::Node.new(:begin, [*prepend, ast]) end end ruby2js = Ruby2JS::Converter.new(ast, comments) @@ -237,9 +244,11 @@ ruby2js.enable_vertical_whitespace if source.include? "\n" ruby2js.convert ruby2js.timestamp options[:file] + + ruby2js.file_name = options[:file] || ast&.loc&.expression&.source_buffer&.name || '' ruby2js end def self.parse(source, file=nil, line=1)