Skip to content

Commit

Permalink
Merge pull request #170 from ScalablyTyped/make-implicit-builders-def…
Browse files Browse the repository at this point in the history
…ault

make enableImplicitOps the default encoding, remove possibility of us…
  • Loading branch information
oyvindberg committed Jul 2, 2020
2 parents c1d4b16 + 4627baa commit 784a965
Show file tree
Hide file tree
Showing 13 changed files with 35 additions and 55 deletions.
9 changes: 5 additions & 4 deletions cli/src/main/scala/org/scalablytyped/converter/cli/Main.scala
Expand Up @@ -48,7 +48,6 @@ object Main {
expandTypeMappings = EnabledTypeMappingExpansion.DefaultSelection,
versions = Versions(Versions.Scala213, Versions.ScalaJs1),
organization = "org.scalablytyped",
enableImplicitOps = false,
)

case class Config(
Expand Down Expand Up @@ -164,8 +163,11 @@ object Main {
.action((x, c) => c.mapConversion(_.copy(ignoredLibs = x.toSet)))
.text(s"Libraries you want to ignore"),
opt[Boolean]("experimentalEnableImplicitOps")
.action((x, c) => c.mapConversion(_.copy(enableImplicitOps = x)))
.text(s"Enable implicit ops"),
.action { (x, c) =>
logger.warn("--experimentalEnableImplicitOps has no effect since it became the default encoding")
c
}
.text(s"Deprecated, doesn't do anything anymore"),
opt[ProjectName]("publish-to-bintray-repo")
.action((x, c) => c.copy(publishBintrayRepo = Some(x)))
.text(
Expand Down Expand Up @@ -247,7 +249,6 @@ object Main {
// "ignoredModulePrefixes" -> conversion.ignoredModulePrefixes.toString,
"versions" -> conversion.versions.toString,
"organization" -> conversion.organization,
"experimentalEnableImplicitOps" -> conversion.enableImplicitOps.toString,
),
)

Expand Down
6 changes: 0 additions & 6 deletions docs/conversion-options.md
Expand Up @@ -3,12 +3,6 @@ id: conversion-options
title: Customize the generated code.
---

### `stExperimentalEnableImplicitOps`
Enable this to get builder-like functionality for generated companion objects.
This will be the default soon, and it'll be better incorporated into the documentation.

For now see [release notes for 1.0.0-beta13](https://github.com/ScalablyTyped/Converter/releases/tag/v1.0.0-beta13)

### `stIgnore`
There are a few reasons you might want to ignore things:
- A dependency of a library you want to use might fail conversion/compilation and you can try to salvage the situation by ignoring it.
Expand Down
Expand Up @@ -16,18 +16,17 @@ case class ConversionOptions(
ignoredModulePrefixes: Set[List[String]],
versions: Versions,
organization: String,
enableImplicitOps: Boolean,
) {
val flavourImpl: flavours.FlavourImpl =
flavour match {
case Flavour.Normal =>
flavours.NormalFlavour(useScalaJsDomTypes, enableImplicitOps, outputPackage)
flavours.NormalFlavour(useScalaJsDomTypes, outputPackage)
case Flavour.Slinky =>
flavours.SlinkyFlavour(outputPackage, enableImplicitOps)
flavours.SlinkyFlavour(outputPackage)
case Flavour.SlinkyNative =>
flavours.SlinkyNativeFlavour(outputPackage, enableImplicitOps)
flavours.SlinkyNativeFlavour(outputPackage)
case Flavour.Japgolly =>
flavours.JapgollyFlavour(outputPackage, enableImplicitOps)
flavours.JapgollyFlavour(outputPackage)
}

}
Expand Down
Expand Up @@ -126,8 +126,7 @@ object Ci {
if (flags contains "-scala212") Versions.Scala212 else Versions.Scala213,
if (flags contains ("-scalajs06")) Versions.ScalaJs06 else Versions.ScalaJs1,
),
organization = organization,
enableImplicitOps = flags contains "-enableImplicitOps",
organization = organization,
),
wantedLibs = wantedLibNames,
enablePublish = flags contains "-publish",
Expand Down
Expand Up @@ -126,11 +126,7 @@ trait ImporterHarness extends AnyFunSuite {
testName: String,
pedantic: Boolean,
update: Boolean,
flavour: FlavourImpl = NormalFlavour(
shouldUseScalaJsDomTypes = false,
enableImplicitOps = true,
outputPkg = Name.typings,
),
flavour: FlavourImpl = NormalFlavour(shouldUseScalaJsDomTypes = false, outputPkg = Name.typings),
): Assertion = {
val testFolder = findTestFolder(testName)
val source = InFolder(testFolder.path / 'in)
Expand Down
Expand Up @@ -2,14 +2,14 @@ package org.scalablytyped.converter.internal
package importer

import org.scalablytyped.converter.internal.scalajs.Name
import org.scalablytyped.converter.internal.scalajs.flavours.{FlavourImpl, JapgollyFlavour, SlinkyFlavour}
import org.scalablytyped.converter.internal.scalajs.flavours.{JapgollyFlavour, SlinkyFlavour}
import org.scalatest.ParallelTestExecution
import org.scalatest.funsuite.AnyFunSuite

class ImporterTest extends AnyFunSuite with ImporterHarness with ParallelTestExecution {
val update = !constants.isCi
val Slinky = SlinkyFlavour(Name("typingsSlinky"), enableImplicitOps = true)
val Japgolly = JapgollyFlavour(Name("typingsJapgolly"), enableImplicitOps = true)
val Slinky = SlinkyFlavour(Name("typingsSlinky"))
val Japgolly = JapgollyFlavour(Name("typingsJapgolly"))

test("augment-module")(assertImportsOk("augment-module", pedantic = false, update = update))
test("typings-json")(assertImportsOk("typings-json", pedantic = true, update = update))
Expand Down
Expand Up @@ -139,7 +139,6 @@ object ImportTypingsGenSources {
Set(),
Versions(Versions.Scala213, Versions.ScalaJs1),
"org.scalablytyped",
enableImplicitOps = true,
)

println(
Expand Down
Expand Up @@ -100,6 +100,7 @@ object ScalablyTypedPluginBase extends AutoPlugin {

val stInternalExpandTypeMappings = settingKey[Selection[String]]("Experimental: enable type mapping expansion")

@deprecated("This setting is now does nothing, because it became the default encoding")
val stExperimentalEnableImplicitOps = settingKey[Boolean]("implicit ops for most traits")
}

Expand All @@ -125,7 +126,7 @@ object ScalablyTypedPluginBase extends AutoPlugin {
stStdlib := List("es6"),
stTypescriptVersion := "3.8",
stUseScalaJsDom := true,
stExperimentalEnableImplicitOps := false,
stExperimentalEnableImplicitOps := true,
stConversionOptions := {
val versions = Versions(
Versions.Scala(scalaVersion = (Compile / Keys.scalaVersion).value),
Expand All @@ -152,7 +153,6 @@ object ScalablyTypedPluginBase extends AutoPlugin {
ignoredModulePrefixes = ignored.map(_.split("/").toList),
versions = versions,
organization = organization,
enableImplicitOps = stExperimentalEnableImplicitOps.value,
)
},
)
Expand Down
Expand Up @@ -9,7 +9,7 @@ import org.scalablytyped.converter.internal.scalajs.flavours.FindProps.Res
/**
* Add a companion object to `@ScalaJSDefined` traits for creating instances with method syntax
*/
final class GenCompanions(findProps: FindProps, enableImplicitOps: Boolean) extends TreeTransformation {
final class GenCompanions(findProps: FindProps) extends TreeTransformation {
override def leaveContainerTree(scope: TreeScope)(container: ContainerTree): ContainerTree =
// Native JS objects cannot contain inner Scala traits, classes or objects (i.e., not extending js.Any)
if (scope.stack.exists { case mod: ModuleTree => mod.isNative; case _ => false })
Expand All @@ -28,18 +28,15 @@ final class GenCompanions(findProps: FindProps, enableImplicitOps: Boolean) exte
val clsRef = TypeRef(cls.codePath, asTypeArgs(unboundedTParams), NoComments)

val geneatedImplicitOps: Option[ClassTree] =
if (!enableImplicitOps) None
else {
findProps.forClassTree(
cls = cls,
scope = scope / cls,
maxNum = Int.MaxValue,
acceptNativeTraits = false,
selfRef = clsRef,
) match {
case Res.One(_, props) if props.nonEmpty => GenImplicitOpsClass(cls, props, cls.codePath, scope)
case _ => None
}
findProps.forClassTree(
cls = cls,
scope = scope / cls,
maxNum = Int.MaxValue,
acceptNativeTraits = false,
selfRef = clsRef,
) match {
case Res.One(_, props) if props.nonEmpty => GenImplicitOpsClass(cls, props, cls.codePath, scope)
case _ => None
}

val generatedCreators: IArray[Tree] =
Expand All @@ -55,9 +52,7 @@ final class GenCompanions(findProps: FindProps, enableImplicitOps: Boolean) exte

case Res.One(_, props) if props.isEmpty => Empty
case Res.One(_, props) =>
val requiredProps =
if (enableImplicitOps) props.filter(_.optionality === Optionality.No)
else props
val requiredProps = props.filter(_.optionality === Optionality.No)

IArray.fromOptions(
Some(generateCreator(Name.APPLY, requiredProps, cls.codePath, unboundedTParams))
Expand All @@ -69,9 +64,7 @@ final class GenCompanions(findProps: FindProps, enableImplicitOps: Boolean) exte
propsMap.toIArray.mapNotNone {
case (_, props) if props.isEmpty => None
case (propsRef, props) =>
val requiredProps =
if (enableImplicitOps) props.filter(_.optionality === Optionality.No)
else props
val requiredProps = props.filter(_.optionality === Optionality.No)

Some(generateCreator(propsRef.name, requiredProps, cls.codePath, unboundedTParams))
.filter(_.params.nonEmpty)
Expand Down
Expand Up @@ -4,13 +4,13 @@ package flavours

import org.scalablytyped.converter.internal.scalajs.transforms.{Adapter, CleanIllegalNames}

case class JapgollyFlavour(outputPkg: Name, enableImplicitOps: Boolean) extends FlavourImplReact {
case class JapgollyFlavour(outputPkg: Name) extends FlavourImplReact {
override val dependencies = Set(Versions.runtime, Versions.scalajsReact)
val rewriter = new CastConversion.TypeRewriterCast(JapgollyTypeConversions(reactNames, scalaJsDomNames))
val memberToPro = new JapgollyMemberToProp(reactNames, rewriter)
val findProps = new FindProps(new CleanIllegalNames(outputPkg), memberToPro, parentsResolver)
val genComponents = new JapgollyGenComponents(reactNames, findProps)
val genCompanions = new GenCompanions(findProps, enableImplicitOps)
val genCompanions = new GenCompanions(findProps)

final override def rewrittenTree(scope: TreeScope, tree: PackageTree): PackageTree = {
val withCompanions = genCompanions.visitPackageTree(scope)(tree)
Expand Down
Expand Up @@ -7,7 +7,6 @@ import org.scalablytyped.converter.internal.scalajs.transforms.CleanIllegalNames

case class NormalFlavour(
shouldUseScalaJsDomTypes: Boolean,
enableImplicitOps: Boolean,
outputPkg: Name,
) extends FlavourImplReact {

Expand All @@ -16,7 +15,7 @@ case class NormalFlavour(
override val rewritesOpt = if (shouldUseScalaJsDomTypes) Some(new TypeRewriterCast(scalaJsDomNames.All)) else None
val memberToProp = new MemberToProp.Default(rewritesOpt)
val findProps = new FindProps(new CleanIllegalNames(outputPkg), memberToProp, parentsResolver)
val genCompanions = new GenCompanions(findProps, enableImplicitOps)
val genCompanions = new GenCompanions(findProps)

final override def rewrittenTree(scope: TreeScope, tree: PackageTree): PackageTree = {
val withCompanions = genCompanions.visitPackageTree(scope)(tree)
Expand Down
Expand Up @@ -5,13 +5,13 @@ package flavours
import org.scalablytyped.converter.internal.scalajs.flavours.CastConversion.TypeRewriterCast
import org.scalablytyped.converter.internal.scalajs.transforms.{Adapter, CleanIllegalNames}

case class SlinkyFlavour(outputPkg: Name, enableImplicitOps: Boolean) extends FlavourImplReact {
case class SlinkyFlavour(outputPkg: Name) extends FlavourImplReact {

override val dependencies = Set(Versions.runtime, Versions.slinkyWeb)
val rewriter = new TypeRewriterCast(SlinkyTypeConversions(scalaJsDomNames, reactNames, isWeb = true))
val memberToProp = new MemberToProp.Default(Some(rewriter))
val findProps = new FindProps(new CleanIllegalNames(outputPkg), memberToProp, parentsResolver)
val genCompanions = new GenCompanions(findProps, enableImplicitOps)
val genCompanions = new GenCompanions(findProps)
val genStBuildingComponent = new SlinkyGenStBuildingComponent(outputPkg)

/* we need the actual typescript react definitions at runtime to compute this lazily */
Expand Down
Expand Up @@ -5,7 +5,7 @@ package flavours
import org.scalablytyped.converter.internal.scalajs.flavours.CastConversion.TypeRewriterCast
import org.scalablytyped.converter.internal.scalajs.transforms.{Adapter, CleanIllegalNames}

case class SlinkyNativeFlavour(outputPkg: Name, enableImplicitOps: Boolean) extends FlavourImplReact {
case class SlinkyNativeFlavour(outputPkg: Name) extends FlavourImplReact {

override val dependencies: Set[Dep] =
Set(Versions.runtime, Versions.slinkyNative, Versions.scalaJsDom)
Expand All @@ -15,7 +15,7 @@ case class SlinkyNativeFlavour(outputPkg: Name, enableImplicitOps: Boolean) exte
val findProps = new FindProps(new CleanIllegalNames(outputPkg), memberToProp, parentsResolver)
val genStBuildingComponent = new SlinkyGenStBuildingComponent(outputPkg)
val gen = new SlinkyGenComponents(SlinkyGenComponents.Native(()), findProps, genStBuildingComponent, reactNames)
val genCompanions = new GenCompanions(findProps, enableImplicitOps)
val genCompanions = new GenCompanions(findProps)

final override def rewrittenTree(scope: TreeScope, tree: PackageTree): PackageTree = {
val withCompanions = genCompanions.visitPackageTree(scope)(tree)
Expand Down

0 comments on commit 784a965

Please sign in to comment.