-
Notifications
You must be signed in to change notification settings - Fork 26.2k
feat(common): throw error for suspicious date patterns #59798
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
Conversation
While I get where you're going with this, I think the idea goes in the right direction, every end of the year we get 5-10 reports of people using Wouldn'it be more reasonable to log a warning/error ? Edit: Phrasing, this is not a random community PR 😅 |
I did a combined TGP with both this error and an analogous one in EDIT: See go/lsc-date-format-templates. |
@@ -83,6 +83,27 @@ export function formatDate( | |||
locale: string, | |||
timezone?: string, | |||
): string { | |||
if (ngDevMode) { | |||
if (format.includes('Y') && !format.includes('w')) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wdyt of excluding "YYYY"
from the check ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't love it: I saw plenty of those in the depot and they were all wrong.
That said, I'm working on getting the tests to pass, and it's a bit awkward. Not sure what the best approach would be there.
I've pushed an update the fixes the tests that this broke, but I'm not happy about it. I'd rather not back off on The equivalent errorprone check in Java has a |
One idea could be that for |
That seems like a reasonable compromise. Is there a standard way to test logged warnings/thrown errors? |
When we're in a DI context, we use the angular/packages/platform-browser/src/hydration.ts Lines 159 to 165 in 9723f03
This is not the case with angular/packages/common/test/directives/ng_optimized_image_spec.ts Lines 1649 to 1655 in 9723f03
With warning being logged using |
PTAL |
9e71f9e
to
fcfabb6
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@shicks thanks a lot for the changes and helping to update Google's codebase (to make it compatible with this change) 👍
Just left a comment with a couple minor proposals.
This doesn't currently account for occurrences within escaped strings within the pattern, e.g.
would incorrectly be rejected. It seems safer to perform this check internally within I'd also consider this a feature, not a fix. It is potentially a hard breakage for occurrences within libraries. |
FWIW, in all of Google's codebase, I did not run into any cases where this was an issue. That's not to say it's impossible, but I suspect it's more theoretical than actual. That said, I think the easiest way to handle this correctly would be to pass an additional mutable argument into the
I'm talking with @AndrewKushnir about waiting for v20 to release this. If you want a concrete change to the commit message, please say what it should be instead. I've also removed the EDIT: added a second alternative, fixed to "reply" to the correct reviewer. |
cc @JoostK |
This is very similar to what I had in mind, this feels much more robust 👍
It was mostly concerning the choice for I'd go with something like this:
One area where the note can probably be improved is to mention how this error can be resolved. I don't think it's necessary to mention that it's not possible to disable this error in intentional cases, given that we really don't expect this to be intentional. I'm on mobile at the moment so haven't cross-refenced whether I actually mention the correct date patterns in the above suggestion. |
I actually really like the second alternative I added above (shicks@66022da), since it's a pretty minor change and only has any effect at all in ngDevMode. I was also thinking a bit more about suppressing the warning. The fact that "YYYY" by itself is o
6D40
nly a console warning because it's used in tests is really smelly, but the problem is that there's not a reasonable way for the developer to say "no, I really mean this". In the Java prior art (errorprone.info) it's suppressible with a |
I folded in the fix for the false positive on quoted chars. I also put together a draft shicks@97d9405 for adding a new |
Fixed the lint |
Ping @AndrewKushnir - is there anything left to do on this? |
Discussed offline: the next step is to run a TGP and if everything looks good, this PR will be ready for merge into the |
Adjusts the date pipe and formatDate function to detect suspicious usages of the week-numbering year formatter without including the week number, as this is often confused for the calendar year and likely to result in incorrect results near New Years, meaning that those issues aren't typically caught during development. This commit starts throwing a development-only error to reveal this issue right away. BREAKING CHANGE: Using the `Y` formatter (week-numbering year) without also including `w` (week number) is now detected as suspicious date pattern, as `y` is typically intended.
Caretaker note (cc @kirjs): this PR contains a breaking change and we'll need one more TGP early next week (before the merge). Once the TGP results are available, please merge and sync this PR individually (without any other changes in a sync CL). |
I ran a TGP overnight and found a single breakage, which is now fixed. I'll kick off another one right now to be ready with fix-forwards when this lands, but we should consider this ready-to-merge internally. |
Caretaker note: After discussing with @AndrewKushnir I'm going to switch target to minor and merge this to allow backsliding, as we're otherwise blocked on 60024. |
This PR was merged into the repository by commit 74cceba. The changes were merged into the following branches: main |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
The banned patterns are as folllows:
Y
(week-based year) withoutw
(week of year)D
(day of year) in the same pattern asM
(month)Such patterns are extremely likely to indicate an error. In particular, week-based year renders an incorrect year about 1% of the time (specifically, a few days before or after Jan 1 each year). Moreover, since it's correct the vast majority of the time, these errors are very difficult to notice and debug.
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
Issue Number: N/A
What is the new behavior?
This is now an error in ngDevMode, directing the user to use
y
instead ofY
.Does this PR introduce a breaking change?
ngDevMode
Other information