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

Is it possible to format an export declaration so that each element is placed on a separate line? #153

Closed
Maximaximum opened this issue Dec 5, 2017 · 3 comments

Comments

@Maximaximum
Copy link

I'm trying to use addExport() to add a long list of exports to a file:

    exportsFile.addExport({
      namedExports: filenames.map(filename => {
        return { name: filename };
      })
    });

This results in such an output (the whole statement is placed in one line):

export {AdminForDetails, Commodity, Company, CompanyBase, CompanyType, Contact, Country, CrawlerStatusEventLogItem, CrawlerStatusPage, CrawlerStatusProduct, CrawlerStatusSupplier, Credential, Currency, ExtendedCompany, ExtendedContact, Gender, LineStatus, NetUser, NetUserExtended, NielsenPublisher, NielsenPublisherCountry, Product, ProductBase, Project, PurchaseAvailabilityType, PurchaseInvoice, PurchaseInvoiceCompany, PurchaseInvoiceLine, PurchaseInvoiceProduct, PurchaseInvoiceSupplier, PurchaseOrder, PurchasingQuery, RoundingType, ShopDepartment, Status, Supplier, SupplierExtended, User, UserType};

However, I'd love to be able to format such a long statement so that each element is on its own line, to improve readability:

export {
  AdminForDetails,
  Commodity,
  Company,
  CompanyBase,
  CompanyType,
  Contact,
  Country,
  CrawlerStatusEventLogItem,
  CrawlerStatusPage,
  CrawlerStatusProduct,
  CrawlerStatusSupplier,
  Credential,
  Currency,
  ExtendedCompany,
  ExtendedContact,
  Gender,
  LineStatus,
  NetUser,
  NetUserExtended,
  NielsenPublisher,
  NielsenPublisherCountry,
  Product,
  ProductBase,
  Project,
  PurchaseAvailabilityType,
  PurchaseInvoice,
  PurchaseInvoiceCompany,
  PurchaseInvoiceLine,
  PurchaseInvoiceProduct,
  PurchaseInvoiceSupplier,
  PurchaseOrder,
  PurchasingQuery,
  RoundingType,
  ShopDepartment,
  Status,
  Supplier,
  SupplierExtended,
  User,
  UserType
}

Is this possible?

@dsherret
Copy link
Owner

dsherret commented Dec 5, 2017

I don't think I'm going to get into too much customization for these at the moment. I'm in the midst of a refactoring of the manipulation part of this library and would rather finish that first. In this scenario it's probably best to write it yourself for now:

// untested, but should show the basic idea
sourceFile.addStatements(writer => {
    writer.writeLine("export {");
    filenames.forEach((filename, i) => {
        writer.indent().write(filename).conditionalWrite(i !== filenames.length - 1, ",").newLine();
    });
    writer.writeLine("};");
});

@Maximaximum
Copy link
Author

Thank you. I've tried this code and it works! :)

Though, I think that the support for different formatting options for all the different kinds of statements should be added someday. Some of the formatting options are already supported (the indentation text, the line endings etc.), and it would be great to have support for the different options the tslint has, for example.

@dsherret
Copy link
Owner

dsherret commented Oct 7, 2018

This will be a lot easier in the next post-16.0.x versions. You will be able to insert named exports and imports using a writer:

// untested
exportsFile.addExportDeclaration({
    namedExports: writer => {
        writer.newLine();
        filenames.forEach((filename, i) => {
            writer.write(fileName).conditionalWrite(i !== filenames.length - 1, ",").newLine();
        });
    }
});

That's probably the best it's going to get. If you use that a lot, then I recommend extracting out the writing to a reusable function within your own code like so:

// untested
exportsFile.addExportDeclaration({
    namedExports: writeCommaSeparatedValuesOnNewLines(filenames)
});

// then in some file you can reuse
import { CodeBlockWriter } from "ts-simple-ast";

export function writeCommaSeparatedValuesOnNewLines(texts: string[]) {
    return (writer: CodeBlockWriter) => {
        writer.newLine();
        texts.forEach((text, i) => {
            writer.write(text).conditionalWrite(i !== texts.length - 1, ",").newLine();
        });
    };
}

@dsherret dsherret closed this as completed Oct 7, 2018
@dsherret dsherret reopened this Apr 3, 2021
@dsherret dsherret closed this as completed Apr 3, 2021
Repository owner locked and limited conversation to collaborators Apr 3, 2021
Repository owner unlocked this conversation Apr 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants