Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

Named imports formatting #2441

Closed
Tracked by #2403
MichaReiser opened this issue Apr 14, 2022 · 2 comments · Fixed by #2498
Closed
Tracked by #2403

Named imports formatting #2441

MichaReiser opened this issue Apr 14, 2022 · 2 comments · Fixed by #2498
Assignees
Labels
A-Formatter Area: formatter I-Normal Implementation: normal understanding of the tool and awareness

Comments

@MichaReiser
Copy link
Contributor

MichaReiser commented Apr 14, 2022

Prettier breaks the named imports if the whole import statements exceed the line width, Rome only does so if the named imports exceed the line width Playground

Input

import somethingSuperLongsomethingSuperLong from 'somethingSuperLongsomethingSuperLongsomethingSuperLong'
import {somethingSuperLongsomethingSuperLong1} from 'somethingSuperLongsomethingSuperLongsomethingSuperLong'
import a234556666666, {somethingSuperLongsomethingSuperLong2} from 'somethingSuperLongsomethingSuperLongsomethingSuperLong'
import {a234556666666dddddddddddddddddd, somethingSuperLongsomethingSuperLong3} from 'somethingSuperLongsomethingSuperLongsomethingSuperLong'

Prettier

import somethingSuperLongsomethingSuperLong from "somethingSuperLongsomethingSuperLongsomethingSuperLong";
import { somethingSuperLongsomethingSuperLong1 } from "somethingSuperLongsomethingSuperLongsomethingSuperLong";
import a234556666666, {
  somethingSuperLongsomethingSuperLong2,
} from "somethingSuperLongsomethingSuperLongsomethingSuperLong";
import {
  a234556666666dddddddddddddddddd,
  somethingSuperLongsomethingSuperLong3,
} from "somethingSuperLongsomethingSuperLongsomethingSuperLong";

Rome

import somethingSuperLongsomethingSuperLong from "somethingSuperLongsomethingSuperLongsomethingSuperLong";
import { somethingSuperLongsomethingSuperLong1 } from "somethingSuperLongsomethingSuperLongsomethingSuperLong";
import a234556666666, { somethingSuperLongsomethingSuperLong2 } from "somethingSuperLongsomethingSuperLongsomethingSuperLong";
import {
  a234556666666dddddddddddddddddd,
  somethingSuperLongsomethingSuperLong3,
} from "somethingSuperLongsomethingSuperLongsomethingSuperLong";

Expected

Rome's formatting to match Prettier's

@IWANABETHATGUY
Copy link
Contributor

I translate the format IR to rust code as below,

use rome_formatter::{
    format_element::ConditionalGroupContent, hard_line_break, indent, soft_block_indent,
    soft_line_break_or_space, space_token, LineWidth,
};

fn main() {
    use rome_formatter::{
        format_elements, group_elements, soft_line_break, token, FormatOptions, Formatted,
    };

    let elements = group_elements(format_elements![
        token("{"),
        soft_block_indent(format_elements![
            soft_line_break_or_space(),
            token("somethingSuperLongsomethingSuperLong2"),
            // space_token(),
            ConditionalGroupContent::new(
                token(","),
                rome_formatter::format_element::GroupPrintMode::Multiline
            )
        ]),
        token("}"),
        space_token(),
        token("from"),
        space_token(),
        token("\"somethingSuperLongsomethingSuperLongsomethingSuperLong\""),
        token(";"),
    ]);

    //     List [
    //     SyntaxTokenSlice("import"),
    //     Space,
    //     SyntaxTokenSlice("ww"),
    //     SyntaxTokenSlice(","),
    //     Space,
    //     Group(
    //         List [
    //             SyntaxTokenSlice("{"),
    //             Indent(
    //                 List [
    //                     Line(SoftOrSpace),
    //                     SyntaxTokenSlice("somethingSuperLongsomethingSuperLong2"),
    //                     ConditionalGroupContent {
    //                         content: StaticToken(","),
    //                         mode: Multiline,
    //                     },
    //                 ],
    //             ),
    //             Line(SoftOrSpace),
    //             SyntaxTokenSlice("}"),
    //         ],
    //     ),
    //     Space,
    //     SyntaxTokenSlice("from"),
    //     Space,
    //     DynamicToken("\"somethingSuperLongsomethingSuperLongsomethingSuperLong\""),
    //     StaticToken(";"),
    //     SyntaxTokenSlice(""),
    //     Line(Hard),
    // ]
    let elements = format_elements![
        token("import"),
        space_token(),
        token("ww"),
        token(","),
        space_token(),
        elements,
        // hard_line_break()
    ];
    println!(
        "{}",
        Formatted::new(elements, FormatOptions::default())
            .print()
            .as_code()
    );
    // assert_eq!(
    //     "a,b",
    //     Formatted::new(elements, FormatOptions::default())
    //         .print()
    //         .as_code()
    // );
}

It prints

import ww, {
        somethingSuperLongsomethingSuperLong2,
} from "somethingSuperLongsomethingSuperLongsomethingSuperLong";

when I adjust format_width to 114 it prints

import ww, { somethingSuperLongsomethingSuperLong2} from "somethingSuperLongsomethingSuperLongsomethingSuperLong";

it should be the same as prettier, but I don't know how to apply it to our formatter code.

@IWANABETHATGUY
Copy link
Contributor

#2498
I make a draft here, could you help me find what I did wrong?

@ematipico ematipico added the I-Normal Implementation: normal understanding of the tool and awareness label May 5, 2022
@ematipico ematipico moved this to Done in Rome 2022 Jun 6, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A-Formatter Area: formatter I-Normal Implementation: normal understanding of the tool and awareness
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

3 participants