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 @@ -554,73 +554,71 @@ describe('throwIfUntypedModule', () => {
describe('throwIfModuleTypeIsUnsupported', () => {
const hasteModuleName = 'moduleName';
const property = {value: 'value', key: {name: 'name'}};
const flowParser = new FlowParser();
const typescriptParser = new TypeScriptParser();

it("don't throw error if module type is FunctionTypeAnnotation in Flow", () => {
const value = {type: 'FunctionTypeAnnotation'};
const language = 'Flow';

expect(() => {
throwIfModuleTypeIsUnsupported(
hasteModuleName,
property.value,
property.key.name,
value.type,
language,
flowParser,
);
}).not.toThrow(UnsupportedModulePropertyParserError);
});
it('throw error if module type is unsupported in Flow', () => {
const value = {type: ''};
const language = 'Flow';

expect(() => {
throwIfModuleTypeIsUnsupported(
hasteModuleName,
property.value,
property.key.name,
value.type,
language,
flowParser,
);
}).toThrow(UnsupportedModulePropertyParserError);
});
it("don't throw error if module type is TSFunctionType in TypeScript", () => {
const value = {type: 'TSFunctionType'};
const language = 'TypeScript';

expect(() => {
throwIfModuleTypeIsUnsupported(
hasteModuleName,
property.value,
property.key.name,
value.type,
language,
typescriptParser,
);
}).not.toThrow(UnsupportedModulePropertyParserError);
});
it("don't throw error if module type is TSMethodSignature in TypeScript", () => {
const value = {type: 'TSMethodSignature'};
const language = 'TypeScript';

expect(() => {
throwIfModuleTypeIsUnsupported(
hasteModuleName,
property.value,
property.key.name,
value.type,
language,
typescriptParser,
);
}).not.toThrow(UnsupportedModulePropertyParserError);
});
it('throw error if module type is unsupported in TypeScript', () => {
const value = {type: ''};
const language = 'TypeScript';

expect(() => {
throwIfModuleTypeIsUnsupported(
hasteModuleName,
property.value,
property.key.name,
value.type,
language,
typescriptParser,
);
}).toThrow(UnsupportedModulePropertyParserError);
});
Expand Down
18 changes: 3 additions & 15 deletions packages/react-native-codegen/src/parsers/error-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,27 +160,15 @@ function throwIfModuleTypeIsUnsupported(
propertyValue: $FlowFixMe,
propertyName: string,
propertyValueType: string,
language: ParserType,
parser: Parser,
) {
if (language === 'Flow' && propertyValueType !== 'FunctionTypeAnnotation') {
throw new UnsupportedModulePropertyParserError(
nativeModuleName,
propertyValue,
propertyName,
propertyValueType,
language,
);
} else if (
language === 'TypeScript' &&
propertyValueType !== 'TSFunctionType' &&
propertyValueType !== 'TSMethodSignature'
) {
if (!parser.functionTypeAnnotation(propertyValueType)) {
throw new UnsupportedModulePropertyParserError(
nativeModuleName,
propertyValue,
propertyName,
propertyValueType,
language,
parser.language(),
);
}
}
Expand Down
4 changes: 4 additions & 0 deletions packages/react-native-codegen/src/parsers/flow/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,10 @@ class FlowParser implements Parser {
};
});
}

functionTypeAnnotation(propertyValueType: string): boolean {
return propertyValueType === 'FunctionTypeAnnotation';
}
}

module.exports = {
Expand Down
7 changes: 7 additions & 0 deletions packages/react-native-codegen/src/parsers/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,11 @@ export interface Parser {
tryParse: ParserErrorCapturer,
cxxOnly: boolean,
): Array<$FlowFixMe>;

/**
* Given a propertyValueType, it returns a boolean specifying if the property is a function type annotation.
* @parameter propertyValueType: the propertyValueType.
* @returns: a boolean specifying if the property is a function type annotation.
*/
functionTypeAnnotation(propertyValueType: string): boolean;
}
4 changes: 4 additions & 0 deletions packages/react-native-codegen/src/parsers/parserMock.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,4 +209,8 @@ export class MockedParser implements Parser {
},
];
}

functionTypeAnnotation(propertyValueType: string): boolean {
return propertyValueType === 'FunctionTypeAnnotation';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ function buildPropertySchema(
property.value,
key.name,
value.type,
parser.language(),
parser,
);

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,13 @@ class TypeScriptParser implements Parser {
};
});
}

functionTypeAnnotation(propertyValueType: string): boolean {
return (
propertyValueType === 'TSFunctionType' ||
propertyValueType === 'TSMethodSignature'
);
}
}

module.exports = {
Expand Down