Skip to content

Add Make task to validate asset strings#3936

Merged
ironman5366 merged 18 commits intomasterfrom
will-asset-strings
Jul 22, 2020
Merged

Add Make task to validate asset strings#3936
ironman5366 merged 18 commits intomasterfrom
will-asset-strings

Conversation

@ironman5366
Copy link
Contributor

New JS code added in #3909 and others require strings configured in assets.js.erb and i18n-strings.js.erb.

The added make task, check_asset_strings, assets that for each such string referenced in a Javascript file, there exists a corresponding entry in one of the erb files

@ironman5366 ironman5366 requested a review from aduth July 16, 2020 20:25
@ironman5366 ironman5366 marked this pull request as ready for review July 16, 2020 20:25
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably worth making this capture lazier:

Suggested change
missing_translations = find_missing(data, /\Wt\(['"](.*)['"]\)/, @translation_strings)
missing_translations = find_missing(data, /\Wt\(['"](.*?)['"]\)/, @translation_strings)

Before: https://regex101.com/r/jW0NiC/1
After: https://regex101.com/r/jW0NiC/2

See: https://www.rexegg.com/regex-quantifiers.html#lazy_solution

Copy link
Contributor Author

@ironman5366 ironman5366 Jul 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see your point with that error, but that also leaves the issue of only matching one translation per line. For completeness, how about something like \Wt\s?\(['"]([^'^"]*)['"]\)

https://regexr.com/58kjk

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

leaves the issue of only matching one translation per line

Would it? Based on the implementation below using scan, it seems like it should match multiple within the same line.

$ irb
irb(main):001:0> "const x = t('one') + t('two');".scan /\Wt\(['"](.*?)['"]\)/
=> [["one"], ["two"]]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure - I was basing it off of the regex101 link you left.

Regardless, the new match group of "anything that's not a quote", is probably more robust anyway. And if we get to the point where there are escaped quotes in the names of translations, there are probably bigger problems in the codebase haha

Comment on lines 26 to 31
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be a good use-case for select:

Suggested change
missing = []
strings = file_data.scan pattern
strings.each do |s|
missing.push(s) unless source.include? s
end
missing
strings = file_data.scan pattern
strings.select { |s| ! source.include? s }

https://ruby-doc.org/core-2.4.1/Array.html#method-i-select

Copy link
Contributor Author

@ironman5366 ironman5366 Jul 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a reject would be better, but I'm happy to oneline it if that's desirable.

strings.reject { |s| source.include? s }

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reject is even better, I agree 👍

Comment on lines 15 to 21
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Depending how this checker could be integrated into e.g. continuous integration, might it be worth raising an exception, or otherwise ensuring that the script exits with a non-zero exit code, so that it can be treated as a failure?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added 👍

aduth and others added 6 commits July 16, 2020 16:46
**Why**: Asset paths include a fingerprint which can't be known to the client unless provided by the server.

Co-authored-by: Will Beddow <william.beddow@gsa.gov>
**Why**: Current Login.gov pages will check for and initialize any accordions present at page load. Since React components can be mounted after page load, and also need to initialize, there should be internal checks to protect against double initialization, since otherwise event handlers may be triggered multiple times.
**Why**: As an abstraction to simply render an image at the known asset path, encapsulating behavior to perform asset lookup by asset context
**Why**: If classList.js is imported (directly or indirectly) through any tested files, an error will occur.

See inline comment for additional details.
**Why**: To recreate the existing document capture flow, we will need to be able to represent collapsible accordion content in React
**Why**: Recreating the existing document capture flow requires integrating existing tips content
@aduth aduth force-pushed the aduth-document-tips branch from 4da514c to 758e4b3 Compare July 16, 2020 20:53
@aduth aduth force-pushed the will-asset-strings branch from 23e6edd to b14cf5f Compare July 16, 2020 20:56
end

def self.check_file(file)
data = File.open(file).read
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally prefer:

data = File.read(file)

but not a huge deal

@aduth aduth force-pushed the aduth-document-tips branch from 1747971 to 6bdf517 Compare July 20, 2020 17:34
Base automatically changed from aduth-document-tips to master July 21, 2020 15:55
Copy link
Contributor

@zachmargolis zachmargolis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One more suggestion, not required, but otherwise LGTM

@aduth
Copy link
Contributor

aduth commented Jul 22, 2020

Not a blocker, and possibly as a future enhancement: It would be nice to have the inverse of this as well, to flag entries in i18n-strings.js.erb which aren't used in any files (and should therefore be removed).

@ironman5366 ironman5366 merged commit f77f2b8 into master Jul 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants