Skip to content

Commit 04a55e9

Browse files
committed
Separate uast parents calculation
Signed-off-by: Konstantin Slavnov <[email protected]>
1 parent ad6da8f commit 04a55e9

File tree

4 files changed

+39
-26
lines changed

4 files changed

+39
-26
lines changed

lookout/style/format/annotations/annotations.py

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""Annotations for style-analyzer."""
2-
from typing import Dict, FrozenSet, Optional, Tuple
2+
from typing import FrozenSet, Optional, Tuple
3+
4+
from lookout.style.format.utils import get_uast_parents
35

46

57
class Annotation:
@@ -58,12 +60,26 @@ def from_node(node: "bblfsh.Node") -> "UASTNodeAnnotation":
5860
return UASTNodeAnnotation(node.start_position.offset, node.end_position.offset, node)
5961

6062

61-
# Should be removed when overlapping annotations of one type are allowed.
6263
class UASTAnnotation(UASTNodeAnnotation):
6364
"""Full UAST of the file annotation."""
6465

66+
def __init__(self, start: int, stop: int, uast: "bblfsh.Node"):
67+
"""
68+
Create new UASTAnnotation instance.
69+
70+
Parents mapping for the tree is built during init.
71+
72+
:param start: Annotation start.
73+
:param stop: Annotation end.
74+
:param uast: UAST.
75+
"""
76+
super().__init__(start, stop, uast)
77+
self._parents = get_uast_parents(uast)
78+
6579
uast = property(lambda self: self._node)
6680

81+
parents = property(lambda self: self._parents)
82+
6783

6884
class TokenAnnotation(Annotation):
6985
"""Annotation for virtual token in the сode."""
@@ -154,21 +170,6 @@ def to_target_annotation(self) -> TargetAnnotation:
154170
return TargetAnnotation(self.start, self.stop, self._target)
155171

156172

157-
class UASTParentsAnnotation(Annotation):
158-
"""
159-
Annotation to store uast parents dict.
160-
161-
Should be removed as soon as UAST provide parents itself.
162-
"""
163-
164-
def __init__(self, start: int, stop: int, parents: Dict[int, "bblfsh.Node"]):
165-
"""Init."""
166-
super().__init__(start, stop)
167-
self._parents = parents
168-
169-
parents = property(lambda self: self._parents)
170-
171-
172173
class AccumulatedIndentationAnnotation(Annotation):
173174
"""Annotates accumulated indentation Token."""
174175

lookout/style/format/feature_extractor.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
from lookout.style.format.annotations.annotated_data import AnnotatedData
1717
from lookout.style.format.annotations.annotations import PathAnnotation, RawTokenAnnotation, \
18-
UASTAnnotation, UASTParentsAnnotation
18+
UASTAnnotation
1919
from lookout.style.format.classes import (
2020
CLASS_INDEX, CLASS_PRINTABLES, CLASS_REPRESENTATIONS, CLS_DOUBLE_QUOTE, CLS_NOOP,
2121
CLS_SINGLE_QUOTE, CLS_SPACE, CLS_SPACE_DEC, CLS_SPACE_INC, CLS_TAB, CLS_TAB_DEC, CLS_TAB_INC,
@@ -725,19 +725,15 @@ def _parse_file(self, file: AnnotatedData) -> Tuple[List[VirtualNode], Dict[int,
725725

726726
# walk the tree: collect nodes with assigned tokens and build the parents map
727727
node_tokens = []
728-
parents = {}
729728
queue = [file.get(UASTAnnotation).uast]
730729
while queue:
731730
node = queue.pop()
732731
if node.internal_type in self.node_fixtures:
733-
node = self.node_fixtures[node.internal_type](node)
734-
for child in node.children:
735-
parents[id(child)] = node
732+
self.node_fixtures[node.internal_type](node)
736733
queue.extend(node.children)
737734
if (node.token or node.start_position and node.end_position
738735
and node.start_position != node.end_position and not node.children):
739736
node_tokens.append(node)
740-
file.add(UASTParentsAnnotation(0, len(file), parents))
741737
node_tokens.sort(key=lambda n: n.start_position.offset)
742738
sentinel = bblfsh.Node()
743739
sentinel.start_position.offset = len(contents)
@@ -845,4 +841,4 @@ def _to_position(raw_lines_data, _lines_start_offset, offset):
845841
y=None,
846842
)
847843
vnodes.append(vnode)
848-
return vnodes, file.get(UASTParentsAnnotation).parents
844+
return vnodes, file.get(UASTAnnotation).parents

lookout/style/format/langs/javascript/uast_fixers.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22
import bblfsh
33

44

5-
def fix_regexp_node(node: bblfsh.Node) -> bblfsh.Node:
5+
def fix_regexp_node(node: bblfsh.Node) -> None:
66
"""
77
Workaround https://github.com/bblfsh/javascript-driver/issues/37.
88
99
Should be removed as soon as issue closed and new driver is used.
1010
"""
1111
node.token = node.properties["pattern"]
12-
return node
1312

1413

1514
NODE_FIXTURES = {

lookout/style/format/utils.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,20 @@ def get_classification_report(y_pred: numpy.ndarray, y_true: numpy.ndarray,
8181
"report_full": report_full,
8282
"confusion_matrix": confusion_matrix,
8383
"target_names": target_names}
84+
85+
86+
def get_uast_parents(uast: "bblfsh.Node") -> Dict[int, "bblfsh.Node"]:
87+
"""
88+
Create a mapping from id of the node in the UAST to its parent Node.
89+
90+
:param uast: UAST to get parents mapping for.
91+
:return: Parents mapping.
92+
"""
93+
parents = {}
94+
queue = [uast]
95+
while queue:
96+
node = queue.pop()
97+
for child in node.children:
98+
parents[id(child)] = node
99+
queue.extend(node.children)
100+
return parents

0 commit comments

Comments
 (0)