Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Typescript #48

Merged
merged 4 commits into from
Oct 4, 2018
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ const keys = extractFromFiles([

- `marker`: The name of the internationalized string marker function. Defaults to `i18n`.
- `keyLoc`: An integer indicating the position of the key in the arguments. Defaults to `0`. Negative numbers, e.g., `-1`, indicate a position relative to the end of the argument list.
- `useTypeScript`: Defaults to `false`.
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of this boolean, maybe the API can be

parser: string with a default to flow?

This way it's extensible.

Of course this will need a throw if a unrecognized parser is passed.

Copy link
Owner

Choose a reason for hiding this comment

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

Using an enum over a boolean is more future proof. It can be interesting if Babel adds a new language in the future or even if someone wants to disable the flow plugin. I'm all 👍

Copy link
Contributor

Choose a reason for hiding this comment

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

Should I open a PR? 😅

Copy link
Owner

Choose a reason for hiding this comment

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

@mathieuletyrant What do you think about it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes you right it's more futur proof :)


### findMissing(locale, keysUsed)

Expand Down
5 changes: 2 additions & 3 deletions src/extractFromCode.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,14 @@ const commentRegExp = /i18n-extract (.+)/;
const commentIgnoreRegExp = /i18n-extract-disable-line/;

export default function extractFromCode(code, options = {}) {
const { marker = 'i18n', keyLoc = 0 } = options;
const { marker = 'i18n', keyLoc = 0, useTypeScript = false } = options;

const ast = parse(code, {
sourceType: 'module',

// Enable all the plugins
plugins: [
'jsx',
'flow',
'asyncFunctions',
'classConstructorCall',
'doExpressions',
Expand All @@ -62,7 +61,7 @@ export default function extractFromCode(code, options = {}) {
'functionBind',
'functionSent',
'dynamicImport',
],
].concat([useTypeScript ? 'typescript' : 'flow']),
});

const keys = [];
Expand Down
34 changes: 34 additions & 0 deletions src/extractFromFiles.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,38 @@ describe('#extractFromFiles()', () => {
'Should work with an array as first parameter',
);
});

it('should work when scanning typescript files', () => {
const keys = extractFromFiles(
['src/extractFromFilesFixture/*.tsx', 'src/extractFromFilesFixture/*.ts'],
{ useTypeScript: true },
);

assert.deepEqual(
[
{
key: 'key3',
loc: { start: { line: 5, column: 0 }, end: { line: 5, column: 12 } },
file: 'src/extractFromFilesFixture/LoginView.tsx',
},
{
key: 'key1',
loc: { start: { line: 6, column: 0 }, end: { line: 6, column: 12 } },
file: 'src/extractFromFilesFixture/LoginView.tsx',
},
{
key: 'key3',
loc: { start: { line: 7, column: 0 }, end: { line: 7, column: 12 } },
file: 'src/extractFromFilesFixture/LogoutView.ts',
},
{
key: 'key1',
loc: { start: { line: 8, column: 0 }, end: { line: 8, column: 12 } },
file: 'src/extractFromFilesFixture/LogoutView.ts',
},
],
keys,
'should work when scanning typescript files',
);
});
});
6 changes: 6 additions & 0 deletions src/extractFromFilesFixture/LoginView.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/* eslint-disable */

import i18n from 'i18n';

i18n('key3');
i18n('key1');
8 changes: 8 additions & 0 deletions src/extractFromFilesFixture/LogoutView.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/* eslint-disable */

import i18n from 'i18n';
Copy link
Owner

@oliviertassinari oliviertassinari Oct 4, 2018

Choose a reason for hiding this comment

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

Maybe we should write some specific TypeScript in this test?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes you right :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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


type i18n = (key: string) => string;

i18n('key3');
i18n('key1');
Loading