Unintended Indentation in Template Literal with Line Breaks

Author: knilinkCreated Apr 1, 2025Updated Apr 1, 2025

When update BlockStatement body, template literals within the code are reformatted with unexpeted indentations.

Steps to Reproduce:

Transformer Code (transform.js):

javascript
module.exports = function (fileInfo, api, options) {
  const j = api.jscodeshift;
  const root = j(fileInfo.source);
  root.find(j.BlockStatement).forEach((path) => {
    path.node.body = [
      ...path.node.body,
      // simply mutate the body by adding a empty statement
      j.emptyStatement(),
    ];
  });
  return root.toSource();
};

Source File (test.js):

Create a test file by

bash
echo -e "{\`\n\`}" > test.js

then the content should be

javascript
{`
`}

Run the transformer

bash
npx jscodeshift -t transform.js test.js

Expected Result:

if it's supposed to format the code then it should be

javascript
{
  `
` 
}

Actual Result:

javascript
{
  `
  ` 
}

where the indentation is unexpectedly added after the line break in template literal

Version

[email protected]