Skip to content
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

Update date ESLint rule to flag usage of new Date() with arguments. #9459

Merged
merged 3 commits into from
Oct 9, 2024

Conversation

tofumatt
Copy link
Collaborator

@tofumatt tofumatt commented Oct 3, 2024

Summary

Addresses issue:

Relevant technical choices

A difference between the IB is that it requests errors with > 0 && < 3, but I think this was actually meant to just be < 3, because we still want errors when no arguments are used, so I made that change.

It seems like all existing instances of new Date() with arguments are valid, so I just added comments.

PR Author Checklist

  • My code is tested and passes existing unit tests.
  • My code has an appropriate set of unit tests which all pass.
  • My code is backward-compatible with WordPress 5.2 and PHP 7.4.
  • My code follows the WordPress coding standards.
  • My code has proper inline documentation.
  • I have added a QA Brief on the issue linked above.
  • I have signed the Contributor License Agreement (see https://cla.developers.google.com/).

Do not alter or remove anything below. The following sections will be managed by moderators only.

Code Reviewer Checklist

  • Run the code.
  • Ensure the acceptance criteria are satisfied.
  • Reassess the implementation with the IB.
  • Ensure no unrelated changes are included.
  • Ensure CI checks pass.
  • Check Storybook where applicable.
  • Ensure there is a QA Brief.
  • Ensure there are no unexpected significant changes to file sizes.

Merge Reviewer Checklist

  • Ensure the PR has the correct target branch.
  • Double-check that the PR is okay to be merged.
  • Ensure the corresponding issue has a ZenHub release assigned.
  • Add a changelog message to the issue.

Copy link

github-actions bot commented Oct 3, 2024

Size Change: 0 B

Total Size: 1.86 MB

ℹ️ View Unchanged
Filename Size
./dist/assets/css/googlesitekit-admin-css-********************.min.css 58.4 kB
./dist/assets/css/googlesitekit-adminbar-css-********************.min.css 11.8 kB
./dist/assets/css/googlesitekit-authorize-application-css-********************.min.css 846 B
./dist/assets/css/googlesitekit-wp-dashboard-css-********************.min.css 8.2 kB
./dist/assets/js/32-********************.js 2.76 kB
./dist/assets/js/33-********************.js 2.25 kB
./dist/assets/js/34-********************.js 3.64 kB
./dist/assets/js/35-********************.js 935 B
./dist/assets/js/36-********************.js 893 B
./dist/assets/js/37-********************.js 1.61 kB
./dist/assets/js/38-********************.js 1.57 kB
./dist/assets/js/39-********************.js 1.61 kB
./dist/assets/js/40-********************.js 1.59 kB
./dist/assets/js/41-********************.js 3.11 kB
./dist/assets/js/analytics-advanced-tracking-********************.js 901 B
./dist/assets/js/googlesitekit-activation-********************.js 24 kB
./dist/assets/js/googlesitekit-ad-blocking-recovery-********************.js 70.5 kB
./dist/assets/js/googlesitekit-adminbar-********************.js 34.6 kB
./dist/assets/js/googlesitekit-api-********************.js 10.1 kB
./dist/assets/js/googlesitekit-components-gm2-********************.js 6.04 kB
./dist/assets/js/googlesitekit-components-gm3-********************.js 10.1 kB
./dist/assets/js/googlesitekit-consent-mode-********************.js 25.6 kB
./dist/assets/js/googlesitekit-data-********************.js 2.37 kB
./dist/assets/js/googlesitekit-datastore-forms-********************.js 8.95 kB
./dist/assets/js/googlesitekit-datastore-location-********************.js 2.08 kB
./dist/assets/js/googlesitekit-datastore-site-********************.js 20.5 kB
./dist/assets/js/googlesitekit-datastore-ui-********************.js 10 kB
./dist/assets/js/googlesitekit-datastore-user-********************.js 26.5 kB
./dist/assets/js/googlesitekit-entity-dashboard-********************.js 81.8 kB
./dist/assets/js/googlesitekit-events-provider-contact-form-7-********************.js 646 B
./dist/assets/js/googlesitekit-events-provider-easy-digital-downloads-********************.js 624 B
./dist/assets/js/googlesitekit-events-provider-mailchimp-********************.js 630 B
./dist/assets/js/googlesitekit-events-provider-ninja-forms-********************.js 712 B
./dist/assets/js/googlesitekit-events-provider-optin-monster-********************.js 675 B
./dist/assets/js/googlesitekit-events-provider-popup-maker-********************.js 634 B
./dist/assets/js/googlesitekit-events-provider-woocommerce-********************.js 657 B
./dist/assets/js/googlesitekit-events-provider-wpforms-********************.js 633 B
./dist/assets/js/googlesitekit-i18n-********************.js 3.93 kB
./dist/assets/js/googlesitekit-main-dashboard-********************.js 156 kB
./dist/assets/js/googlesitekit-modules-ads-********************.js 33.1 kB
./dist/assets/js/googlesitekit-modules-adsense-********************.js 124 kB
./dist/assets/js/googlesitekit-modules-analytics-4-********************.js 176 kB
./dist/assets/js/googlesitekit-modules-********************.js 22.1 kB
./dist/assets/js/googlesitekit-modules-pagespeed-insights-********************.js 22.5 kB
./dist/assets/js/googlesitekit-modules-reader-revenue-manager-********************.js 22 kB
./dist/assets/js/googlesitekit-modules-search-console-********************.js 59.6 kB
./dist/assets/js/googlesitekit-modules-sign-in-with-google-********************.js 1.01 kB
./dist/assets/js/googlesitekit-modules-tagmanager-********************.js 32.1 kB
./dist/assets/js/googlesitekit-notifications-********************.js 22.5 kB
./dist/assets/js/googlesitekit-polyfills-********************.js 377 B
./dist/assets/js/googlesitekit-settings-********************.js 125 kB
./dist/assets/js/googlesitekit-splash-********************.js 86.7 kB
./dist/assets/js/googlesitekit-user-input-********************.js 60.9 kB
./dist/assets/js/googlesitekit-vendor-********************.js 321 kB
./dist/assets/js/googlesitekit-widgets-********************.js 89.7 kB
./dist/assets/js/googlesitekit-wp-dashboard-********************.js 62.4 kB
./dist/assets/js/runtime-********************.js 1.38 kB

compressed-size-action

Copy link

github-actions bot commented Oct 3, 2024

Build files for 34510c2 have been deleted.

Copy link
Collaborator

@aaemnnosttv aaemnnosttv left a comment

Choose a reason for hiding this comment

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

Thanks @tofumatt – this looks good, just a few thoughts and small things to address.

Comment on lines 65 to 66
node.callee.property.name === 'now' &&
node.arguments.length <= 2
Copy link
Collaborator

Choose a reason for hiding this comment

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

Date.now doesn't accept any parameters, so this should be triggered regardless of being used properly or not :)

@@ -53,7 +53,7 @@ module.exports = {
NewExpression( node ) {
if (
node.callee.name === 'Date' &&
node.arguments.length === 0
node.arguments.length <= 2
Copy link
Collaborator

Choose a reason for hiding this comment

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

I prefer using the simpler logical operator where possible. WDYT?

Suggested change
node.arguments.length <= 2
node.arguments.length < 3

Copy link
Collaborator Author

@tofumatt tofumatt Oct 9, 2024

Choose a reason for hiding this comment

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

I usually find the "less than or equal to" easier to scan, because I don't have to do complex arithmetic in my head.

With < 3 I need to calculate something eg. "What comes before 3 here, logically? And should it be an Integer or a Floating point number?" etc., but for <= 2 I can just go "oh, it's two or less". 😅

Copy link
Collaborator

Choose a reason for hiding this comment

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

but for <= 2 I can just go "oh, it's two or less".

I don't feel super strongly about it, so happy to keep what you have. Removing the "or" from that thought is what I mean by logically simpler.

@@ -36,7 +36,7 @@ const ruleTester = new RuleTester( {
ruleTester.run( 'no-direct-date', rule, {
valid: [
'const date = select( CORE_USER ).getReferenceDate();',
'const createTime = new Date( Date.now() - DAY_IN_SECONDS * 1.5 * 1000 ).toISOString();',
'const createTime = new Date( DAY_IN_SECONDS * 1.5 * 1000, 20, 3 ).toISOString();',
Copy link
Collaborator

Choose a reason for hiding this comment

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

This instance isn't exactly using the constructor properly. The arguments are year, monthIndex, day which don't quite align with the usage here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ah, true, I was just using bogus values but I'll have them reflect real-world usage 👍🏻

Comment on lines 61 to 63
? // Valid use of `new Date()` with an argument.
// eslint-disable-next-line sitekit/no-direct-date
new Date( dateStringValue ).getTime()
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is valid if it uses a timestamp as shown in the docblock (complete with timezone), however this would be incorrect for one of our YYYY-MM-DD date strings as noted in the description of the issue.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Oh, right, I missed the ternary showing that this would be a string in this case 😓 Thanks 👍🏻

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Oh, actually, this is only used in one place and is designed to accept strings:

baseModuleStore.actions.setPropertyCreateTime = ( value ) => {
return originalSetPropertyCreateTime(
convertDateStringToUNIXTimestamp( value )
);
};

Linting isn't the right way to protect against invalid arguments here, it would need to be an invariant on the length of the datetime string.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Agreed, SGTM

.eslintrc.json Show resolved Hide resolved
@aaemnnosttv
Copy link
Collaborator

Failing tests all seem to be from known unrelated instabilities.

@aaemnnosttv aaemnnosttv merged commit 1820fa3 into develop Oct 9, 2024
20 of 24 checks passed
@aaemnnosttv aaemnnosttv deleted the 9032 branch October 9, 2024 18:54
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.

2 participants