Giter Site home page Giter Site logo

dprint / dprint-plugin-typescript Goto Github PK

View Code? Open in Web Editor NEW
238.0 7.0 48.0 2.23 MB

TypeScript and JavaScript code formatting plugin for dprint.

Home Page: https://dprint.dev/plugins/typescript

License: MIT License

Rust 99.59% JavaScript 0.22% TypeScript 0.19%

dprint-plugin-typescript's Introduction

dprint-plugin-typescript

CI

TypeScript formatting plugin for dprint.

This uses the swc parser for TypeScript written in Rust (it's super fast).

Install

See the GitHub releases.

Development

The tests are in the ./tests/specs folder. To run the tests, run cargo test.

Building Wasm file

You may wish to try out the plugin by building from source:

  1. Run cargo build --target wasm32-unknown-unknown --release --features "wasm"
  2. Reference the file at ./target/wasm32-unknown-unknown/release/dprint_plugin_typescript.wasm in a dprint configuration file.

dprint-plugin-typescript's People

Contributors

abextm avatar alcalzone avatar andersk avatar bartlomieju avatar casieber avatar davo-canva avatar declanvong avatar dsherret avatar ekohilas avatar jakebailey avatar joscha avatar joshleeb avatar kachick avatar kdy1 avatar kitsonk avatar liamolucko avatar lucacasonato avatar luisherranz avatar magic-akari avatar magurotuna avatar marvinhagemeister avatar nicolo-ribaudo avatar ryan-rushton avatar sonic12040 avatar stagas avatar terrorjacktyl avatar thristhart avatar todor-a avatar ty3uk avatar upsuper avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

dprint-plugin-typescript's Issues

Configuration for placing logical/binary operators on previous or next line

For example, right now the default does this:

const mathResult = 1 + 2 * 6
    + moreMath * math;

But maybe someone would prefer:

const mathResult = 1 + 2 * 6 +
    moreMath * math;

The default should remain the same since it's easier to read though.

Probably "newLineOperatorPosition" or just "operatorPosition" with options "sameLine" and "nextLine" (or maybe "preferNextLine").

Also applies to ternary operators.

Configuration for using braces in a switch statement

Some example code:

switch (test) {
    case 5: { return 5; }
    case 6: {
        return 6;
    }
    case 7:
        return 7;
}

This would tie into the "useBraces" configuration and could be turned off by setting switchCase.useBraces to false.

Force multi-line arguments sometimes causes same call to appear twice

Format jsdocs

JSDocs should be formatted.

  • Configuration to force single line JS docs when able (default).

Add preferHanging configuration for expressions

Right now by default, array literals will be hanging unless they are explicitly made multi-line. This should be changed to be multi-line by default and work similarly to the new preferHangingArguments. The code between these two areas should be shared as well because arrays are comma separated values.

Add preferHanging configuration for statements

Building on #173.

Applies to:

  • If statements
  • While statements
  • For/for in/for of statements
  • Do while statements
  • Maybe more I can't remember off the top of my head at the moment :)

For example, the following:

if (testingThisOutWith && someIdentifiersThatAreReallyLong && goingFurtherAndFruther) {
}

Currently formats like so by default:

if (testingThisOutWith && someIdentifiersThatAreReallyLong
    && goingFurtherAndFruther)
{
}

But instead it should only format that way when its preferHanging configuration is true. By default, it should format like this:

if (
    testingThisOutWith && someIdentifiersThatAreReallyLong
    && goingFurtherAndFruther
) {
}

...and would format like so with #169:

if (
    testingThisOutWith
    && someIdentifiersThatAreReallyLong
    && goingFurtherAndFruther
) {
}

Ability to increase line width of a node

It would be cool if in certain cases you could just say "I don't care... format this part, but let it go long".

Something like:

// dprint-line-width: 180

Edit: I think this would just need to be something like Signal::StartLineWidth(180) then Signal::EndLineWidth (stored in a stack with the first line width as the first item... the stack should never be empty)

Remove `useSpaceSeparators` and only have per ast node configuration

It would be better to remove the global useSpaceSeparators because I don't want certain behaviour by default.

For example, I like a space after an if keyword:

if (true) {}

But I don't like it after the function name:

function test () {
}

I also don't like it after the new keyword in constructor types and constructor function signatures:

new (...etc...)

useSpaceSeparators implies that there should be a space in both those cases.


Instead of the above, leave it to just having per AST node configuration.

Relates to #164.

Rename useSpaces

Too similar to useTabs. Probably useSpaceSeparators would be more clear.

Missing semi-colon in export default statement in certain cases

Incorrect newlines for binary expression with === operator

preferHangingArguments: true - Should make multiline in certain scenarios

The following:

it("should get the class descendants", () => {
    doTest("class Base {} class Child1 extends Base {} class Child2 extends Base {} class Grandchild1<T> extends Child1 {} class GreatGrandChild1<T> extends Grandchild1<T> {}",
        "Base", ["Child1", "Child2", "Grandchild1", "GreatGrandChild1"]);
});

Should not format as this:

it("should get the class descendants", () => {
    doTest(
    "class Base {} class Child1 extends Base {} class Child2 extends Base {} class Grandchild1<T> extends Child1 {} class GreatGrandChild1<T> extends Grandchild1<T> {}",
        "Base", ["Child1", "Child2", "Grandchild1", "GreatGrandChild1"]);
});

...when preferHangingArguments is true. Instead it should just make that multi-line.

Error about symbol being thrown

Declare keyword erroneously added in ambient namespace

declare module "@dsherret/to-absolute-glob" {
    function toAbsoluteGlob(pattern: string, options?: toAbsoluteGlob.Options): string;
    namespace toAbsoluteGlob {
        interface Options {
            cwd?: string;
            root?: string;
        }
    }
    export = toAbsoluteGlob;
}

Formatted as:

declare module "@dsherret/to-absolute-glob" {
    function toAbsoluteGlob(pattern: string, options?: toAbsoluteGlob.Options): string;
    declare namespace toAbsoluteGlob {
        declare interface Options {
            cwd?: string;
            root?: string;
        }
    }
    export = toAbsoluteGlob;
}

Add spaceAfterTaggedTemplate

Description

It'd be nice if there is a way to customize how space is added in tagged template.

Actual sample code

function html() {}

a = html`<div>${run()}</div>`;

Formatted sample code

function html() {}

a = html `<div>${run()}</div>`; // Space is added in between `html` keyword and the template literal

Add configuration for forcing comment text to fit in line width

Right now comments are left as-is other than the space added on single line comments, but it might be good to add a configuration option that will split them up when they exceed the line width. I'm not sure if this should apply to trailing comments, but it should apply to ones on their own line.

Idea provided to me by @macterra while I was talking to him about this.

Also, would be cool to maybe have some kind of balancing in order to make the right side look somewhat justified.

Line wrapping for object arguments

For a column limit of 80:

f("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", {
  a: 1
});

would get unnecessarily wrapped to

f(
  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  {
    a: 1
  }
);

which doesn't look good for options object functions. This is unlike prettier.

Fix comments inside empty braces and brackets

Need to fix comments in empty braces and brackets (ex. [] or {}). Pretty sure that’s not working now in some scenarios based on looking at the code today while fixing something else. A common implementation of this would be nice.

Configuration to put params/args on newlines instead of hanging

Right now the default behaviour is to prefer hanging indentation:

funcCall(testing, testing, testing,
    testing, testing);

And someone can break that up by adding a newline after the first arg:

funcCall(
testing, testing, testing,
    testing, testing);

// formats as
funcCall(
    testing,
    testing,
    testing,
    testing,
    testing
);

But it would be nice if there was a setting so that it will always break it up to a newline when hanging.

Not sure what this should be called (and should it apply to union/intersection types and logical/binary expressions? Obviously there would be control for each kind of node)

  • preferHanging - Current behaviour.
  • forceNewlinesIfHanging - Forces newlines when it exceeds the line width. (Or maybe forceMultiLineIfHanging?)

Switch statement with braces should format with brace on same line

Blank lines option

Is possible to have an option to add blank line rule(s) in the formatter?
Eg transform this

class Temp {
    @Dec1
    prop1! int;
    @Dec2
    prop2! string;
}

to this:

class Temp {
    @Dec1
    prop1! int;

    @Dec2
    prop2! string;
}

or this:

class Temp {

    @Dec1
    prop1! int;

    @Dec2
    prop2! string;

}

Configuration for operator position

This is to control whether the user wants:

const test = 5 + 7 +
    4;
// or
const test = 5 + 7
    + 4;

operatorPosition: "sameLine" | "nextLine" (defaults to "nextLine")

Space Configuration

Building on #166.

It would be good to implement all the following scenarios and way more:

// from typescript compiler api (typescript.d.ts)
readonly insertSpaceAfterCommaDelimiter?: boolean;
readonly insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis?: boolean;
readonly insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets?: boolean;
readonly insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces?: boolean;
readonly insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces?: boolean;

// done
readonly insertSpaceBeforeAndAfterBinaryOperators?: boolean;
readonly insertSpaceAfterKeywordsInControlFlowStatements?: boolean;
readonly insertSpaceAfterFunctionKeywordForAnonymousFunctions?: boolean;
readonly insertSpaceBeforeFunctionParenthesis?: boolean;
readonly insertSpaceAfterTypeAssertion?: boolean;
readonly insertSpaceBeforeTypeAnnotation?: boolean;
readonly insertSpaceAfterSemicolonInForStatements?: boolean;
readonly insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces?: boolean;
readonly insertSpaceAfterConstructor?: boolean;

Single line if statement with braces should not go to multi-line when useBraces: always and singleBodyPosition: sameLine

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.