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..d8aa36c 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)}), 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'",