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

new-for-builtins: Correct fix for Date() #2543

Merged
merged 5 commits into from
Jan 25, 2025

Conversation

fisker
Copy link
Collaborator

@fisker fisker commented Jan 24, 2025

typeof new Date()
'object'
typeof Date()
'string'

@fisker fisker marked this pull request as draft January 24, 2025 08:46
@fisker fisker marked this pull request as ready for review January 24, 2025 08:49
> Error 1/1
`␊
> 1 | const foo = Date(/*comment*/);␊
Copy link
Contributor

Choose a reason for hiding this comment

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

Why no fix this to

const foo = String(new Date(/*comment*/));

It should be safe

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It's suggestion, see L1455

Copy link
Collaborator Author

@fisker fisker Jan 24, 2025

Choose a reason for hiding this comment

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

Sorry, I misunderstood, we should try not remove comments, and it's hard to fix to your version.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The correct fix seems like

		function * fix(fixer) {
			yield * fixSpaceAroundKeyword(fixer, node, sourceCode);
			yield fixer.insertTextBefore(node, 'String(');
			yield fixer.insertTextAfter(node, ')');
			yield * switchCallExpressionToNewExpression(node, sourceCode, fixer, /* fixSpaces */ false);
			yield * removeArguments();
		}

Just not worth the effort

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Maybe you wonder why switchCallExpressionToNewExpression is necessary, something like

Array(globalThis)[0].Date(/*comment*/);

is not easy to fix correctly, but switchCallExpressionToNewExpression already considered this.

@sindresorhus sindresorhus merged commit e5e17f8 into sindresorhus:main Jan 25, 2025
17 checks passed
@fisker fisker deleted the date-fix branch January 26, 2025 00:12
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.

None yet

3 participants