diff --git a/tools/hermes-parser/js/hermes-eslint/src/scope-manager/referencer/ClassVisitor.js b/tools/hermes-parser/js/hermes-eslint/src/scope-manager/referencer/ClassVisitor.js index 134736076bd..67b116d46bf 100644 --- a/tools/hermes-parser/js/hermes-eslint/src/scope-manager/referencer/ClassVisitor.js +++ b/tools/hermes-parser/js/hermes-eslint/src/scope-manager/referencer/ClassVisitor.js @@ -18,6 +18,7 @@ import type { Identifier, MethodDefinition, PropertyDefinition, + StaticBlock, } from 'hermes-estree'; import type {Referencer} from './Referencer'; @@ -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; @@ -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); } diff --git a/tools/hermes-parser/js/hermes-estree/src/generated/predicates.js b/tools/hermes-parser/js/hermes-estree/src/generated/predicates.js index a5d78836886..d99ab36e9fa 100644 --- a/tools/hermes-parser/js/hermes-estree/src/generated/predicates.js +++ b/tools/hermes-parser/js/hermes-estree/src/generated/predicates.js @@ -172,6 +172,7 @@ import type { ReturnStatement, SequenceExpression, SpreadElement, + StaticBlock, StringLiteralTypeAnnotation, StringTypeAnnotation, Super, @@ -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'; } diff --git a/tools/hermes-parser/js/hermes-estree/src/types.js b/tools/hermes-parser/js/hermes-estree/src/types.js index c46be16079e..4bc54288d00 100644 --- a/tools/hermes-parser/js/hermes-estree/src/types.js +++ b/tools/hermes-parser/js/hermes-estree/src/types.js @@ -232,6 +232,7 @@ export type AFunction = export type Statement = | BlockStatement + | StaticBlock | BreakStatement | ClassDeclaration | ComponentDeclaration @@ -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 { @@ -293,6 +298,11 @@ export interface BlockStatement extends BaseNode { +body: $ReadOnlyArray; } +export interface StaticBlock extends BaseNode { + +type: 'StaticBlock'; + +body: $ReadOnlyArray; +} + export interface ExpressionStatement extends BaseNode { +type: 'ExpressionStatement'; +expression: Expression; @@ -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 diff --git a/tools/hermes-parser/js/hermes-parser/__tests__/HermesParser-test.js b/tools/hermes-parser/js/hermes-parser/__tests__/HermesParser-test.js index 2f885b1d1b4..4bbcebb745c 100644 --- a/tools/hermes-parser/js/hermes-parser/__tests__/HermesParser-test.js +++ b/tools/hermes-parser/js/hermes-parser/__tests__/HermesParser-test.js @@ -151,7 +151,7 @@ test('Semantic validation', () => { new SyntaxError( `'return' not in a function (1:0) return 1; -^~~~~~~~~`, +^~~~~~`, ), ); @@ -179,7 +179,7 @@ test('Allow return outside function', () => { new SyntaxError( `'return' not in a function (1:0) return 1 -^~~~~~~~`, +^~~~~~`, ), ); diff --git a/tools/hermes-parser/js/hermes-parser/src/HermesParserNodeDeserializers.js b/tools/hermes-parser/js/hermes-parser/src/HermesParserNodeDeserializers.js index b4e6ffe75e1..b9291a390e1 100644 --- a/tools/hermes-parser/js/hermes-parser/src/HermesParserNodeDeserializers.js +++ b/tools/hermes-parser/js/hermes-parser/src/HermesParserNodeDeserializers.js @@ -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', @@ -1975,6 +1982,7 @@ module.exports = [ deserializeDebuggerStatement, deserializeEmptyStatement, deserializeBlockStatement, + deserializeStaticBlock, deserializeBreakStatement, deserializeContinueStatement, deserializeThrowStatement, diff --git a/tools/hermes-parser/js/hermes-parser/src/generated/ESTreeVisitorKeys.js b/tools/hermes-parser/js/hermes-parser/src/generated/ESTreeVisitorKeys.js index 414a9374089..3318a23d3ae 100644 --- a/tools/hermes-parser/js/hermes-parser/src/generated/ESTreeVisitorKeys.js +++ b/tools/hermes-parser/js/hermes-parser/src/generated/ESTreeVisitorKeys.js @@ -233,6 +233,7 @@ module.exports = { ReturnStatement: ['argument'], SequenceExpression: ['expressions'], SpreadElement: ['argument'], + StaticBlock: ['body'], StringLiteralTypeAnnotation: [], StringTypeAnnotation: [], Super: [], diff --git a/tools/hermes-parser/js/hermes-parser/src/generated/ParserVisitorKeys.js b/tools/hermes-parser/js/hermes-parser/src/generated/ParserVisitorKeys.js index cfd592599bc..616b016a148 100644 --- a/tools/hermes-parser/js/hermes-parser/src/generated/ParserVisitorKeys.js +++ b/tools/hermes-parser/js/hermes-parser/src/generated/ParserVisitorKeys.js @@ -564,6 +564,9 @@ export const HERMES_AST_VISITOR_KEYS = { SpreadElement: { argument: 'Node', }, + StaticBlock: { + body: 'NodeList', + }, StringLiteral: {}, StringLiteralTypeAnnotation: {}, StringTypeAnnotation: {}, diff --git a/tools/hermes-parser/js/hermes-transform/src/generated/node-types.js b/tools/hermes-parser/js/hermes-transform/src/generated/node-types.js index b02196ae5d6..d2987b5880d 100644 --- a/tools/hermes-parser/js/hermes-transform/src/generated/node-types.js +++ b/tools/hermes-parser/js/hermes-transform/src/generated/node-types.js @@ -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, @@ -982,6 +983,10 @@ export type SpreadElementProps = { +argument: MaybeDetachedNode, }; +export type StaticBlockProps = { + +body: $ReadOnlyArray>, +}; + export type StringLiteralTypeAnnotationProps = { +value: StringLiteralTypeAnnotationType['value'], +raw: StringLiteralTypeAnnotationType['raw'], @@ -3057,6 +3062,18 @@ export function SpreadElement(props: { return node; } +export function StaticBlock(props: { + ...StaticBlockProps, + +parent?: ESNode, +}): DetachedNode { + const node = detachedProps((props.parent: $FlowFixMe), { + type: 'StaticBlock', + body: props.body.map(n => asDetachedNodeForCodeGen(n)), + }); + setParentPointersInDirectChildren((node: $FlowFixMe)); + return node; +} + export function StringLiteralTypeAnnotation(props: { ...StringLiteralTypeAnnotationProps, +parent?: ESNode,