hamnis / dataclass-scalafix Goto Github PK
View Code? Open in Web Editor NEWLicense: Apache License 2.0
License: Apache License 2.0
Ref #4
Use Scala 3
package lmcoursier.definitions
import dataclass._
@data class Strict(
include: Set[(String, String)] = Set(("*", "*")),
exclude: Set[(String, String)] = Set.empty,
) {
}
[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
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.
import dataclass.data
@data class Foo(bar: String)
object Foo {
val baz = "baz"
}
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"
}
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)
}
package lmcoursier.definitions
import dataclass._
@data class Authentication(
user: String,
password: String,
) {
override def toString(): String =
withPassword("****")
.productIterator
.mkString("Authentication(", ", ", ")")
}
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] ^
we dont really need the @data
annotation after the fact.
Thanks for working on this!
Do you mind publishing this to Maven Central?
to make this easier to use we should consider adding a plugin for this that just invokes scalafix correctly.
https://github.com/alexarchambault/data-class#adding-fields
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)"
}
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)
}
@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.
Use Scala 2.12
package lmcoursier.definitions
import dataclass._
@data class Strict(
include: Set[(String, String)] = Set(("*", "*")),
exclude: Set[(String, String)] = Set.empty,
) {
}
[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] ^
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.