Skip to content

Commit

Permalink
Replace all %checks with type guards in hermes-parser js code
Browse files Browse the repository at this point in the history
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
  • Loading branch information
SamChou19815 authored and facebook-github-bot committed Sep 27, 2024
1 parent 27abbc4 commit a8b4d84
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* LICENSE file in the root directory of this source tree.
*
* @flow strict
* @format
* @noformat
*/

'use strict';
Expand Down Expand Up @@ -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' ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* LICENSE file in the root directory of this source tree.
*
* @flow strict
* @format
* @noformat
*/

'use strict';
Expand All @@ -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'
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* LICENSE file in the root directory of this source tree.
*
* @flow strict
* @format
* @noformat
*/

'use strict';
Expand All @@ -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'
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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' ||
Expand Down

0 comments on commit a8b4d84

Please sign in to comment.