Skip to content
This repository has been archived by the owner on Mar 8, 2020. It is now read-only.

Commit

Permalink
preserve raw regexp literals in the native ast; fixes #22
Browse files Browse the repository at this point in the history
Signed-off-by: Denys Smirnov <denys@sourced.tech>
  • Loading branch information
Denys Smirnov committed Jan 3, 2019
1 parent b63bee4 commit 9bb1d02
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
2 changes: 1 addition & 1 deletion driver/normalizer/annotation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
),
Expand Down
14 changes: 13 additions & 1 deletion driver/normalizer/normalizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,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"),
Expand All @@ -49,6 +49,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)}),
Expand Down
6 changes: 4 additions & 2 deletions fixtures/regexp-literal.js.sem.uast
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
},
},
expression: { '@type': "javascript:RegExpLiteral",
'@token': "a",
'@token': "/a/",
'@role': [Expression, Literal, Regexp],
'@pos': { '@type': "uast:Positions",
start: { '@type': "uast:Position",
Expand All @@ -58,6 +58,7 @@
},
},
flags: "",
pattern: "a",
},
},
{ '@type': "javascript:ExpressionStatement",
Expand Down Expand Up @@ -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",
Expand All @@ -105,6 +106,7 @@
},
},
flags: "g",
pattern: "a",
},
{ '@type': "uast:String",
'@role': [Argument, Call],
Expand Down
6 changes: 4 additions & 2 deletions fixtures/regexp-literal.js.uast
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
},
},
expression: { '@type': "RegExpLiteral",
'@token': "a",
'@token': "/a/",
'@role': [Expression, Literal, Regexp],
'@pos': { '@type': "uast:Positions",
start: { '@type': "uast:Position",
Expand All @@ -58,6 +58,7 @@
},
},
flags: "",
pattern: "a",
},
},
{ '@type': "ExpressionStatement",
Expand Down Expand Up @@ -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",
Expand All @@ -105,6 +106,7 @@
},
},
flags: "g",
pattern: "a",
},
{ '@type': "StringLiteral",
'@token': "'x'",
Expand Down

0 comments on commit 9bb1d02

Please sign in to comment.