Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Parsing of super/defined? keywords.

Signed-off-by: Yorick Peterse <yorickpeterse@gmail.com>
  • Loading branch information...
commit c13bdd159be693443dab965c35b524d10fbd76aa 1 parent 86f75e2
@YorickPeterse authored
View
1  lib/ruby-lint.rb
@@ -18,6 +18,7 @@
require 'ruby-lint/token/case_token'
require 'ruby-lint/token/regexp_token'
require 'ruby-lint/token/class_token'
+require 'ruby-lint/token/keyword_token'
require 'ruby-lint/parser_error'
require 'ruby-lint/iterator'
require 'ruby-lint/callback'
View
30 lib/ruby-lint/parser.rb
@@ -937,12 +937,12 @@ def on_until(statement, body)
# @return [RubyLint::Token::StatementToken]
#
def on_defined(token)
- return Token::StatementToken.new(
- :type => :defined,
- :statement => token,
- :line => token.line,
- :column => token.column,
- :code => token.code
+ return Token::KeywordToken.new(
+ :name => 'defined?',
+ :parameters => [token],
+ :line => token.line,
+ :column => token.column,
+ :code => token.code
)
end
@@ -952,7 +952,7 @@ def on_defined(token)
# @return [RubyLint::Token::MethodToken]
#
def on_zsuper
- return Token::MethodToken.new(
+ return Token::KeywordToken.new(
:name => 'super',
:line => lineno,
:column => column,
@@ -961,6 +961,22 @@ def on_zsuper
end
##
+ # Called when a call to `super` with parameters is found.
+ #
+ # @param [Array] params An array of parameters passed to the keyword.
+ # @return [RubyLint::Token::KeywordToken]
+ #
+ def on_super(params)
+ return Token::KeywordToken.new(
+ :name => 'super',
+ :parameters => params,
+ :line => lineno,
+ :column => column,
+ :code => code(lineno)
+ )
+ end
+
+ ##
# Called when a variable is referenced.
#
# @param [RubyLint::Token::Token] variable The variable that was referenced.
View
43 lib/ruby-lint/token/keyword_token.rb
@@ -0,0 +1,43 @@
+module RubyLint
+ module Token
+ ##
+ # {RubyLint::Token::KeywordToken} is a token class similar to
+ # {RubyLint::Token::MethodToken} but instead of being used for regular
+ # method calls it's soley used for keywords such as `super` and
+ # `defined?()`.
+ #
+ class KeywordToken < Token
+ ##
+ # Array of tokens for the method parameters.
+ #
+ # @return [Array]
+ #
+ attr_accessor :parameters
+
+ ##
+ # Token containing details about the block passed to the method.
+ #
+ # @return [RubyLint::Token::BlockToken]
+ #
+ attr_accessor :block
+
+ ##
+ # @see RubyLint::Token::Token#initialize
+ #
+ def initialize(*args)
+ @type = :keyword
+
+ super
+
+ @parameters = [] unless @parameters
+ end
+
+ ##
+ # @see RubyLint::Token::Token#child_nodes
+ #
+ def child_nodes
+ return super << @parameters << [@block]
+ end
+ end # KeywordToken
+ end # Token
+end # RubyLint
View
8 lib/ruby-lint/token/method_token.rb
@@ -9,7 +9,6 @@ class MethodToken < Token
##
# The receiver of the method call, if any.
#
- # @since 2012-08-05
# @return [RubyLint::Token::Token]
#
attr_accessor :receiver
@@ -17,7 +16,6 @@ class MethodToken < Token
##
# Symbol containing the method separator, if any.
#
- # @since 2012-08-05
# @return [Symbol]
#
attr_accessor :operator
@@ -25,15 +23,13 @@ class MethodToken < Token
##
# Array of tokens for the method parameters.
#
- # @since 2012-07-29
- # @return [RubyLint::Token::Parameters]
+ # @return [Array]
#
attr_accessor :parameters
##
# Token containing details about the block passed to the method.
#
- # @since 2012-08-05
# @return [RubyLint::Token::BlockToken]
#
attr_accessor :block
@@ -46,7 +42,7 @@ def initialize(*args)
super
- @parameters = ParametersToken.new unless @parameters
+ @parameters = [] unless @parameters
end
##
View
61 spec/ruby-lint/parser/keywords.rb
@@ -0,0 +1,61 @@
+require File.expand_path('../../../helper', __FILE__)
+
+describe 'Rlint::Parser' do
+ it 'Parse a defined? keyword' do
+ token = RubyLint::Parser.new('defined?(Foobar)').parse[0]
+
+ token.class.should == RubyLint::Token::KeywordToken
+ token.type.should == :keyword
+ token.name.should == 'defined?'
+
+ token.parameters.class.should == Array
+ token.parameters.length.should == 1
+
+ param = token.parameters[0]
+
+ param.class.should == RubyLint::Token::VariableToken
+ param.type.should == :constant
+ param.name.should == 'Foobar'
+ end
+
+ it 'Parse a super keyword without parameters' do
+ token = RubyLint::Parser.new('super').parse[0]
+
+ token.class.should == RubyLint::Token::KeywordToken
+ token.type.should == :keyword
+ token.name.should == 'super'
+
+ token.parameters.class.should == Array
+ token.parameters.empty?.should == true
+ end
+
+ it 'Parse a super keyword with parameters' do
+ token = RubyLint::Parser.new('super(10)').parse[0]
+
+ token.class.should == RubyLint::Token::KeywordToken
+ token.type.should == :keyword
+ token.name.should == 'super'
+
+ token.parameters.class.should == Array
+ token.parameters.length.should == 1
+
+ param = token.parameters[0]
+
+ param.class.should == RubyLint::Token::Token
+ param.type.should == :integer
+ param.value.should == '10'
+ end
+
+ it 'Parse a super keyword with a block' do
+ token = RubyLint::Parser.new('super { |f| }').parse[0]
+
+ token.class.should == RubyLint::Token::KeywordToken
+ token.type.should == :keyword
+ token.name.should == 'super'
+
+ token.parameters.class.should == Array
+ token.parameters.empty?.should == true
+
+ token.block.class.should == RubyLint::Token::BlockToken
+ end
+end
View
11 spec/ruby-lint/parser/statements.rb
@@ -588,15 +588,4 @@
token.else.value[0].class.should == RubyLint::Token::VariableToken
token.else.value[0].name.should == 'false'
end
-
- it 'Parse a defined? statement' do
- token = RubyLint::Parser.new('defined?(Foobar)').parse[0]
-
- token.class.should == RubyLint::Token::StatementToken
- token.type.should == :defined
-
- token.statement.class.should == RubyLint::Token::VariableToken
- token.statement.type.should == :constant
- token.statement.name.should == 'Foobar'
- end
end
Please sign in to comment.
Something went wrong with that request. Please try again.