benjamn / benjamn/recast

Recast is adding extra newlines in ObjectExpressions

Open
#242 8 comments 21 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
5.3k
Forks
364
Avg merge
3d 8h
Merged PRs (30d)
3

Description

Refer to the following fiddle: http://astexplorer.net/#/27xKOBJJGN

When I have an object with a structure such as this:

const styles = {
  base: {
    height: '100%',
  },
  ribbon: {
    backgroundColor: 'black',
  },
};

and I do anything with the body of the ObjectExpression like this:

export default function(file, api) {
  const j = api.jscodeshift;
  window.j = j;
  const { statement, statements } = j.template;
  return j(file.source)
    .find(j.VariableDeclarator)
    .replaceWith(p => {
        p.node.init = j.objectExpression(p.node.init.properties);
        return p.node;
    })
    .toSource();
};

then the following output is gotten:

const styles = {
  base: {
    height: '100%',
  },

  ribbon: {
    backgroundColor: 'black',
  }
};

see that extra newline after base and before ribbon. These lines are added after every property, which is adding unnecessary changes to my codebase. Is there a way to suppress that? or is that a bug?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked AST Explorer fiddle and trace how ObjectExpression properties are printed when toSource() is called after rebuilding the object expression. Compare the transformed output with the original formatting; done means no extra blank lines are inserted between object properties.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.