Skip to content

Unintended Indentation in Template Literal with Line Breaks #664

@knilink

Description

@knilink

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

Steps to Reproduce:

Transformer Code (transform.js):

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

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

then the content should be

{`
`}

Run the transformer

npx jscodeshift -t transform.js test.js

Expected Result:

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

{
  `
` 
}

Actual Result:

{
  `
  ` 
}

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

Version

[email protected]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions