Skip to content

Commit 95c96c6

Browse files
authored
refactor: normalize root/out/declaration dir in macro (#810)
1 parent 251ac0e commit 95c96c6

File tree

4 files changed

+14
-9
lines changed

4 files changed

+14
-9
lines changed

ts/defs.bzl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,12 @@ def ts_project(
338338
resolve_json_module = resolve_json_module,
339339
)
340340

341+
# Normalize common directory path shortcuts such as None vs "." vs "./"
342+
out_dir = _clean_dir_arg(out_dir)
343+
root_dir = _clean_dir_arg(root_dir)
344+
declaration_dir = _clean_dir_arg(declaration_dir)
345+
346+
# Inferred paths
341347
typings_out_dir = declaration_dir if declaration_dir else out_dir
342348
tsbuildinfo_path = ts_build_info_file if ts_build_info_file else name + ".tsbuildinfo"
343349

@@ -527,3 +533,8 @@ def _invoke_custom_transpiler(type_str, transpiler, transpile_target_name, srcs,
527533
)
528534
else:
529535
fail("%s attribute should be a rule/macro or a skylib partial. Got %s" % (type_str, type(transpiler)))
536+
537+
def _clean_dir_arg(p):
538+
if not p or p == "." or p == "./":
539+
return None
540+
return p.removeprefix("./")

ts/private/ts_lib.bzl

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,6 @@ def _is_ts_src(src, allow_js, resolve_json_module, include_typings):
236236

237237
def _to_out_path(f, out_dir, root_dir):
238238
f = f[f.find(":") + 1:]
239-
out_dir = out_dir if out_dir != "." else None
240239

241240
if out_dir and f.startswith(out_dir + "/"):
242241
return f
@@ -249,8 +248,6 @@ def _to_out_path(f, out_dir, root_dir):
249248

250249
def _to_js_out_paths(srcs, out_dir, root_dir, allow_js, resolve_json_module, ext_map, default_ext):
251250
outs = []
252-
out_dir = _remove_leading_dot_slash(out_dir)
253-
root_dir = _remove_leading_dot_slash(root_dir)
254251
for f in srcs:
255252
if _is_ts_src(f, allow_js, resolve_json_module, False):
256253
out = _to_out_path(f, out_dir, root_dir)
@@ -349,9 +346,6 @@ def _declare_outputs(ctx, paths):
349346
for path in paths
350347
]
351348

352-
def _remove_leading_dot_slash(string_path):
353-
return string_path.removeprefix("./") if string_path else string_path
354-
355349
lib = struct(
356350
declare_outputs = _declare_outputs,
357351
join = _join,

ts/private/ts_project.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ See https://github.com/aspect-build/rules_ts/issues/361 for more details.
167167
# Add user specified arguments *before* rule supplied arguments
168168
common_args.extend(ctx.attr.args)
169169

170-
if (ctx.attr.out_dir and ctx.attr.out_dir != ".") or ctx.attr.root_dir:
170+
if ctx.attr.out_dir or ctx.attr.root_dir:
171171
# TODO: add validation that excludes is non-empty in this case, as passing the --outDir or --declarationDir flag
172172
# to TypeScript causes it to set a default for excludes such that it won't find our sources that were copied-to-bin.
173173
# See https://github.com/microsoft/TypeScript/issues/59036 and https://github.com/aspect-build/rules_ts/issues/644

ts/test/mock_transpiler.bzl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def mock(name, srcs, source_map = False, **kwargs):
5353
srcs = srcs,
5454
# Calculate pre-declared outputs so they can be referenced as targets.
5555
# This is an optional transpiler feature aligning with the default tsc transpiler.
56-
js_outs = lib.calculate_js_outs(srcs, ".", ".", False, False, False, False),
57-
map_outs = lib.calculate_map_outs(srcs, ".", ".", True, False, False, False) if source_map else [],
56+
js_outs = lib.calculate_js_outs(srcs, None, None, False, False, False, False),
57+
map_outs = lib.calculate_map_outs(srcs, None, None, True, False, False, False) if source_map else [],
5858
**kwargs
5959
)

0 commit comments

Comments
 (0)