Sha256: 82e0122cdc17276b65a9ab1dc693fe7d325d5a4889948843beac62b75fed33d3

Contents?: true

Size: 1.96 KB

Versions: 27

Compression:

Stored size: 1.96 KB

Contents

require 'ruby2js'

module Ruby2JS
  module Filter
    module CamelCase
      include SEXP

      WHITELIST = %w{
        attr_accessor
      }

      def camelCase(symbol)
        symbol.to_s.gsub(/_[a-z]/) {|match| match[1].upcase}
      end

      def on_send(node)
        if node.children[0] == nil and WHITELIST.include? node.children[1].to_s
          super
        elsif node.children[1] =~ /_.*\w$/
          super S(:send , node.children[0],
            camelCase(node.children[1]), *node.children[2..-1])
        else
          super
        end
      end

      def on_def(node)
        if node.children[0] =~ /_.*\w$/
          super S(:def , camelCase(node.children[0]), *node.children[1..-1])
        else
          super
        end
      end

      def on_optarg(node)
        if node.children[0] =~ /_.*\w$/
          super S(:optarg , camelCase(node.children[0]), *node.children[1..-1])
        else
          super
        end
      end

      def on_lvar(node)
        if node.children[0] =~ /_.*\w$/
          super S(:lvar , camelCase(node.children[0]), *node.children[1..-1])
        else
          super
        end
      end

      def on_arg(node)
        if node.children[0] =~ /_.*\w$/
          super S(:arg , camelCase(node.children[0]), *node.children[1..-1])
        else
          super
        end
      end

      def on_lvasgn(node)
        if node.children[0] =~ /_.*\w$/
          super S(:lvasgn , camelCase(node.children[0]), *node.children[1..-1])
        else
          super
        end
      end

      def on_sym(node)
        if node.children[0] =~ /_.*\w$/
          super S(:sym , camelCase(node.children[0]), *node.children[1..-1])
        else
          super
        end
      end

      def on_defs(node)
        if node.children[1] =~ /_.*\w$/
          super S(:defs , node.children[0],
            camelCase(node.children[1]), *node.children[2..-1])
        else
          super
        end
      end
    end

    DEFAULTS.push CamelCase
  end
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
ruby2js-3.3.6 lib/ruby2js/filter/camelCase.rb
ruby2js-3.3.5 lib/ruby2js/filter/camelCase.rb
ruby2js-3.3.4 lib/ruby2js/filter/camelCase.rb
ruby2js-3.3.3 lib/ruby2js/filter/camelCase.rb
ruby2js-3.3.2 lib/ruby2js/filter/camelCase.rb
ruby2js-3.3.1 lib/ruby2js/filter/camelCase.rb
ruby2js-3.3.0 lib/ruby2js/filter/camelCase.rb
ruby2js-3.2.0 lib/ruby2js/filter/camelCase.rb
ruby2js-3.1.2 lib/ruby2js/filter/camelCase.rb
ruby2js-3.1.1 lib/ruby2js/filter/camelCase.rb
ruby2js-3.1.0 lib/ruby2js/filter/camelCase.rb
ruby2js-3.0.15 lib/ruby2js/filter/camelCase.rb
ruby2js-3.0.14 lib/ruby2js/filter/camelCase.rb
ruby2js-3.0.13 lib/ruby2js/filter/camelCase.rb
ruby2js-3.0.12 lib/ruby2js/filter/camelCase.rb
ruby2js-3.0.11 lib/ruby2js/filter/camelCase.rb
ruby2js-3.0.10 lib/ruby2js/filter/camelCase.rb
ruby2js-3.0.9 lib/ruby2js/filter/camelCase.rb
ruby2js-3.0.8 lib/ruby2js/filter/camelCase.rb
ruby2js-3.0.7 lib/ruby2js/filter/camelCase.rb