From e2a484a5375ff540a63981144c884520eae77b11 Mon Sep 17 00:00:00 2001 From: Denys Smirnov Date: Thu, 3 Jan 2019 16:43:57 +0200 Subject: [PATCH] preserve raw regexp literals in the native ast; fixes #22 Signed-off-by: Denys Smirnov --- driver/normalizer/annotation.go | 2 +- driver/normalizer/normalizer.go | 17 ++++++++++++++--- fixtures/regexp-literal.js.sem.uast | 6 ++++-- fixtures/regexp-literal.js.uast | 6 ++++-- 4 files changed, 23 insertions(+), 8 deletions(-) diff --git a/driver/normalizer/annotation.go b/driver/normalizer/annotation.go index aefc6ba..926bfef 100644 --- a/driver/normalizer/annotation.go +++ b/driver/normalizer/annotation.go @@ -144,7 +144,7 @@ var Annotations = []Mapping{ // Literals AnnotateType("RegExpLiteral", FieldRoles{ - "pattern": {Rename: uast.KeyToken}, + "raw": {Rename: uast.KeyToken}, }, role.Expression, role.Literal, role.Regexp, ), diff --git a/driver/normalizer/normalizer.go b/driver/normalizer/normalizer.go index 76661ce..5d35d8c 100644 --- a/driver/normalizer/normalizer.go +++ b/driver/normalizer/normalizer.go @@ -32,7 +32,7 @@ var Preprocessors = []Mapping{ Part("_", Obj{"loc": AnyNode(nil)}), Part("_", Obj{}), ), - // preserve raw string literal + // preserve raw string and regexp literals Map( Part("_", Obj{ uast.KeyType: String("StringLiteral"), @@ -47,6 +47,18 @@ var Preprocessors = []Mapping{ "value": Var("raw"), }), ), + Map( + Part("_", Obj{ + uast.KeyType: String("RegExpLiteral"), + "extra": Obj{ + "raw": Var("raw"), + }, + }), + Part("_", Obj{ + uast.KeyType: String("RegExpLiteral"), + "raw": Var("raw"), + }), + ), // drop extra info for other nodes (it duplicates other node fields) Map( Part("_", Obj{"extra": AnyNode(nil)}), @@ -306,7 +318,6 @@ func (op singleQuote) Construct(st *State, n nodes.Node) (nodes.Node, error) { if !ok { return nil, ErrUnexpectedType.New(nodes.String(""), n) } - s := string(sn) - s = quoteSingle(s) + s := quoteSingle(string(sn)) return nodes.String(s), nil } diff --git a/fixtures/regexp-literal.js.sem.uast b/fixtures/regexp-literal.js.sem.uast index f27c831..57f2043 100644 --- a/fixtures/regexp-literal.js.sem.uast +++ b/fixtures/regexp-literal.js.sem.uast @@ -43,7 +43,7 @@ }, }, expression: { '@type': "javascript:RegExpLiteral", - '@token': "a", + '@token': "/a/", '@role': [Expression, Literal, Regexp], '@pos': { '@type': "uast:Positions", start: { '@type': "uast:Position", @@ -58,6 +58,7 @@ }, }, flags: "", + pattern: "a", }, }, { '@type': "javascript:ExpressionStatement", @@ -90,7 +91,7 @@ }, arguments: [ { '@type': "javascript:RegExpLiteral", - '@token': "a", + '@token': "/a/g", '@role': [Argument, Call, Expression, Literal, Regexp], '@pos': { '@type': "uast:Positions", start: { '@type': "uast:Position", @@ -105,6 +106,7 @@ }, }, flags: "g", + pattern: "a", }, { '@type': "uast:String", '@role': [Argument, Call], diff --git a/fixtures/regexp-literal.js.uast b/fixtures/regexp-literal.js.uast index 7cbb9bf..71ee0ac 100644 --- a/fixtures/regexp-literal.js.uast +++ b/fixtures/regexp-literal.js.uast @@ -43,7 +43,7 @@ }, }, expression: { '@type': "RegExpLiteral", - '@token': "a", + '@token': "/a/", '@role': [Expression, Literal, Regexp], '@pos': { '@type': "uast:Positions", start: { '@type': "uast:Position", @@ -58,6 +58,7 @@ }, }, flags: "", + pattern: "a", }, }, { '@type': "ExpressionStatement", @@ -90,7 +91,7 @@ }, arguments: [ { '@type': "RegExpLiteral", - '@token': "a", + '@token': "/a/g", '@role': [Argument, Call, Expression, Literal, Regexp], '@pos': { '@type': "uast:Positions", start: { '@type': "uast:Position", @@ -105,6 +106,7 @@ }, }, flags: "g", + pattern: "a", }, { '@type': "StringLiteral", '@token': "'x'",