Skip to content
This repository was archived by the owner on May 4, 2020. It is now read-only.

Commit 7df5060

Browse files
author
Long Ho
committed
fix(babel-plugin-react-intl): fix pragma parsing so it can search for non-import nodes
1 parent ad4b19d commit 7df5060

File tree

3 files changed

+38
-28
lines changed

3 files changed

+38
-28
lines changed

packages/babel-plugin-react-intl/src/index.ts

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ interface MessageDescriptor {
4545
export type ExtractedMessageDescriptor = MessageDescriptor &
4646
Partial<SourceLocation> & {file?: string};
4747

48-
export type ExtractionResult = {
48+
export type ExtractionResult<M = Record<string, string>> = {
4949
messages: ExtractedMessageDescriptor[];
50-
meta: Map<string, string>;
50+
meta: M;
5151
};
5252

5353
type MessageDescriptorPath = Record<
@@ -89,7 +89,7 @@ interface BabelTransformationFile {
8989

9090
interface State {
9191
ReactIntlMessages: Map<string, ExtractedMessageDescriptor>;
92-
ReactIntlMeta: Map<string, string>;
92+
ReactIntlMeta: Record<string, string>;
9393
}
9494

9595
function getICUMessageValue(
@@ -361,7 +361,7 @@ export default declare((api: any, options: OptionsSchema) => {
361361
pre() {
362362
if (!this.ReactIntlMessages) {
363363
this.ReactIntlMessages = new Map();
364-
this.ReactIntlMeta = new Map();
364+
this.ReactIntlMeta = {};
365365
}
366366
},
367367

@@ -411,26 +411,32 @@ export default declare((api: any, options: OptionsSchema) => {
411411
},
412412

413413
visitor: {
414-
ImportDeclaration(path) {
415-
const comments = path.node.leadingComments;
414+
Program(path) {
415+
const {body} = path.node;
416416
const {ReactIntlMeta} = this;
417-
if (!pragma || !comments) {
417+
if (!pragma) {
418418
return;
419419
}
420-
const pragmaLine = comments
421-
.map(c => c.value)
422-
.find(c => c.includes(pragma));
423-
if (!pragmaLine) {
424-
return;
420+
for (const {leadingComments} of body) {
421+
if (!leadingComments) {
422+
continue;
423+
}
424+
const pragmaLineNode = leadingComments.find(c =>
425+
c.value.includes(pragma)
426+
);
427+
if (!pragmaLineNode) {
428+
continue;
429+
}
430+
431+
pragmaLineNode.value
432+
.split(pragma)[1]
433+
.trim()
434+
.split(/\s+/g)
435+
.forEach(kv => {
436+
const [k, v] = kv.split(':');
437+
ReactIntlMeta[k] = v;
438+
});
425439
}
426-
pragmaLine
427-
.split(pragma)[1]
428-
.trim()
429-
.split(/\s+/g)
430-
.forEach(kv => {
431-
const [k, v] = kv.split(':');
432-
ReactIntlMeta.set(k, v);
433-
});
434440
},
435441
JSXOpeningElement(
436442
path,

packages/babel-plugin-react-intl/test/__snapshots__/index.test.ts.snap

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Object {
99
"id": "foo.bar.baz",
1010
},
1111
],
12-
"meta": Map {},
12+
"meta": Object {},
1313
}
1414
`;
1515

@@ -84,8 +84,8 @@ Object {
8484
"id": "escaped.apostrophe",
8585
},
8686
],
87-
"meta": Map {
88-
"project" => "amazing",
87+
"meta": Object {
88+
"project": "amazing",
8989
},
9090
}
9191
`;
@@ -197,7 +197,9 @@ Object {
197197
"id": "foo.bar.baz",
198198
},
199199
],
200-
"meta": Map {},
200+
"meta": Object {
201+
"project": "amazing2",
202+
},
201203
}
202204
`;
203205

@@ -217,6 +219,7 @@ function _getRequireWildcardCache() { if (typeof WeakMap !== \\"function\\") ret
217219
218220
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== \\"object\\" && typeof obj !== \\"function\\") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
219221
222+
// @react-intl project:amazing2
220223
class Foo extends _react.Component {
221224
render() {
222225
return (
@@ -260,7 +263,7 @@ Object {
260263
"id": "foo.bar.baz",
261264
},
262265
],
263-
"meta": Map {},
266+
"meta": Object {},
264267
}
265268
`;
266269

@@ -328,7 +331,7 @@ Object {
328331
"id": "foo.bar.baz",
329332
},
330333
],
331-
"meta": Map {},
334+
"meta": Object {},
332335
}
333336
`;
334337

@@ -382,7 +385,7 @@ Object {
382385
"id": "foo",
383386
},
384387
],
385-
"meta": Map {},
388+
"meta": Object {},
386389
}
387390
`;
388391

@@ -453,7 +456,7 @@ Object {
453456
"id": "foo",
454457
},
455458
],
456-
"meta": Map {},
459+
"meta": Object {},
457460
}
458461
`;
459462

packages/babel-plugin-react-intl/test/fixtures/descriptionsAsObjects/actual.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React, {Component} from 'react';
22
import {FormattedMessage} from 'react-intl';
33

4+
// @react-intl project:amazing2
45
export default class Foo extends Component {
56
render() {
67
return (

0 commit comments

Comments
 (0)