Skip to content

Commit ffeac8d

Browse files
committed
Fix _parse_file and add new test
Signed-off-by: konstantin <[email protected]>
1 parent 059d120 commit ffeac8d

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

lookout/style/format/features.py

+34
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,26 @@ def to_comment(self, correct_y: int) -> Comment:
148148
comment.text = comment.text.replace("<", "`").replace(">", "`")
149149
return comment
150150

151+
def to_code(self, correct_y: int) -> str:
152+
"""
153+
Writes the code with regard to the correct node class.
154+
:param correct_y: the index of the correct node class.
155+
:return: code string
156+
"""
157+
if correct_y == CLASS_INDEX[CLS_NOOP]:
158+
return ""
159+
if correct_y == CLASS_INDEX[CLS_NOOP]:
160+
comment.text = "format: %s at column %d should be removed" % (
161+
CLASSES[self.y], self.start.col)
162+
elif self.y == CLASS_INDEX[CLS_NOOP]:
163+
comment.text = "format: %s should be inserted at column %d" % (
164+
CLASSES[correct_y], self.start.col)
165+
else:
166+
comment.text = "format: replace %s with %s at column %d" % (
167+
CLASSES[self.y], CLASSES[correct_y], self.start.col)
168+
comment.text = comment.text.replace("<", "`").replace(">", "`")
169+
return comment
170+
151171

152172
CLS_SPACE = "<space>"
153173
CLS_SPACE_INC = "<+space>"
@@ -159,6 +179,18 @@ def to_comment(self, correct_y: int) -> Comment:
159179
CLS_SINGLE_QUOTE = "'"
160180
CLS_DOUBLE_QUOTE = '"'
161181
CLS_NOOP = "<noop>"
182+
CLASSES_REPR = {
183+
CLS_SPACE: " ",
184+
CLS_TAB: "\t",
185+
CLS_NEWLINE: "\n",
186+
CLS_SPACE_INC: "<nan>",
187+
CLS_SPACE_DEC: "<nan>",
188+
CLS_TAB_INC: "<nan>",
189+
CLS_TAB_DEC: "<nan>",
190+
CLS_SINGLE_QUOTE: "'",
191+
CLS_DOUBLE_QUOTE: '"',
192+
CLS_NOOP: "",
193+
}
162194
CLASSES = (CLS_SPACE, CLS_TAB, CLS_NEWLINE, CLS_SPACE_INC, CLS_SPACE_DEC,
163195
CLS_TAB_INC, CLS_TAB_DEC, CLS_SINGLE_QUOTE, CLS_DOUBLE_QUOTE, CLS_NOOP)
164196
CLASS_INDEX = {cls: i for i, cls in enumerate(CLASSES)}
@@ -639,6 +671,8 @@ def _parse_file(self, contents: str, root: bblfsh.Node, path: str) -> \
639671
parser = self.tokens.PARSER
640672
searchsorted = numpy.searchsorted
641673
for node in node_tokens:
674+
if node.start_position.offset < pos:
675+
continue
642676
if node.start_position.offset > pos:
643677
sumlen = 0
644678
diff = contents[pos:node.start_position.offset]
1.86 KB
Binary file not shown.

lookout/style/format/tests/test_features.py

+9
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ def setUpClass(cls):
2525
cls.uast = bblfsh.Node.FromString(fin.read())
2626
cls.extractor = FeatureExtractor("javascript", parents_depth=2, siblings_window=5)
2727

28+
def test_parse_file2(self):
29+
test_js_code_filepath = Path(__file__).parent / "for_parse_test.js.xz"
30+
with lzma.open(str(test_js_code_filepath), mode="rt") as f:
31+
code = f.read()
32+
uast = bblfsh.BblfshClient("0.0.0.0:9432").parse(
33+
filename="", language="javascript", contents=code.encode()).uast
34+
nodes, parents = self.extractor._parse_file(code, uast, test_js_code_filepath)
35+
self.assertEqual("".join(n.value for n in nodes), code)
36+
2837
def test_parse_file(self):
2938
nodes, parents = self.extractor._parse_file(self.contents, self.uast, "test_file")
3039
text = []

0 commit comments

Comments
 (0)