Skip to content

Conversation

@baevm
Copy link
Contributor

@baevm baevm commented Sep 25, 2025

Fixed #2613 bug with String.fromCharCode.bind(String), i hope I understood this comment correctly

optional: false,
})) {
return ['fromCodePoint', node.callee.object.property];
}
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 not the correct solution, we should change the listerner to MemberExpresion.

Meaning, no matter how String.fromCharCode is used, it should be reported.

const x = String.fromCharCode

String.fromCharCode + 1

Copy link
Collaborator

@fisker fisker Sep 25, 2025

Choose a reason for hiding this comment

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

And the String.fromCharCode(code) condition above should be removed.

Copy link
Collaborator

Choose a reason for hiding this comment

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

const create = () => ({
	CallExpression(node) {
		// Report `charCodeAt` call
	},
	MemberExpression() {
		// Report `String.fromCharCode`
	}
});

Copy link
Collaborator

Choose a reason for hiding this comment

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

You can even try to use GlobalReferenceTracker which handles more cases, but it's hard to understand. I prefer MemberExpression check for now which is good enough.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

my bad 🤦 , fixed in e075d07, also added extra test case for const x = String.fromCharCode

Copy link
Collaborator

@fisker fisker Sep 26, 2025

Choose a reason for hiding this comment

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

The logic is correct now, thank you!

We can improve the code quantity a little bit,

  1. We can reuse the problem reporting part. something like function getProblem(node, replacement){...} should be good.
  2. getReplacement...() can be inlined since they dont need return "replacement", we already know which case we are dealing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@baevm baevm force-pushed the fix-prefer-code-point branch 2 times, most recently from e075d07 to 9a10b46 Compare September 26, 2025 16:55
@baevm baevm force-pushed the fix-prefer-code-point branch from 9a10b46 to 6b1c893 Compare September 26, 2025 17:08
@fisker fisker changed the title prefer-code-point: fix String.fromCharCode.bind(String) case prefer-code-point: Report cases that String.fromCharCode not called Oct 9, 2025
@sindresorhus sindresorhus merged commit 1d682a1 into sindresorhus:main Oct 10, 2025
19 checks passed
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.

[prefer-code-point] doesn't report on const fromCharCode = String.fromCharCode.bind(String)

3 participants