Skip to content

Commit 1dbe78c

Browse files
committed
Cleanup.
1 parent f2f245c commit 1dbe78c

File tree

16 files changed

+49
-105
lines changed

16 files changed

+49
-105
lines changed

.eslintrc.cjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module.exports = {
66
ignorePatterns: ['packages/*/lib'],
77
overrides: [
88
{
9-
files: ['./test/TestUtil.tsx'],
9+
files: ['./test/TestUtil.tsx', '**/__tests__/**/*.tsx'],
1010
rules: {
1111
'no-console': 0,
1212
},

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"build": "pnpm -r build",
99
"build:all": "pnpm -r build && pnpm -r build:types && pnpm install && cd example && pnpm build:fbt",
1010
"clean": "rm -rf packages/*/lib",
11-
"dev": "cd example && pnpm dev",
11+
"dev": "cd example && pnpm build:fbt && pnpm dev",
1212
"lint:format": "prettier --cache --check .",
1313
"format": "prettier --write .",
1414
"jest": "NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules --no-warnings\" node_modules/.bin/jest",

packages/babel-plugin-fbt-runtime/src/index.tsx

+11-24
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {
44
isCallExpression,
55
isObjectExpression,
66
nullLiteral,
7-
ObjectExpression,
87
objectExpression,
98
objectProperty,
109
stringLiteral,
@@ -16,11 +15,7 @@ import {
1615
replaceClearTokensWithTokenAliases,
1716
SENTINEL,
1817
} from 'babel-plugin-fbt';
19-
import type {
20-
ObjectWithJSFBT,
21-
TableJSFBT,
22-
TableJSFBTTreeLeaf,
23-
} from 'babel-plugin-fbt/src/index.tsx';
18+
import type { ObjectWithJSFBT } from 'babel-plugin-fbt/src/index.tsx';
2419
import invariant from 'invariant';
2520

2621
type Plugin = { opts: Partial<PluginOptions> & { fbtSentinel?: string } };
@@ -40,17 +35,6 @@ function getPluginOptions(plugin: Plugin): {
4035
};
4136
}
4237

43-
function convertJSFBTLeafToRuntimeInputText(leaf: TableJSFBTTreeLeaf) {
44-
return replaceClearTokensWithTokenAliases(leaf.text, leaf.tokenAliases);
45-
}
46-
47-
function appendHashKeyOption(optionsNode: ObjectExpression, jsfbt: TableJSFBT) {
48-
return objectExpression([
49-
...(optionsNode == null ? [] : [...optionsNode.properties]),
50-
objectProperty(identifier('hk'), stringLiteral(fbtHashKey(jsfbt.t))),
51-
]);
52-
}
53-
5438
export default function BabelPluginFbtRuntime() {
5539
return {
5640
name: 'fbt-runtime',
@@ -102,9 +86,8 @@ export default function BabelPluginFbtRuntime() {
10286
phrase.slice(sentinelLength, phrase.length - sentinelLength)
10387
) as ObjectWithJSFBT;
10488

105-
const runtimeInput = mapLeaves(
106-
parsedPhrase.jsfbt.t,
107-
convertJSFBTLeafToRuntimeInputText
89+
const runtimeInput = mapLeaves(parsedPhrase.jsfbt.t, (leaf) =>
90+
replaceClearTokensWithTokenAliases(leaf.text, leaf.tokenAliases)
10891
);
10992
path.replaceWithSourceString(JSON.stringify(runtimeInput));
11093

@@ -128,10 +111,14 @@ export default function BabelPluginFbtRuntime() {
128111
optionsNode,
129112
typeof optionsNode
130113
);
131-
parentNode.arguments[2] = appendHashKeyOption(
132-
optionsNode,
133-
parsedPhrase.jsfbt
134-
);
114+
115+
parentNode.arguments[2] = objectExpression([
116+
...(optionsNode == null ? [] : [...optionsNode.properties]),
117+
objectProperty(
118+
identifier('hk'),
119+
stringLiteral(fbtHashKey(parsedPhrase.jsfbt.t))
120+
),
121+
]);
135122
},
136123
},
137124
};

packages/babel-plugin-fbt/src/fbtJenkinsHash.tsx

+1-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ export default function fbtJenkinsHash(
2727
);
2828
invariant(
2929
desc != null,
30-
'Expect `desc` to be nonnull as `TableJSFBTTree` should contain at least ' +
31-
'one leaf.'
30+
'Expect `desc` to be nonnull as `TableJSFBTTree` should contain at least one leaf.'
3231
);
3332
const key = JSON.stringify(hashInputTree) + '|' + desc;
3433
return jenkinsHash(key);

packages/fbt/src/FbtHooks.tsx

-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import {
1212
export type FbtResolvedPayload = {
1313
contents: NestedFbtContentItems;
1414
errorListener: IFbtErrorListener | null | undefined;
15-
extraOptions: ExtraOptionValues | null | undefined;
1615
patternHash: PatternHash | null | undefined;
1716
patternString: PatternString;
1817
};
@@ -55,9 +54,6 @@ export type FbtTranslatedInput = {
5554

5655
export type FbtEnumHashKeyTable = Partial<Record<FbtTableKey, PatternString>>;
5756
export type FbtInputOpts = {
58-
// enum hash key
59-
ehk?: FbtEnumHashKeyTable;
60-
eo?: ExtraOptionValues;
6157
// hash key
6258
hk?: string;
6359
};

packages/fbt/src/FbtResult.tsx

-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import type { FbtResolvedPayload } from './FbtHooks.tsx';
21
import FbtReactUtil from './FbtReactUtil.tsx';
32
import FbtResultBase from './FbtResultBase.tsx';
43
import {
@@ -34,8 +33,4 @@ export default class FbtResult extends FbtResultBase implements IFbtResultBase {
3433
FbtReactUtil.injectReactShim(this);
3534
}
3635
}
37-
38-
static get(input: FbtResolvedPayload): FbtResult {
39-
return new FbtResult(input.contents, input.errorListener);
40-
}
4136
}

packages/fbt/src/FbtTranslations.tsx

+10-11
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ export default {
1919
return currentTranslations;
2020
},
2121

22-
getTranslatedInput(
23-
input: FbtRuntimeCallInput
24-
): FbtTranslatedInput | null | undefined {
25-
const { args, options } = input;
22+
getTranslatedInput({
23+
args,
24+
options,
25+
}: FbtRuntimeCallInput): FbtTranslatedInput | null | undefined {
2626
const hashKey = options?.hk;
2727
const { locale } = FbtHooks.getViewerContext();
2828
const table = currentTranslations[locale];
@@ -33,13 +33,12 @@ export default {
3333
}
3434
}
3535

36-
if (hashKey == null || table?.[hashKey] == null) {
37-
return null;
38-
}
39-
return {
40-
args,
41-
table: table[hashKey],
42-
};
36+
return hashKey == null || table?.[hashKey] == null
37+
? null
38+
: {
39+
args,
40+
table: table[hashKey],
41+
};
4342
},
4443

4544
mergeTranslations(newTranslations: TranslationDict): undefined {

packages/fbt/src/__mocks__/getFbtResult.tsx

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import type { FbtResolvedPayload } from '../FbtHooks.tsx';
22
import FbtResult from '../FbtResult.tsx';
33

4-
export default function getFbtResult(input: FbtResolvedPayload): FbtResult {
5-
const contents = input.contents;
4+
export default function getFbtResult({
5+
contents,
6+
}: FbtResolvedPayload): FbtResult {
67
return (contents?.length === 1 && typeof contents[0] === 'string'
78
? contents[0]
89
: contents) as unknown as FbtResult;

packages/fbt/src/__tests__/fbt-runtime-test.tsx

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { describe, expect, it, jest } from '@jest/globals';
22
import fbtRuntime from '../fbt.tsx';
33
import FbtHooks, { FbtRuntimeInput } from '../FbtHooks.tsx';
4-
import FbtResult from '../FbtResult.tsx';
54
import { FbtTableArg } from '../FbtTableAccessor.tsx';
5+
import { init } from '../index.tsx';
66
import intlNumUtils from '../intlNumUtils.tsx';
77
// Warning: importing JS modules outside of beforeEach blocks is generally bad practice
88
// in jest tests. We might need to move these modules inside beforeEach().
@@ -15,10 +15,13 @@ const FEW = String(IntlVariations.NUMBER_FEW);
1515
const MALE = String(IntlVariations.GENDER_MALE);
1616
const FEMALE = String(IntlVariations.GENDER_FEMALE);
1717

18-
FbtHooks.register({
19-
getFbtResult: FbtResult.get,
18+
init({
19+
translations: {},
2020
});
2121

22+
// Ignore missing translations.
23+
console.warn = jest.fn();
24+
2225
describe('fbt', () => {
2326
it('should handle variated numbers', () => {
2427
FbtHooks.register({

packages/fbt/src/__tests__/fbt-test.tsx

-23
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@ import { Children, Component } from 'react';
66
import getFbtResult from '../__mocks__/getFbtResult.tsx';
77
import fbtInternal from '../fbt.tsx';
88
import FbtHooks, {
9-
FbtResolvedPayload,
109
FbtRuntimeCallInput,
1110
FbtTranslatedInput,
1211
} from '../FbtHooks.tsx';
1312
import init from '../fbtInit.tsx';
14-
import FbtResult from '../FbtResult.tsx';
1513
import FbtTranslations from '../FbtTranslations.tsx';
1614
import GenderConst from '../GenderConst.tsx';
1715
import { fbt } from '../index.tsx';
@@ -285,27 +283,6 @@ describe('fbt', () => {
285283
);
286284
});
287285

288-
it('should pass extra options to FbtHooks.getFbtResult', () => {
289-
FbtHooks.register({
290-
getFbtResult(input: FbtResolvedPayload) {
291-
const { extraOptions } = input;
292-
const string = FbtResult.get(input).toString();
293-
if (extraOptions?.renderStringInBracket === 'yes') {
294-
return new FbtResult([`[${string}]`], input.errorListener);
295-
}
296-
return new FbtResult([string], input.errorListener);
297-
},
298-
});
299-
expect(fbtInternal._('A simple string', null)._contents[0]).toEqual(
300-
'A simple string'
301-
);
302-
expect(
303-
fbtInternal._('Another simple string', null, {
304-
eo: { renderStringInBracket: 'yes' },
305-
})._contents[0]
306-
).toEqual('[Another simple string]');
307-
});
308-
309286
describe('given a string that is only made of contiguous tokens', () => {
310287
it('should return a list of token values without empty strings', () => {
311288
const fbtParams = ['hello', 'world'];

packages/fbt/src/fbs.tsx

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { PatternHash, PatternString } from 'babel-plugin-fbt';
22
import invariant from 'invariant';
33
import fbt from './fbt.tsx';
4-
import FbtHooks, { ExtraOptionValues } from './FbtHooks.tsx';
4+
import FbtHooks from './FbtHooks.tsx';
55
import FbtPureStringResult from './FbtPureStringResult.tsx';
66
import type { ParamVariationType } from './FbtRuntimeTypes.tsx';
77
import type { FbtTableArg } from './FbtTableAccessor.tsx';
@@ -46,15 +46,13 @@ const FbsImpl = {
4646
_wrapContent(
4747
fbtContent: NestedFbtContentItems | string,
4848
translation: PatternString,
49-
hash?: PatternHash | null,
50-
extraOptions?: ExtraOptionValues | null
49+
hash?: PatternHash | null
5150
): Fbs {
5251
const contents = typeof fbtContent === 'string' ? [fbtContent] : fbtContent;
5352
const errorListener = FbtHooks.getErrorListener({ hash, translation });
5453
return FbtHooks.getFbsResult({
5554
contents,
5655
errorListener,
57-
extraOptions,
5856
patternHash: hash,
5957
patternString: translation,
6058
});

packages/fbt/src/fbt.tsx

+3-19
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import type { FbtTableKey, PatternHash, PatternString } from 'babel-plugin-fbt';
44
import invariant from 'invariant';
55
import FbtHooks, {
6-
ExtraOptionValues,
76
FbtInputOpts,
87
FbtRuntimeInput,
98
FbtTableArgs,
@@ -230,8 +229,7 @@ function fbtName(
230229
function wrapContent(
231230
fbtContent: NestedFbtContentItems | string,
232231
translation: PatternString,
233-
hash?: PatternHash | null,
234-
extraOptions?: ExtraOptionValues | null
232+
hash?: PatternHash | null
235233
): FbtResult {
236234
const contents = typeof fbtContent === 'string' ? [fbtContent] : fbtContent;
237235
const errorListener = FbtHooks.getErrorListener({
@@ -241,7 +239,6 @@ function wrapContent(
241239
const result = FbtHooks.getFbtResult({
242240
contents,
243241
errorListener,
244-
extraOptions,
245242
patternHash: hash,
246243
patternString: translation,
247244
});
@@ -279,25 +276,13 @@ const fbt = function () {};
279276
*
280277
* @param options - options for runtime
281278
* translation dictionary access. hk stands for hash key which is used to look
282-
* up translated payload in React Native. ehk stands for enum hash key which
283-
* contains a structured enums to hash keys map which will later be traversed
284-
* to look up enum-less translated payload.
279+
* up translated payload in React Native.
285280
*/
286281
fbt._ = function fbtCallsite(
287282
inputTable: FbtRuntimeInput,
288283
inputArgs?: FbtTableArgs | null,
289284
options?: FbtInputOpts | null
290285
): FbtResult {
291-
/*
292-
if (options?.hk || options?.ehk) {
293-
return {
294-
text: inputTable,
295-
fbt: true,
296-
hashKey: options.hk,
297-
};
298-
}
299-
*/
300-
301286
let { args, table: pattern } = FbtHooks.getTranslatedInput({
302287
args: inputArgs,
303288
options,
@@ -369,8 +354,7 @@ fbt._ = function fbtCallsite(
369354
const result = this._wrapContent(
370355
fbtContent as NestedFbtContentItems,
371356
patternString,
372-
patternHash,
373-
options?.eo
357+
patternHash
374358
);
375359
if (!hasSubstitutions) {
376360
this.cachedResults[patternString] = result;

packages/fbt/src/fbtInit.tsx

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import FbtHooks, { FbtHookRegistrations } from './FbtHooks.tsx';
1+
import FbtHooks, {
2+
FbtHookRegistrations,
3+
FbtResolvedPayload,
4+
} from './FbtHooks.tsx';
25
import FbtResult from './FbtResult.tsx';
36
import FbtTranslations, { TranslationDict } from './FbtTranslations.tsx';
47
import getFbsResult from './getFbsResult.tsx';
@@ -9,7 +12,8 @@ export type FbtInitInput = {
912
translations: TranslationDict;
1013
};
1114

12-
const getFbtResult = FbtResult.get.bind(FbtResult);
15+
const getFbtResult = ({ contents, errorListener }: FbtResolvedPayload) =>
16+
new FbtResult(contents, errorListener);
1317

1418
export default function fbtInit(input: FbtInitInput): void {
1519
FbtTranslations.registerTranslations(input.translations);

packages/fbt/src/getFbsResult.tsx

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import type { FbtResolvedPayload } from './FbtHooks.tsx';
22
import FbtPureStringResult from './FbtPureStringResult.tsx';
33

4-
export default function getFbsResult(
5-
input: FbtResolvedPayload
6-
): FbtPureStringResult {
7-
return new FbtPureStringResult(input.contents, input.errorListener);
4+
export default function getFbsResult({
5+
contents,
6+
errorListener,
7+
}: FbtResolvedPayload): FbtPureStringResult {
8+
return new FbtPureStringResult(contents, errorListener);
89
}

0 commit comments

Comments
 (0)