Skip to content

Commit

Permalink
Extract isModuleRegistryCall function in parsers/utils (#35139)
Browse files Browse the repository at this point in the history
Summary:
This PR is a task from #34872:
> Extract the function isModuleRegistryCall ([Flow](https://github.com/facebook/react-native/blob/main/packages/react-native-codegen/src/parsers/flow/utils.js#L175-L211) [TypeScript](https://github.com/facebook/react-native/blob/main/packages/react-native-codegen/src/parsers/typescript/utils.js#L167-L203)) into a single function in the parsers/utils.js file and replace its invocation with this new function.

## 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] - Extract the function isModuleRegistryCall in parsers/utils

Pull Request resolved: #35139

Test Plan: I tested using Jest and Flow commands.

Reviewed By: rshest

Differential Revision: D40850471

Pulled By: cipolleschi

fbshipit-source-id: 34ec8ea4d7175e205315d60f200df093f1204b7b
  • Loading branch information
Antoine Doubovetzky authored and facebook-github-bot committed Oct 31, 2022
1 parent 56d7a87 commit 83e2126
Show file tree
Hide file tree
Showing 8 changed files with 200 additions and 97 deletions.
146 changes: 146 additions & 0 deletions packages/react-native-codegen/src/parsers/__tests__/utils-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const {
verifyPlatforms,
visit,
buildSchemaFromConfigType,
isModuleRegistryCall,
} = require('../utils.js');
const {ParserError} = require('../errors');

Expand Down Expand Up @@ -435,4 +436,149 @@ describe('buildSchemaFromConfigType', () => {
});
});
});

describe('isModuleRegistryCall', () => {
describe('when node is not of CallExpression type', () => {
it('returns false', () => {
const node = {
type: 'NotCallExpression',
};
expect(isModuleRegistryCall(node)).toBe(false);
});
});

describe('when node is of CallExpressionType', () => {
describe('when callee type is not of MemberExpression type', () => {
it('returns false', () => {
const node = {
type: 'CallExpression',
callee: {
type: 'NotMemberExpression',
},
};
expect(isModuleRegistryCall(node)).toBe(false);
});
});

describe('when callee type is of MemberExpression type', () => {
describe('when memberExpression has an object of type different than "Identifier"', () => {
it('returns false', () => {
const node = {
type: 'CallExpression',
callee: {
type: 'MemberExpression',
object: {
type: 'NotIdentifier',
name: 'TurboModuleRegistry',
},
},
};
expect(isModuleRegistryCall(node)).toBe(false);
});
});

describe('when memberExpression has an object of name different than "TurboModuleRegistry"', () => {
it('returns false', () => {
const node = {
type: 'CallExpression',
callee: {
type: 'MemberExpression',
object: {
type: 'Identifier',
name: 'NotTurboModuleRegistry',
},
},
};
expect(isModuleRegistryCall(node)).toBe(false);
});
});

describe('when memberExpression has an object of type "Identifier" and name "TurboModuleRegistry', () => {
describe('when memberExpression has a property of type different than "Identifier"', () => {
it('returns false', () => {
const node = {
type: 'CallExpression',
callee: {
type: 'MemberExpression',
object: {
type: 'Identifier',
name: 'TurboModuleRegistry',
},
property: {
type: 'NotIdentifier',
name: 'get',
},
},
};
expect(isModuleRegistryCall(node)).toBe(false);
});
});

describe('when memberExpression has a property of name different than "get" or "getEnforcing', () => {
it('returns false', () => {
const node = {
type: 'CallExpression',
callee: {
type: 'MemberExpression',
object: {
type: 'Identifier',
name: 'TurboModuleRegistry',
},
property: {
type: 'Identifier',
name: 'NotGet',
},
},
};
expect(isModuleRegistryCall(node)).toBe(false);
});
});

describe('when memberExpression has a property of type "Identifier" and of name "get" or "getEnforcing', () => {
describe('when memberExpression is computed', () => {
it('returns false', () => {
const node = {
type: 'CallExpression',
callee: {
type: 'MemberExpression',
object: {
type: 'Identifier',
name: 'TurboModuleRegistry',
},
property: {
type: 'Identifier',
name: 'get',
},
computed: true,
},
};
expect(isModuleRegistryCall(node)).toBe(false);
});
});

describe('when memberExpression is not computed', () => {
it('returns true', () => {
const node = {
type: 'CallExpression',
callee: {
type: 'MemberExpression',
object: {
type: 'Identifier',
name: 'TurboModuleRegistry',
},
property: {
type: 'Identifier',
name: 'get',
},
computed: false,
},
};
expect(isModuleRegistryCall(node)).toBe(true);
});
});
});
});
});
});
});
});
7 changes: 5 additions & 2 deletions packages/react-native-codegen/src/parsers/flow/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ import type {SchemaType} from '../../CodegenSchema.js';
// $FlowFixMe[untyped-import] there's no flowtype flow-parser
const flowParser = require('flow-parser');
const fs = require('fs');
const {buildSchemaFromConfigType, getConfigType} = require('../utils');
const {
buildSchemaFromConfigType,
getConfigType,
isModuleRegistryCall,
} = require('../utils');
const {buildComponentSchema} = require('./components');
const {wrapComponentSchema} = require('./components/schema');
const {buildModuleSchema} = require('./modules');
const {isModuleRegistryCall} = require('./utils');

function Visitor(infoMap: {isComponent: boolean, isModule: boolean}) {
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,8 @@ import type {NativeModuleTypeAnnotation} from '../../../CodegenSchema.js';
const {nullGuard} = require('../../parsers-utils');

const {throwIfMoreThanOneModuleRegistryCalls} = require('../../error-utils');
const {visit} = require('../../utils');
const {
resolveTypeAnnotation,
getTypes,
isModuleRegistryCall,
} = require('../utils.js');
const {visit, isModuleRegistryCall} = require('../../utils');
const {resolveTypeAnnotation, getTypes} = require('../utils.js');
const {
unwrapNullable,
wrapNullable,
Expand Down
40 changes: 0 additions & 40 deletions packages/react-native-codegen/src/parsers/flow/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,48 +116,8 @@ function getValueFromTypes(value: ASTNode, types: TypeDeclarationMap): ASTNode {
return value;
}

// TODO(T71778680): Flow-type ASTNodes.
function isModuleRegistryCall(node: $FlowFixMe): boolean {
if (node.type !== 'CallExpression') {
return false;
}

const callExpression = node;

if (callExpression.callee.type !== 'MemberExpression') {
return false;
}

const memberExpression = callExpression.callee;
if (
!(
memberExpression.object.type === 'Identifier' &&
memberExpression.object.name === 'TurboModuleRegistry'
)
) {
return false;
}

if (
!(
memberExpression.property.type === 'Identifier' &&
(memberExpression.property.name === 'get' ||
memberExpression.property.name === 'getEnforcing')
)
) {
return false;
}

if (memberExpression.computed) {
return false;
}

return true;
}

module.exports = {
getValueFromTypes,
resolveTypeAnnotation,
getTypes,
isModuleRegistryCall,
};
8 changes: 5 additions & 3 deletions packages/react-native-codegen/src/parsers/typescript/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ import type {SchemaType} from '../../CodegenSchema.js';

const babelParser = require('@babel/parser');
const fs = require('fs');
const {buildSchemaFromConfigType, getConfigType} = require('../utils');
const {
buildSchemaFromConfigType,
getConfigType,
isModuleRegistryCall,
} = require('../utils');
const {buildComponentSchema} = require('./components');
const {wrapComponentSchema} = require('./components/schema');
const {buildModuleSchema} = require('./modules');

const {isModuleRegistryCall} = require('./utils');

function Visitor(infoMap: {isComponent: boolean, isModule: boolean}) {
return {
CallExpression(node: $FlowFixMe) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,8 @@ const {
throwIfMoreThanOneModuleRegistryCalls,
throwIfUnsupportedFunctionParamTypeAnnotationParserError,
} = require('../../error-utils');
const {visit} = require('../../utils');
const {
resolveTypeAnnotation,
getTypes,
isModuleRegistryCall,
} = require('../utils.js');
const {visit, isModuleRegistryCall} = require('../../utils');
const {resolveTypeAnnotation, getTypes} = require('../utils.js');
const {
unwrapNullable,
wrapNullable,
Expand Down
40 changes: 0 additions & 40 deletions packages/react-native-codegen/src/parsers/typescript/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,47 +109,7 @@ function resolveTypeAnnotation(
};
}

// TODO(T108222691): Use flow-types for @babel/parser
function isModuleRegistryCall(node: $FlowFixMe): boolean {
if (node.type !== 'CallExpression') {
return false;
}

const callExpression = node;

if (callExpression.callee.type !== 'MemberExpression') {
return false;
}

const memberExpression = callExpression.callee;
if (
!(
memberExpression.object.type === 'Identifier' &&
memberExpression.object.name === 'TurboModuleRegistry'
)
) {
return false;
}

if (
!(
memberExpression.property.type === 'Identifier' &&
(memberExpression.property.name === 'get' ||
memberExpression.property.name === 'getEnforcing')
)
) {
return false;
}

if (memberExpression.computed) {
return false;
}

return true;
}

module.exports = {
resolveTypeAnnotation,
getTypes,
isModuleRegistryCall,
};
40 changes: 40 additions & 0 deletions packages/react-native-codegen/src/parsers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,45 @@ function getConfigType(
}
}

// TODO(T71778680): Flow-type ASTNodes.
function isModuleRegistryCall(node: $FlowFixMe): boolean {
if (node.type !== 'CallExpression') {
return false;
}

const callExpression = node;

if (callExpression.callee.type !== 'MemberExpression') {
return false;
}

const memberExpression = callExpression.callee;
if (
!(
memberExpression.object.type === 'Identifier' &&
memberExpression.object.name === 'TurboModuleRegistry'
)
) {
return false;
}

if (
!(
memberExpression.property.type === 'Identifier' &&
(memberExpression.property.name === 'get' ||
memberExpression.property.name === 'getEnforcing')
)
) {
return false;
}

if (memberExpression.computed) {
return false;
}

return true;
}

module.exports = {
getConfigType,
extractNativeModuleName,
Expand All @@ -223,4 +262,5 @@ module.exports = {
parseFile,
visit,
buildSchemaFromConfigType,
isModuleRegistryCall,
};

0 comments on commit 83e2126

Please sign in to comment.