Skip to content

Commit

Permalink
JS changes for class static blocks
Browse files Browse the repository at this point in the history
Summary:
Static blocks are now declared nodes in the JS.

Original Author: [email protected]
Original Git: 2ff3274
Original Reviewed By: pieterv
Original Revision: D65847383

Reviewed By: fbmal7

Differential Revision: D66711284

fbshipit-source-id: 164ba58254d6d4ff612befda0ffb53a90fe44b27
  • Loading branch information
avp authored and facebook-github-bot committed Dec 3, 2024
1 parent 3ea2fe9 commit cc426dd
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import type {
Identifier,
MethodDefinition,
PropertyDefinition,
StaticBlock,
} from 'hermes-estree';
import type {Referencer} from './Referencer';

Expand Down Expand Up @@ -125,6 +126,12 @@ class ClassVisitor extends Visitor {
this._referencer.visitFunction(node.value);
}

visitStaticBlock(node: StaticBlock): void {
this._referencer.scopeManager.nestClassStaticBlockScope(node);
this._referencer.visitChildren(node);
this._referencer.close(node);
}

visitType: (?ESNode) => void = (node): void => {
if (!node) {
return;
Expand All @@ -150,6 +157,10 @@ class ClassVisitor extends Visitor {
this.visitMethod(node);
}

StaticBlock(node: StaticBlock): void {
this.visitStaticBlock(node);
}

Identifier(node: Identifier): void {
this._referencer.visit(node);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ import type {
ReturnStatement,
SequenceExpression,
SpreadElement,
StaticBlock,
StringLiteralTypeAnnotation,
StringTypeAnnotation,
Super,
Expand Down Expand Up @@ -915,6 +916,11 @@ export function isSpreadElement(node /*: ESNode | Token */) /*: implies node is
}


export function isStaticBlock(node /*: ESNode | Token */) /*: implies node is StaticBlock */ {
return node.type === 'StaticBlock';
}


export function isStringLiteralTypeAnnotation(node /*: ESNode | Token */) /*: implies node is StringLiteralTypeAnnotation */ {
return node.type === 'StringLiteralTypeAnnotation';
}
Expand Down
14 changes: 12 additions & 2 deletions tools/hermes-parser/js/hermes-estree/src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ export type AFunction =

export type Statement =
| BlockStatement
| StaticBlock
| BreakStatement
| ClassDeclaration
| ComponentDeclaration
Expand Down Expand Up @@ -281,7 +282,11 @@ export type StatementParentSingle =
| ForInStatement
| ForOfStatement;
// nodes that can be the parent of a statement that store the statements in an array
export type StatementParentArray = SwitchCase | Program | BlockStatement;
export type StatementParentArray =
| SwitchCase
| Program
| BlockStatement
| StaticBlock;
export type StatementParent = StatementParentSingle | StatementParentArray;

export interface EmptyStatement extends BaseNode {
Expand All @@ -293,6 +298,11 @@ export interface BlockStatement extends BaseNode {
+body: $ReadOnlyArray<Statement>;
}

export interface StaticBlock extends BaseNode {
+type: 'StaticBlock';
+body: $ReadOnlyArray<Statement>;
}

export interface ExpressionStatement extends BaseNode {
+type: 'ExpressionStatement';
+expression: Expression;
Expand Down Expand Up @@ -910,7 +920,7 @@ export type ClassPropertyNameNonComputed =
| Identifier
| StringLiteral;

export type ClassMember = PropertyDefinition | MethodDefinition;
export type ClassMember = PropertyDefinition | MethodDefinition | StaticBlock;
export type ClassMemberWithNonComputedName =
| PropertyDefinitionWithNonComputedName
| MethodDefinitionConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ test('Semantic validation', () => {
new SyntaxError(
`'return' not in a function (1:0)
return 1;
^~~~~~~~~`,
^~~~~~`,
),
);

Expand Down Expand Up @@ -179,7 +179,7 @@ test('Allow return outside function', () => {
new SyntaxError(
`'return' not in a function (1:0)
return 1
^~~~~~~~`,
^~~~~~`,
),
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,13 @@ function deserializeBlockStatement() {
body: this.deserializeNodeList(),
};
}
function deserializeStaticBlock() {
return {
type: 'StaticBlock',
loc: this.addEmptyLoc(),
body: this.deserializeNodeList(),
};
}
function deserializeBreakStatement() {
return {
type: 'BreakStatement',
Expand Down Expand Up @@ -1975,6 +1982,7 @@ module.exports = [
deserializeDebuggerStatement,
deserializeEmptyStatement,
deserializeBlockStatement,
deserializeStaticBlock,
deserializeBreakStatement,
deserializeContinueStatement,
deserializeThrowStatement,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ module.exports = {
ReturnStatement: ['argument'],
SequenceExpression: ['expressions'],
SpreadElement: ['argument'],
StaticBlock: ['body'],
StringLiteralTypeAnnotation: [],
StringTypeAnnotation: [],
Super: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,9 @@ export const HERMES_AST_VISITOR_KEYS = {
SpreadElement: {
argument: 'Node',
},
StaticBlock: {
body: 'NodeList',
},
StringLiteral: {},
StringLiteralTypeAnnotation: {},
StringTypeAnnotation: {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ import type {
ReturnStatement as ReturnStatementType,
SequenceExpression as SequenceExpressionType,
SpreadElement as SpreadElementType,
StaticBlock as StaticBlockType,
StringLiteralTypeAnnotation as StringLiteralTypeAnnotationType,
StringTypeAnnotation as StringTypeAnnotationType,
Super as SuperType,
Expand Down Expand Up @@ -982,6 +983,10 @@ export type SpreadElementProps = {
+argument: MaybeDetachedNode<SpreadElementType['argument']>,
};

export type StaticBlockProps = {
+body: $ReadOnlyArray<MaybeDetachedNode<StaticBlockType['body'][number]>>,
};

export type StringLiteralTypeAnnotationProps = {
+value: StringLiteralTypeAnnotationType['value'],
+raw: StringLiteralTypeAnnotationType['raw'],
Expand Down Expand Up @@ -3057,6 +3062,18 @@ export function SpreadElement(props: {
return node;
}

export function StaticBlock(props: {
...StaticBlockProps,
+parent?: ESNode,
}): DetachedNode<StaticBlockType> {
const node = detachedProps<StaticBlockType>((props.parent: $FlowFixMe), {
type: 'StaticBlock',
body: props.body.map(n => asDetachedNodeForCodeGen(n)),
});
setParentPointersInDirectChildren((node: $FlowFixMe));
return node;
}

export function StringLiteralTypeAnnotation(props: {
...StringLiteralTypeAnnotationProps,
+parent?: ESNode,
Expand Down

0 comments on commit cc426dd

Please sign in to comment.