Add Make task to validate asset strings#3936
Conversation
lib/asset_checker.rb
Outdated
There was a problem hiding this comment.
Probably worth making this capture lazier:
| 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
There was a problem hiding this comment.
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?\(['"]([^'^"]*)['"]\)
There was a problem hiding this comment.
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"]]
There was a problem hiding this comment.
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
lib/asset_checker.rb
Outdated
There was a problem hiding this comment.
Might be a good use-case for select:
| 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 } |
There was a problem hiding this comment.
I think a reject would be better, but I'm happy to oneline it if that's desirable.
strings.reject { |s| source.include? s }
There was a problem hiding this comment.
reject is even better, I agree 👍
lib/asset_checker.rb
Outdated
There was a problem hiding this comment.
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?
**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
4da514c to
758e4b3
Compare
23e6edd to
b14cf5f
Compare
| end | ||
|
|
||
| def self.check_file(file) | ||
| data = File.open(file).read |
There was a problem hiding this comment.
I personally prefer:
data = File.read(file)but not a huge deal
1747971 to
6bdf517
Compare
zachmargolis
left a comment
There was a problem hiding this comment.
One more suggestion, not required, but otherwise LGTM
|
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 |
New JS code added in #3909 and others require strings configured in
assets.js.erbandi18n-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