From ec2a5ee7c471dee9030e04897fd4c51a15b29c07 Mon Sep 17 00:00:00 2001 From: David Han-Sze-Chuen <182644+kayhadrin@users.noreply.github.com> Date: Tue, 7 Sep 2021 11:26:34 -0700 Subject: [PATCH] fix(babel-plugin-fbt): detect fbt JS callsites from FbtCollector (#263) Summary: Running string extraction from JS files containing only `` or `fbs()` callsites doesn't work. This PR should fix it. Pull Request resolved: https://github.com/facebook/fbt/pull/263 Test Plan: `yarn clean-test` Reviewed By: pkqinys Differential Revision: D30744411 Pulled By: kayhadrin fbshipit-source-id: cb563d7cbfad047176db127a7395e187e060ec09 --- .../babel-plugin-fbt/src/bin/FbtCollector.js | 3 +- .../__snapshots__/collectFBT-test.js.snap | 40 ++++++++++++++----- .../src/bin/__tests__/collectFBT-test.js | 7 +++- 3 files changed, 39 insertions(+), 11 deletions(-) diff --git a/packages/babel-plugin-fbt/src/bin/FbtCollector.js b/packages/babel-plugin-fbt/src/bin/FbtCollector.js index 490cc0e9..410c4734 100644 --- a/packages/babel-plugin-fbt/src/bin/FbtCollector.js +++ b/packages/babel-plugin-fbt/src/bin/FbtCollector.js @@ -13,6 +13,7 @@ const {extractEnumsAndFlattenPhrases} = require('../FbtShiftEnums'); const fbt = require('../index'); +const FbtUtil = require('../FbtUtil'); const fs = require('graceful-fs'); export type ExternalTransform = ( @@ -107,7 +108,7 @@ class FbtCollector implements IFbtCollector { reactNativeMode: this._config.reactNativeMode, }; - if (!/<[Ff]bt|fbt(\.c)?\s*\(/.test(source)) { + if (!FbtUtil.textContainsFbtLikeModule(source)) { return; } diff --git a/packages/babel-plugin-fbt/src/bin/__tests__/__snapshots__/collectFBT-test.js.snap b/packages/babel-plugin-fbt/src/bin/__tests__/__snapshots__/collectFBT-test.js.snap index 2dd796e4..7e7a3067 100644 --- a/packages/babel-plugin-fbt/src/bin/__tests__/__snapshots__/collectFBT-test.js.snap +++ b/packages/babel-plugin-fbt/src/bin/__tests__/__snapshots__/collectFBT-test.js.snap @@ -1288,30 +1288,29 @@ Object { } `; -exports[`collectFbt should extract fbt.c strings 1`] = ` +exports[`collectFbt should extract fbs strings 1`] = ` Object { "childParentMappings": Object {}, "phrases": Array [ Object { - "col_beg": 6, - "col_end": 23, - "common": true, + "col_beg": 27, + "col_end": 52, "jsfbt": Object { "m": Array [], "t": Object { - "desc": "Indicates an editor field is required.", - "text": "Required", + "desc": "foo", + "text": "bar", }, }, - "line_beg": 3, - "line_end": 3, + "line_beg": 1, + "line_end": 1, "project": "", }, ], } `; -exports[`collectFbt should extract strings 1`] = ` +exports[`collectFbt should extract fbt strings 1`] = ` Object { "childParentMappings": Object {}, "phrases": Array [ @@ -1333,6 +1332,29 @@ Object { } `; +exports[`collectFbt should extract fbt.c strings 1`] = ` +Object { + "childParentMappings": Object {}, + "phrases": Array [ + Object { + "col_beg": 6, + "col_end": 23, + "common": true, + "jsfbt": Object { + "m": Array [], + "t": Object { + "desc": "Indicates an editor field is required.", + "text": "Required", + }, + }, + "line_beg": 3, + "line_end": 3, + "project": "", + }, + ], +} +`; + exports[`collectFbt should extract strings from a custom collector 1`] = ` Object { "childParentMappings": Object { diff --git a/packages/babel-plugin-fbt/src/bin/__tests__/collectFBT-test.js b/packages/babel-plugin-fbt/src/bin/__tests__/collectFBT-test.js index fe9334a1..0b9361c5 100644 --- a/packages/babel-plugin-fbt/src/bin/__tests__/collectFBT-test.js +++ b/packages/babel-plugin-fbt/src/bin/__tests__/collectFBT-test.js @@ -41,11 +41,16 @@ describe('collectFbt', () => { return JSON.parse(JSON.stringify(output)); } - it('should extract strings', () => { + it('should extract fbt strings', () => { var res = collect('const fbt = require(\'fbt\');bar'); expect(res).toMatchSnapshot(); }); + it('should extract fbs strings', () => { + var res = collect('const fbs = require(\'fbs\');bar'); + expect(res).toMatchSnapshot(); + }); + it('should extract the author from Docblock', () => { var res = collect( [