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

Fix comments not being extracted in .tsx files #101

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
6 changes: 4 additions & 2 deletions src/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down
20 changes: 20 additions & 0 deletions tests/fixtures/Comments.tsx
Original file line number Diff line number Diff line change
@@ -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 = () => (
<div>
<GetText message="Translate me" comment="This is a comment" />

{
// Translators: This is also a comment
gettext('Translate me too')
}
</div>
)

export default TsxComments
12 changes: 11 additions & 1 deletion tests/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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)
Expand Down