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

Enhancement: Add support for ts-morph code formatting #6

Closed
ul1sbrt opened this issue Dec 9, 2020 · 2 comments
Closed

Enhancement: Add support for ts-morph code formatting #6

ul1sbrt opened this issue Dec 9, 2020 · 2 comments
Assignees
Labels
enhancement New feature or request

Comments

@ul1sbrt
Copy link

ul1sbrt commented Dec 9, 2020

Enhancement / Request / Wish
Provide a possibility to adjust the settings of code formats through the cli of cds2types and its used dependency "ts-morph". We would like to enable the trailing comma feature to avoid unnecessary warnings/errors and afterwards manual fixes of the outputted typescript file.

    export enum Entity {
        CodeList = "sap.common.CodeList",
        Countries = "sap.common.Countries",
        Currencies = "sap.common.Currencies",
        Languages = "sap.common.Languages" <-- missing
    }

Assumption
You are using ESLint with active rule "require trailing comma" in objects. See https://eslint.org/docs/rules/comma-dangle
The "researched" change is related to ts-morph and supports the feature request.

Codeing
Via ts-morph it is possible to active the option "useTrailingCommas", stated here: https://ts-morph.com/manipulation/settings

const project = new Project({
    // these are the defaults
    manipulationSettings: {
         ...
        // Whether to use trailing commas in multi-line scenarios where trailing
        // commas would be used.
        useTrailingCommas: false,
    },
});

Coding from /src/program.ts, function "run":

public async run(options: IOptions): Promise<void> {
        this.interfacePrefix = options.prefix;
        ...
        const project = new morph.Project();
        ...
    }

Solution
Either add a new option "useTrailingComma" to your cli and pass it to the ts-morph project.
cds2types -c ./src/services.cds -o ./db/entities.ts -p I -usetrailingcomma

Or a more generic version, to pass multiple settings/options for ts-morph in a appropriate way.

Disclaimer
Even if my research is wrong, the feature requests remains the same 👍

@mrbandler mrbandler added the enhancement New feature or request label Dec 12, 2020
@iDschepe
Copy link
Collaborator

iDschepe commented Jan 5, 2021

Maybe these parameters could be moved into a cds2types.json?

@mrbandler
Copy link
Owner

mrbandler commented Jan 7, 2021

Hello everyone,

I just published a new version (v2.6.0) which allows you to pass a -f, --format flag to the CLI.

@ul1sbrt thank you for your research, it helped a lot, sadly the useTrailingCommas setting on the ts-morph project has no effect, I am still looking into it and will open an issue on their repo.

@iDschepe thank you for the suggestions! This is something I am planning to add in the currently planned version 2 re-write.
Which will include this issue aswell.

After the initial idea didn't work I tried to add ESLint with auto-fix support after I generate the source code, here the real trouble started.
ESLint has the comma-dangle rule which works and is auto-fixable but it doesn't affect Typescript code like enums (which in our case are the important code parts).
typescript-eslint extended the comma-dangle rule to accommodate for enums, generic type lists, and tuples, although for it to work you have to disable the core rule and suddenly it's no longer auto-fixable.

{
  // note you must disable the base rule as it can report incorrect errors
  "comma-dangle": "off",
  "@typescript-eslint/comma-dangle": ["error"]
}

You can find the complete docs on it here.

So, to make a long story short, if you'd like to format the resulting source I'd recommend setting up Prettier in your project (if you did not already) as the CLI now reads the projects Prettier configuration and applies it upon file creation.

This is the configuration I tested with:

{
    "trailingComma": "es5",
    "tabWidth": 4, // default is 2
    "semi": true,
    "singleQuote": false,
}

NOTE: cds2types is not only able to use the trailingComma rule but ingests the complete configuration and makes use of it.

I hope this fixes the underlying problem. If problems arise from the new version please respond to this issue.

@mrbandler mrbandler self-assigned this Jan 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants