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

Unified comment handling #73

Merged
merged 2 commits into from
Mar 21, 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
70 changes: 41 additions & 29 deletions driver/normalizer/normalizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var Preprocessors = []Mapping{
}.Mapping(),

Map(
Part("_", Obj{"loc": AnyNode(nil)}),
Part("_", Obj{"loc": Any()}),
Part("_", Obj{}),
),
// preserve raw string and regexp literals
Expand Down Expand Up @@ -72,18 +72,55 @@ var Preprocessors = []Mapping{
),
}

// allNormalizedTypes are all uast.KeyType that are processed by current normalizer
// where we currently drop the coments node off.
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"),
nodes.String("ImportDeclaration"),
nodes.String("ImportDeclaration"),
nodes.String("ImportSpecifier"),
nodes.String("ImportDefaultSpecifier"),
nodes.String("ImportNamespaceSpecifier"),
nodes.String("FunctionDeclaration"),
}

// Normalizers is the main block of normalization rules to convert native AST to semantic UAST.
var Normalizers = []Mapping{
//FIXME(bzz): save all 3 *Commnets in a new parent Group node \w order
Map( // this is not reversible
Part("_", Fields{
{Name: uast.KeyType, Op: Check(In(allNormalizedTypes...), Var("t"))},
{Name: "leadingComments", Drop: true, Op: Any()},
}),
Part("_", Obj{uast.KeyType: Var("t")}),
),
Map( // this is not reversible
Part("_", Fields{
{Name: uast.KeyType, Op: Check(In(allNormalizedTypes...), Var("t"))},
{Name: "innerComments", Drop: true, Op: Any()},
}),
Part("_", Obj{uast.KeyType: Var("t")}),
),
Map( // this is not reversible
Part("_", Fields{
{Name: uast.KeyType, Op: Check(In(allNormalizedTypes...), Var("t"))},
{Name: "trailingComments", Drop: true, Op: Any()},
}),
Part("_", Obj{uast.KeyType: Var("t")}),
),
MapSemantic("Identifier", uast.Identifier{}, MapObj(
Fields{
{Name: "name", Op: Var("name")},
//FIXME(bzz): map Flow variable types properly
{Name: "typeAnnotation", Drop: true, Op: Any()},
//FIXME(bzz): map Flow "Optional Prameter" properly
{Name: "optional", Drop: true, Op: Any()},
//FIXME(bzz): map both once we agree how
{Name: "leadingComments", Drop: true, Op: Any()},
{Name: "trailingComments", Drop: true, Op: Any()},
},
Obj{
"Name": Var("name"),
Expand All @@ -100,9 +137,6 @@ var Normalizers = []Mapping{
MapSemantic("StringLiteral", uast.String{}, MapObj(
Fields{
{Name: "value", Op: singleQuote{Var("val")}},
//FIXME(bzz): save both once we agree how
{Name: "leadingComments", Drop: true, Op: Any()},
{Name: "trailingComments", Drop: true, Op: Any()},
},
Obj{
"Value": Var("val"),
Expand All @@ -112,9 +146,6 @@ var Normalizers = []Mapping{
MapSemantic("StringLiteral", uast.String{}, MapObj(
Fields{
{Name: "value", Op: Quote(Var("val"))},
//FIXME(bzz): save both once we agree how
{Name: "leadingComments", Drop: true, Op: Any()},
{Name: "trailingComments", Drop: true, Op: Any()},
},
Obj{
"Value": Var("val"),
Expand All @@ -136,10 +167,6 @@ var Normalizers = []Mapping{
Fields{
{Name: "body", Op: Var("stmts")},
{Name: "directives", Op: Arr()}, // TODO: find an example
// FIXME(bzz): make sure such comments are linked properly
{Name: "innerComments", Drop: true, Op: Any()},
{Name: "leadingComments", Drop: true, Op: Any()},
{Name: "trailingComments", Drop: true, Op: Any()},
},
Obj{
"Statements": Var("stmts"),
Expand All @@ -150,9 +177,6 @@ var Normalizers = []Mapping{
{Name: "source", Op: Var("path")},
// empty un-used array
{Name: "specifiers", Drop: true, Op: Arr()},
// FIXME(bzz): make sure such comments are linked properly
{Name: "leadingComments", Drop: true, Op: Any()},
{Name: "trailingComments", Drop: true, Op: Any()},
},
Obj{
"Path": Var("path"),
Expand All @@ -166,9 +190,6 @@ var Normalizers = []Mapping{
// common
Fields{
{Name: "source", Op: Var("path")},
// FIXME(bzz): make sure such comments are linked properly
{Name: "leadingComments", Drop: true, Op: Any()},
{Name: "trailingComments", Drop: true, Op: Any()},
},
Objs{
// namespace
Expand All @@ -179,9 +200,6 @@ var Normalizers = []Mapping{
{Name: uast.KeyType, Op: String("ImportNamespaceSpecifier")},
{Name: uast.KeyPos, Op: Var("local_pos")},
{Name: "local", Op: Var("local")},
// FIXME(bzz): make sure such comments are linked properly
{Name: "leadingComments", Drop: true, Op: Any()},
{Name: "trailingComments", Drop: true, Op: Any()},
}),
},
// specific type
Expand Down Expand Up @@ -249,9 +267,6 @@ var Normalizers = []Mapping{
MapSemantic("ImportDefaultSpecifier", uast.Alias{}, MapObj(
Fields{
{Name: "local", Op: Var("local")},
//FIXME(bzz): save this once we agree how
{Name: "leadingComments", Drop: true, Op: Any()},
{Name: "trailingComments", Drop: true, Op: Any()},
},
Obj{
"Name": Var("local"),
Expand All @@ -277,9 +292,6 @@ var Normalizers = []Mapping{
// https://flow.org/en/docs/types/generics/
// see fixtures/ext_typedecl.js#34 func makeWeakCache
{Name: "typeParameters", Drop: true, Op: Any()},
// FIXME(bzz): make sure such comments are linked properly
{Name: "leadingComments", Drop: true, Op: Any()},
{Name: "trailingComments", Drop: true, Op: Any()},
{Name: "params", Op: Each("params", Cases("param_case",
// Identifier
Check(
Expand Down
1 change: 1 addition & 0 deletions fixtures/issue65-op.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var a = 0 + 1;
141 changes: 141 additions & 0 deletions fixtures/issue65-op.js.native
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
{
comments: [],
end: 15,
loc: {
end: {
column: 0,
line: 2,
},
start: {
column: 0,
line: 1,
},
},
program: {
body: [
{
declarations: [
{
end: 13,
id: {
end: 5,
loc: {
end: {
column: 5,
line: 1,
},
identifierName: "a",
start: {
column: 4,
line: 1,
},
},
name: "a",
start: 4,
type: "Identifier",
},
init: {
end: 13,
left: {
end: 9,
extra: {
raw: "0",
rawValue: 0,
},
loc: {
end: {
column: 9,
line: 1,
},
start: {
column: 8,
line: 1,
},
},
start: 8,
type: "NumericLiteral",
value: 0,
},
loc: {
end: {
column: 13,
line: 1,
},
start: {
column: 8,
line: 1,
},
},
operator: "+",
right: {
end: 13,
extra: {
raw: "1",
rawValue: 1,
},
loc: {
end: {
column: 13,
line: 1,
},
start: {
column: 12,
line: 1,
},
},
start: 12,
type: "NumericLiteral",
value: 1,
},
start: 8,
type: "BinaryExpression",
},
loc: {
end: {
column: 13,
line: 1,
},
start: {
column: 4,
line: 1,
},
},
start: 4,
type: "VariableDeclarator",
},
],
end: 14,
kind: "var",
loc: {
end: {
column: 14,
line: 1,
},
start: {
column: 0,
line: 1,
},
},
start: 0,
type: "VariableDeclaration",
},
],
directives: [],
end: 15,
loc: {
end: {
column: 0,
line: 2,
},
start: {
column: 0,
line: 1,
},
},
sourceType: "module",
start: 0,
type: "Program",
},
start: 0,
type: "File",
}
Loading