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

Inherits:
Base
  • Object
show all
Defined in:
/var/www/sass-pages/.sass/lib/sass/tree/visitors/check_nesting.rb

Overview

A visitor for checking that all nodes are properly nested.

Constant Summary

Instance Method Summary

Methods inherited from Base

#node_name, visit, #visit_if

Constructor Details

- (CheckNesting) initialize (protected)

A new instance of CheckNesting



5
6
7
# File '/var/www/sass-pages/.sass/lib/sass/tree/visitors/check_nesting.rb', line 5

def initialize
  @parents = []
end

Instance Method Details

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

Returns:

  • (Boolean)


48
49
50
# File '/var/www/sass-pages/.sass/lib/sass/tree/visitors/check_nesting.rb', line 48

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_extend_parent?(parent, child) (protected)

Returns:

  • (Boolean)


53
54
55
56
57
58
59
60
61
62
63
64
# File '/var/www/sass-pages/.sass/lib/sass/tree/visitors/check_nesting.rb', line 53

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

  if !child.disabled? &&
      directive = @parents.find {|p| p.is_a?(Sass::Tree::DirectiveNode)}
    child.disable!
    Sass::Util.sass_warn "DEPRECATION WARNING on line \#{child.line}\#{\" of \#{child.filename}\" if child.filename}:\n  Using @extend within directives (e.g. \#{directive.name}) is deprecated.\n  It will be an error in Sass 3.2.\n  This will only work once @extend is supported natively in the browser.\n"
    nil
  end
end

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

Returns:

  • (Boolean)


80
81
82
83
84
# File '/var/www/sass-pages/.sass/lib/sass/tree/visitors/check_nesting.rb', line 80

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

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

Returns:

  • (Boolean)


72
73
74
# File '/var/www/sass-pages/.sass/lib/sass/tree/visitors/check_nesting.rb', line 72

def invalid_function_parent?(parent, child)
  "Functions may only be defined at the root of a document." unless parent.is_a?(Sass::Tree::RootNode)
end

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

Returns:

  • (Boolean)


87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File '/var/www/sass-pages/.sass/lib/sass/tree/visitors/check_nesting.rb', line 87

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?
  # If this is a nested @import, we need to make sure it doesn't have anything
  # that's legal at top-level but not in the current context (e.g. mixin defs).
  child.imported_file.to_tree.children.each {|c| visit(c)}
  nil
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)


103
104
105
# File '/var/www/sass-pages/.sass/lib/sass/tree/visitors/check_nesting.rb', line 103

def invalid_mixindef_parent?(parent, child)
  "Mixins may only be defined at the root of a document." unless parent.is_a?(Sass::Tree::RootNode)
end

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

Returns:

  • (Boolean)


108
109
110
111
112
# File '/var/www/sass-pages/.sass/lib/sass/tree/visitors/check_nesting.rb', line 108

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

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

Returns:

  • (Boolean)


116
117
118
119
120
# File '/var/www/sass-pages/.sass/lib/sass/tree/visitors/check_nesting.rb', line 116

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

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

Returns:

  • (Boolean)


122
123
124
# File '/var/www/sass-pages/.sass/lib/sass/tree/visitors/check_nesting.rb', line 122

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)



9
10
11
12
13
14
15
16
17
18
19
# File '/var/www/sass-pages/.sass/lib/sass/tree/visitors/check_nesting.rb', line 9

def visit(node)
  if error = @parent && (
      try_send("invalid_#{node_name @parent}_child?", @parent, node) ||
      try_send("invalid_#{node_name node}_parent?", @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)



23
24
25
26
27
28
29
30
31
# File '/var/www/sass-pages/.sass/lib/sass/tree/visitors/check_nesting.rb', line 23

def visit_children(parent)
  old_parent = @parent
  @parent = parent unless is_any_of?(parent, SCRIPT_NODES)
  @parents.push parent
  super
ensure
  @parent = old_parent
  @parents.pop
end

- visit_import(node) (protected)



40
41
42
43
44
45
46
# File '/var/www/sass-pages/.sass/lib/sass/tree/visitors/check_nesting.rb', line 40

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_root(node) (protected)



33
34
35
36
37
38
# File '/var/www/sass-pages/.sass/lib/sass/tree/visitors/check_nesting.rb', line 33

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