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

Adding method with useTrailingCommas modifies template strings unexpectedly #1603

Open
kingston opened this issue Jan 10, 2025 · 0 comments
Open

Comments

@kingston
Copy link

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},/*`;
    }
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant