Skip to content

Commit

Permalink
Don't strip OptionalCallExpression optional properties if they are false
Browse files Browse the repository at this point in the history
Reviewed By: josephsavona

Differential Revision: D49380148

fbshipit-source-id: 289911363e8adb1c4845eff9d104fc3f08a3ec0d
  • Loading branch information
pieterv authored and facebook-github-bot committed Sep 19, 2023
1 parent f8c73fd commit 4bd441c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,25 @@ import {parse as parseEspreeOriginal} from 'espree';
import {BABEL_VISITOR_KEYS, parse as parseHermesOriginal} from './parse';
import {SimpleTraverser} from '../src/traverse/SimpleTraverser';

function cleanAstForHermes(ast: mixed): mixed {
function cleanAstForHermes(ast: $FlowFixMe, style: 'babel' | 'estree'): mixed {
if (style === 'babel') {
// Babel changes what properties are stripped by each version, to support some
// older versions of Babel we don't exactly match the output of the latest babel
// version in all cases. This code allows us to strip properties when comparing
// AST's to ensure we can continue matching latest Babel in the tests.
SimpleTraverser.traverse(ast, {
enter(node: $FlowFixMe) {
// Most older version of babel expect this property.
if (node.type === 'OptionalCallExpression' && node.optional === false) {
// $FlowExpectedError[cannot-write]
delete node.optional;
}
},
leave() {},
visitorKeys: BABEL_VISITOR_KEYS,
});
}

return JSON.parse(
// $FlowExpectedError[incompatible-call]
JSON.stringify(ast, (_, value) => {
Expand Down Expand Up @@ -132,7 +150,7 @@ export function parseHermes(source: string, style: 'babel' | 'estree'): mixed {
sourceType: 'module',
tokens: false,
});
return cleanAstForHermes(ast);
return cleanAstForHermes(ast, style);
}

export type AlignmentExpectation = $ReadOnly<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1050,8 +1050,7 @@ function transformNode(node: ESNodeOrBabelNode): ESNodeOrBabelNode | null {

return node;
}
case 'CallExpression':
case 'OptionalCallExpression': {
case 'CallExpression': {
if (node.optional === false) {
// $FlowExpectedError[cannot-write]
delete node.optional;
Expand All @@ -1062,6 +1061,13 @@ function transformNode(node: ESNodeOrBabelNode): ESNodeOrBabelNode | null {
}
return node;
}
case 'OptionalCallExpression': {
if (node.typeArguments == null) {
// $FlowExpectedError[cannot-write]
delete node.typeArguments;
}
return node;
}
case 'MemberExpression': {
// $FlowExpectedError[cannot-write]
delete node.optional;
Expand Down

0 comments on commit 4bd441c

Please sign in to comment.