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", diff --git a/src/parse.js b/src/parse.js index 8863017..e55f082 100644 --- a/src/parse.js +++ b/src/parse.js @@ -370,9 +370,11 @@ 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 ) 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)