Skip to content

fix(37791): Using object destructuring with ECMAScript's private field as computed property name leads to runtime error #38135

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

Merged
merged 1 commit into from
Jan 4, 2021

Conversation

a-tarasyuk
Copy link
Contributor

Fixes #37791

@sandersn sandersn added the For Milestone Bug PRs that fix a bug with a specific milestone label May 5, 2020
@rbuckton
Copy link
Contributor

@typescript-bot pack this

@typescript-bot
Copy link
Collaborator

typescript-bot commented Dec 18, 2020

Heya @rbuckton, I've started to run the tarball bundle task on this PR at 667e845. You can monitor the build here.

@rbuckton
Copy link
Contributor

rbuckton commented Dec 18, 2020

I don't think this sufficiently resolves the issue. There are two different problems here that need to be addressed:

  1. Somewhere in the classFields.ts transform part of the previous AST is being added to the output AST without being visited.
  2. Its important to know where the computed property name is. While your fix addresses the object literal case, it is still broken when used with a nested class declaration:
class Foo {
    #value;
    constructor(value) {
        this.#value = value;
    }
    getValue(data) {
        const obj = this;
        class Bar {
            #y;
            [obj.#value]() {}
        }
        return value;
    }
}

I think the problem with (1) is in visitComputedPropertyName:

expressions.push(name.expression);

This should probably read:

expressions.push(node.expression);

Because name is the original AST node, and node is the visited AST node.

The problem with (2) is that there's only one pendingExpressions array, when this should really be a fresh array when you descend into each new function scope.

Removing visitComputedPropertyName from regular visitors does seem to address (2), but I think we need more tests to be sure.

@a-tarasyuk
Copy link
Contributor Author

@rbuckton Thanks for the review 👍🏻. I've added additional changes/tests.

Copy link
Contributor

@rbuckton rbuckton left a comment

Choose a reason for hiding this comment

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

Looks good!

@rbuckton rbuckton merged commit 841b1a5 into microsoft:master Jan 4, 2021
@a-tarasyuk
Copy link
Contributor Author

@rbuckton Thanks

Zzzen pushed a commit to Zzzen/TypeScript that referenced this pull request Jan 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
For Milestone Bug PRs that fix a bug with a specific milestone
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Using object destructuring with ECMAScript's private field as computed property name leads to runtime error
4 participants