"""readable_example build macro

Confirms the JS compilability of some transpiled Java.


Example usage:

# Creates verification target
readable_example(
    name = "foobar",
    srcs = glob(["*.java"]),
)

"""


load("/third_party/java_src/j2cl/build_def/j2cl_util", "J2CL_OPTIMIZED_DEFS")
load("/third_party/java/j2cl/j2cl_library", "j2cl_library")


def readable_example(
    name, srcs, supporting_srcs=[], native_srcs=[],
    deps=[], js_deps=[], plugins=[], javacopts=[],  test_externs_list=None,
    _declare_legacy_namespace=False):
  """Macro that confirms the JS compilability of some transpiled Java.

  deps are Labels of j2cl_library() rules. NOT labels of
  java_library() rules.

  Args:
    name: The name of the readable example to generate.
    srcs: Source files to make readable output for.
    supporting_srcs: Source files referenced by the primary srcs.
    native_srcs: Foo.native.js files to merge in.
    deps: J2CL libraries referenced by the srcs.
    js_deps: JS libraries referenced by the srcs.
    plugins: APT processors to execute when generating readable output.
    javacopts: Custom opts to pass to the Java library rule.
    test_externs_list: Custom externs to support build test verification.
    _declare_legacy_namespace: Whether to use legacy namespaces in output.
  """

  if supporting_srcs:
    j2cl_library(
        name="supporting_" + name,
        srcs=supporting_srcs,
        javacopts=[
            "-source 8",
            "-target 8",
            "-Xep:SelfEquals:OFF",  # See go/self-equals-lsc
            "-Xep:IdentityBinaryExpression:OFF",
        ] + javacopts,
        native_srcs=native_srcs,
        deps=deps,
        plugins=plugins,
        _js_deps=js_deps,
        _declare_legacy_namespace=_declare_legacy_namespace,
        _test_externs_list=test_externs_list,
    )

  # Transpile the Java files.
  j2cl_library(
      name=name,
      srcs=srcs,
      javacopts=[
          "-source 8",
          "-target 8"
      ] + javacopts,
      native_srcs=native_srcs,
      deps=deps + (["supporting_" + name] if supporting_srcs else []),
      plugins=plugins,
      _js_deps=js_deps,
      _readable_source_maps=True,
      _declare_legacy_namespace=_declare_legacy_namespace,
      _test_externs_list=test_externs_list,
  )

  # Verify compilability of generated JS.
  if not test_externs_list:
    test_externs_list=["//javascript/externs:common"]
  native.js_binary(
      name=name + "_binary",
      defs=J2CL_OPTIMIZED_DEFS + ["--summary_detail_level=3"],
      compiler="//javascript/tools/jscompiler:head",
      externs_list=test_externs_list,
      deps=[":" + name],
  )

@@ -344,16 +344,68 @@
 brary%22)%0A
+load(%22/tools/build_rules/build_test%22, %22build_test%22)%0A
 %0A%0Adef re
@@ -1489,58 +1489,8 @@
 s=%5B%0A
-            %22-source 8%22,%0A            %22-target 8%22,%0A
     
@@ -1691,24 +1691,59 @@
 ns=plugins,%0A
+        generate_build_test=False,%0A
         _js_
@@ -1821,54 +1821,8 @@
 ce,%0A
-        _test_externs_list=test_externs_list,%0A
     
@@ -1924,65 +1924,8 @@
 pts=
-%5B%0A          %22-source 8%22,%0A          %22-target 8%22%0A      %5D + 
 java
@@ -2052,24 +2052,57 @@
 ns=plugins,%0A
+      generate_build_test=False,%0A
       _js_de
@@ -2210,52 +2210,8 @@
 ce,%0A
-      _test_externs_list=test_externs_list,%0A
   )%0A
@@ -2538,16 +2538,16 @@
 s_list,%0A
-
       de
@@ -2563,12 +2563,98 @@
  name%5D,%0A  )%0A
+%0A  build_test(%0A      name=name + %22_build_test%22,%0A      targets=%5Bname + %22_binary%22%5D,%0A  )%0A

Replace J2CL readable build targets with build_test targets to reduce presubmit load.