Skip to content

Commit

Permalink
GenerateModuleJavaSpec: Replace string replace with string templates
Browse files Browse the repository at this point in the history
Summary:
## Benefits:
- Improved Readability
- Improved type-safety with templates

Changelog: [Internal]

Differential Revision: D24386277

fbshipit-source-id: 28fbc0eab2c4455d391a1644b5c42c0cb39f69ad
  • Loading branch information
RSNara authored and facebook-github-bot committed Oct 20, 2020
1 parent 5a73af6 commit ac46837
Showing 1 changed file with 27 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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,
Expand Down Expand Up @@ -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'),
}),
);
});

Expand Down

0 comments on commit ac46837

Please sign in to comment.