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

feat: enable declarations by default when unspecified in tsconfig dict #181

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 1 addition & 1 deletion docs/rules.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion ts/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def ts_project(
deps = [],
extends = None,
allow_js = False,
declaration = False,
declaration = None,
source_map = False,
declaration_map = False,
resolve_json_module = None,
Expand Down Expand Up @@ -291,6 +291,9 @@ def ts_project(
tsconfig = "tsconfig.json"

if type(tsconfig) == type(dict()):
# Output declarations by default if unspecified and not extending a config which may specify
declaration = False if declaration == False or extends else True
Copy link
Member

Choose a reason for hiding this comment

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

had to read four times to understand the outcome 🗡️

Copy link
Member Author

Choose a reason for hiding this comment

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

Have a recommendation or preference to use instead?


# Copy attributes <-> tsconfig properties
# TODO: fail if compilerOptions includes a conflict with an attribute?
compiler_options = tsconfig.setdefault("compilerOptions", {})
Expand Down
25 changes: 19 additions & 6 deletions ts/test/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ load(":transpiler_tests.bzl", "transpiler_test_suite")
load("//ts:defs.bzl", "ts_project")
load("@aspect_rules_js//js:defs.bzl", "js_test")


_TSCONFIG = {
"compilerOptions": {
"declaration": True,
Expand Down Expand Up @@ -41,6 +40,12 @@ write_file(
content = ["export const a: string = \"1\";"],
)

write_file(
name = "gen_index2_ts",
out = "index2.ts",
content = ["export const b: string = \"2\";"],
)

write_file(
name = "gen_deep_src",
out = "root/deep/root/deep_src.ts",
Expand Down Expand Up @@ -94,15 +99,23 @@ ts_project(
tsconfig = _TSCONFIG,
)

ts_project(
name = "transpile_with_default_dts_test",
srcs = ["index2.ts"],
tags = ["manual"],
transpiler = mock,
tsconfig = {},
)

transpiler_test_suite()

# Ensure that when determining output location, the `root_dir` attribute is only removed once.
ts_project(
name = "rootdir_works_with_repeated_directory",
srcs = ["root/deep/root/deep_src.ts"],
root_dir = "root",
transpiler = mock,
tsconfig = _TSCONFIG,
root_dir = "root",
)

copy_file(
Expand All @@ -113,8 +126,8 @@ copy_file(

js_test(
name = "worker_test",
entry_point = ":ts_project_worker.test.js",
data = [
":ts_project_worker.js"
]
)
":ts_project_worker.js",
],
entry_point = ":ts_project_worker.test.js",
)
18 changes: 13 additions & 5 deletions ts/test/mock_transpiler.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ def mock(name, srcs, js_outs, map_outs, **kwargs):

In real usage you would wrap a rule like
https://github.com/aspect-build/rules_swc/blob/main/docs/swc.md

Args:
name: rule name prefix
srcs: ts sources
js_outs: js files to generate
map_outs: map files to generate
**kwargs: unused
"""

for i, s in enumerate(srcs):
Expand All @@ -19,8 +26,9 @@ def mock(name, srcs, js_outs, map_outs, **kwargs):
out = js_outs[i],
)

write_file(
name = "_{}_{}_map".format(name, s),
out = map_outs[i],
content = [_DUMMY_SOURCEMAP % s],
)
if i < len(map_outs):
write_file(
name = "_{}_{}_map".format(name, s),
out = map_outs[i],
content = [_DUMMY_SOURCEMAP % s],
)
23 changes: 10 additions & 13 deletions ts/test/transpiler_tests.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ transitive_declarations_test = unittest.make(_impl0, attrs = {
"expected_declarations": attr.string_list(default = ["big.d.ts"]),
})

def _impl1(ctx):
def _expected_js_impl(ctx):
env = unittest.begin(ctx)

js_files = []
Expand All @@ -28,27 +28,24 @@ def _impl1(ctx):

return unittest.end(env)

transpile_with_failing_typecheck_test = unittest.make(_impl1, attrs = {
transpile_with_failing_typecheck_test = unittest.make(_expected_js_impl, attrs = {
"lib": attr.label(default = "transpile_with_typeerror"),
"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[DefaultInfo].files.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 = {
transpile_with_dts_test = unittest.make(_expected_js_impl, attrs = {
"lib": attr.label(default = "transpile_with_dts"),
"expected_js": attr.string_list(default = ["index.js", "index.js.map"]),
})

transpile_with_default_dts_test = unittest.make(_expected_js_impl, attrs = {
"lib": attr.label(default = "transpile_with_default_dts_test"),
"expected_js": attr.string_list(default = ["index2.js"]),
"expected_declarations": attr.string_list(default = ["index2.d.ts"]),
})

def transpiler_test_suite():
unittest.suite("t0", transitive_declarations_test)
unittest.suite("t1", transpile_with_failing_typecheck_test)
unittest.suite("t2", transpile_with_dts_test)
unittest.suite("t3", transpile_with_default_dts_test)