Skip to content

Adding method with useTrailingCommas modifies template strings unexpectedly #1603

@kingston

Description

@kingston

Describe the bug

Version: 25.0.0

It's a very specific scenario, but I'm reporting just in case there's something underlying that's more concerning. If you enable useTrailingCommas, adding a method to an object literal with a template string of the form ${p}/* will add an extra comma to the template string ${p},/*.

I might have time to try to dig deeper but figured I'd report it in case anything obvious sprung to mind for the root cause. (since it seems oddly specific for repro)

To Reproduce

import { Node, Project, SyntaxKind } from 'ts-morph';

const project = new Project({
  manipulationSettings: {
    useTrailingCommas: true,
  },
});
const sourceFile = project.createSourceFile('example.ts', 'func({});');

const callExpressions = sourceFile.getDescendantsOfKind(
  SyntaxKind.CallExpression,
);

const objectLiteral = callExpressions[0].getArguments()[0];

if (!Node.isObjectLiteralExpression(objectLiteral)) {
  throw new Error('Object literal expected');
}

objectLiteral.addMethod({
  name: 'logTemplate',
  statements: ['`${p}/*`;'],
  parameters: [{ name: 'p', type: 'string' }],
});

console.info(sourceFile.getFullText());

Expected behavior

func({
    logTemplate(p: string) {
        `${p}/*`;
    }
});

Actual behavior

func({
    logTemplate(p: string) {
        `${p},/*`;
    }
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions