-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add validators for Flow AST node fields #7107
Conversation
cc @loganfsmyth |
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/6381/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome work!
Just a couple comments and:
- We should probably make sure any type's visitor list is up to date with their fields so they can be set in their builder (similar to the DeclareModule comment I left above)
- Can you run
node scripts/generate-babel-types-docs
to regenerate @babel/types' README?
}, | ||
}); | ||
|
||
defineType("DeclareExportDeclaration", { | ||
visitor: ["declaration", "specifiers", "source"], | ||
aliases: ["Flow", "FlowDeclaration", "Statement", "Declaration"], | ||
fields: { | ||
// todo | ||
declaration: validateOptionalType("TSType"), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TSType -> Flow
// todo | ||
key: validateType("Identifier"), | ||
value: validateType(["Flow", "Identifier"]), | ||
kind: validate(assertOneOf("init")), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"init" | "get" | "set"
id: validateOptionalType("Identifier"), | ||
key: validateType("Flow"), | ||
value: validateType(["Flow", "Identifier"]), | ||
static: validate(assertValueType("boolean")), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIRC, this can have variance:
http://astexplorer.net/#/gist/46bffcf1acdaccb35dda30ee3713da48/1f7ef4e223f06eefbc87a893a9a875313b5c9145
// todo | ||
id: validateType(["Identifier", "StringLiteral"]), | ||
body: validateType("BlockStatement"), | ||
kind: { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs to either be:
validateOptional(assertOneOf("CommonJS", "ES"))
or
{
optional: true,
validate: assertOneOf("CommonJS", "ES"),
}
typeParameters: validateOptionalType("TypeParameterDeclaration"), | ||
extends: validateOptionalType("InterfaceExtends"), | ||
mixins: validateOptional(arrayOfType("Flow")), | ||
body: validateType("ObjectTypeAnnotation"), | ||
}, | ||
}); | ||
|
||
defineType("DeclareModule", { | ||
visitor: ["id", "body"], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's add kind
to visitor list so we can do something like:
t.declareModule(t.identifier('d'), t.blockStatement([]), "ES")
@existentialism Thanks for all the comments - very helpful! One question (otherwise comments are all addressed)-
Currently, it looks like |
It is intentionals, because |
@nicolo-ribaudo Can you clarify - should |
}); | ||
|
||
defineType("ArrayTypeAnnotation", { | ||
visitor: ["elementType"], | ||
aliases: ["Flow"], | ||
fields: { | ||
// todo | ||
elementType: validateType("Flow"), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This whould also allow thing like TypeAlias
and ClassImplements
, but it should only allow types.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What would you use instead? FlowBaseAnnotation
?
Yep, that's what I get for reviewing before coffee... I meant to say For ref: https://github.com/babel/babel/blob/master/packages/babel-types/src/definitions/core.js#L48 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are many places (I just comented few of them) where you used Flow
but it includes too many types. I think that we could introduce a new FlowType
alias.
// todo | ||
id: validateType("Identifier"), | ||
typeParameters: validateOptionalType("TypeParameterDeclaration"), | ||
right: validateType("Flow"), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also here, it should only allow types.
id: validateType("Identifier"), | ||
typeParameters: validateOptionalType("TypeParameterDeclaration"), | ||
extends: validate(arrayOfType("InterfaceExtends")), | ||
mixins: validate(arrayOfType("InterfaceExtends")), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just for curiosity, what is this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
}, | ||
}); | ||
|
||
defineType("DeclareModule", { | ||
visitor: ["id", "body"], | ||
visitor: ["id", "body", "kind"], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's add
kind
to visitor list so we can do something like:
t.declareModule(t.identifier('d'), t.blockStatement([]), "ES")
IIRC, the parameters are defined in the builder
array. visitor
should only contain the nodes which should be traversed.
Not always, it should be something like |
@existentialism @nicolo-ribaudo All comments are addressed (pending @nicolo-ribaudo Let me know what should go in |
I think it should contain any node whose name ends with |
Can we go ahead and merge this? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I left a few comments where I think that FlowType
should be used. I also noticed a problem with the name of a field and a place where the type should be Expression
instead of Flow
.
// todo | ||
id: validateType("Identifier"), | ||
typeParameters: validateOptionalType("TypeParameterDeclaration"), | ||
supertype: validateOptionalType("Flow"), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FlowType
typeParameters: validateOptionalType("TypeParameterDeclaration"), | ||
params: validate(arrayOfType("FunctionTypeParam")), | ||
rest: validateOptionalType("FunctionTypeParam"), | ||
returnType: validateType("Flow"), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FlowType
}, | ||
}); | ||
|
||
defineType("FunctionTypeParam", { | ||
visitor: ["name", "typeAnnotation"], | ||
aliases: ["Flow"], | ||
fields: { | ||
// todo | ||
name: validateOptionalType("Identifier"), | ||
typeAnnotation: validateType("Flow"), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FlowType
fields: { | ||
// todo | ||
types: validate(arrayOfType("Flow")), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FlowType
fields: { | ||
// todo | ||
typeAnnotation: validateType("Flow"), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FlowType
typeAnnotation: { | ||
validate: assertNodeType("Flow"), | ||
}, | ||
typeAnnotation: validateType("Flow"), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FlowType
}, | ||
}); | ||
|
||
defineType("TypeCastExpression", { | ||
visitor: ["expression", "typeAnnotation"], | ||
aliases: ["Flow", "ExpressionWrapper", "Expression"], | ||
fields: { | ||
// todo | ||
expression: validateType("Flow"), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Expression
, I think.
}, | ||
name: validate(assertValueType("string")), | ||
bound: validateOptionalType("TypeAnnotation"), | ||
default: validateOptionalType("Flow"), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FlowType
assertEach(assertNodeType("Flow")), | ||
), | ||
}, | ||
params: validate(arrayOfType("Flow")), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FlowType
fields: { | ||
// todo | ||
types: validate(arrayOfType("Flow")), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FlowType
@nicolo-ribaudo Done. |
^ Looks like this PR caught a bug in an existing test! |
In this PR, we add types to the existing Flow AST nodes. This brings the quality of Flow AST node definitions up to par with existing definitions for TypeScript/Core/Experimental/etc. nodes, and enables:
I tried my best to make sure field type definitions are thorough and complete. I used existing Babel unit tests as my source of truth.
This PR complements, and is independent of, #7101.