mxmvshnvsk / i18n-unused

The static analyze tool for finding, marking and removing unused and missing i18n translations in your JavaScript project
MIT License
128 stars 21 forks source link

ignoreComments skips sections of code when true #42

Open GaryGiebler opened 1 year ago

GaryGiebler commented 1 year ago

If you set ignoreComments true, the app can skip entire sections of code and report translations unused which are actually used.

A single line comment using / and / will cause the app to skip every line of code up to the next comment.

The following is only true if the line begins with '*/' - the '^' character causes the statement to match the beginning of the line:

const isEndOfMultilineComment = (str: string): boolean => /^(*\/)/.test(str);

Even if you remove the '^' so the app catches the end of comments using '*/' it still skips code after single line comments.

This code in translations.ts sets skip true and skips subsequent lines of code:

  if (isStartOfMultilineComment(_str) || isEndOfMultilineComment(_str)) {
    skip = isStartOfMultilineComment(_str);
  }

The same statement causes the app to include the line containing '/*' in the case of multiline comments.

To fix these errors:

  1. Remove the '^' in the isEndOfMultilineComment statement: const isEndOfMultilineComment = (str: string): boolean => /(*\/)/.test(str);

  2. Add the following statement to handle single line comments before the above statement: if (isStartOfMultilineComment(_str) && isEndOfMultilineComment(_str)) { skip = false; return acc; }

  3. Modify this statement to exclude the line with the end of a multiline comment '*/' : if (skip || isInlineComment(_str) || isHTMLComment(_str)) {

to be: if (skip || isInlineComment(_str) || isHTMLComment(_str) || isEndOfMultilineComment(_str)) {

mxmvshnvsk commented 1 year ago

Hi, thx for your issue, I'll try add it later, no time at all, or if u have enough time, please, make PR.

GaryGiebler commented 1 year ago

I tried to post the code in another branch to create a PR but it gave me permissions issues so I just posted the solution here.