Skip to content

babel-types: Add TypeScript definitions#5856

Merged
hzoo merged 6 commits into7.0from
unknown repository
Jul 25, 2017
Merged

babel-types: Add TypeScript definitions#5856
hzoo merged 6 commits into7.0from
unknown repository

Conversation

@ghost
Copy link
Copy Markdown

@ghost ghost commented Jun 13, 2017

Q A
Patch: Bug Fix? no
Major: Breaking Change? Yes in a few places, but only where it was wrong before.
Minor: New Feature? yes
Deprecations? no
Spec Compliancy? yes
Tests Added/Pass? n/a (see below)
Fixed Tickets
License MIT
Doc PR
Dependency Changes

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.js in babel/babylon#523.

},
};

defineType("CallExpression", {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This now requires a builder property.

@@ -279,41 +302,44 @@ defineType("FunctionExpression", {
inherits: "FunctionDeclaration",
aliases: ["Scopable", "Function", "BlockParent", "FunctionParent", "Expression", "Pureish"],
fields: {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this necessary? It already inherits.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't look like "inherits" merges things? opts.fields = opts.fields || inherits.fields || {};

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But all the fields are the same, right?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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", {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This now requires a builder property.

validate: (function () {
const normal = assertNodeType("Expression");
const computed = assertNodeType("Identifier", "StringLiteral", "NumericLiteral");
const normal = assertNodeType("Identifier", "StringLiteral", "NumericLiteral");
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shit, did I get these backwards? 😳

@@ -558,14 +575,12 @@ defineType("ObjectProperty", {

defineType("RestElement", {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This now requires a builder property.

} from "./index";
import { functionCommon, patternLikeCommon } from "./core";

defineType("AssignmentPattern", {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This now requires a builder property.

@@ -26,10 +28,11 @@ defineType("AssignmentPattern", {

defineType("ArrayPattern", {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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; })(),
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is icky.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a proposal for arrow function generators at Stage 1 (not implemented) https://github.com/tc39/proposals

@hzoo hzoo added the PR: New Feature 🚀 A type of pull request used for our changelog categories label Jun 14, 2017
@JamesHenry
Copy link
Copy Markdown
Member

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

@ghost
Copy link
Copy Markdown
Author

ghost commented Jun 14, 2017

@codecov
Copy link
Copy Markdown

codecov bot commented Jun 14, 2017

Codecov Report

Merging #5856 into 7.0 will increase coverage by 0.08%.
The diff coverage is 98.83%.

Impacted file tree graph

@@            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
Impacted Files Coverage Δ
packages/babel-types/src/definitions/flow.js 100% <ø> (ø) ⬆️
packages/babel-types/src/definitions/typescript.js 100% <100%> (ø)
packages/babel-types/src/definitions/core.js 98.61% <100%> (+0.08%) ⬆️
...ckages/babel-types/src/definitions/tsFlowCommon.js 100% <100%> (ø)
packages/babel-types/src/definitions/es2015.js 96.96% <88.88%> (+0.3%) ⬆️
...bel-plugin-transform-es2015-parameters/src/rest.js 98.14% <0%> (-1.86%) ⬇️
...bel-plugin-transform-es2015-classes/src/vanilla.js 91.06% <0%> (+0.42%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 5387d9f...3b4f31a. Read the comment docs.

defineType("TSDeclareMethod", {
visitor: ["decorators", "key", "typeParameters", "params", "returnType"],
fields: classMethodOrDeclareMethodCommon,
});
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these for overload declarations, or for things that have the declare keyword?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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: {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like generator got removed?

generator: {
  default: false,
  validate: assertValueType("boolean"),
}


defineType("CallExpression", {
visitor: ["callee", "arguments"],
const callOrNew = {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we just have NewExpression inherit from CallExpression?

},
id: {
validate: assertNodeType("Identifier"),
optional: true, // May be null for `export default function`
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Who's bright idea was this?!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

export default function () {} should be named *default*, which you can't really represent as a valid Identifier :)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving this to babel/babylon#630.

assertEach(assertNodeType("LVal")),
),
...functionCommon,
expression: {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
Copy link
Copy Markdown
Member

@jridgewell jridgewell Jul 14, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UGH. 🙄

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same reason as function. They're both special-cased in the spec to allow omitting the name specifically in this case.

Copy link
Copy Markdown
Member

@hzoo hzoo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

awesome work again andy, sorry for the wait

@hzoo hzoo merged commit 248743e into babel:7.0 Jul 25, 2017
@ghost ghost deleted the ts-definitions branch July 26, 2017 17:46
@lock lock bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Oct 6, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 6, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area: typescript outdated A closed issue/PR that is archived due to age. Recommended to make a new issue PR: New Feature 🚀 A type of pull request used for our changelog categories

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants