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

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

Overview

A visitor for checking that all nodes are properly nested.

Constant Summary collapse

CONTROL_NODES =
[Sass::Tree::EachNode, Sass::Tree::ForNode, Sass::Tree::IfNode, Sass::Tree::WhileNode]
SCRIPT_NODES =
[Sass::Tree::ImportNode, Sass::Tree::MixinNode] + CONTROL_NODES
VALID_EXTEND_PARENTS =
[Sass::Tree::RuleNode, Sass::Tree::MixinDefNode]
VALID_FUNCTION_CHILDREN =
[
  Sass::Tree::CommentNode,  Sass::Tree::DebugNode, Sass::Tree::ReturnNode,
  Sass::Tree::VariableNode, Sass::Tree::WarnNode
] + CONTROL_NODES
VALID_IMPORT_PARENTS =
[
  Sass::Tree::IfNode,   Sass::Tree::ForNode, Sass::Tree::WhileNode,
  Sass::Tree::EachNode, Sass::Tree::MixinDefNode
]
VALID_PROP_CHILDREN =
[Sass::Tree::CommentNode, Sass::Tree::PropNode, Sass::Tree::MixinNode] + CONTROL_NODES
VALID_PROP_PARENTS =
[Sass::Tree::RuleNode, Sass::Tree::PropNode,
Sass::Tree::MixinDefNode, Sass::Tree::DirectiveNode]

Constants inherited from Base

Base::NODE_NAME_RE

Instance Method Summary collapse

Methods inherited from Base

#node_name, visit, #visit_if

Instance Method Details

#invalid_charset_parent?(parent, child) ⇒ Boolean (protected)

Returns:

  • (Boolean)


47
48
49
# File 'lib/sass/tree/visitors/check_nesting.rb', line 47

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

#invalid_extend_parent?(parent, child) ⇒ Boolean (protected)

Returns:

  • (Boolean)


52
53
54
55
56
# File 'lib/sass/tree/visitors/check_nesting.rb', line 52

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

#invalid_function_child?(parent, child) ⇒ Boolean (protected)

Returns:

  • (Boolean)


66
67
68
69
70
# File 'lib/sass/tree/visitors/check_nesting.rb', line 66

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

#invalid_function_parent?(parent, child) ⇒ Boolean (protected)

Returns:

  • (Boolean)


58
59
60
# File 'lib/sass/tree/visitors/check_nesting.rb', line 58

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

#invalid_import_parent?(parent, child) ⇒ Boolean (protected)

Returns:

  • (Boolean)


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/sass/tree/visitors/check_nesting.rb', line 76

def invalid_import_parent?(parent, child)
  if is_any_of?(@real_parent, VALID_IMPORT_PARENTS)
    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

#invalid_import_real_parent?(parent, child) ⇒ Boolean (protected)

Returns:

  • (Boolean)


92
93
94
# File 'lib/sass/tree/visitors/check_nesting.rb', line 92

def invalid_import_real_parent?(parent, child)
  
end

#invalid_mixindef_parent?(parent, child) ⇒ Boolean (protected)

Returns:

  • (Boolean)


96
97
98
# File 'lib/sass/tree/visitors/check_nesting.rb', line 96

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

#invalid_prop_child?(parent, child) ⇒ Boolean (protected)

Returns:

  • (Boolean)


101
102
103
104
105
# File 'lib/sass/tree/visitors/check_nesting.rb', line 101

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

#invalid_prop_parent?(parent, child) ⇒ Boolean (protected)

Returns:

  • (Boolean)


109
110
111
112
113
# File 'lib/sass/tree/visitors/check_nesting.rb', line 109

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

#invalid_return_parent?(parent, child) ⇒ Boolean (protected)

Returns:

  • (Boolean)


115
116
117
# File 'lib/sass/tree/visitors/check_nesting.rb', line 115

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)



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/sass/tree/visitors/check_nesting.rb', line 5

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



22
23
24
25
26
27
28
29
30
# File 'lib/sass/tree/visitors/check_nesting.rb', line 22

def visit_children(parent)
  old_parent = @parent
  @parent = parent unless is_any_of?(parent, SCRIPT_NODES)
  old_real_parent, @real_parent = @real_parent, parent
  super
ensure
  @parent = old_parent
  @real_parent = old_real_parent
end

#visit_import(node) (protected)



39
40
41
42
43
44
45
# File 'lib/sass/tree/visitors/check_nesting.rb', line 39

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)



32
33
34
35
36
37
# File 'lib/sass/tree/visitors/check_nesting.rb', line 32

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