Skip to content

Commit

Permalink
Add aliases for Standardized, TypeScript, and Flow (#13666)
Browse files Browse the repository at this point in the history
* Ensure non-standard types have an appropriate alias

This helps with filter out the types. Eg, I'm concerned with just the core JS types, and in I can derive them via:

```typescript
type Core = Exclude<t.Node, t.TypeScript | t.Flow>;
```

* Lint

* Export deprecated alias types

* Add docs descriptions for new aliases

* Add Standardized alias

* Fix inherits aliases

* Filter aliases from node types

* Remove Proposal alias
  • Loading branch information
jridgewell committed Oct 28, 2021
1 parent 248aa9d commit b179365
Show file tree
Hide file tree
Showing 14 changed files with 530 additions and 136 deletions.
3 changes: 3 additions & 0 deletions packages/babel-traverse/src/path/generated/asserts.ts
Expand Up @@ -328,6 +328,7 @@ export interface NodePathAssetions {
): asserts this is NodePath<t.MemberExpression>;
assertMetaProperty(opts?: object): asserts this is NodePath<t.MetaProperty>;
assertMethod(opts?: object): asserts this is NodePath<t.Method>;
assertMiscellaneous(opts?: object): asserts this is NodePath<t.Miscellaneous>;
assertMixedTypeAnnotation(
opts?: object,
): asserts this is NodePath<t.MixedTypeAnnotation>;
Expand Down Expand Up @@ -437,6 +438,7 @@ export interface NodePathAssetions {
assertSpreadProperty(
opts?: object,
): asserts this is NodePath<t.SpreadProperty>;
assertStandardized(opts?: object): asserts this is NodePath<t.Standardized>;
assertStatement(opts?: object): asserts this is NodePath<t.Statement>;
assertStaticBlock(opts?: object): asserts this is NodePath<t.StaticBlock>;
assertStringLiteral(opts?: object): asserts this is NodePath<t.StringLiteral>;
Expand Down Expand Up @@ -660,6 +662,7 @@ export interface NodePathAssetions {
assertTypeParameterInstantiation(
opts?: object,
): asserts this is NodePath<t.TypeParameterInstantiation>;
assertTypeScript(opts?: object): asserts this is NodePath<t.TypeScript>;
assertTypeofTypeAnnotation(
opts?: object,
): asserts this is NodePath<t.TypeofTypeAnnotation>;
Expand Down
3 changes: 3 additions & 0 deletions packages/babel-traverse/src/path/generated/validators.ts
Expand Up @@ -191,6 +191,7 @@ export interface NodePathValidators {
isMemberExpression(opts?: object): this is NodePath<t.MemberExpression>;
isMetaProperty(opts?: object): this is NodePath<t.MetaProperty>;
isMethod(opts?: object): this is NodePath<t.Method>;
isMiscellaneous(opts?: object): this is NodePath<t.Miscellaneous>;
isMixedTypeAnnotation(opts?: object): this is NodePath<t.MixedTypeAnnotation>;
isModuleDeclaration(opts?: object): this is NodePath<t.ModuleDeclaration>;
isModuleExpression(opts?: object): this is NodePath<t.ModuleExpression>;
Expand Down Expand Up @@ -274,6 +275,7 @@ export interface NodePathValidators {
isSequenceExpression(opts?: object): this is NodePath<t.SequenceExpression>;
isSpreadElement(opts?: object): this is NodePath<t.SpreadElement>;
isSpreadProperty(opts?: object): this is NodePath<t.SpreadProperty>;
isStandardized(opts?: object): this is NodePath<t.Standardized>;
isStatement(opts?: object): this is NodePath<t.Statement>;
isStaticBlock(opts?: object): this is NodePath<t.StaticBlock>;
isStringLiteral(opts?: object): this is NodePath<t.StringLiteral>;
Expand Down Expand Up @@ -399,6 +401,7 @@ export interface NodePathValidators {
isTypeParameterInstantiation(
opts?: object,
): this is NodePath<t.TypeParameterInstantiation>;
isTypeScript(opts?: object): this is NodePath<t.TypeScript>;
isTypeofTypeAnnotation(
opts?: object,
): this is NodePath<t.TypeofTypeAnnotation>;
Expand Down
7 changes: 6 additions & 1 deletion packages/babel-types/scripts/generators/ast-types.js
Expand Up @@ -49,7 +49,9 @@ interface BaseNode {
export type CommentTypeShorthand = "leading" | "inner" | "trailing";
export type Node = ${t.TYPES.sort().join(" | ")};\n\n`;
export type Node = ${t.TYPES.filter(k => !t.FLIPPED_ALIAS_KEYS[k])
.sort()
.join(" | ")};\n\n`;

const deprecatedAlias = {};
for (const type in t.DEPRECATED_KEYS) {
Expand Down Expand Up @@ -115,6 +117,9 @@ export interface ${deprecatedAlias[type]} extends BaseNode {
code += ` ${type}: ${type};\n`;
}
code += "}\n\n";
code += `export type DeprecatedAliases = ${Object.keys(
t.DEPRECATED_KEYS
).join(" | ")}\n\n`;

return code;
}
Expand Down
5 changes: 5 additions & 0 deletions packages/babel-types/scripts/generators/docs.js
Expand Up @@ -217,6 +217,8 @@ const aliasDescriptions = {
"A cover of [Literal](https://tc39.es/ecma262/#sec-primary-expression-literals)s, [Regular Expression Literal](https://tc39.es/ecma262/#sec-primary-expression-regular-expression-literals)s and [Template Literal](https://tc39.es/ecma262/#sec-template-literals)s.",
Loop: "A cover of loop statements.",
Method: "A cover of object methods and class methods.",
Miscellaneous:
"A cover of non-standard AST types that are sometimes useful for development.",
ModuleDeclaration:
"A cover of ImportDeclaration and [ExportDeclaration](#exportdeclaration)",
ModuleSpecifier:
Expand All @@ -233,12 +235,15 @@ const aliasDescriptions = {
"A cover of AST nodes which do not have side-effects. In other words, there is no observable behaviour changes if they are evaluated more than once.",
Scopable:
"A cover of [FunctionParent](#functionparent) and [BlockParent](#blockparent).",
Standardized:
"A cover of AST nodes which are part of an official ECMAScript specification.",
Statement:
"A cover of any [Statement](https://tc39.es/ecma262/#prod-Statement)s.",
TSBaseType: "A cover of primary TypeScript type annotations.",
TSEntityName: "A cover of ts entities.",
TSType: "A cover of TypeScript type annotations.",
TSTypeElement: "A cover of TypeScript type declarations.",
TypeScript: "A cover of AST nodes defined for TypeScript.",
Terminatorless:
"A cover of AST nodes whose semantic will change when a line terminator is inserted between the operator and the operand.",
UnaryLike: "A cover of UnaryExpression and SpreadElement.",
Expand Down
18 changes: 18 additions & 0 deletions packages/babel-types/src/asserts/generated/index.ts
Expand Up @@ -1490,6 +1490,12 @@ export function assertTSTypeParameter(
): asserts node is t.TSTypeParameter {
assert("TSTypeParameter", node, opts);
}
export function assertStandardized(
node: object | null | undefined,
opts?: object | null,
): asserts node is t.Standardized {
assert("Standardized", node, opts);
}
export function assertExpression(
node: object | null | undefined,
opts?: object | null,
Expand Down Expand Up @@ -1742,6 +1748,18 @@ export function assertJSX(
): asserts node is t.JSX {
assert("JSX", node, opts);
}
export function assertMiscellaneous(
node: object | null | undefined,
opts?: object | null,
): asserts node is t.Miscellaneous {
assert("Miscellaneous", node, opts);
}
export function assertTypeScript(
node: object | null | undefined,
opts?: object | null,
): asserts node is t.TypeScript {
assert("TypeScript", node, opts);
}
export function assertTSTypeElement(
node: object | null | undefined,
opts?: object | null,
Expand Down

0 comments on commit b179365

Please sign in to comment.