lukasgeiter / gettext-extractor

A flexible and powerful Gettext message extractor with support for JavaScript, TypeScript, JSX and HTML.
MIT License
98 stars 21 forks source link

Can't extract comments from some situations #34

Closed mappu closed 5 years ago

mappu commented 5 years ago

Hi, thanks for this project -

I have some situations where comments are not properly extracted. I don't have an exact test case, but it's something like:

  1. multiple map key assignments
var x = {
   "foo": translate("foo"), /// TRANSLATORS: description of foo
   "bar": translate("bar"), /// TRANSLATORS: description of bar
   "baz": translate("baz"), /// TRANSLATORS: description of baz
}

In this case only the first comment would be extracted and matched.

  1. Some long concatenation
var x = "foo" +
   /// TRANSLATORS: comments for bar
   translate("bar") + 
   /// TRANSLATORS: comments for baz
   translate("baz") +
   "foo"
;

In this case none of the comments are extracted and matched.

I read the discussion in https://github.com/lukasgeiter/gettext-extractor/issues/4#issuecomment-304471094 so I guess these constructs have some different representation in the AST that is not understood.

In my codebase I can work around this by extracting to a local variable first before building the longer construct. The comments can be extracted in this way, but, it's less clear.

lukasgeiter commented 5 years ago

Thanks for reporting this issue. I'll try to fix this in the next release.

lukasgeiter commented 5 years ago

I've just published version 3.5.2 which should resolve the first issue.

As for the second example, this should already be working. However, you'll have to explicitly enable the extraction of comments on the previous line with otherLineLeading:

JsExtractors.callExpression('...', {
    arguments: { ... },
    comments: {
        otherLineLeading: true,
        sameLineLeading: true,
        sameLineTrailing: true
    }
})