Skip to content

Commit

Permalink
refactor: re-order fields in JsInfo for readability (#1648)
Browse files Browse the repository at this point in the history
  • Loading branch information
gregmagolan committed May 7, 2024
1 parent de18a66 commit b853747
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 37 deletions.
18 changes: 9 additions & 9 deletions js/private/js_helpers.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,8 @@ def gather_runfiles(
transitive_files_depsets.append(gather_files_from_js_info(
targets = data + deps,
include_sources = include_sources,
include_transitive_sources = include_transitive_sources,
include_types = include_types,
include_transitive_sources = include_transitive_sources,
include_transitive_types = include_transitive_types,
include_npm_sources = include_npm_sources,
))
Expand Down Expand Up @@ -285,17 +285,17 @@ def envs_for_log_level(log_level):
def gather_files_from_js_info(
targets,
include_sources,
include_transitive_sources,
include_types,
include_transitive_sources,
include_transitive_types,
include_npm_sources):
"""Gathers files from JsInfo and NpmPackageStoreInfo providers.
Args:
targets: list of target to gather from
include_sources: see js_info_files documentation
include_transitive_sources: see js_info_files documentation
include_types: see js_info_files documentation
include_transitive_sources: see js_info_files documentation
include_transitive_types: see js_info_files documentation
include_npm_sources: see js_info_files documentation
Expand All @@ -309,18 +309,18 @@ def gather_files_from_js_info(
for target in targets
if JsInfo in target and hasattr(target[JsInfo], "sources")
]
if include_transitive_sources:
files_depsets.extend([
target[JsInfo].transitive_sources
for target in targets
if JsInfo in target and hasattr(target[JsInfo], "transitive_sources")
])
if include_types:
files_depsets.extend([
target[JsInfo].types
for target in targets
if JsInfo in target and hasattr(target[JsInfo], "types")
])
if include_transitive_sources:
files_depsets.extend([
target[JsInfo].transitive_sources
for target in targets
if JsInfo in target and hasattr(target[JsInfo], "transitive_sources")
])
if include_transitive_types:
files_depsets.extend([
target[JsInfo].transitive_types
Expand Down
50 changes: 28 additions & 22 deletions js/private/js_info.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -3,53 +3,59 @@
JsInfo = provider(
doc = "Encapsulates information provided by rules in rules_js and derivative rule sets",
fields = {
"sources": "A depset of source files produced by the target",
"types": "A depset of typings files produced by the target",
"transitive_sources": "A depset of source files produced by the target and the target's transitive deps",
"transitive_types": "A depset of declaration files produced by the target and the target's transitive deps",
"npm_sources": "A depset of files in npm package dependencies of the target and the target's transitive deps",
"npm_package_store_infos": "A depset of NpmPackageStoreInfo providers from non-dev npm dependencies of the target and the target's transitive dependencies to use as direct dependencies when linking downstream npm_package targets with npm_link_package",
"sources": "A depset of source files produced by the target",
"transitive_types": "A depset of declaration files produced by the target and the target's transitive deps",
"transitive_sources": "A depset of source files produced by the target and the target's transitive deps",
},
)

def js_info(
types = depset(),
npm_sources = depset(),
npm_package_store_infos = depset(),
sources = depset(),
types = depset(),
transitive_sources = depset(),
transitive_types = depset(),
transitive_sources = depset()):
npm_sources = depset(),
npm_package_store_infos = depset()):
"""Construct a JsInfo.
Args:
sources: See JsInfo documentation
types: See JsInfo documentation
transitive_sources: See JsInfo documentation
transitive_types: See JsInfo documentation
npm_sources: See JsInfo documentation
npm_package_store_infos: See JsInfo documentation
sources: See JsInfo documentation
transitive_types: See JsInfo documentation
transitive_sources: See JsInfo documentation
Returns:
A JsInfo provider
"""
if type(sources) != "depset":
msg = "Expected sources to be a depset but got {}".format(type(sources))
fail(msg)
if type(types) != "depset":
fail("Expected types to be a depset")
msg = "Expected types to be a depset but got {}".format(type(types))
fail(msg)
if type(transitive_sources) != "depset":
msg = "Expected transitive_sources to be a depset but got {}".format(type(transitive_sources))
fail(msg)
if type(transitive_types) != "depset":
msg = "Expected transitive_types to be a depset but got {}".format(type(transitive_types))
fail(msg)
if type(npm_sources) != "depset":
fail("Expected npm_sources to be a depset")
msg = "Expected npm_sources to be a depset but got {}".format(type(npm_sources))
fail(msg)
if type(npm_package_store_infos) != "depset":
fail("Expected npm_package_store_infos to be a depset")
if type(sources) != "depset":
fail("Expected sources to be a depset")
if type(transitive_types) != "depset":
fail("Expected transitive_types to be a depset")
if type(transitive_sources) != "depset":
fail("Expected transitive_sources to be a depset")
msg = "Expected npm_package_store_infos to be a depset but got {}".format(type(npm_package_store_infos))
fail(msg)

return JsInfo(
sources = sources,
types = types,
transitive_sources = transitive_sources,
transitive_types = transitive_types,
npm_sources = npm_sources,
npm_package_store_infos = npm_package_store_infos,
sources = sources,
transitive_types = transitive_types,
transitive_sources = transitive_sources,
)
2 changes: 1 addition & 1 deletion js/private/js_info_files.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ def _js_info_files_impl(ctx):
return DefaultInfo(files = _gather_files_from_js_info(
targets = ctx.attr.srcs,
include_sources = ctx.attr.include_sources,
include_transitive_sources = ctx.attr.include_transitive_sources,
include_types = ctx.attr.include_types,
include_transitive_sources = ctx.attr.include_transitive_sources,
include_transitive_types = ctx.attr.include_transitive_types,
include_npm_sources = ctx.attr.include_npm_sources,
))
Expand Down
6 changes: 3 additions & 3 deletions js/private/js_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -214,12 +214,12 @@ def _js_library_impl(ctx):

return [
js_info(
sources = sources,
types = types,
transitive_sources = transitive_sources,
transitive_types = transitive_types,
npm_sources = npm_sources,
npm_package_store_infos = npm_package_store_infos,
sources = sources,
transitive_types = transitive_types,
transitive_sources = transitive_sources,
),
DefaultInfo(
files = sources,
Expand Down
2 changes: 1 addition & 1 deletion js/private/js_run_devserver.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ def _js_run_devserver_impl(ctx):
transitive_runfiles = [_gather_files_from_js_info(
targets = ctx.attr.data,
include_sources = True,
include_transitive_sources = ctx.attr.include_transitive_sources,
include_types = ctx.attr.include_types,
include_transitive_sources = ctx.attr.include_transitive_sources,
include_transitive_types = ctx.attr.include_types,
include_npm_sources = ctx.attr.include_npm_sources,
)]
Expand Down
2 changes: 1 addition & 1 deletion js/private/test/create_launcher/custom_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ def _custom_test_impl(ctx):
transitive_files = js_lib_helpers.gather_files_from_js_info(
targets = ctx.attr.data,
include_sources = True,
include_transitive_sources = ctx.attr.include_transitive_sources,
include_types = ctx.attr.include_types,
include_transitive_sources = ctx.attr.include_transitive_sources,
include_transitive_types = ctx.attr.include_types,
include_npm_sources = ctx.attr.include_npm_sources,
),
Expand Down

0 comments on commit b853747

Please sign in to comment.