Skip to content

Can I inert inline commentBlock before an argument? #688

@cimak

Description

@cimak

I have:

f('foo');

and I want to insert inline block comment, like this:

f(/* comment */'foo');

Unfortunately, Im always getting newline between comment and argument:

// arg.comments = [ j.commentBlock(` comment `) ];
f(/* comment */
'foo');

// arg.comments = [ j.commentBlock(` comment `, true, false) ];
f(/* comment */
'foo');

// arg.comments = [ j.commentBlock(` comment `, false, true) ];
f('foo'/* comment */);

Any Ideas?

Playground:
https://astexplorer.net/#/gist/efea82bb2a028629e17f70a97e43a355/9d951cc12f51086f315d6772443c603dfb020aa4

Transformer:

export default function transformer(file: FileInfo, api: API) {
    const j = api.jscodeshift
    const root = j(file.source);
    
    root
        .find(j.CallExpression, { callee: { type: "Identifier", name: "f" } })
        .forEach((path) => {
            const arg = path.node.arguments[0];
            arg.comments = [ j.commentBlock(` comment `) ];
        });


    return root.toSource({ quote: 'single' });
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions