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

Semicolon changes output of code #38477

Closed
negative0 opened this issue Apr 30, 2021 · 2 comments
Closed

Semicolon changes output of code #38477

negative0 opened this issue Apr 30, 2021 · 2 comments
Labels
invalid Issues and PRs that are invalid.

Comments

@negative0
Copy link

What steps will reproduce the bug?

const f1 = async (abc) => {
        console.log(abc);
        return [10, 12];
}

const f2 = async () => {
        let match = {a: 10};
        let a,b;
        match = {
                ...match,
                b: 20
        }
        [a,b] = await f1(match);
}

f2();

and

const f1 = async (abc) => {
        console.log(abc);
        return [10, 12];
}

const f2 = async () => {
        let match = {a: 10};
        let a,b;
        match = {
                ...match,
                b: 20
        };
        [a,b] = await f1(match);
}

f2();

These codes give different outputs. The only difference is a semicolon after match

How often does it reproduce? Is there a required condition?

Every time. No required condition, I think.

What is the expected behavior?

{ a: 10, b: 20 }

What do you see instead?

{ a: 10 }

Additional information

@Ayase-252 Ayase-252 added the invalid Issues and PRs that are invalid. label Apr 30, 2021
@Ayase-252
Copy link
Member

Ayase-252 commented Apr 30, 2021

It's definitely not a bug in Node.js but a quirk of JavaScript.

By the spec, the semicolon ; is important in this case. Without the ;, the {}[expression] is intepreted as property access. So the first snippet is equivalent with

const f2 = async () => {
        let match = {a: 10};
        let a,b;
        // match is `[10, 12]`, as you can verify.
        match = ({
                ...match,
                b: 20
        }[a,b] = await f1(match));
}

@aduh95
Copy link
Contributor

aduh95 commented Apr 30, 2021

You can use tools such as https://astexplorer.net/ to try to decipher what JS you are writing. To get the expected output, you need to either:

  • use semicolons
  • use let or const keyword when doing the assignment: const [a,b] = await f1(match)
  • send a proposal to TC39 for changing the ECMAScript spec.

Closing this now, but feel free to keep asking questions if you need more help.

EDIT: FYI, here's how Prettier styles it with the no-semi option: https://prettier.io/playground/#N4Igxg9gdgLgprEAuc0DOMAEAzAjJgXkwEM0BPKMTACmICMwBKQgPk2AB0pMfefIoaCABs4AOmEQA5rQaMA3Fz58ATnBgBXFdwDauAAwAaTLgBMAXUVQAvly4CMOU4RLlKNZgTaduynqKwAW2IYMAALF2BiJBN9ays-f3USQzoExODQiKIfRLzMMULM8MMlfL86GNN9Mr942uUdYlTzF2IAd2IASyw8amKwhS5bKC5sU2ohqBBDEAgABxgu9GRQYhUVCHaABXWENGQQYmFOsgPZuhViMABrdQBleeuuqClkGBUNOFm4QLo4AAmAMBABliK8NMQpHAAGIQFSZJavQ7EDQwCAzEBhGCBYQAdTCPTgaCeYDg932PS6ADcemRDmA0OcQC80HAVDBtlcpMFkNhjmzZgArNAADwAQldbg9iIE4CCXnA+QLviARaL7i8pKIAIoaCDwZXCQUgJ4qNkqQ50ehwYSY+YqF4wPFdAEwMLIAAcRlNmzZeKu80ODuJ7OpStmAEd9fAuQsDihSABaKBwQGAzFqaNdNRcqG8pD842qtmBLpGk1oLW6mNKwsq2Ywegut0epCmRtXLrCLUAYQggQLIGJAFZMRo2QAVegJosm6lfACSUGBsHuYEdiwAgiv7jAyKIK3BrNYgA

@aduh95 aduh95 closed this as completed Apr 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid Issues and PRs that are invalid.
Projects
None yet
Development

No branches or pull requests

3 participants