Skip to content

feat(45549): track string literal references within call expressions #45791

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

Closed

Conversation

paranoiacblack
Copy link
Contributor

* Enables string literals in call expression to refer to property names.
* Enables property names in call expression to refer to string literals.
* Changes string literal rename/reference behavior when string literals are in a call expression.
  Previously, all string references with matching text would reference one another globally and
  allow renaming even if such an operation would result in a compiler error.

Fixes #45549

@typescript-bot typescript-bot added the For Backlog Bug PRs that fix a backlog bug label Sep 9, 2021
    * Enables string literals in call expression to refer to property names.
    * Enables property names in call expression to refer to string literals.
    * Changes string literal rename/reference behavior when string literals are in a call expression.
      Previously, all string references with matching text would reference one another globally and
      allow renaming even if such an operation would result in a compiler error.

    Fixes microsoft#45549
// might all refer to each other, but if the string literal being considered is in a
// call expression, any reference should also be from the same call expression.
const refHasSameScope = !callExpr || callExpr === findAncestor(ref, isCallExpression);
const refHasSameText = isStringLiteralLike(ref) || isPropertyNameLiteral(ref) && getTextOfIdentifierOrLiteral(ref) === node.text;
Copy link
Member

Choose a reason for hiding this comment

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

Currently all string literal refs are true without checking whether their text is === node.text. I think it should instead be:

Suggested change
const refHasSameText = isStringLiteralLike(ref) || isPropertyNameLiteral(ref) && getTextOfIdentifierOrLiteral(ref) === node.text;
const refHasSameText = (isStringLiteralLike(ref) || isPropertyNameLiteral(ref)) && getTextOfIdentifierOrLiteral(ref) === node.text;

}
else {
// Special case: Every string literal refers at least to itself.
if (node === ref) return nodeEntry(ref, EntryKind.StringLiteral);
Copy link
Member

Choose a reason for hiding this comment

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

Does this happen? I'd expect getPossibleSymbolReferenceNodes should filter node out of the refs list.
It's fine if it doesn't, but maybe we should consider making it do that.


if (type && isStringLiteralLike(ref) && refHasSameText) {
const refType = getContextualTypeFromParentOrAncestorTypeNode(ref, checker);
if ((refHasSameScope || !type.isStringLiteral() || type.isUnionOrIntersection()) && type === refType) {
Copy link
Member

Choose a reason for hiding this comment

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

!type.isStringLiteral() is different from the old check type !== checker.getStringType(). Does that invert the set of refs that will be marked as references?

return nodeEntry(ref, EntryKind.StringLiteral);
}
}
if (callExpr) {
if (refHasSameScope && isPropertyNameLiteral(ref) && refHasSameText) {
Copy link
Member

Choose a reason for hiding this comment

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

A nested a could refer to something else besides the string literal node "a" even though they're both used inside the same call expression.

eg

f(a => { a: a }, "a")

const params = sig.getParameters();
for (let i = 0; i < params.length; i++) {
const paramType = checker.getParameterType(sig, i);
if (isLiteralTypeNode(ref.parent) && paramType.isStringLiteral() && refHasSameText) {
Copy link
Member

Choose a reason for hiding this comment

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

why is it isLiteralTypeNode(ref.parent) and not isLiteralTypeNode(ref)?

@sandersn sandersn self-assigned this Feb 19, 2022
@sandersn
Copy link
Member

@paranoiacblack Do you want to keep working on this? Otherwise I'd like to close it to reduce the number of open PRs.

@sandersn
Copy link
Member

sandersn commented Jun 1, 2022

To help with PR housekeeping, I'm going to close this PR since it's pretty old now.

@sandersn sandersn closed this Jun 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
For Backlog Bug PRs that fix a backlog bug
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Feature request: Track references of string literals on keys and vice-versa for better intellisense
3 participants