Skip to content

Commit

Permalink
update node creator codegen to better handle optional props
Browse files Browse the repository at this point in the history
Summary:
previously the codegen would emit something like `DetachedNode<null | Identifier>` for an optional property - which technically correct, but pretty hard to use.
This just updates the codegen to mark optional properties as optional so that they can be omitted or set to an explicit `null`.

Reviewed By: pieterv

Differential Revision: D32269199

fbshipit-source-id: 24517ed25925fac2b140b837aa822c8b6e1a76df
  • Loading branch information
bradzacher authored and facebook-github-bot committed Nov 12, 2021
1 parent 4bb7a9e commit 8b8e095
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,15 @@ export function BlockStatement({
export function ExpressionStatement({
parent,
expression,
directive = null,
...props
}: {
+expression: DetachedNode<ExpressionStatementType['expression']>,
+directive?: ExpressionStatementType['directive'],
+directive?: ?ExpressionStatementType['directive'],
+parent?: ESNode,
}): DetachedNode<ExpressionStatementType> {
const node = detachedProps<ExpressionStatementType>(parent, {
type: 'ExpressionStatement',
expression,
directive,
...props,
});
setParentPointersInDirectChildren(node);
return node;
Expand Down Expand Up @@ -96,7 +94,7 @@ export function VariableDeclarator({
parent,
...props
}: {
+init: DetachedNode<VariableDeclaratorType['init']>,
+init?: ?DetachedNode<VariableDeclaratorType['init']>,
+id: DetachedNode<VariableDeclaratorType['id']>,
+parent?: ESNode,
}): DetachedNode<VariableDeclaratorType> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import type {
RegExpLiteral as RegExpLiteralType,
TemplateElement as TemplateElementType,
Identifier as IdentifierType,
ExpressionStatement as ExpressionStatementType,
BooleanLiteral as BooleanLiteralType,
NumericLiteral as NumericLiteralType,
NullLiteral as NullLiteralType,
Expand Down Expand Up @@ -96,26 +95,6 @@ export function Identifier({
return node;
}
// hermes always emits the directive prop, but nobody should ever want to use it
// so we default it to `null` here
export function ExpressionStatement({
parent,
expression,
directive = null,
}: {
+expression: DetachedNode<ExpressionStatementType['expression']>,
+directive?: ExpressionStatementType['directive'],
+parent?: ESNode,
}): DetachedNode<ExpressionStatementType> {
const node = detachedProps<ExpressionStatementType>(parent, {
type: 'ExpressionStatement',
expression,
directive,
});
setParentPointersInDirectChildren(node);
return node;
}
//
// Literals require a "raw" which is added by the estree transform, not hermes.
//
Expand Down
5 changes: 4 additions & 1 deletion tools/hermes-parser/js/scripts/genTransformNodeTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ const NODES_WITH_SPECIAL_HANDLING = new Set([
'RegExpLiteral',
'TemplateElement',
'Identifier',
'ExpressionStatement',
'BooleanLiteral',
'NumericLiteral',
'NullLiteral',
Expand Down Expand Up @@ -70,6 +69,10 @@ export function ${node.name}({parent, ...props}: {
} else if (arg.type === 'NodeList') {
type = `$ReadOnlyArray<DetachedNode<${type}[number]>>`;
}
if (arg.optional) {
return `+${arg.name}?: ?${type}`;
}
return `+${arg.name}: ${type}`;
})
.join(',\n')},
Expand Down

0 comments on commit 8b8e095

Please sign in to comment.