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
3 changes: 3 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Bring 'prettier' in line with tslint options:
trailingComma: all
arrowParens: always
9 changes: 8 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
cache: yarn
language: node_js
node_js:
- "6"

script:
- yarn build-ci
# Ideally we wouldn't commit transpiled .js or .js.map files, but for now:
- if ! git diff --exit-code --name-only HEAD; then
echo "Changed files detected (listed above). Please run 'yarn build' before you commit.";
exit 1;
fi
44 changes: 29 additions & 15 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import ts = require("typescript");

function isMethodCall(el: ts.Declaration, methodName: string): el is ts.VariableDeclaration {
function isMethodCall(
el: ts.Declaration,
methodName: string,
): el is ts.VariableDeclaration {
return (
ts.isVariableDeclaration(el) &&
el.initializer &&
Expand Down Expand Up @@ -31,7 +34,9 @@ type ElementName = "FormattedMessage";
type MethodName = "defineMessages" | "formatMessage";
type MessageExtracter = (obj: ts.ObjectLiteralExpression) => Message[];

function extractMessagesForDefineMessages(objLiteral: ts.ObjectLiteralExpression): Message[] {
function extractMessagesForDefineMessages(
objLiteral: ts.ObjectLiteralExpression,
): Message[] {
const messages: Message[] = [];
objLiteral.properties.forEach((p) => {
const message: Message = {};
Expand All @@ -43,7 +48,10 @@ function extractMessagesForDefineMessages(objLiteral: ts.ObjectLiteralExpression
p.initializer.properties.forEach((ip) => {
if (ts.isIdentifier(ip.name) || ts.isLiteralExpression(ip.name)) {
const name = ip.name.text;
if (ts.isPropertyAssignment(ip) && ts.isStringLiteral(ip.initializer)) {
if (
ts.isPropertyAssignment(ip) &&
ts.isStringLiteral(ip.initializer)
) {
message[name] = ip.initializer.text;
}
}
Expand All @@ -54,21 +62,26 @@ function extractMessagesForDefineMessages(objLiteral: ts.ObjectLiteralExpression
return messages;
}

function extractMessagesForFormatMessage(objLiteral: ts.ObjectLiteralExpression): Message[] {
function extractMessagesForFormatMessage(
objLiteral: ts.ObjectLiteralExpression,
): Message[] {
const message: Message = {};
objLiteral.properties.forEach((p) => {
if (
ts.isPropertyAssignment(p) &&
(ts.isIdentifier(p.name) || ts.isLiteralExpression(p.name)) &&
ts.isStringLiteral(p.initializer)
) {
message[p.name.text] = p.initializer.text;
}
});
message[p.name.text] = p.initializer.text;
}
});
return [message];
}

function extractMessagesForNode(node: ts.Node, extractMessages: MessageExtracter): Message[] {
function extractMessagesForNode(
node: ts.Node,
extractMessages: MessageExtracter,
): Message[] {
const res: Message[] = [];
function find(n: ts.Node): Message[] {
if (ts.isObjectLiteralExpression(n)) {
Expand All @@ -81,7 +94,10 @@ function extractMessagesForNode(node: ts.Node, extractMessages: MessageExtracter
return res;
}

function forAllVarDecls(node: ts.Node, cb: (decl: ts.VariableDeclaration) => void) {
function forAllVarDecls(
node: ts.Node,
cb: (decl: ts.VariableDeclaration) => void,
) {
if (ts.isVariableDeclaration(node)) {
cb(node);
} else {
Expand All @@ -96,10 +112,7 @@ function findJsxOpeningLikeElementsWithName(
const messages: ts.JsxOpeningLikeElement[] = [];
function findJsxElement(n: ts.Node): undefined {
// Is this a JsxElement with an identifier name?
if (
ts.isJsxOpeningLikeElement(n) &&
ts.isIdentifier(n.tagName)
) {
if (ts.isJsxOpeningLikeElement(n) && ts.isIdentifier(n.tagName)) {
// Does the tag name match what we're looking for?
const childTagName = n.tagName;
if (childTagName.text === tagName) {
Expand Down Expand Up @@ -137,7 +150,6 @@ function findMethodCallsWithName(

/**
* Parse tsx files
*
* @export
* @param {string} contents
* @returns {array}
Expand Down Expand Up @@ -177,7 +189,9 @@ function main(contents: string): {}[] {
// found nothing
// tslint:disable-next-line:no-any
const a = attr as any; // TODO find correct types to avoid "any"
if (!a.name || !a.initializer) { return; }
if (!a.name || !a.initializer) {
return;
}
msg[a.name.text] =
a.initializer.text || a.initializer.expression.text;
});
Expand Down
6 changes: 3 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.

13 changes: 10 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@
"description": "Extracts string messages from TypeScript React components or ts files that use React Intl.",
"main": "lib/index.js",
"scripts": {
"build": "yarn run format:auto && tsc && yarn run _test",
"build-ci": "tsc && yarn run format:check && yarn run _test",
"format:auto": "prettier --config .prettierrc *.ts --write",
"format:check": "prettier --config .prettierrc *.ts --list-different",
"start": "tsc -w",
"test": "tsc && tslint -c tslint.json '*.ts' && ava --verbose"
"test": "tsc && yarn run _test && echo Please run 'yarn build' before you commit",
"_test": "tslint -c tslint.json '*.ts' && ava --verbose"
},
"repository": {
"type": "git",
Expand All @@ -24,12 +29,14 @@
},
"homepage": "https://github.com/bang88/typescript-react-intl#readme",
"dependencies": {
"tslint": "^5.9.1",
"typescript": "^2.6.2"
},
"devDependencies": {
"@types/react": "^15.0.27",
"@types/react-intl": "^2.2.9",
"ava": "^0.19.1"
"ava": "^0.19.1",
"prettier": "1.10.2",
"tslint": "^5.9.1",
"tslint-config-prettier": "^1.6.0"
}
}
3 changes: 2 additions & 1 deletion tslint.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"defaultSeverity": "error",
"extends": [
"tslint:recommended"
"tslint:recommended",
"tslint-config-prettier"
],
"jsRules": {},
"rules": {
Expand Down
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2258,6 +2258,10 @@ preserve@^0.2.0:
version "0.2.0"
resolved "https://registry.yarnpkg.com/preserve/-/preserve-0.2.0.tgz#815ed1f6ebc65926f865b310c0713bcb3315ce4b"

prettier@1.10.2:
version "1.10.2"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.10.2.tgz#1af8356d1842276a99a5b5529c82dd9e9ad3cc93"

pretty-format@^19.0.0:
version "19.0.0"
resolved "https://registry.yarnpkg.com/pretty-format/-/pretty-format-19.0.0.tgz#56530d32acb98a3fa4851c4e2b9d37b420684c84"
Expand Down Expand Up @@ -2774,6 +2778,10 @@ tslib@^1.8.0, tslib@^1.8.1:
version "1.8.1"
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.8.1.tgz#6946af2d1d651a7b1863b531d6e5afa41aa44eac"

tslint-config-prettier@^1.6.0:
version "1.6.0"
resolved "https://registry.yarnpkg.com/tslint-config-prettier/-/tslint-config-prettier-1.6.0.tgz#fec1ee8fb07e8f033c63fed6b135af997f31962a"

tslint@^5.9.1:
version "5.9.1"
resolved "https://registry.yarnpkg.com/tslint/-/tslint-5.9.1.tgz#1255f87a3ff57eb0b0e1f0e610a8b4748046c9ae"
Expand Down