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

Fix other escape strings from JS #81

Merged
merged 5 commits into from
Apr 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 9 additions & 38 deletions driver/normalizer/normalizer.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package normalizer

import (
"strconv"
"strings"

"gopkg.in/bblfsh/sdk.v2/uast"
Expand Down Expand Up @@ -37,10 +36,10 @@ var Preprocessors = []Mapping{
Map(
Part("_", Obj{
uast.KeyType: String("StringLiteral"),
"value": Any(),
"value": Any(),
"extra": Fields{
{Name: "raw", Op: Var("raw")},
{Name: "rawValue", Op: Any()},
{Name: "rawValue", Op: Var("norm")},
//TODO(bzz): make sure parenthesis mapping is consistent \w other drivers
{Name: "parenthesized", Drop: true, Op: Any()},
{Name: "parenStart", Drop: true, Op: Any()},
Expand All @@ -49,6 +48,7 @@ var Preprocessors = []Mapping{
Part("_", Obj{
uast.KeyType: String("StringLiteral"),
"value": Var("raw"),
"normValue": Var("norm"),
}),
),
Map(
Expand Down Expand Up @@ -79,7 +79,6 @@ var allNormalizedTypes = []nodes.Value{
nodes.String("Identifier"),
nodes.String("JSXIdentifier"),
nodes.String("StringLiteral"),
nodes.String("StringLiteral"),
nodes.String("CommentLine"),
nodes.String("CommentBlock"),
nodes.String("BlockStatement"),
Expand Down Expand Up @@ -136,7 +135,8 @@ var Normalizers = []Mapping{
)),
MapSemantic("StringLiteral", uast.String{}, MapObj(
Fields{
{Name: "value", Op: singleQuote{Var("val")}},
{Name: "normValue", Op: Var("val")},
{Name: "value", Drop: true, Op: Check(singleQuote{}, Any())}, // only used in Annotated
},
Obj{
"Value": Var("val"),
Expand All @@ -145,7 +145,8 @@ var Normalizers = []Mapping{
)),
MapSemantic("StringLiteral", uast.String{}, MapObj(
Fields{
{Name: "value", Op: doubleQuote(Var("val"))},
{Name: "normValue", Op: Var("val")},
{Name: "value", Drop: true, Op: Any()}, // only used in Annotated
},
Obj{
"Value": Var("val"),
Expand Down Expand Up @@ -245,7 +246,7 @@ var Normalizers = []Mapping{
}),
"Names": Var("names"),
//FIXME(bzz): this should be true ONLY for wildcard imports
"All": Bool(true),
"All": Bool(true),
},
// normal import
{
Expand Down Expand Up @@ -361,7 +362,6 @@ var Normalizers = []Mapping{
}

type singleQuote struct {
op Op
}

func (op singleQuote) Kinds() nodes.Kind {
Expand All @@ -374,34 +374,5 @@ func (op singleQuote) Check(st *State, n nodes.Node) (bool, error) {
return false, nil
}
s := string(sn)
if !strings.HasPrefix(s, `'`) || !strings.HasSuffix(s, `'`) {
return false, nil
}
s, err := unquoteSingle(s)
if err != nil {
return false, err
}
return op.op.Check(st, nodes.String(s))
}

func (op singleQuote) Construct(st *State, n nodes.Node) (nodes.Node, error) {
n, err := op.op.Construct(st, n)
if err != nil {
return nil, err
}
sn, ok := n.(nodes.String)
if !ok {
return nil, ErrUnexpectedType.New(nodes.String(""), n)
}
s := quoteSingle(string(sn))
return nodes.String(s), nil
}

// doubleQuote is a transformer.Quote + JS-specific escape sequence handing
func doubleQuote(op Op) Op {
return StringConv(op, func(s string) (string, error) {
return unquoteDouble(s)
}, func(s string) (string, error) {
return strconv.Quote(s), nil
})
return strings.HasPrefix(s, `'`) && strings.HasSuffix(s, `'`), nil
}
183 changes: 0 additions & 183 deletions driver/normalizer/strconv.go

This file was deleted.

84 changes: 0 additions & 84 deletions driver/normalizer/strconv_test.go

This file was deleted.

2 changes: 2 additions & 0 deletions fixtures/59-jsx-flow.js.uast
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
col: 31,
},
},
normValue: "react",
},
specifiers: [
{ '@type': "ImportNamespaceSpecifier",
Expand Down Expand Up @@ -212,6 +213,7 @@
col: 22,
},
},
normValue: "mod",
},
specifiers: [
{ '@type': "ImportDefaultSpecifier",
Expand Down
Loading