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

how to remove empty line using jscodeshift? #578

Open
sibelius opened this issue Dec 14, 2023 · 1 comment
Open

how to remove empty line using jscodeshift? #578

sibelius opened this issue Dec 14, 2023 · 1 comment

Comments

@sibelius
Copy link
Contributor

I want to transform this

const obj = {
  a: 'a',

  b: 'b',


  c: 'c',
  d: 'd',
}

into this

const obj = {
  a: 'a',
  b: 'b',
  c: 'c',
  d: 'd',
}

this is my first approach

import type { API, FileInfo, Options } from 'jscodeshift';

import { getPrintOptions } from '../getPrintOptions';

function transform(file: FileInfo, api: API, options: Options) {
  const j = api.jscodeshift; // alias the jscodeshift API

  const root = j(file.source); // parse JS code into an AST

  const printOptions = getPrintOptions(options);

  let linesToRemove = [];

  root
    .find(j.ObjectExpression)
    .forEach((path) => {
      // Find properties in the object
      const properties = path.node.properties;

      // // Sort properties by their starting line number
      const sortedProperties = properties.sort((a, b) => a.loc.start.line - b.loc.start.line);

      // Remove any blank lines between properties
      for (let i = 0; i < sortedProperties.length - 1; i++) {
        const endLine = sortedProperties[i].loc.end.line;
        const nextStartLine = sortedProperties[i + 1].loc.start.line;

        // Remove empty lines between properties
        if (nextStartLine - endLine >= 1) {
          for (let line = endLine + 1; line < nextStartLine; line++) {
            linesToRemove = [
              ...linesToRemove,
              line,
            ];

            console.log({
              line,
            });
          }
        }
      }

      return path.node;
    });

  // linesToRemove.sort((a, b) => b - a).map((line) => {
  //   root.find(j.Literal).at(line).remove();
    // root.find(j.Identifier).at(line).remove();

    // root.find(j.Literal, { loc: { start: { line } } }).remove();
  // });

  return root.toSource(printOptions);
}

module.exports = transform;
module.exports.parser = 'tsx';
@ElonVolo
Copy link
Collaborator

ElonVolo commented Dec 15, 2023

I believe that anything involving spacing is going to be taking place at the recast layer as opposed to the jscodeshift layer, so it's kind of out of our hands.

You'd probably get the most mileage out of creating lint/prettier rules for your codebase and running the transformed code through that, which is what I understand what Meta does when they use jscodeshift.

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

2 participants