Skip to content

Commit d9f2cbe

Browse files
tarunrajputfacebook-github-bot
authored andcommitted
extract Visitor to parsers primitives (#36459)
Summary: Part of Codegen Issue #34872 > [Codegen 88] Move the Visitor.js file from parsers/flow/Visitor.js to parser-promitives.js. Copy the TSInterfaceDeclaration(node: $FlowFixMe) function and add it to the Visitor.js just copied. Remove the parsers/typescript/Visitor.js. Make sure we use the same Visitor in both parsers. (We will end up with a Visitor that is the union of the two, being able to handle both Flow and TS. In this specific case, this trade-off make sense as it allows us to remove one file, several duplicated lines for a small price.) ## Changelog <!-- Help reviewers and the release process by writing your own changelog entry. Pick one each for the category and type tags: [ANDROID|GENERAL|IOS|INTERNAL] [BREAKING|ADDED|CHANGED|DEPRECATED|REMOVED|FIXED|SECURITY] - Message For more details, see: https://reactnative.dev/contributing/changelogs-in-pull-requests --> [Internal][Changed] - Extract Visitor function to parsers primitives and remove both parsers visitor files Pull Request resolved: #36459 Test Plan: ``` yarn jest yarn flow yarn lint yarn format-check ``` Reviewed By: cortinico Differential Revision: D44021825 Pulled By: cipolleschi fbshipit-source-id: ea465404830402c44081143ee0539107dc75776c
1 parent 347d6f8 commit d9f2cbe

File tree

7 files changed

+140
-91
lines changed

7 files changed

+140
-91
lines changed

packages/react-native-codegen/src/parsers/__tests__/parsers-commons-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import {
2424
} from '../parsers-commons';
2525
import type {ParserType} from '../errors';
2626

27-
const {Visitor} = require('../flow/Visitor');
27+
const {Visitor} = require('../parsers-primitives');
2828
const {wrapComponentSchema} = require('../schema.js');
2929
const {buildComponentSchema} = require('../flow/components');
3030
const {buildModuleSchema} = require('../parsers-commons.js');

packages/react-native-codegen/src/parsers/__tests__/parsers-primitives-test.js

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const {
3030
emitMixed,
3131
typeAliasResolution,
3232
typeEnumResolution,
33+
Visitor,
3334
} = require('../parsers-primitives.js');
3435
const {MockedParser} = require('../parserMock');
3536
const {emitUnion} = require('../parsers-primitives');
@@ -1155,3 +1156,102 @@ describe('emitArrayType', () => {
11551156
});
11561157
});
11571158
});
1159+
1160+
describe('Visitor', () => {
1161+
describe('CallExpression', () => {
1162+
it('sets isComponent to true if callee type is Identifier and callee name is codegenNativeComponent', () => {
1163+
const infoMap = {isComponent: false, isModule: false};
1164+
const node = {
1165+
callee: {type: 'Identifier', name: 'codegenNativeComponent'},
1166+
};
1167+
const visitor = Visitor(infoMap);
1168+
visitor.CallExpression(node);
1169+
1170+
expect(infoMap.isComponent).toBe(true);
1171+
});
1172+
1173+
it('should not set isComponent to true if callee type is not Identifier or callee name is not codegenNativeComponent', () => {
1174+
const infoMap = {isComponent: false, isModule: false};
1175+
const node = {
1176+
callee: {type: '', name: ''},
1177+
};
1178+
const visitor = Visitor(infoMap);
1179+
visitor.CallExpression(node);
1180+
1181+
expect(infoMap.isComponent).toBe(false);
1182+
});
1183+
1184+
it('sets isModule to true if isModuleRegistryCall', () => {
1185+
const infoMap = {isComponent: false, isModule: false};
1186+
const node = {
1187+
type: 'CallExpression',
1188+
callee: {
1189+
type: 'MemberExpression',
1190+
object: {type: 'Identifier', name: 'TurboModuleRegistry'},
1191+
property: {type: 'Identifier', name: 'getEnforcing'},
1192+
},
1193+
};
1194+
const visitor = Visitor(infoMap);
1195+
visitor.CallExpression(node);
1196+
1197+
expect(infoMap.isModule).toBe(true);
1198+
});
1199+
1200+
it('should not set isModule to true if not isModuleRegistryCall', () => {
1201+
const infoMap = {isComponent: false, isModule: false};
1202+
const node = {
1203+
callee: {
1204+
type: 'Expression',
1205+
},
1206+
};
1207+
const visitor = Visitor(infoMap);
1208+
visitor.CallExpression(node);
1209+
1210+
expect(infoMap.isModule).toBe(false);
1211+
});
1212+
});
1213+
1214+
describe('InterfaceExtends', () => {
1215+
it('sets isModule to true if module interface extends TurboModule', () => {
1216+
const infoMap = {isComponent: false, isModule: false};
1217+
const node = {id: {name: 'TurboModule'}};
1218+
1219+
const visitor = Visitor(infoMap);
1220+
visitor.InterfaceExtends(node);
1221+
1222+
expect(infoMap.isModule).toBe(true);
1223+
});
1224+
1225+
it('should not set isModule to true if module interface does not extends TurboModule', () => {
1226+
const infoMap = {isComponent: false, isModule: false};
1227+
const node = {id: {name: ''}};
1228+
1229+
const visitor = Visitor(infoMap);
1230+
visitor.InterfaceExtends(node);
1231+
1232+
expect(infoMap.isModule).toBe(false);
1233+
});
1234+
});
1235+
1236+
describe('TSInterfaceDeclaration', () => {
1237+
it('sets isModule to true if TypeScript Interface Declaration extends TurboModule', () => {
1238+
const infoMap = {isComponent: false, isModule: false};
1239+
const node = {extends: [{expression: {name: 'TurboModule'}}]};
1240+
1241+
const visitor = Visitor(infoMap);
1242+
visitor.TSInterfaceDeclaration(node);
1243+
1244+
expect(infoMap.isModule).toBe(true);
1245+
});
1246+
1247+
it('should not set isModule to true if TypeScript Interface Declaration does not extends TurboModule', () => {
1248+
const infoMap = {isComponent: false, isModule: false};
1249+
const node = {extends: [{expression: {name: ''}}]};
1250+
1251+
const visitor = Visitor(infoMap);
1252+
visitor.TSInterfaceDeclaration(node);
1253+
1254+
expect(infoMap.isModule).toBe(false);
1255+
});
1256+
});
1257+
});

packages/react-native-codegen/src/parsers/flow/Visitor.js

Lines changed: 0 additions & 41 deletions
This file was deleted.

packages/react-native-codegen/src/parsers/flow/parser.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const {flowTranslateTypeAnnotation} = require('./modules');
3131
const flowParser = require('flow-parser');
3232

3333
const {buildSchema} = require('../parsers-commons');
34-
const {Visitor} = require('./Visitor');
34+
const {Visitor} = require('../parsers-primitives');
3535
const {buildComponentSchema} = require('./components');
3636
const {wrapComponentSchema} = require('../schema.js');
3737
const {buildModuleSchema} = require('../parsers-commons.js');

packages/react-native-codegen/src/parsers/parsers-primitives.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ const {
5959
translateFunctionTypeAnnotation,
6060
} = require('./parsers-commons');
6161

62+
const {isModuleRegistryCall} = require('./utils');
63+
6264
function emitBoolean(nullable: boolean): Nullable<BooleanTypeAnnotation> {
6365
return wrapNullable(nullable, {
6466
type: 'BooleanTypeAnnotation',
@@ -439,6 +441,40 @@ function emitArrayType(
439441
);
440442
}
441443

444+
function Visitor(infoMap: {isComponent: boolean, isModule: boolean}): {
445+
[type: string]: (node: $FlowFixMe) => void,
446+
} {
447+
return {
448+
CallExpression(node: $FlowFixMe) {
449+
if (
450+
node.callee.type === 'Identifier' &&
451+
node.callee.name === 'codegenNativeComponent'
452+
) {
453+
infoMap.isComponent = true;
454+
}
455+
456+
if (isModuleRegistryCall(node)) {
457+
infoMap.isModule = true;
458+
}
459+
},
460+
InterfaceExtends(node: $FlowFixMe) {
461+
if (node.id.name === 'TurboModule') {
462+
infoMap.isModule = true;
463+
}
464+
},
465+
TSInterfaceDeclaration(node: $FlowFixMe) {
466+
if (
467+
Array.isArray(node.extends) &&
468+
node.extends.some(
469+
extension => extension.expression.name === 'TurboModule',
470+
)
471+
) {
472+
infoMap.isModule = true;
473+
}
474+
},
475+
};
476+
}
477+
442478
module.exports = {
443479
emitArrayType,
444480
emitBoolean,
@@ -459,4 +495,5 @@ module.exports = {
459495
typeAliasResolution,
460496
typeEnumResolution,
461497
translateArrayTypeAnnotation,
498+
Visitor,
462499
};

packages/react-native-codegen/src/parsers/typescript/Visitor.js

Lines changed: 0 additions & 47 deletions
This file was deleted.

packages/react-native-codegen/src/parsers/typescript/parser.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const {typeScriptTranslateTypeAnnotation} = require('./modules');
3131
const babelParser = require('@babel/parser');
3232

3333
const {buildSchema} = require('../parsers-commons');
34-
const {Visitor} = require('./Visitor');
34+
const {Visitor} = require('../parsers-primitives');
3535
const {buildComponentSchema} = require('./components');
3636
const {wrapComponentSchema} = require('../schema.js');
3737
const {buildModuleSchema} = require('../parsers-commons.js');

0 commit comments

Comments
 (0)