Skip to content

ES5 generator transform produces extra function values when yielding nested object #11046

@Yeaseen

Description

@Yeaseen

Describe the bug

When transpiling generator functions with recursion to ES5, SWC emits additional function values in the iteration result. Node.js and Babel correctly yield only the primitive values, but SWC’s output incorrectly includes anonymous functions.

Input code

function* traverse(obj) {
    for (let key of Object.keys(obj)) {
        if (typeof obj[key] === 'object') yield* traverse(obj[key]);
        else yield obj[key];
    }
}

const obj = {
    data: [1, 2, 3],
    nested: {
        fieldName: 'Nested',
        fieldData: 'Data'
    },
    get dataGenerator() {
        return traverse(this);
    }
}; 

function func1() {
    const generator = (function() {
        return obj.dataGenerator;
    })();
    
    const res = [];
    for (let value of generator) {
        res.push(value);
    }
    
    return res;
}

function main() {
    const res = func1();
    console.log(res);
}

main();

Config

Link to the code that reproduces this issue

https://play.swc.rs/?version=1.13.5&code=H4sIAAAAAAAAA21Ry26DMBC8%2Byv2hl0hpLQ3ELdKvaUfgHJwYCGkxI5sEwlV%2Bff6hQtVffFjZmd2vP0sWjNK8QJG8QcqjVSerwy%2BCdjVSwV0QgNfuIDs4fN8xdYU9qY9beW5NfZAzXJHS7NQYzknqOsaMumLMgbLiFO3N%2FI0ViURnDQGXhIJ4JM8CWml0MYBUEfjjhteQnPI4TWHt1PuHwVqg1256a13ikd%2BwxKyo0ezfA%2B%2Be6HMbVkwDITBhncmHyhQcSMV3WZWaGYlfhOZy6hZargCQvr4v%2BAOh1QckgyrqM1DV%2BZ%2FBjZysesiejAa3TaiCrWVa%2BK%2FpQk%2B%2BDSjm2Ey3fvo4j7rC%2FW0FCFJxz4srXKDSKlufBR%2FQgX%2FGLdKgJywmORALcy8RiitfgBEkr05gQIAAA%3D%3D&config=H4sIAAAAAAAAA1WPSQ6DMAxF95wi8pptu%2BgdOIQVDArKpDiVGiHu3pCBll38%2F7Ofsg9CwMYSXmLPzzx4DEzhmnPCyUb85ARIGmQZlI8w9nbjs1pQM5XoqA1EDCvFssWPhoN2jqnjLTPKqiX9C6UzPhDzHTxRtKumu25oSjBufpeyfSQmT1X%2FhB%2FUZddhUDz1zXr2%2BAIDhbm2FQEAAA%3D%3D

SWC Info output

No response

Expected behavior

SWC's ES5 should print: [ 1, 2, 3, 'Nested', 'Data' ]

Actual behavior

It prints: [ 1, 2, 3, 'Nested', 'Data', [Function (anonymous)], [Function (anonymous)], [Function (anonymous)] ]

Version

1.13.5

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions