mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
GenerateModuleJavaSpec: Replace string replace with string templates
Summary: ## Benefits: - Improved Readability - Improved type-safety with templates Changelog: [Internal] Differential Revision: D24386277 fbshipit-source-id: 28fbc0eab2c4455d391a1644b5c42c0cb39f69ad
This commit is contained in:
committed by
Facebook GitHub Bot
parent
5a73af684d
commit
ac46837d35
+27
-17
@@ -26,7 +26,18 @@ const {unwrapNullable} = require('../../parsers/flow/modules/utils');
|
||||
|
||||
type FilesOutput = Map<string, string>;
|
||||
|
||||
const moduleTemplate = `
|
||||
const FileTemplate = ({
|
||||
packageName,
|
||||
className,
|
||||
methods,
|
||||
imports,
|
||||
}: $ReadOnly<{|
|
||||
packageName: string,
|
||||
className: string,
|
||||
methods: string,
|
||||
imports: string,
|
||||
|}>) => {
|
||||
return `
|
||||
/**
|
||||
* ${'C'}opyright (c) Facebook, Inc. and its affiliates.
|
||||
*
|
||||
@@ -38,18 +49,19 @@ const moduleTemplate = `
|
||||
* @nolint
|
||||
*/
|
||||
|
||||
package ::_PACKAGENAME_::;
|
||||
package ${packageName};
|
||||
|
||||
::_IMPORTS_::
|
||||
${imports}
|
||||
|
||||
public abstract class ::_CLASSNAME_:: extends ReactContextBaseJavaModule implements ReactModuleWithSpec, TurboModule {
|
||||
public ::_CLASSNAME_::(ReactApplicationContext reactContext) {
|
||||
public abstract class ${className} extends ReactContextBaseJavaModule implements ReactModuleWithSpec, TurboModule {
|
||||
public ${className}(ReactApplicationContext reactContext) {
|
||||
super(reactContext);
|
||||
}
|
||||
|
||||
::_METHODS_::
|
||||
${methods}
|
||||
}
|
||||
`;
|
||||
};
|
||||
|
||||
function translateFunctionParamToJavaType(
|
||||
param: NativeModuleMethodParamSchema,
|
||||
@@ -354,17 +366,15 @@ module.exports = {
|
||||
|
||||
files.set(
|
||||
`${className}.java`,
|
||||
moduleTemplate
|
||||
.replace(
|
||||
/::_IMPORTS_::/g,
|
||||
Array.from(imports)
|
||||
.sort()
|
||||
.map(p => `import ${p};`)
|
||||
.join('\n'),
|
||||
)
|
||||
.replace(/::_PACKAGENAME_::/g, packageName)
|
||||
.replace(/::_CLASSNAME_::/g, className)
|
||||
.replace(/::_METHODS_::/g, methods.filter(m => !!m).join('\n\n')),
|
||||
FileTemplate({
|
||||
packageName,
|
||||
className,
|
||||
methods: methods.filter(Boolean).join('\n\n'),
|
||||
imports: Array.from(imports)
|
||||
.sort()
|
||||
.map(p => `import ${p};`)
|
||||
.join('\n'),
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user