Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 36 additions & 17 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,46 @@ interface LooseObject {
[key: string]: any
}

function findProps(node: ts.Node): LooseObject[] {
function findProps(node: ts.Node, tagName: string): LooseObject[] {
var res: LooseObject[] = [];
find(node);
function find(node: ts.Node): LooseObject[] {
if (!node) {
return undefined;
}
if (ts.isObjectLiteralExpression(node)) {
node.properties.forEach(p => {
var prop: LooseObject = {};
if (
ts.isPropertyAssignment(p) &&
ts.isObjectLiteralExpression(p.initializer) &&
p.initializer.properties
) {
p.initializer.properties.forEach(ip => {
if (ts.isIdentifier(ip.name)) {
let name = ip.name.text
if (ts.isPropertyAssignment(ip) && ts.isStringLiteral(ip.initializer)) {
prop[name] = ip.initializer.text;
node.properties.forEach(p => {
var prop: LooseObject = {};
if (
ts.isPropertyAssignment(p) &&
ts.isObjectLiteralExpression(p.initializer) &&
p.initializer.properties
) {
p.initializer.properties.forEach(ip => {
if (ts.isIdentifier(ip.name)) {
let name = ip.name.text
if (ts.isPropertyAssignment(ip) && ts.isStringLiteral(ip.initializer)) {
prop[name] = ip.initializer.text;
}
}
});
res.push(prop);
}
});

if (tagName === "formatMessage") {
var prop: LooseObject = {};
let name;

node.properties.forEach(p => {
if (ts.isPropertyAssignment(p) && ts.isStringLiteral(p.initializer)) {
name = (p.name as any).escapedText;
prop[name] = p.initializer.text;
}
});

res.push(prop);
}
});
}
}
return ts.forEachChild(node, find);
}
Expand Down Expand Up @@ -85,7 +99,7 @@ function findFirstJsxOpeningLikeElementWithName(
el.initializer.arguments.length
) {
var nodeProps = el.initializer.arguments[0];
var props = findProps(nodeProps);
var props = findProps(nodeProps, tagName);
// props is an array of LooseObject
res = res.concat(props);
}
Expand Down Expand Up @@ -136,6 +150,11 @@ function main(contents: string): {}[] {
"defineMessages",
true
);
var fm = findFirstJsxOpeningLikeElementWithName(
sourceFile,
"formatMessage",
true
);

var res = elements
.map(element => {
Expand All @@ -152,7 +171,7 @@ function main(contents: string): {}[] {
})
.filter(r => !emptyObject(r));

return res.concat(dm);
return res.concat(dm).concat(fm);
}

export default main;
18 changes: 15 additions & 3 deletions lib/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions test/app/wrappedComponent.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import React from 'react'
import { FormattedMessage, injectIntl } from 'react-intl'
import messages from './defineMessages'

class WrappedComponent extends React.Component<any, any>{
render() {
const { formatMessage } = this.props.intl;

const emailPlaceholder = formatMessage({
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How to ensure formatMessage is from 'react-intl' but not from user defined. does this check is safe enough. at least don't break existing code.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Never mind.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is injected from the hoc from injectIntl from react-intl. I did not mean to break existing code but I wasn't sure about the way I implemented this...

id: "emailPlaceholder",
defaultMessage: "Email"
});

return <div>
<input type="text" placeholder={emailPlaceholder} />
</div>
}
}

export default injectIntl(WrappedComponent);
12 changes: 12 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,15 @@ test("<FormattedMessage/> should work with StatelessComponent", t => {

t.deepEqual(res, expected);
});

test("formatMessage() API should work with a wrapped by injectIntl Component", t => {
var content = fs.readFileSync(__dirname + "/app/wrappedComponent.tsx");

var res = p(content.toString());

var expected = [{ id: "emailPlaceholder", defaultMessage: "Email" }];

t.is(res.length, 1);

t.deepEqual(res, expected);
});