From bf2fde6751fe82ee6b8a2ff7f6a4ac28ab07bbc8 Mon Sep 17 00:00:00 2001 From: Juanjo Alvarez Date: Thu, 8 Mar 2018 17:56:06 +0100 Subject: [PATCH] Remove leading # from comments Signed-off-by: Juanjo Alvarez --- driver/normalizer/tonode.go | 24 +++++++++++++++++------- fixtures/comments.rb.uast | 6 +++--- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/driver/normalizer/tonode.go b/driver/normalizer/tonode.go index e1523a9..2464136 100644 --- a/driver/normalizer/tonode.go +++ b/driver/normalizer/tonode.go @@ -1,6 +1,10 @@ package normalizer -import "gopkg.in/bblfsh/sdk.v1/uast" +import ( + "strings" + + "gopkg.in/bblfsh/sdk.v1/uast" +) // ToNode is an instance of `uast.ObjectToNode`, defining how to transform an // into a UAST (`uast.Node`). @@ -35,12 +39,18 @@ var ToNode = &uast.ObjectToNode{ // Native parser wrongly set positions at individual lines in multiline // strings at 0, remove the position on those to avoid confusion - if t, ok := n["type"].(string); ok && t == "str" { - if endCol, ok := n["end_col"].(float64); ok && endCol == 0 { - delete(n, "start_col") - delete(n, "end_col") - delete(n, "start_line") - delete(n, "end_line") + if t, ok := n["type"].(string); ok { + if t == "str" { + if endCol, ok := n["end_col"].(float64); ok && endCol == 0 { + delete(n, "start_col") + delete(n, "end_col") + delete(n, "start_line") + delete(n, "end_line") + } + } else if t == "comment" { + if text, ok := n["text"].(string); ok && strings.HasPrefix(text, "#") { + n["text"] = text[1:] + } } } } diff --git a/fixtures/comments.rb.uast b/fixtures/comments.rb.uast index c13da4f..1b04a91 100644 --- a/fixtures/comments.rb.uast +++ b/fixtures/comments.rb.uast @@ -38,7 +38,7 @@ UAST: . . . . . . documentation: false . . . . . . inline: true . . . . . . internalRole: comments -. . . . . . text: # previous +. . . . . . text: previous . . . . . } . . . . } . . . . 1: comment { @@ -57,7 +57,7 @@ UAST: . . . . . . documentation: false . . . . . . inline: true . . . . . . internalRole: comments -. . . . . . text: # inline +. . . . . . text: inline . . . . . } . . . . } . . . . 2: comment { @@ -76,7 +76,7 @@ UAST: . . . . . . documentation: false . . . . . . inline: true . . . . . . internalRole: comments -. . . . . . text: # after +. . . . . . text: after . . . . . } . . . . } . . . . 3: int {