Skip to content

Commit

Permalink
Support for overriding upstream types in eqwalizer_types.erl
Browse files Browse the repository at this point in the history
Summary: Use module `eqwalizer_types` to override any upstream type, similar to how `eqwalizer_specs` is used.

Reviewed By: ilya-klyuchnikov

Differential Revision: D65831604

fbshipit-source-id: 4e6f1c30412d6e5f991c91e001cd81f395cc872f
  • Loading branch information
VLanvin authored and facebook-github-bot committed Nov 13, 2024
1 parent b7c5d4c commit 759af3c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* Copyright (c) Meta Platforms, Inc. and affiliates. All rights reserved.
*
* This source code is licensed under the Apache 2.0 license found in
* the LICENSE file in the root directory of this source tree.
*/

package com.whatsapp.eqwalizer.ast.stub

import com.whatsapp.eqwalizer.ast.Forms.TypeDecl
import com.whatsapp.eqwalizer.ast.Id

private object CustomTypes {
private lazy val customTypes: Map[String, Map[Id, TypeDecl]] =
Db.getModuleStub("eqwalizer_types") match {
case Some(stub) =>
stub.types.toList
.map { case (id, ty) =>
val Array(module, tyName) = id.name.split(":")
val id1 = Id(tyName, id.arity)
val ty1 = ty.copy(id = id1)(ty.pos)
(module, id1) -> ty1
}
.groupBy { case ((module, _), _) => module }
.view
.mapValues(_.map { case ((_, id), ty) => id -> ty }.toMap)
.toMap
case None =>
Map.empty
}

def getType(module: String, id: Id): Option[TypeDecl] =
for {
types <- customTypes.get(module)
ty <- types.get(id)
} yield ty
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,11 @@ object DbApi {
def getImports(module: String): Option[Map[Id, String]] =
Db.getModuleStub(module).map(_.imports)

def getType(module: String, id: Id): Option[TypeDecl] =
Db.getModuleStub(module).flatMap(_.types.get(id))
def getType(module: String, id: Id): Option[TypeDecl] = {
CustomTypes.getType(module, id).orElse {
Db.getModuleStub(module).flatMap(_.types.get(id))
}
}

def getPrivateOpaque(module: String, id: Id): Option[TypeDecl] =
Db.getModuleStub(module).flatMap(_.privateOpaques.get(id))
Expand Down

0 comments on commit 759af3c

Please sign in to comment.