Skip to content

Commit

Permalink
Move isModuleInterface function (Flow, TypeScript) to the Flow and Ty…
Browse files Browse the repository at this point in the history
…peScript parsers. (#36268)

Summary:
Task from #34872

> [Codegen 82] Move isModuleInterface function (Flow, TypeScript) to the Flow and TypeScript parsers.

## Changelog

[INTERNAL] [CHANGED] - Moved isModuleInterface function to to the Flow and TypeScript parsers.

Pull Request resolved: #36268

Test Plan: ` yarn test react-native-codegen`

Reviewed By: christophpurrer

Differential Revision: D43535948

Pulled By: rshest

fbshipit-source-id: 7a2db05008783499168b0ce3fa58fedbac2b4e79
  • Loading branch information
kyawthura-gg authored and facebook-github-bot committed Feb 23, 2023
1 parent d528fe2 commit 85245af
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,28 @@ describe('FlowParser', () => {
]);
});
});

describe('isModuleInterface', () => {
it('returns true if it is a valid node', () => {
const node = {
type: 'InterfaceDeclaration',
extends: [
{
type: 'InterfaceExtends',
id: {
name: 'TurboModule',
},
},
],
};
expect(parser.isModuleInterface(node)).toBe(true);
});

it('returns false if it is a invalid node', () => {
const node = {};
expect(parser.isModuleInterface(node)).toBe(false);
});
});
});

describe('TypeScriptParser', () => {
Expand Down Expand Up @@ -116,4 +138,26 @@ describe('TypeScriptParser', () => {
]);
});
});

describe('isModuleInterface', () => {
it('returns true if it is a valid node', () => {
const node = {
type: 'TSInterfaceDeclaration',
extends: [
{
type: 'TSExpressionWithTypeArguments',
expression: {
name: 'TurboModule',
},
},
],
};
expect(parser.isModuleInterface(node)).toBe(true);
});

it('returns false if it is a invalid node', () => {
const node = {};
expect(parser.isModuleInterface(node)).toBe(false);
});
});
});
11 changes: 1 addition & 10 deletions packages/react-native-codegen/src/parsers/flow/modules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,15 +330,6 @@ function translateTypeAnnotation(
}
}

function isModuleInterface(node: $FlowFixMe) {
return (
node.type === 'InterfaceDeclaration' &&
node.extends.length === 1 &&
node.extends[0].type === 'InterfaceExtends' &&
node.extends[0].id.name === 'TurboModule'
);
}

function buildModuleSchema(
hasteModuleName: string,
/**
Expand All @@ -350,7 +341,7 @@ function buildModuleSchema(
): NativeModuleSchema {
const types = getTypes(ast);
const moduleSpecs = (Object.values(types): $ReadOnlyArray<$FlowFixMe>).filter(
isModuleInterface,
t => parser.isModuleInterface(t),
);

throwIfModuleInterfaceNotFound(
Expand Down
9 changes: 9 additions & 0 deletions packages/react-native-codegen/src/parsers/flow/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,15 @@ class FlowParser implements Parser {
value: member.init?.value ?? member.id.name,
}));
}

isModuleInterface(node: $FlowFixMe): boolean {
return (
node.type === 'InterfaceDeclaration' &&
node.extends.length === 1 &&
node.extends[0].type === 'InterfaceExtends' &&
node.extends[0].id.name === 'TurboModule'
);
}
}

module.exports = {
Expand Down
5 changes: 5 additions & 0 deletions packages/react-native-codegen/src/parsers/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,9 @@ export interface Parser {
* Calculates enum's members
*/
parseEnumMembers(typeAnnotation: $FlowFixMe): NativeModuleEnumMembers;

/**
* Given a node, it returns true if it is a module interface
*/
isModuleInterface(node: $FlowFixMe): boolean;
}
9 changes: 9 additions & 0 deletions packages/react-native-codegen/src/parsers/parserMock.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,13 @@ export class MockedParser implements Parser {
},
];
}

isModuleInterface(node: $FlowFixMe): boolean {
return (
node.type === 'InterfaceDeclaration' &&
node.extends.length === 1 &&
node.extends[0].type === 'InterfaceExtends' &&
node.extends[0].id.name === 'TurboModule'
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -436,15 +436,6 @@ function translateTypeAnnotation(
}
}

function isModuleInterface(node: $FlowFixMe) {
return (
node.type === 'TSInterfaceDeclaration' &&
node.extends?.length === 1 &&
node.extends[0].type === 'TSExpressionWithTypeArguments' &&
node.extends[0].expression.name === 'TurboModule'
);
}

function buildModuleSchema(
hasteModuleName: string,
/**
Expand All @@ -456,7 +447,7 @@ function buildModuleSchema(
): NativeModuleSchema {
const types = getTypes(ast);
const moduleSpecs = (Object.values(types): $ReadOnlyArray<$FlowFixMe>).filter(
isModuleInterface,
t => parser.isModuleInterface(t),
);

throwIfModuleInterfaceNotFound(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,15 @@ class TypeScriptParser implements Parser {
value: member.initializer?.value ?? member.id.name,
}));
}

isModuleInterface(node: $FlowFixMe): boolean {
return (
node.type === 'TSInterfaceDeclaration' &&
node.extends?.length === 1 &&
node.extends[0].type === 'TSExpressionWithTypeArguments' &&
node.extends[0].expression.name === 'TurboModule'
);
}
}
module.exports = {
TypeScriptParser,
Expand Down

0 comments on commit 85245af

Please sign in to comment.