babel-types: Add TypeScript definitions#5856
Conversation
| }, | ||
| }; | ||
|
|
||
| defineType("CallExpression", { |
There was a problem hiding this comment.
This now requires a builder property.
| @@ -279,41 +302,44 @@ defineType("FunctionExpression", { | |||
| inherits: "FunctionDeclaration", | |||
| aliases: ["Scopable", "Function", "BlockParent", "FunctionParent", "Expression", "Pureish"], | |||
| fields: { | |||
There was a problem hiding this comment.
Is this necessary? It already inherits.
There was a problem hiding this comment.
It doesn't look like "inherits" merges things? opts.fields = opts.fields || inherits.fields || {};
There was a problem hiding this comment.
But all the fields are the same, right?
There was a problem hiding this comment.
A FunctionExpression can't have declare. id and body are the same but I don't think it would be worth extracting those out separately.
| @@ -441,22 +467,18 @@ defineType("MemberExpression", { | |||
| }); | |||
|
|
|||
| defineType("NewExpression", { | |||
There was a problem hiding this comment.
This now requires a builder property.
| validate: (function () { | ||
| const normal = assertNodeType("Expression"); | ||
| const computed = assertNodeType("Identifier", "StringLiteral", "NumericLiteral"); | ||
| const normal = assertNodeType("Identifier", "StringLiteral", "NumericLiteral"); |
There was a problem hiding this comment.
Shit, did I get these backwards? 😳
| @@ -558,14 +575,12 @@ defineType("ObjectProperty", { | |||
|
|
|||
| defineType("RestElement", { | |||
There was a problem hiding this comment.
This now requires a builder property.
| } from "./index"; | ||
| import { functionCommon, patternLikeCommon } from "./core"; | ||
|
|
||
| defineType("AssignmentPattern", { |
There was a problem hiding this comment.
This now requires a builder property.
| @@ -26,10 +28,11 @@ defineType("AssignmentPattern", { | |||
|
|
|||
| defineType("ArrayPattern", { | |||
There was a problem hiding this comment.
This now requires a builder property.
| params: { | ||
| validate: chain(assertValueType("array"), assertEach(assertNodeType("LVal"))), | ||
| // Unlike other functions, arrow functions are never generators. | ||
| ...(() => { const x = functionCommon; delete x.generator; return x; })(), |
There was a problem hiding this comment.
There is a proposal for arrow function generators at Stage 1 (not implemented) https://github.com/tc39/proposals
|
Thanks for sharing @Andy-MS 👍 I'll use this to build out a separate, shared test suite now that we are much closer to convergence |
|
Hi @JamesHenry, I did write a validator that you can find in preview at https://github.com/andy-ms/babel/blob/typescript/packages/babel-plugin-transform-typescript/test/astUtils.js . |
Codecov Report
@@ Coverage Diff @@
## 7.0 #5856 +/- ##
==========================================
+ Coverage 85.25% 85.34% +0.08%
==========================================
Files 284 286 +2
Lines 9963 10030 +67
Branches 2781 2781
==========================================
+ Hits 8494 8560 +66
Misses 969 969
- Partials 500 501 +1
Continue to review full report at Codecov.
|
| defineType("TSDeclareMethod", { | ||
| visitor: ["decorators", "key", "typeParameters", "params", "returnType"], | ||
| fields: classMethodOrDeclareMethodCommon, | ||
| }); |
There was a problem hiding this comment.
Are these for overload declarations, or for things that have the declare keyword?
There was a problem hiding this comment.
Could be either. It's any method with no body.
declare class C1 { // ClassDeclaration with declare: true
m(): void; // TSDeclareMethod
}
class C2 {
m(x: number): number; // TSDeclareMethod
m(x: string): string; // TSDeclareMethod
m(x: any) { return x; } // ClassMethod
}| }); | ||
|
|
||
| export const functionCommon = { | ||
| params: { |
There was a problem hiding this comment.
Looks like generator got removed?
generator: {
default: false,
validate: assertValueType("boolean"),
}
|
|
||
| defineType("CallExpression", { | ||
| visitor: ["callee", "arguments"], | ||
| const callOrNew = { |
There was a problem hiding this comment.
Could we just have NewExpression inherit from CallExpression?
| }, | ||
| id: { | ||
| validate: assertNodeType("Identifier"), | ||
| optional: true, // May be null for `export default function` |
There was a problem hiding this comment.
Who's bright idea was this?!
There was a problem hiding this comment.
export default function () {} should be named *default*, which you can't really represent as a valid Identifier :)
| assertEach(assertNodeType("LVal")), | ||
| ), | ||
| ...functionCommon, | ||
| expression: { |
There was a problem hiding this comment.
Instead of adding this, we should be removing it from babylon.
| }, | ||
| id: { | ||
| validate: assertNodeType("Identifier"), | ||
| optional: true, // Missing if this is the child of an ExportDefaultDeclaration. |
There was a problem hiding this comment.
Same reason as function. They're both special-cased in the spec to allow omitting the name specifically in this case.
hzoo
left a comment
There was a problem hiding this comment.
awesome work again andy, sorry for the wait
Companion to babel/babylon#523. This adds definitions for all of the TypeScript nodes.
I tested this by recursively validating TypeScript trees parsed from DefinitelyTyped. That script also tests the TS plugin, so I'll wait to include the script in the PR that adds the plugin.
CC @JamesHenry @soda0289 This defines the spec for TypeScript nodes, and duplicates the information found in
types.jsin babel/babylon#523.