3bf5683a1baf5c11b1a67b69ab7c74a6ea4ed6db
[lunaix-os.git] / lunaix-os / scripts / build-tools / lcfg2 / rules.py
1 import ast
2
3 from .ast_validator import RuleCollection, rule
4 from lib.utils import Schema
5
6 class SyntaxRule(RuleCollection):
7     NodeAssigment = Schema(ast.Subscript, 
8                             value=Schema(ast.Name, id='__lzLut__'), 
9                             ctx=ast.Store)
10     TrivialValue = Schema(Schema.Union(
11         ast.Constant, 
12         ast.Name
13     ))
14
15     BoolOperators = Schema(Schema.Union(ast.Or, ast.And))
16     
17     TrivialTest    = Schema(ast.Compare, 
18                           left=TrivialValue, 
19                           ops=[Schema.Union(ast.Eq)],
20                           comparators=[ast.Constant])
21     
22     InlineIf       = Schema(ast.IfExp, 
23                             test=Schema.Union(TrivialTest, TrivialValue), 
24                             body=TrivialValue, 
25                             orelse=TrivialValue)
26     
27     TrivialLogic   = Schema(ast.BoolOp, 
28                             op=BoolOperators, 
29                             values=Schema.List(
30                                 Schema.Union(TrivialTest, ast.Name)
31                             ))
32     
33     TrivialReturn  = Schema(Schema.Union(
34         TrivialValue,
35         ast.JoinedStr
36     ))
37
38     def __init__(self):
39         super().__init__()
40     
41     @rule(ast.If, None, "dynamic-logic")
42     def __dynamic_logic(self, reducer, node):
43         """
44         Conditional branching could interfering dependency resolving
45         """
46         return False
47     
48     @rule(ast.While, None, "while-loop")
49     def __while_loop(self, reducer, node):
50         """
51         loop construct may impact with readability.
52         """
53         return False
54     
55     @rule(ast.For, None, "for-loop")
56     def __for_loop(self, reducer, node):
57         """
58         loop construct may impact with readability.
59         """
60         return False
61     
62     @rule(ast.ClassDef, None, "class-def")
63     def __class_definition(self, reducer, node):
64         """
65         use of custom class is not recommended
66         """
67         return False
68     
69     @rule(ast.Dict, None, "complex-struct")
70     def __complex_datastruct(self, reducer, node):
71         """
72         use of complex data structure is not recommended
73         """
74         return False
75     
76     @rule(ast.Subscript, NodeAssigment, "side-effect-option")
77     def __side_effect(self, reducer, node):
78         """
79         Option modifying other options dynamically unpredictable behaviour
80         """
81         return False
82     
83     @rule(ast.Return, None, "non-trivial-value")
84     def __nontrivial_return(self, reducer, node):
85         """
86         Option default should be kept as constant or simple membership check
87         """
88         return SyntaxRule.TrivialReturn == node.value