Skip to content

Commit

Permalink
fix(babel-plugin-fbt): detect fbt JS callsites from FbtCollector (#263)
Browse files Browse the repository at this point in the history
Summary:
Running string extraction from JS files containing only `<fbs>` or `fbs()` callsites doesn't work.
This PR should fix it.

Pull Request resolved: #263

Test Plan: `yarn clean-test`

Differential Revision: D30744411

Pulled By: kayhadrin

fbshipit-source-id: cc6a1b948184f08f3ae702e1c1b6f53f286f2578
  • Loading branch information
kayhadrin authored and facebook-github-bot committed Sep 3, 2021
1 parent a1df470 commit 8b24d3f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 11 deletions.
3 changes: 2 additions & 1 deletion packages/babel-plugin-fbt/src/bin/FbtCollector.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

const {extractEnumsAndFlattenPhrases} = require('../FbtShiftEnums');
const fbt = require('../index');
const FbtUtil = require('../FbtUtil');
const fs = require('graceful-fs');

export type ExternalTransform = (
Expand Down Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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\');<fbt desc="foo">bar</fbt>');
expect(res).toMatchSnapshot();
});

it('should extract fbs strings', () => {
var res = collect('const fbs = require(\'fbs\');<fbs desc="foo">bar</fbs>');
expect(res).toMatchSnapshot();
});

it('should extract the author from Docblock', () => {
var res = collect(
[
Expand Down

0 comments on commit 8b24d3f

Please sign in to comment.