Skip to content

Commit

Permalink
Merge pull request #9459 from google/9032
Browse files Browse the repository at this point in the history
  • Loading branch information
aaemnnosttv authored Oct 9, 2024
2 parents 3233a32 + 34510c2 commit 1820fa3
Show file tree
Hide file tree
Showing 15 changed files with 88 additions and 14 deletions.
11 changes: 11 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,17 @@
"off"
]
}
},
{
"files": [
"webpack/*",
"**/__factories__/*",
"*.stories.js",
"*.test.js"
],
"rules": {
"sitekit/no-direct-date": "off"
}
}
],
"plugins": [
Expand Down
4 changes: 4 additions & 0 deletions assets/js/components/GoogleChart/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,8 @@ export default function GoogleChart( props ) {

// Only use markers if the date is within the current date range.
const dateMarkersInRange = dateMarkers.filter( ( dateMarker ) => {
// Valid use of `new Date()` with an argument.
// eslint-disable-next-line sitekit/no-direct-date
return isDateWithinRange( new Date( dateMarker.date ) );
} );

Expand All @@ -260,6 +262,8 @@ export default function GoogleChart( props ) {

// Add the dotted line and tooltip for each date marker.
dateMarkersInRange.forEach( ( dateMarker, index ) => {
// Valid use of `new Date()` with an argument.
// eslint-disable-next-line sitekit/no-direct-date
const dateFromMarker = new Date( dateMarker.date );

const chartLine = document.getElementById(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ export default function BannerNotification( props ) {
const { value: dismissed } = await getItem( cacheKeyDismissed );

if ( dismissed ) {
// Valid use of `new Date()` with an argument.
// eslint-disable-next-line sitekit/no-direct-date
const expiration = new Date( dismissed );

expiration.setSeconds(
Expand Down
5 changes: 5 additions & 0 deletions assets/js/modules/adsense/util/data-mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,9 +257,14 @@ export function getAdSenseMockResponse( args ) {
const ops = [
// Converts range number to a date string.
map( ( item ) => {
// Valid use of `new Date()` with an argument.
// eslint-disable-next-line sitekit/no-direct-date
const updatedMilliseconds = new Date( startDate ).setDate(
startDate.getDate() + item
);

// Valid use of `new Date()` with an argument.
// eslint-disable-next-line sitekit/no-direct-date
return getDateString( new Date( updatedMilliseconds ) );
} ),
// Add dimension and metric values.
Expand Down
2 changes: 2 additions & 0 deletions assets/js/modules/adsense/util/site-stats-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ export function getSiteStatsDataForGoogleChart(
];

const nextDate = ( date ) => {
// Valid use of `new Date()` with an argument.
// eslint-disable-next-line sitekit/no-direct-date
const next = new Date( date );
next.setDate( date.getDate() + 1 );
return next;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ export default function UserCountGraph( props ) {
? [
{
date: getDateString(
// Valid use of `new Date()` with an argument.
// eslint-disable-next-line sitekit/no-direct-date
new Date( propertyCreateTime )
),
text: __(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,8 @@ const baseSelectors = {
const endDate = select( CORE_USER ).getReferenceDate();

return {
// Valid use of `new Date()` with an argument.
// eslint-disable-next-line sitekit/no-direct-date
startDate: getDateString( new Date( propertyCreateTime ) ),
endDate,
dimensions: [ `customEvent:${ customDimension }` ],
Expand Down
2 changes: 2 additions & 0 deletions assets/js/modules/analytics-4/datastore/partial-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,8 @@ const baseSelectors = {
return undefined;
}

// Valid use of `new Date()` with an argument.
// eslint-disable-next-line sitekit/no-direct-date
const startDate = getDateString( new Date( propertyCreateTime ) );
const endDate = select( CORE_USER ).getReferenceDate();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ export default function AnalyticsStats( props ) {
if ( propertyCreateTime ) {
dateMarkers = [
{
// Valid use of `new Date()` with an argument.
// eslint-disable-next-line sitekit/no-direct-date
date: getDateString( new Date( propertyCreateTime ) ),
text: __(
'Google Analytics property created',
Expand Down
5 changes: 5 additions & 0 deletions assets/js/modules/search-console/util/data-mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,14 @@ export function getSearchConsoleMockResponse( args ) {
const ops = [
// Converts range number to a date string.
map( ( item ) => {
// Valid use of `new Date()` with an argument.
// eslint-disable-next-line sitekit/no-direct-date
const updatedMilliseconds = new Date( startDate ).setDate(
startDate.getDate() + item
);

// Valid use of `new Date()` with an argument.
// eslint-disable-next-line sitekit/no-direct-date
return getDateString( new Date( updatedMilliseconds ) );
} ),
// Add dimension and metric values.
Expand Down
7 changes: 6 additions & 1 deletion assets/js/util/convert-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,12 @@ export const convertSecondsToArray = ( seconds ) => {
export const convertDateStringToUNIXTimestamp = ( dateStringValue ) => {
const unixTimestamp =
dateStringValue && ! Number.isInteger( dateStringValue )
? new Date( dateStringValue ).getTime()
? // Valid use of `new Date()` with an argument, because this should only
// be passed full time strings, not `YYYY-MM-DD` style dates.
//
// See: https://github.com/google/site-kit-wp/pull/9459#discussion_r1790660073
// eslint-disable-next-line sitekit/no-direct-date
new Date( dateStringValue ).getTime()
: dateStringValue;

if ( isNaN( unixTimestamp ) || ! unixTimestamp ) {
Expand Down
6 changes: 6 additions & 0 deletions assets/js/util/dates.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,10 @@ export function isValidDateString( dateString = '' ) {
return false;
}

// Valid use of `new Date()`, constructing a new date from the string.
// eslint-disable-next-line sitekit/no-direct-date
const date = new Date( dateString );

return isDate( date ) && ! isNaN( date );
}

Expand Down Expand Up @@ -217,5 +220,8 @@ export function dateSub( relativeDate, duration ) {
const timestamp = isValidDateString( relativeDate )
? Date.parse( relativeDate )
: relativeDate.getTime();

// Valid use of `new Date()` using calculations.
// eslint-disable-next-line sitekit/no-direct-date
return new Date( timestamp - duration * 1000 );
}
12 changes: 1 addition & 11 deletions packages/eslint-plugin/configs/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,6 @@ module.exports = {
'sitekit/jsdoc-tag-grouping': [ 'error' ],
'sitekit/jsdoc-tag-order': [ 'error' ],
'sitekit/no-yield-dispatch': [ 'error' ],
'sitekit/no-direct-date': [
'error',
{
ignoreFiles: [
'*/webpack/*',
'*/__factories__/*',
'*.stories.js',
'*.test.js',
],
},
],
'sitekit/no-direct-date': [ 'error' ],
},
};
2 changes: 1 addition & 1 deletion packages/eslint-plugin/rules/no-direct-date.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ module.exports = {
NewExpression( node ) {
if (
node.callee.name === 'Date' &&
node.arguments.length === 0
node.arguments.length <= 2
) {
report( node );
}
Expand Down
38 changes: 37 additions & 1 deletion packages/eslint-plugin/rules/no-direct-date.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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( 2024, 5, 9 ).toISOString();',
],
invalid: [
{
Expand All @@ -57,5 +57,41 @@ ruleTester.run( 'no-direct-date', rule, {
},
],
},
{
code: 'const date = new Date( 12345 );',
errors: [
{
message:
"Avoid using 'new Date()'. Use select( CORE_USER ).getReferenceDate() or add a comment explaining why the reference date should not be used here.",
},
],
},
{
code: 'const timestamp = Date.now( 12345 );',
errors: [
{
message:
"Avoid using 'Date.now()'. Use select( CORE_USER ).getReferenceDate() or add a comment explaining why the reference date should not be used here.",
},
],
},
{
code: 'const date = new Date( 12345, 2 );',
errors: [
{
message:
"Avoid using 'new Date()'. Use select( CORE_USER ).getReferenceDate() or add a comment explaining why the reference date should not be used here.",
},
],
},
{
code: 'const timestamp = Date.now( 12345, 2 );',
errors: [
{
message:
"Avoid using 'Date.now()'. Use select( CORE_USER ).getReferenceDate() or add a comment explaining why the reference date should not be used here.",
},
],
},
],
} );

0 comments on commit 1820fa3

Please sign in to comment.