From 761a1d5d96780485fdef3b6b09346f6c039ae689 Mon Sep 17 00:00:00 2001 From: David Han-Sze-Chuen <182644+kayhadrin@users.noreply.github.com> Date: Thu, 2 Sep 2021 16:37:04 -0700 Subject: [PATCH] fix(babel-plugin-fbt): detect fbt JS callsites from FbtCollector --- .../babel-plugin-fbt/src/bin/FbtCollector.js | 3 ++- .../src/bin/__tests__/collectFBT-test.js | 17 ++++++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/packages/babel-plugin-fbt/src/bin/FbtCollector.js b/packages/babel-plugin-fbt/src/bin/FbtCollector.js index 1a294f11..08d9341c 100644 --- a/packages/babel-plugin-fbt/src/bin/FbtCollector.js +++ b/packages/babel-plugin-fbt/src/bin/FbtCollector.js @@ -14,6 +14,7 @@ const {extractEnumsAndFlattenPhrases} = require('../FbtShiftEnums'); // eslint-disable-next-line fb-www/no-module-aliasing const fbt = require('../index'); +const FbtUtil = require('../FbtUtil'); const fs = require('graceful-fs'); const path = require('path'); @@ -101,7 +102,7 @@ class FbtCollector implements IFbtCollector { options.filename = filename; } - if (!/<[Ff]bt|fbt(\.c)?\s*\(/.test(source)) { + if (!FbtUtil.textContainsFbtLikeModule(source)) { return; } 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 61f4a6ea..2e6031d8 100644 --- a/packages/babel-plugin-fbt/src/bin/__tests__/collectFBT-test.js +++ b/packages/babel-plugin-fbt/src/bin/__tests__/collectFBT-test.js @@ -60,7 +60,7 @@ describe('collectFbt', () => { } } - it('should extract strings', () => { + it('should extract fbt strings', () => { var res = collect('const fbt = require(\'fbt\');bar'); var expected = { @@ -75,6 +75,21 @@ describe('collectFbt', () => { expect(actual).toEqual(expected); }); + it('should extract fbs strings', () => { + var res = collect('const fbs = require(\'fbs\');bar'); + + var expected = { + type: 'text', + desc: 'foo', + jsfbt: 'bar', + }; + + var actual = {}; + Object.keys(expected).map(key => (actual[key] = res.phrases[0][key])); + + expect(actual).toEqual(expected); + }); + it('should still extract strings if file-level doNotExtract is set to false', () => { var res = collect( [