From a8b4d843344c965a09868bbe7875f606b4f3df4b Mon Sep 17 00:00:00 2001 From: Sam Zhou Date: Thu, 26 Sep 2024 23:34:26 -0700 Subject: [PATCH] Replace all %checks with type guards in hermes-parser js code Summary: I have to use comment-type and noformat these files, because the toolchain used by hermes cannot handle the new syntax. Changelog: [Internal] Reviewed By: panagosg7 Differential Revision: D63496291 fbshipit-source-id: 5c9ab4098ebbe3edd58e6eedac8735cb0c445157 --- .../referencer/PatternVisitor.js | 6 ++-- .../scope-manager/referencer/VisitorBase.js | 5 ++-- .../src/transform/astNodeMutationHelpers.js | 2 ++ .../src/traverse/getVisitorKeys.js | 5 ++-- .../utils/isValidModuleDeclarationParent.js | 28 ++++++++++++++++--- 5 files changed, 33 insertions(+), 13 deletions(-) diff --git a/tools/hermes-parser/js/hermes-eslint/src/scope-manager/referencer/PatternVisitor.js b/tools/hermes-parser/js/hermes-eslint/src/scope-manager/referencer/PatternVisitor.js index 454a04af365..3c8c550144c 100644 --- a/tools/hermes-parser/js/hermes-eslint/src/scope-manager/referencer/PatternVisitor.js +++ b/tools/hermes-parser/js/hermes-eslint/src/scope-manager/referencer/PatternVisitor.js @@ -5,7 +5,7 @@ * LICENSE file in the root directory of this source tree. * * @flow strict - * @format + * @noformat */ 'use strict'; @@ -43,8 +43,8 @@ type PatternVisitorTypeAnnotationCallback = ( pattern: BindingName, ) => void; -// $FlowFixMe[deprecated-type] -function isPattern(node: ESNode): boolean %checks { + +function isPattern(node: ESNode): boolean { return ( node.type === 'Identifier' || node.type === 'ObjectPattern' || diff --git a/tools/hermes-parser/js/hermes-eslint/src/scope-manager/referencer/VisitorBase.js b/tools/hermes-parser/js/hermes-eslint/src/scope-manager/referencer/VisitorBase.js index 8f2f3b15ce0..0401e0d7229 100644 --- a/tools/hermes-parser/js/hermes-eslint/src/scope-manager/referencer/VisitorBase.js +++ b/tools/hermes-parser/js/hermes-eslint/src/scope-manager/referencer/VisitorBase.js @@ -5,7 +5,7 @@ * LICENSE file in the root directory of this source tree. * * @flow strict - * @format + * @noformat */ 'use strict'; @@ -19,8 +19,7 @@ type VisitorOptions = $ReadOnly<{ childVisitorKeys?: VisitorKeysType | null, }>; -// $FlowFixMe[deprecated-type] -function isNode(node: mixed): boolean %checks { +function isNode(node: mixed) /*: implies node is {+[string]: mixed} */ { return ( typeof node === 'object' && node != null && typeof node.type === 'string' ); diff --git a/tools/hermes-parser/js/hermes-parser/src/transform/astNodeMutationHelpers.js b/tools/hermes-parser/js/hermes-parser/src/transform/astNodeMutationHelpers.js index 1786a9a52d4..40a71e703ff 100644 --- a/tools/hermes-parser/js/hermes-parser/src/transform/astNodeMutationHelpers.js +++ b/tools/hermes-parser/js/hermes-parser/src/transform/astNodeMutationHelpers.js @@ -53,6 +53,7 @@ function getParentKey( } } else if (Array.isArray(parent[key])) { for (let i = 0; i < parent[key].length; i += 1) { + // $FlowExpectedError[invalid-tuple-index] const current = parent[key][i]; if (current === target) { return {type: 'array', node: parent, key, targetIndex: i}; @@ -132,6 +133,7 @@ export function setParentPointersInDirectChildren( ): void { for (const key: $FlowFixMe of getVisitorKeys(node, visitorKeys)) { if (isNode(node[key])) { + // $FlowExpectedError[cannot-write] node[key].parent = node; } else if (Array.isArray(node[key])) { for (const child of node[key]) { diff --git a/tools/hermes-parser/js/hermes-parser/src/traverse/getVisitorKeys.js b/tools/hermes-parser/js/hermes-parser/src/traverse/getVisitorKeys.js index b2b0b67527a..241d7c118c7 100644 --- a/tools/hermes-parser/js/hermes-parser/src/traverse/getVisitorKeys.js +++ b/tools/hermes-parser/js/hermes-parser/src/traverse/getVisitorKeys.js @@ -5,7 +5,7 @@ * LICENSE file in the root directory of this source tree. * * @flow strict - * @format + * @noformat */ 'use strict'; @@ -15,8 +15,7 @@ import type {VisitorKeys as VisitorKeysType} from '../generated/ESTreeVisitorKey import FlowVisitorKeys from '../generated/ESTreeVisitorKeys'; -// $FlowFixMe[deprecated-type] -export function isNode(thing: mixed): boolean %checks { +export function isNode(thing: mixed) /*: implies thing is {+[string]: mixed} */ { return ( typeof thing === 'object' && thing != null && typeof thing.type === 'string' ); diff --git a/tools/hermes-parser/js/hermes-transform/src/transform/mutations/utils/isValidModuleDeclarationParent.js b/tools/hermes-parser/js/hermes-transform/src/transform/mutations/utils/isValidModuleDeclarationParent.js index b1f5650954b..0cc0c800c61 100644 --- a/tools/hermes-parser/js/hermes-transform/src/transform/mutations/utils/isValidModuleDeclarationParent.js +++ b/tools/hermes-parser/js/hermes-transform/src/transform/mutations/utils/isValidModuleDeclarationParent.js @@ -5,14 +5,34 @@ * LICENSE file in the root directory of this source tree. * * @flow strict-local - * @format + * @noformat */ -import type {ESNode, ModuleDeclaration, Statement} from 'hermes-estree'; +import type { + ESNode, + // Used in flow comments + // eslint-disable-next-line no-unused-vars + ExportAllDeclaration, + // Used in flow comments + // eslint-disable-next-line no-unused-vars + ExportDefaultDeclaration, + // Used in flow comments + // eslint-disable-next-line no-unused-vars + ExportNamedDeclaration, + // Used in flow comments + // eslint-disable-next-line no-unused-vars + ImportDeclaration, + ModuleDeclaration, + Statement, +} from 'hermes-estree'; import type {DetachedNode} from '../../../detachedNode'; -// $FlowFixMe[deprecated-type] -function isModuleDeclaration(node: ESNode): boolean %checks { +function isModuleDeclaration(node: ESNode) /*: node is ( + | ImportDeclaration + | ExportNamedDeclaration + | ExportDefaultDeclaration + | ExportAllDeclaration +) */ { return ( node.type === 'ImportDeclaration' || node.type === 'ExportNamedDeclaration' ||