Class: Sass::Tree::Visitors::CheckNesting

Inherits:
Base
  • Object
show all
Defined in:
/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/tree/visitors/check_nesting.rb

Overview

A visitor for checking that all nodes are properly nested.

Constant Summary

Instance Method Summary (collapse)

Methods inherited from Base

node_name, visit, #visit_if

Constructor Details

- (CheckNesting) initialize (protected)

Returns a new instance of CheckNesting



5
6
7
8
9
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/tree/visitors/check_nesting.rb', line 5

def initialize
  @parents = []
  @parent = nil
  @current_mixin_def = nil
end

Instance Method Details

- (Boolean) invalid_charset_parent?(parent, child) (protected)

Returns:

  • (Boolean)


89
90
91
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/tree/visitors/check_nesting.rb', line 89

def invalid_charset_parent?(parent, child)
  "@charset may only be used at the root of a document." unless parent.is_a?(Sass::Tree::RootNode)
end

- (Boolean) invalid_content_parent?(parent, child) (protected)

Returns:

  • (Boolean)


80
81
82
83
84
85
86
87
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/tree/visitors/check_nesting.rb', line 80

def invalid_content_parent?(parent, child)
  if @current_mixin_def
    @current_mixin_def.has_content = true
    nil
  else
    "@content may only be used within a mixin."
  end
end

- (Boolean) invalid_extend_parent?(parent, child) (protected)

Returns:

  • (Boolean)


94
95
96
97
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/tree/visitors/check_nesting.rb', line 94

def invalid_extend_parent?(parent, child)
  return if is_any_of?(parent, VALID_EXTEND_PARENTS)
  "Extend directives may only be used within rules."
end

- (Boolean) invalid_function_child?(parent, child) (protected)

Returns:

  • (Boolean)


127
128
129
130
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/tree/visitors/check_nesting.rb', line 127

def invalid_function_child?(parent, child)
  return if is_any_of?(child, VALID_FUNCTION_CHILDREN)
  "Functions can only contain variable declarations and control directives."
end

- (Boolean) invalid_function_parent?(parent, child) (protected)

Returns:

  • (Boolean)


118
119
120
121
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/tree/visitors/check_nesting.rb', line 118

def invalid_function_parent?(parent, child)
  return if (@parents.map {|p| p.class} & INVALID_IMPORT_PARENTS).empty?
  "Functions may not be defined within control directives or other mixins."
end

- (Boolean) invalid_import_parent?(parent, child) (protected)

Returns:

  • (Boolean)


101
102
103
104
105
106
107
108
109
110
111
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/tree/visitors/check_nesting.rb', line 101

def invalid_import_parent?(parent, child)
  unless (@parents.map {|p| p.class} & INVALID_IMPORT_PARENTS).empty?
    return "Import directives may not be used within control directives or mixins."
  end
  return if parent.is_a?(Sass::Tree::RootNode)
  return "CSS import directives may only be used at the root of a document." if child.css_import?
rescue Sass::SyntaxError => e
  e.modify_backtrace(:filename => child.imported_file.options[:filename])
  e.add_backtrace(:filename => child.filename, :line => child.line)
  raise e
end

- (Boolean) invalid_mixindef_parent?(parent, child) (protected)

Returns:

  • (Boolean)


113
114
115
116
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/tree/visitors/check_nesting.rb', line 113

def invalid_mixindef_parent?(parent, child)
  return if (@parents.map {|p| p.class} & INVALID_IMPORT_PARENTS).empty?
  "Mixins may not be defined within control directives or other mixins."
end

- (Boolean) invalid_prop_child?(parent, child) (protected)

Returns:

  • (Boolean)


135
136
137
138
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/tree/visitors/check_nesting.rb', line 135

def invalid_prop_child?(parent, child)
  return if is_any_of?(child, VALID_PROP_CHILDREN)
  "Illegal nesting: Only properties may be nested beneath properties."
end

- (Boolean) invalid_prop_parent?(parent, child) (protected)

Returns:

  • (Boolean)


142
143
144
145
146
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/tree/visitors/check_nesting.rb', line 142

def invalid_prop_parent?(parent, child)
  return if is_any_of?(parent, VALID_PROP_PARENTS)
  "Properties are only allowed within rules, directives, mixin includes, or other properties." +
    child.pseudo_class_selector_message
end

- (Boolean) invalid_return_parent?(parent, child) (protected)

Returns:

  • (Boolean)


148
149
150
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/tree/visitors/check_nesting.rb', line 148

def invalid_return_parent?(parent, child)
  "@return may only be used within a function." unless parent.is_a?(Sass::Tree::FunctionNode)
end

- visit(node) (protected)



11
12
13
14
15
16
17
18
19
20
21
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/tree/visitors/check_nesting.rb', line 11

def visit(node)
  if (error = @parent && (
      try_send(@parent.class.invalid_child_method_name, @parent, node) ||
      try_send(node.class.invalid_parent_method_name, @parent, node)))
    raise Sass::SyntaxError.new(error)
  end
  super
rescue Sass::SyntaxError => e
  e.modify_backtrace(:filename => node.filename, :line => node.line)
  raise e
end

- visit_children(parent) (protected)



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/tree/visitors/check_nesting.rb', line 26

def visit_children(parent)
  old_parent = @parent

  # When checking a static tree, resolve at-roots to be sure they won't send
  # nodes where they don't belong.
  if parent.is_a?(Sass::Tree::AtRootNode) && parent.resolved_value
    old_parents = @parents
    @parents = @parents.reject {|p| parent.exclude_node?(p)}
    @parent = Sass::Util.enum_with_index(@parents.reverse).
      find {|p, i| !transparent_parent?(p, @parents[-i - 2])}.first

    begin
      return super
    ensure
      @parents = old_parents
      @parent = old_parent
    end
  end

  unless transparent_parent?(parent, old_parent)
    @parent = parent
  end

  @parents.push parent
  begin
    super
  ensure
    @parent = old_parent
    @parents.pop
  end
end

- visit_import(node) (protected)



65
66
67
68
69
70
71
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/tree/visitors/check_nesting.rb', line 65

def visit_import(node)
  yield
rescue Sass::SyntaxError => e
  e.modify_backtrace(:filename => node.children.first.filename)
  e.add_backtrace(:filename => node.filename, :line => node.line)
  raise e
end

- visit_mixindef(node) (protected)



73
74
75
76
77
78
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/tree/visitors/check_nesting.rb', line 73

def visit_mixindef(node)
  @current_mixin_def, old_mixin_def = node, @current_mixin_def
  yield
ensure
  @current_mixin_def = old_mixin_def
end

- visit_root(node) (protected)



58
59
60
61
62
63
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/tree/visitors/check_nesting.rb', line 58

def visit_root(node)
  yield
rescue Sass::SyntaxError => e
  e.sass_template ||= node.template
  raise e
end