Skip to content

Commit

Permalink
type updates and transform node updates
Browse files Browse the repository at this point in the history
Summary:
This diff:
1) fixes some bugs in the AST types
2) splits some ambiguous node types into more specific types (eg `ObjectTypeProperty` into `ObjectTypePropertySignature` and `ObjectTypeMethodSignature`)
3) refactors node transforms into separate files for cleanliness
4) adds new node creator functions for the aforementioned new node types
5) updates code to use the new functions

Reviewed By: pieterv

Differential Revision: D40921197

fbshipit-source-id: 42330f80814cd002a6cc70fc91915f9f8cfa654b
  • Loading branch information
bradzacher authored and facebook-github-bot committed Nov 3, 2022
1 parent b47267a commit 85613e1
Show file tree
Hide file tree
Showing 16 changed files with 1,168 additions and 705 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,14 @@ function getInterfaces(): $ReadOnlyMap<

const typesThatShouldBeSkipped = new Set([
// These types have a special union type declared to allow consumers to refine on `.computed`
'PropertyDefinition',
'MethodDefinition',
'BinaryExpression',
'DeclareExportDeclaration',
'ExportNamedDeclaration',
'MemberExpression',
'MethodDefinition',
'ObjectTypeProperty',
'Property',
'BinaryExpression',
'PropertyDefinition',
]);
const propertiesThatShouldBeSkipped = new Map([
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

import type {Program, ESNode} from 'hermes-estree';

// $FlowExpectedError[untyped-import]
// $FlowExpectedError
import {visit} from 'esrecurse';
import {parse} from '../../src';
import {analyze} from '../../src/scope-manager';
Expand Down
131 changes: 110 additions & 21 deletions tools/hermes-parser/js/hermes-estree/src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -853,7 +853,7 @@ interface BaseClass extends BaseNode {
+body: ClassBody;

+typeParameters: null | TypeParameterDeclaration;
+superTypeParameters: null | TypeParameterDeclaration;
+superTypeParameters: null | TypeParameterInstantiation;
+implements: $ReadOnlyArray<ClassImplements>;
+decorators: $ReadOnlyArray<Decorator>;
}
Expand Down Expand Up @@ -1027,13 +1027,30 @@ export type NamedDeclaration =
| InterfaceDeclaration
| EnumDeclaration;

export interface ExportNamedDeclaration extends BaseNode {
interface ExportNamedDeclarationBase extends BaseNode {
+type: 'ExportNamedDeclaration';
+declaration?: NamedDeclaration | null;
+specifiers: $ReadOnlyArray<ExportSpecifier>;
+source?: Literal | null;
+source?: StringLiteral | null;
+exportKind: 'value' | 'type';
}
export interface ExportNamedDeclarationWithSpecifiers
extends ExportNamedDeclarationBase {
+type: 'ExportNamedDeclaration';
+declaration: null;
+source: null;
+specifiers: $ReadOnlyArray<ExportSpecifier>;
}
export interface ExportNamedDeclarationWithDeclaration
extends ExportNamedDeclarationBase {
+type: 'ExportNamedDeclaration';
+declaration: NamedDeclaration;
+source: null;
+specifiers: [];
}
export type ExportNamedDeclaration =
| ExportNamedDeclarationWithSpecifiers
| ExportNamedDeclarationWithDeclaration;

export interface ExportSpecifier extends BaseNode {
+type: 'ExportSpecifier';
Expand Down Expand Up @@ -1065,6 +1082,7 @@ export interface AwaitExpression extends BaseNode {
export type TypeAnnotationType =
| NumberTypeAnnotation
| StringTypeAnnotation
| BigIntTypeAnnotation
| BooleanTypeAnnotation
| NullLiteralTypeAnnotation
| AnyTypeAnnotation
Expand Down Expand Up @@ -1128,6 +1146,9 @@ export interface NumberTypeAnnotation extends BaseNode {
export interface StringTypeAnnotation extends BaseNode {
+type: 'StringTypeAnnotation';
}
export interface BigIntTypeAnnotation extends BaseNode {
+type: 'BigIntTypeAnnotation';
}
export interface BooleanTypeAnnotation extends BaseNode {
+type: 'BooleanTypeAnnotation';
}
Expand Down Expand Up @@ -1170,7 +1191,7 @@ export interface BigIntLiteralTypeAnnotation extends BaseNode {
export interface BooleanLiteralTypeAnnotation extends BaseNode {
+type: 'BooleanLiteralTypeAnnotation';
+value: boolean;
+raw: string;
+raw: 'true' | 'false';
}
export interface ArrayTypeAnnotation extends BaseNode {
+type: 'ArrayTypeAnnotation';
Expand Down Expand Up @@ -1199,7 +1220,7 @@ export interface TypeofTypeAnnotation extends BaseNode {
}
export interface TupleTypeAnnotation extends BaseNode {
+type: 'TupleTypeAnnotation';
+types: TypeAnnotationType;
+types: $ReadOnlyArray<TypeAnnotationType>;
}

// type T = { [[foo]]: number };
Expand Down Expand Up @@ -1258,23 +1279,58 @@ export interface ObjectTypeAnnotation extends BaseNode {
+callProperties: $ReadOnlyArray<ObjectTypeCallProperty>;
+internalSlots: $ReadOnlyArray<ObjectTypeInternalSlot>;
}
export interface ObjectTypeProperty extends BaseNode {
interface ObjectTypePropertyBase extends BaseNode {
+type: 'ObjectTypeProperty';
+key: Identifier | StringLiteral;
+value: TypeAnnotationType;
+method: boolean;
+optional: boolean;
+static: boolean;
+proto: false; // ???
+static: boolean; // only applies to the "declare class" case
+proto: boolean; // only applies to the "declare class" case
+variance: Variance | null;
+kind: 'init' | 'get' | 'set';

+parent: ObjectTypeAnnotation;
}
export interface ObjectTypeMethodSignature extends ObjectTypePropertyBase {
+type: 'ObjectTypeProperty';
+value: FunctionTypeAnnotation;
+method: true;
+optional: false;
+variance: null;
+kind: 'init';

+parent: ObjectTypeAnnotation;
}
export interface ObjectTypePropertySignature extends ObjectTypePropertyBase {
+type: 'ObjectTypeProperty';
+value: TypeAnnotationType;
+method: false;
+optional: boolean;
+variance: Variance | null;
+kind: 'init';

+parent: ObjectTypeAnnotation;
}
export interface ObjectTypeAccessorSignature extends ObjectTypePropertyBase {
+type: 'ObjectTypeProperty';
+value: FunctionTypeAnnotation;
+method: false;
+optional: false;
+variance: null;
+kind: 'get' | 'set';

+parent: ObjectTypeAnnotation;
}
export type ObjectTypeProperty =
| ObjectTypeMethodSignature
| ObjectTypePropertySignature
| ObjectTypeAccessorSignature;

export interface ObjectTypeCallProperty extends BaseNode {
+type: 'ObjectTypeCallProperty';
+value: FunctionTypeAnnotation;
+static: false; // can't be static
+static: boolean; // can only be static when defined on a declare class

+parent: ObjectTypeAnnotation;
}
Expand All @@ -1283,7 +1339,7 @@ export interface ObjectTypeIndexer extends BaseNode {
+id: null | Identifier;
+key: TypeAnnotationType;
+value: TypeAnnotationType;
+static: false; // can't be static
+static: boolean; // can only be static when defined on a declare class
+variance: null | Variance;

+parent: ObjectTypeAnnotation;
Expand Down Expand Up @@ -1329,15 +1385,15 @@ export interface InterfaceDeclaration extends BaseInterfaceDeclaration {
export interface InterfaceExtends extends BaseNode {
+type: 'InterfaceExtends';
+id: Identifier;
+typeParameters: null | TypeParameterDeclaration;
+typeParameters: null | TypeParameterInstantiation;

+parent: InterfaceDeclaration | DeclareInterface;
}

export interface ClassImplements extends BaseNode {
+type: 'ClassImplements';
+id: Identifier;
+typeParameters: null | TypeParameterDeclaration;
+typeParameters: null | TypeParameterInstantiation;

+parent: AClass | DeclareClass;
}
Expand Down Expand Up @@ -1479,7 +1535,12 @@ export interface DeclareVariable extends BaseNode {

export interface DeclareFunction extends BaseNode {
+type: 'DeclareFunction';
+id: Identifier;
// the function signature is stored as a type annotation on the ID
+id: interface extends Identifier {
+typeAnnotation: interface extends TypeAnnotation {
+typeAnnotation: FunctionTypeAnnotation,
},
};
+predicate: InferredPredicate | DeclaredPredicate | null;
}

Expand Down Expand Up @@ -1508,21 +1569,49 @@ export interface DeclareExportAllDeclaration extends BaseNode {
+source: StringLiteral;
}

export interface DeclareExportDeclaration extends BaseNode {
interface DeclareExportDeclarationBase extends BaseNode {
+type: 'DeclareExportDeclaration';
+specifiers: $ReadOnlyArray<ExportSpecifier>;
+source: StringLiteral | null;
+default: boolean;
}
export interface DeclareExportDefaultDeclaration
extends DeclareExportDeclarationBase {
+type: 'DeclareExportDeclaration';
+declaration: DeclareClass | DeclareFunction | TypeAnnotationType;
+default: true;
// default cannot have a source
+source: null;
// default cannot have specifiers
+specifiers: [];
}
export interface DeclareExportDeclarationNamedWithDeclaration
extends DeclareExportDeclarationBase {
+type: 'DeclareExportDeclaration';
+declaration:
| TypeAlias
| DeclareVariable
| DeclareClass
| DeclareFunction
| DeclareOpaqueType
| DeclareInterface
| TypeAnnotationType
| null;
+source: StringLiteral | null;
+default: boolean;
| DeclareOpaqueType
| DeclareVariable;
+default: false;
+source: null;
// default cannot have specifiers and a declaration
+specifiers: [];
}
export interface DeclareExportDeclarationNamedWithSpecifiers
extends DeclareExportDeclarationBase {
+type: 'DeclareExportDeclaration';
// with a source you can't have a declaration
+declaration: null;
+default: false;
+source: StringLiteral;
+specifiers: $ReadOnlyArray<ExportSpecifier>;
}
export type DeclareExportDeclaration =
| DeclareExportDefaultDeclaration
| DeclareExportDeclarationNamedWithDeclaration
| DeclareExportDeclarationNamedWithSpecifiers;

export interface DeclareModuleExports extends BaseNode {
+type: 'DeclareModuleExports';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1296,32 +1296,47 @@ x?.();
});
context.modifyNodeInPlace(node, {
properties: [
t.ObjectTypeProperty({
t.ObjectTypeMethodSignature({
key: t.Identifier({name: 'a'}),
value: func,
method: true,
optional: false,
static: false,
proto: false,
variance: null,
kind: 'init',
}),
t.ObjectTypeProperty({
t.ObjectTypePropertySignature({
key: t.Identifier({name: 'b'}),
value: func,
method: false,
optional: false,
static: false,
proto: false,
variance: null,
kind: 'init',
}),
t.ObjectTypeAccessorSignature({
key: t.Identifier({name: 'c'}),
value: func,
kind: 'get',
}),
t.ObjectTypeAccessorSignature({
key: t.Identifier({name: 'd'}),
// setters must have a param hence new func
value: t.FunctionTypeAnnotation({
params: [
t.FunctionTypeParam({
name: t.Identifier({
name: 'param',
}),
optional: false,
typeAnnotation: t.StringTypeAnnotation(),
}),
],
returnType: t.VoidTypeAnnotation(),
rest: null,
typeParameters: null,
this: null,
}),
kind: 'set',
}),
],
});
},
}));
expect(result).toBe(`\
type A = {a(): void, b: () => void};
type A = {a(): void, b: () => void, get c(): void, set d(param: string): void};
`);
});
});
Loading

0 comments on commit 85613e1

Please sign in to comment.