Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Starting in 0.48.0, the library appears to not fully support the use case of formating kotlin snippets outside of any filesystem #1738

Closed
mgroth0 opened this issue Dec 20, 2022 · 2 comments · Fixed by #1746
Milestone

Comments

@mgroth0
Copy link

mgroth0 commented Dec 20, 2022

Copied and pasted from Slack:

I am trying to upgrade from 0.47.1 to 0.48.0. I am usiing the API to format the code contained in strings (no files). I used to use this for .kts kotlin code:

ExperimentalParams(
		  text = code,
		  script = true,
		  cb = { _: LintError, _: Boolean ->     
           } 
)

But now that this is deprecated, its unclear how I would format kts code. KtLintRuleEngine.format internally creates a Code instance as seen below:

    public fun format(
        code: String,
        filePath: Path? = null,
        callback: (LintError, Boolean) -> Unit = { _, _ -> },
    ): String =
        format(
            Code(
                text = code,
                fileName = filePath?.absolutePathString(),
            ),
            callback,
        )

The script property of Code is false by default. However, both the function above and the Code class is internal. So I don’t know what the proper way is to format .kts code strings since I can’t find a public property or argument for setting script to true any more.

@paul-dingemans on slack answered:

If I am correct you can just provide any filename ending with “.kts”. It is not needed that the file really exists. Problem with having both a filename and script property was that they could be inconsistent, like file “something.kt” with “script = true”.

However in my personal opinion, I think this is not ideal because passing a file path suggests that some IO might occur. In cases like mine where I am just formatting a string snippet of kts code with no associated file or project, this doesn't make sense. The function KtLintRuleEngine.format says '.editorconfig' files on the path are taken into account but this is not desirable for cases where snippets of kotlin code existing outside any file or project are being linted or formated.

@mgroth0 mgroth0 changed the title Migrating from 0.47.1 to 0.48.0 for formating ".kts" strings (non-files) is confusing Starting in 0.48.0, the library appears to not fully support the use case of formating kotlin snippets outside of any filesystem Dec 20, 2022
@paul-dingemans paul-dingemans added this to the 0.48.1 milestone Dec 20, 2022
@paul-dingemans
Copy link
Collaborator

In 0.48.1 either clarify API documentation or provide a new API to format code snippets (without setting filename/path).

paul-dingemans added a commit to paul-dingemans/ktlint that referenced this issue Dec 24, 2022
…t snippet without having to specify a file path

Closes pinterest#1738
paul-dingemans added a commit that referenced this issue Dec 27, 2022
* Add API so that KtLint API consumer is able to process a Kotlin script snippet without having to specify a file path

Closes #1738
@mgroth0
Copy link
Author

mgroth0 commented Jan 15, 2023

Thanks @paul-dingemans !

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants