Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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