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 <[email protected]>
  • Loading branch information
Denys Smirnov committed Jan 22, 2019
1 parent 676f7ea commit e2a484a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 8 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
17 changes: 14 additions & 3 deletions driver/normalizer/normalizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -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)}),
Expand Down Expand Up @@ -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
}
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 e2a484a

Please sign in to comment.