From 11460e869bc8bbb981f4c2fbe6c8ef464fa9336b Mon Sep 17 00:00:00 2001 From: Sahin Yort Date: Thu, 13 Jan 2022 22:32:08 +0100 Subject: [PATCH] fix: filter out .d.ts before passing srcs to transpiler (#3238) --- packages/typescript/index.bzl | 11 +++++++-- .../test/ts_project/swc/BUILD.bazel | 23 +++++++++++++++++++ .../typescript/test/ts_project/swc/tests.bzl | 16 +++++++++++++ 3 files changed, 48 insertions(+), 2 deletions(-) diff --git a/packages/typescript/index.bzl b/packages/typescript/index.bzl index 7f9ef4ff81..5f5069e581 100644 --- a/packages/typescript/index.bzl +++ b/packages/typescript/index.bzl @@ -497,6 +497,13 @@ def ts_project( typecheck_target_name = "%s_typecheck" % name test_target_name = "%s_typecheck_test" % name + transpile_srcs = [s for s in srcs if _lib.is_ts_src(s, allow_js)] + if (len(transpile_srcs) != len(js_outs)): + fail("ERROR: illegal state: transpile_srcs has length {} but js_outs has length {}".format( + len(transpile_srcs), + len(js_outs), + )) + common_kwargs = { "tags": kwargs.get("tags", []), "visibility": kwargs.get("visibility", None), @@ -505,7 +512,7 @@ def ts_project( if type(transpiler) == "function" or type(transpiler) == "rule": transpiler( name = transpile_target_name, - srcs = srcs, + srcs = transpile_srcs, js_outs = js_outs, map_outs = map_outs, **common_kwargs @@ -514,7 +521,7 @@ def ts_project( partial.call( transpiler, name = transpile_target_name, - srcs = srcs, + srcs = transpile_srcs, js_outs = js_outs, map_outs = map_outs, **common_kwargs diff --git a/packages/typescript/test/ts_project/swc/BUILD.bazel b/packages/typescript/test/ts_project/swc/BUILD.bazel index b1cb6346d0..9de18ce80f 100644 --- a/packages/typescript/test/ts_project/swc/BUILD.bazel +++ b/packages/typescript/test/ts_project/swc/BUILD.bazel @@ -25,6 +25,18 @@ write_file( content = ["export const a: string = 1"], ) +write_file( + name = "gen_lib_dts", + out = "lib.d.ts", + content = ["export const a: string;"], +) + +write_file( + name = "gen_index_ts", + out = "index.ts", + content = ["export const a: string = \"1\";"], +) + ts_project( name = "transpile_with_swc", srcs = ["big.ts"], @@ -53,4 +65,15 @@ ts_project( tsconfig = _TSCONFIG, ) +ts_project( + name = "transpile_with_dts", + srcs = [ + "index.ts", + "lib.d.ts", + ], + tags = ["manual"], + transpiler = swc_macro, + tsconfig = _TSCONFIG, +) + test_suite() diff --git a/packages/typescript/test/ts_project/swc/tests.bzl b/packages/typescript/test/ts_project/swc/tests.bzl index 0a17f9062d..d5ccf4bc57 100644 --- a/packages/typescript/test/ts_project/swc/tests.bzl +++ b/packages/typescript/test/ts_project/swc/tests.bzl @@ -33,6 +33,22 @@ transpile_with_failing_typecheck_test = unittest.make(_impl1, attrs = { "expected_js": attr.string_list(default = ["typeerror.js", "typeerror.js.map"]), }) +def _impl2(ctx): + env = unittest.begin(ctx) + + js_files = [] + for js in ctx.attr.lib[JSModuleInfo].sources.to_list(): + js_files.append(js.basename) + asserts.equals(env, ctx.attr.expected_js, sorted(js_files)) + + return unittest.end(env) + +transpile_with_dts_test = unittest.make(_impl2, attrs = { + "lib": attr.label(default = "transpile_with_dts"), + "expected_js": attr.string_list(default = ["index.js", "index.js.map"]), +}) + def test_suite(): unittest.suite("t0", transitive_declarations_test) unittest.suite("t1", transpile_with_failing_typecheck_test) + unittest.suite("t2", transpile_with_dts_test)