Coder Social home page Coder Social logo

dataclass-scalafix's People

Contributors

andrelfpinto avatar eed3si9n avatar hamnis avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

dataclass-scalafix's Issues

since annotation is not supported

https://github.com/alexarchambault/data-class#adding-fields

steps

package lmcoursier.credentials

import dataclass._

@data class DirectCredentials(
  host: String = "",
  username: String = "",
  password: String = "",
  @since
  realm: Option[String] = None,
  @since
  optional: Boolean = true,
  @since
  matchHost: Boolean = false,
  httpsOnly: Boolean = true
) {
  override def toString(): String = s"DirectCredentials(host=$host, username=$username)"
}

problem

Here's the generated code:

object DirectCredentials {
def apply(host: String, username: String, password: String, @since realm: Option[String], @since optional: Boolean, @since matchHost: Boolean, httpsOnly: Boolean): DirectCredentials = new DirectCredentials(host, username, password, realm, optional, matchHost, httpsOnly)
def apply(): DirectCredentials = new DirectCredentials("", "", "", None, true, false, true)
}

expectation

@since or @since("1.0.0") denotes newly introduced fields, and there should be def apply(...) without them, which helps to keep binary compatible API.

Doesn't work with methods that use withX

steps

package lmcoursier.definitions

import dataclass._

@data class Authentication(
  user: String,
  password: String,
) {
  override def toString(): String =
    withPassword("****")
      .productIterator
      .mkString("Authentication(", ", ", ")")
}

problem

This fails at the step where semanticdb is produced for the original file:

[error] /Users/xxx/work/sbt-coursier/modules/definitions/src/main/scala/lmcoursier/definitions/Authentication.scala:16:5: not found: value withPassword
[error]     withPassword("****")
[error]     ^

Reuse existing companion object

Minimized code

import dataclass.data

@data class Foo(bar: String)

object Foo {
  val baz = "baz"
}

Output

import dataclass.data

@data final class Foo private (val bar: String)
    extends Product
    with Serializable {

  ...

}

object Foo {
  def apply(bar: String): Foo = new Foo(bar)
}

object Foo {
  val baz = "baz"
}

Expectation

import dataclass.data

@data final class Foo private (val bar: String)
    extends Product
    with Serializable {

  ...

}

object Foo {
  val baz = "baz"

  def apply(bar: String): Foo = new Foo(bar)
}

Doesn't work with Scala 2.12

steps

Use Scala 2.12

package lmcoursier.definitions

import dataclass._

@data class Strict(
  include: Set[(String, String)] = Set(("*", "*")),
  exclude: Set[(String, String)] = Set.empty,
) {
}

problems

[error] /Users/xxxx/work/sbt-coursier/modules/lm-coursier/target/scala-2.12/src_managed/main/scala/lmcoursier/definitions/Strict.scala:37:14: method productElementName overrides nothing
[error] override def productElementName(n: Int) = n match {
[error]              ^
[error] /Users/xxxx/work/sbt-coursier/modules/lm-coursier/target/scala-2.12/src_managed/main/scala/lmcoursier/definitions/Strict.scala:51:14: method productElementNames overrides nothing
[error] override def productElementNames = {
[error]              ^

Create sbt plugin

to make this easier to use we should consider adding a plugin for this that just invokes scalafix correctly.

Doesn't work with Scala 2.13 or 3

Ref #4

steps

Use Scala 3

package lmcoursier.definitions

import dataclass._

@data class Strict(
  include: Set[(String, String)] = Set(("*", "*")),
  exclude: Set[(String, String)] = Set.empty,
) {
}

problems

[error] -- [E164] Declaration Error: /Users/xxx/work/sbt-coursier/modules/lm-coursier/target/scala-3.1.3/src_managed/main/scala/lmcoursier/definitions/Strict.scala:37:4
[error] 37 |def productElementName(n: Int) = n match {
[error]    |    ^
[error]    |error overriding method productElementName in trait Product of type (n: Int): String;
[error]    |  method productElementName of type (n: Int): String needs `override` modifier
[error] -- [E164] Declaration Error: /Users/xxx/work/sbt-coursier/modules/lm-coursier/target/scala-3.1.3/src_managed/main/scala/lmcoursier/definitions/Strict.scala:51:4
[error] 51 |def productElementNames = {
[error]    |    ^
[error]    |error overriding method productElementNames in trait Product of type => Iterator[String];
[error]    |  method productElementNames of type => Iterator[String] needs `override` modifier

notes

Using Scalafix from sbt, I am guessing that the rule will always use the same version as the metabuild, in this case Scala 2.12.
I don't know if there's a way to detect that the target dialect should be Scala 3.
https://github.com/scalacenter/scalafix/blob/main/scalafix-core/src/main/scala/scalafix/v1/SemanticDocument.scala doesn't seem to contain Scala version.

Maven artifact

Thanks for working on this!
Do you mind publishing this to Maven Central?

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.