Extracts modules/utils.js files from the flow and typescript folders in parsers-commons (#34898)

Summary:
This PR is a task from https://github.com/facebook/react-native/issues/34872
_Extract the modules/utils.js from the flow and typescript folders in a shared parsers-commons.js file. Then, have the two parsers use the same wrapModuleSchema function for modules._
(`wrapModuleSchema` is a copy-paste mistake, in this case it is `wrapNullable` and `unwrapNullable`)

## Changelog

<!-- Help reviewers and the release process by writing your own changelog entry. For an example, see:
https://reactnative.dev/contributing/changelogs-in-pull-requests
-->

[Internal] [Changed] - Extracts Codegen's modules/utils.js files from the flow and typescript folders in parsers-commons

Pull Request resolved: https://github.com/facebook/react-native/pull/34898

Test Plan:
I ran `yarn jest react-native-codegen`:
<img width="775" alt="Capture d’écran 2022-10-07 à 21 29 48" src="https://user-images.githubusercontent.com/17070498/194639515-a446c2cf-daf3-43a1-9833-cd546ca5865e.png">

Reviewed By: cipolleschi

Differential Revision: D40193740

Pulled By: cipolleschi

fbshipit-source-id: 02cbacc215fe5dd9bdd0839d8796587ab2821906
This commit is contained in:
Antoine Doubovetzky
2022-10-08 08:22:58 -07:00
committed by Facebook GitHub Bot
parent 0d3596aa4b
commit 24efebf83d
15 changed files with 44 additions and 103 deletions
@@ -22,7 +22,7 @@ import type {
import type {AliasResolver} from './Utils';
const {createAliasResolver, getModules} = require('./Utils');
const {unwrapNullable} = require('../../parsers/flow/modules/utils');
const {unwrapNullable} = require('../../parsers/parsers-commons');
type FilesOutput = Map<string, string>;
@@ -21,7 +21,7 @@ import type {
import type {AliasResolver} from './Utils';
const {createAliasResolver, getModules} = require('./Utils');
const {indent} = require('../Utils');
const {unwrapNullable} = require('../../parsers/flow/modules/utils');
const {unwrapNullable} = require('../../parsers/parsers-commons');
type FilesOutput = Map<string, string>;
@@ -22,7 +22,7 @@ import type {
import type {AliasResolver} from './Utils';
const {createAliasResolver, getModules} = require('./Utils');
const {unwrapNullable} = require('../../parsers/flow/modules/utils');
const {unwrapNullable} = require('../../parsers/parsers-commons');
type FilesOutput = Map<string, string>;
@@ -22,7 +22,7 @@ import type {
import type {AliasResolver} from './Utils';
const {createAliasResolver, getModules} = require('./Utils');
const {unwrapNullable} = require('../../parsers/flow/modules/utils');
const {unwrapNullable} = require('../../parsers/parsers-commons');
type FilesOutput = Map<string, string>;
@@ -32,7 +32,7 @@ const {capitalize} = require('../../Utils');
const {
unwrapNullable,
wrapNullable,
} = require('../../../parsers/flow/modules/utils');
} = require('../../../parsers/parsers-commons');
type StructContext = 'CONSTANTS' | 'REGULAR';
@@ -17,7 +17,7 @@ import type {Nullable} from '../../../../CodegenSchema';
import type {StructTypeAnnotation, ConstantsStruct} from '../StructCollector';
import type {StructSerilizationOutput} from './serializeStruct';
const {unwrapNullable} = require('../../../../parsers/flow/modules/utils');
const {unwrapNullable} = require('../../../../parsers/parsers-commons');
const StructTemplate = ({
hasteModuleName,
@@ -17,7 +17,7 @@ import type {Nullable} from '../../../../CodegenSchema';
import type {StructTypeAnnotation, RegularStruct} from '../StructCollector';
import type {StructSerilizationOutput} from './serializeStruct';
const {unwrapNullable} = require('../../../../parsers/flow/modules/utils');
const {unwrapNullable} = require('../../../../parsers/parsers-commons');
const StructTemplate = ({
hasteModuleName,
@@ -27,7 +27,7 @@ const {capitalize} = require('../../Utils');
const {
wrapNullable,
unwrapNullable,
} = require('../../../parsers/flow/modules/utils');
} = require('../../../parsers/parsers-commons');
const ProtocolMethodTemplate = ({
returnObjCType,
@@ -16,7 +16,7 @@ import type {
} from '../../../../CodegenSchema';
const {parseString} = require('../../index.js');
const {unwrapNullable} = require('../utils');
const {unwrapNullable} = require('../../../parsers-commons');
const {
UnsupportedFlowGenericParserError,
UnsupportedFlowTypeAnnotationParserError,
@@ -32,7 +32,7 @@ const {
visit,
isModuleRegistryCall,
} = require('../utils.js');
const {unwrapNullable, wrapNullable} = require('./utils');
const {unwrapNullable, wrapNullable} = require('../../parsers-commons');
const {
IncorrectlyParameterizedFlowGenericParserError,
MisnamedModuleFlowInterfaceParserError,
@@ -1,45 +0,0 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict
* @format
*/
'use strict';
import type {
NativeModuleTypeAnnotation,
Nullable,
} from '../../../CodegenSchema.js';
function unwrapNullable<+T: NativeModuleTypeAnnotation>(
x: Nullable<T>,
): [T, boolean] {
if (x.type === 'NullableTypeAnnotation') {
return [x.typeAnnotation, true];
}
return [x, false];
}
function wrapNullable<+T: NativeModuleTypeAnnotation>(
nullable: boolean,
typeAnnotation: T,
): Nullable<T> {
if (!nullable) {
return typeAnnotation;
}
return {
type: 'NullableTypeAnnotation',
typeAnnotation,
};
}
module.exports = {
unwrapNullable,
wrapNullable,
};
@@ -10,7 +10,12 @@
'use strict';
import type {SchemaType, NativeModuleSchema} from '../CodegenSchema.js';
import type {
SchemaType,
NativeModuleSchema,
NativeModuleTypeAnnotation,
Nullable,
} from '../CodegenSchema.js';
function wrapModuleSchema(
nativeModuleSchema: NativeModuleSchema,
@@ -23,6 +28,32 @@ function wrapModuleSchema(
};
}
function unwrapNullable<+T: NativeModuleTypeAnnotation>(
x: Nullable<T>,
): [T, boolean] {
if (x.type === 'NullableTypeAnnotation') {
return [x.typeAnnotation, true];
}
return [x, false];
}
function wrapNullable<+T: NativeModuleTypeAnnotation>(
nullable: boolean,
typeAnnotation: T,
): Nullable<T> {
if (!nullable) {
return typeAnnotation;
}
return {
type: 'NullableTypeAnnotation',
typeAnnotation,
};
}
module.exports = {
wrapModuleSchema,
unwrapNullable,
wrapNullable,
};
@@ -16,7 +16,7 @@ import type {
} from '../../../../CodegenSchema';
const {parseString} = require('../../index.js');
const {unwrapNullable} = require('../utils');
const {unwrapNullable} = require('../../../parsers-commons');
const {
UnsupportedTypeScriptGenericParserError,
UnsupportedTypeScriptTypeAnnotationParserError,
@@ -32,7 +32,7 @@ const {
visit,
isModuleRegistryCall,
} = require('../utils.js');
const {unwrapNullable, wrapNullable} = require('./utils');
const {unwrapNullable, wrapNullable} = require('../../parsers-commons');
const {
IncorrectlyParameterizedTypeScriptGenericParserError,
MisnamedModuleTypeScriptInterfaceParserError,
@@ -1,45 +0,0 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict
* @format
*/
'use strict';
import type {
NativeModuleTypeAnnotation,
Nullable,
} from '../../../CodegenSchema.js';
function unwrapNullable<+T: NativeModuleTypeAnnotation>(
x: Nullable<T>,
): [T, boolean] {
if (x.type === 'NullableTypeAnnotation') {
return [x.typeAnnotation, true];
}
return [x, false];
}
function wrapNullable<+T: NativeModuleTypeAnnotation>(
nullable: boolean,
typeAnnotation: T,
): Nullable<T> {
if (!nullable) {
return typeAnnotation;
}
return {
type: 'NullableTypeAnnotation',
typeAnnotation,
};
}
module.exports = {
unwrapNullable,
wrapNullable,
};