Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fixture for declaration transpiler test #708

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions ts/test/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("//ts:defs.bzl", "ts_project")
load(":flags_test.bzl", "ts_project_flags_test_suite")
load(":mock_transpiler.bzl", "mock")
load(":mock_declaration_transpiler.bzl", mock_declaration_transpiler = "mock")
load(":mock_transpiler.bzl", mock_transpiler = "mock")
load(":transpiler_tests.bzl", "transpiler_test_suite")
load(":ts_project_test.bzl", "ts_project_test_suite")

Expand All @@ -15,8 +16,9 @@ ts_project_flags_test_suite(name = "ts_project_flags_test")
ts_project(
name = "rootdir_works_with_repeated_directory",
srcs = ["root/deep/root/deep_src.ts"],
declaration_transpiler = mock_declaration_transpiler,
root_dir = "root",
transpiler = mock,
transpiler = mock_transpiler,
tsconfig = {
"compilerOptions": {
"declaration": True,
Expand All @@ -31,3 +33,9 @@ bzl_library(
visibility = ["//visibility:public"],
deps = ["@aspect_rules_ts//ts/private:ts_lib"],
)

bzl_library(
name = "mock_declaration_transpiler",
srcs = ["mock_declaration_transpiler.bzl"],
visibility = ["//visibility:public"],
)
61 changes: 61 additions & 0 deletions ts/test/mock_declaration_transpiler.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
"Fixture to demonstrate a custom declaration transpiler for ts_project"

load("@aspect_rules_js//js:providers.bzl", "JsInfo")
load("@aspect_rules_ts//ts/private:ts_lib.bzl", "lib")

def _mock_impl(ctx):
out_files = []

for src in ctx.files.srcs:
out_path = src.short_path

if out_path.endswith(".json"):
continue

dts_file = ctx.actions.declare_file(lib.relative_to_package(out_path.replace(".ts", ".d.ts"), ctx))
out_files.append(dts_file)

ctx.actions.run_shell(
inputs = [src],
outputs = [dts_file],
command = "cp $@",
arguments = [src.path, dts_file.path],
)

output_types_depset = depset(out_files)

return [
JsInfo(
target = ctx.label,
sources = depset(),
types = output_types_depset,
transitive_sources = depset(),
transitive_types = depset(),
npm_sources = depset(),
npm_package_store_infos = depset(),
),
DefaultInfo(
files = output_types_depset,
),
]

mock_impl = rule(
attrs = {
"srcs": attr.label_list(
doc = "TypeScript source files",
allow_files = True,
mandatory = True,
),
},
implementation = _mock_impl,
)

def mock(name, srcs, **kwargs):
# Run the rule producing those pre-declared outputs as well as any other outputs
# which can not be determined ahead of time such as within directories, grouped
# within a filegroup() etc.
mock_impl(
name = name,
srcs = srcs,
**kwargs
)
29 changes: 27 additions & 2 deletions ts/test/mock_transpiler.bzl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"Fixture to demonstrate a custom transpiler for ts_project"

load("@aspect_rules_js//js:libs.bzl", "js_lib_helpers")
load("@aspect_rules_js//js:providers.bzl", "JsInfo")
load("@aspect_rules_ts//ts/private:ts_lib.bzl", "lib")

_DUMMY_SOURCEMAP = """{"version":3,"sources":["%s"],"mappings":"AAAO,KAAK,CAAC","file":"in.js","sourcesContent":["fake"]}"""
Expand All @@ -21,15 +23,38 @@ def _mock_impl(ctx):
inputs = [src],
outputs = [js_file],
command = "cp $@",
arguments = [src.path, js_file.path.replace(".ts", ".js")],
arguments = [src.path, js_file.path],
)

ctx.actions.write(
output = map_file,
content = _DUMMY_SOURCEMAP % src.short_path,
)

return DefaultInfo(files = depset(out_files))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did this have to switch? The transpiler should just have to output the transpiled js or dts (+maps) as default info?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That just means the rule can be used directly for things like js_library(deps) I guess, instead of how ts_project (the macro) or js_library(srcs) uses the DefaultInfo.

I guess it does not harm, but also isn't used in these tests 🤷

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah it's probably worth doing in case folks crib this as an example (though we could also provide a production-ready transpiler using oxc as an example ;)

output_sources_depset = depset(out_files)

runfiles = js_lib_helpers.gather_runfiles(
ctx = ctx,
sources = output_sources_depset,
data = [],
deps = ctx.attr.srcs,
)

return [
JsInfo(
target = ctx.label,
sources = output_sources_depset,
types = depset(),
transitive_sources = depset(),
transitive_types = depset(),
npm_sources = depset(),
npm_package_store_infos = depset(),
),
DefaultInfo(
files = output_sources_depset,
runfiles = runfiles,
),
]

mock_impl = rule(
attrs = {
Expand Down
Loading