From 8c805f3dfdb00c079d2f6b250a9c00a006360474 Mon Sep 17 00:00:00 2001 From: Alexander Wallin Date: Thu, 10 Aug 2023 13:32:26 +0200 Subject: [PATCH 1/3] 1.16.1 --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 5d058c0..ec349de 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "react-gettext-parser", - "version": "1.16.0", + "version": "1.16.1", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "react-gettext-parser", - "version": "1.16.0", + "version": "1.16.1", "license": "ISC", "dependencies": { "@babel/eslint-parser": "^7.22.10", diff --git a/package.json b/package.json index 89584c8..8eda8ca 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-gettext-parser", - "version": "1.16.0", + "version": "1.16.1", "description": "A gettext parser for React. Spits out .pot files!", "engines": { "node": ">=4.0.0", From e65d0532122cb9be87b0aa99615ca2bddb4b4a92 Mon Sep 17 00:00:00 2001 From: Alexander Wallin Date: Mon, 20 Nov 2023 16:40:52 +0100 Subject: [PATCH 2/3] =?UTF-8?q?Fix=20comments=20not=20being=20extracted=20?= =?UTF-8?q?in=20.tsx=20files=C3=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/parse.js | 7 +++++-- tests/fixtures/Comments.tsx | 20 ++++++++++++++++++++ tests/tests.js | 12 +++++++++++- 3 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 tests/fixtures/Comments.tsx diff --git a/src/parse.js b/src/parse.js index 8863017..2b7b3a0 100644 --- a/src/parse.js +++ b/src/parse.js @@ -370,11 +370,14 @@ export const getTraverser = (cb = noop, opts = {}) => { } // Extract comments for translators - if (Array.isArray(parent.leadingComments) === true) { + const leadingComments = + parent.leadingComments || parent.expression?.leadingComments + if (Array.isArray(leadingComments) === true) { const translatorCommentRegex = /Translators:.+/ - const commentNode = parent.leadingComments.find( + const commentNode = leadingComments.find( (x) => translatorCommentRegex.test(x.value) === true ) + console.log({ commentNode }) if (commentNode !== undefined) { const commentLine = commentNode.value diff --git a/tests/fixtures/Comments.tsx b/tests/fixtures/Comments.tsx new file mode 100644 index 0000000..e8f218a --- /dev/null +++ b/tests/fixtures/Comments.tsx @@ -0,0 +1,20 @@ +import React from 'react' +import { GetText, gettext } from 'gettext-lib' + +// This is a unique key word in typescript, we use this to test the parser +type T = { + readonly key: string +} + +const TsxComments = () => ( +
+ + + { + // Translators: This is also a comment + gettext('Translate me too') + } +
+) + +export default TsxComments diff --git a/tests/tests.js b/tests/tests.js index 35f8eb8..0382293 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -23,7 +23,7 @@ const getSource = (file) => fs.readFileSync(path.join(__dirname, 'fixtures', file), 'utf8') // eslint-disable-next-line -const getJson = file => require(`./fixtures/${file}`) +const getJson = (file) => require(`./fixtures/${file}`) describe('react-gettext-parser', () => { describe('basic extraction', () => { @@ -74,6 +74,16 @@ describe('react-gettext-parser', () => { ) }) + it("should extract translators' comments from .tsx files", () => { + const code = getSource('Comments.tsx') + const messages = extractMessages(code, { sourceType: TYPESCRIPT }) + + expect(messages[0].comments.extracted[0]).to.equal('This is a comment') + expect(messages[1].comments.extracted[0]).to.equal( + 'This is also a comment' + ) + }) + it('should do nothing when no strings are found', () => { const code = getSource('NoStrings.js') const messages = extractMessages(code) From bf23dae87b84ef87277d10405cd7a143cc1d2059 Mon Sep 17 00:00:00 2001 From: Alexander Wallin Date: Mon, 20 Nov 2023 16:41:32 +0100 Subject: [PATCH 3/3] Remove console.log --- src/parse.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/parse.js b/src/parse.js index 2b7b3a0..e55f082 100644 --- a/src/parse.js +++ b/src/parse.js @@ -377,7 +377,6 @@ export const getTraverser = (cb = noop, opts = {}) => { const commentNode = leadingComments.find( (x) => translatorCommentRegex.test(x.value) === true ) - console.log({ commentNode }) if (commentNode !== undefined) { const commentLine = commentNode.value