Skip to content

Commit

Permalink
feat: Expose FbtCommonConstants as a babel-plugin-fbt option
Browse files Browse the repository at this point in the history
Summary: Enable providing common strings in babel-plugin-fbt as an option

Reviewed By: fantazic

Differential Revision: D18672440

fbshipit-source-id: 160a489d5207ed8e907fb50c61dfe47499caaf23
  • Loading branch information
John Watson authored and facebook-github-bot committed Nov 25, 2019
1 parent 0f68f7f commit 26c5cd8
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 225 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -64,6 +64,9 @@ We haven't had the best track record of code/feature changes before this date, b

</details>

- 0.12.0:
- Support common strings as plugin option

- 0.11.1:
- Remove #!shebang from bin scripts and point to wrappers in `node_modules/.bin` shortcut paths

Expand Down
35 changes: 35 additions & 0 deletions packages/babel-plugin-fbt/FbtCommon.js
@@ -0,0 +1,35 @@
/**
* Copyright 2004-present Facebook. All Rights Reserved.
*
* @flow
*/

'use strict';

/*::
type FbtCommonMap = {
[text: string]: string
}
*/

const textToDesc /*: FbtCommonMap*/ = {};

const FbtCommon = {
init(
opts /*: {fbtCommon?: FbtCommonMap, fbtCommonPath?: string} */ = {},
) /*: void */ {
if (opts.fbtCommon) {
Object.assign(textToDesc, opts.fbtCommon);
}
if (opts.fbtCommonPath != null) {
// $FlowFixMe - dynamic require
Object.assign(textToDesc, require(opts.fbtCommonPath));
}
},

getDesc(text /*: string */) /*: ?string */ {
return textToDesc[text];
}
};

module.exports = FbtCommon;
215 changes: 0 additions & 215 deletions packages/babel-plugin-fbt/FbtCommonConstants.js

This file was deleted.

5 changes: 5 additions & 0 deletions packages/babel-plugin-fbt/__tests__/fbs-test.js
Expand Up @@ -46,6 +46,11 @@ describe('Test declarative (jsx) <fbs> syntax translation', () => {
withFbsRequireStatement(`
const fbsCommonElem = <fbs common={true}>Post</fbs>;
`),
{
fbtCommon: {
Post: 'Button to post a comment',
},
},
),
).toMatchSnapshot();
});
Expand Down
2 changes: 2 additions & 0 deletions packages/babel-plugin-fbt/bin/FbtCollector.js
Expand Up @@ -40,6 +40,7 @@ export type TransformOptions = {|
filename?: string,
extraOptions?: mixed,
fbtEnumManifest?: FbtEnumManifest,
fbtCommonPath?: string,
auxiliaryTexts?: boolean,
reactNativeMode?: boolean,
|}
Expand Down Expand Up @@ -85,6 +86,7 @@ class FbtCollector {
fbtEnumManifest,
auxiliaryTexts: this._config.auxiliaryTexts,
reactNativeMode: this._config.reactNativeMode,
fbtCommonPath: this._config.fbtCommonPath,
};
if (filename != null) {
options.filename = filename;
Expand Down
@@ -0,0 +1,3 @@
{
"Required": "Indicates an editor field is required."
}
14 changes: 11 additions & 3 deletions packages/babel-plugin-fbt/bin/__tests__/collectFBT-test.js
Expand Up @@ -14,14 +14,22 @@
*/
'use strict';

var childProcess = require('child_process');
const childProcess = require('child_process');
const path = require('path');

const commonPath = path.resolve(__dirname, 'FbtCommonForTests.json');

describe('collectFBT', () => {
function collect(source, options = {}) {
var collectOptions = [require.resolve('../collectFBT'), '--packager=none'];
var collectOptions = [
require.resolve('../collectFBT'),
'--packager=none',
'--fbt-common-path=' + commonPath,
];
if (options.react_native_mode || false) {
collectOptions = collectOptions.concat('--react-native-mode');
collectOptions.push('--react-native-mode');
}

var child = childProcess.spawnSync(
process.env.NODE_BINARY || 'node',
collectOptions,
Expand Down
9 changes: 9 additions & 0 deletions packages/babel-plugin-fbt/bin/collectFBT.js
Expand Up @@ -27,6 +27,7 @@ const yargs = require('yargs');

const args = {
AUXILIARY_TEXTS: 'auxiliary-texts',
COMMON_STRINGS: 'fbt-common-path',
HASH: 'hash-module',
HELP: 'h',
MANIFEST: 'manifest',
Expand Down Expand Up @@ -93,6 +94,13 @@ const argv = yargs
'Interpret stdin as JSON map of {<enum-manifest-file>: ' +
'[<source_file1>, ...]}. Otherwise stdin itself will be parsed',
)
.string(args.COMMON_STRINGS)
.default(args.COMMON_STRINGS, null)
.describe(
args.COMMON_STRINGS,
'Optional path to the common strings module. ' +
'This is a map from {[text]: [description]}.',
)
.boolean(args.PRETTY)
.default(args.PRETTY, false)
.describe(args.PRETTY, 'Pretty-print the JSON output')
Expand Down Expand Up @@ -140,6 +148,7 @@ const fbtCollector = new FbtCollector(
auxiliaryTexts: argv[args.AUXILIARY_TEXTS],
plugins: argv[args.PLUGINS],
reactNativeMode: argv[args.REACT_NATIVE_MODE],
fbtCommonPath: argv[args.COMMON_STRINGS],
},
extraOptions,
);
Expand Down
11 changes: 5 additions & 6 deletions packages/babel-plugin-fbt/index.js
Expand Up @@ -16,7 +16,7 @@
'use strict';

const FbtAutoWrap = require('./FbtAutoWrap');
const FbtCommonConstants = require('./FbtCommonConstants');
const FbtCommon = require('./FbtCommon');
const {
FbtBooleanOptions,
FbtRequiredAttributes,
Expand Down Expand Up @@ -80,6 +80,7 @@ function BabelPluginFbt(babel) {
this.opts.fbtSentinel = this.opts.fbtSentinel || '__FBT__';
this.opts.fbtBase64 = this.opts.fbtBase64;

FbtCommon.init(this.opts);
FbtMethodCallVisitors.setEnumManifest(getEnumManifest(this.opts));
initExtraOptions(this);
initDefaultOptions(this);
Expand Down Expand Up @@ -126,7 +127,7 @@ function BabelPluginFbt(babel) {
const textValue = normalizeSpaces(
expandStringConcat(moduleName, t, text).value.trim(),
);
const descValue = FbtCommonConstants[textValue];
const descValue = FbtCommon.getDesc(textValue);
if (!descValue) {
throwAt(
path.node,
Expand Down Expand Up @@ -537,17 +538,15 @@ function BabelPluginFbt(babel) {
if (path.node.arguments.length !== 1) {
throwAt(
path.node,
`Expected ${moduleName}.c to have exactly 1 argument. ${
path.node.arguments.length
} was given.`,
`Expected ${moduleName}.c to have exactly 1 argument. ${path.node.arguments.length} was given.`,
);
}

const text = normalizeSpaces(
expandStringConcat(moduleName, t, path.node.arguments[0]).value,
).trim();

const desc = FbtCommonConstants[text];
const desc = FbtCommon.getDesc(text);
if (!desc) {
throwAt(path.node, getUnknownCommonStringErrorMessage(moduleName, text));
}
Expand Down

0 comments on commit 26c5cd8

Please sign in to comment.