Skip to content

Commit

Permalink
feat: enable declarations by default when unspecified in tsconfig dict
Browse files Browse the repository at this point in the history
Fix #30
  • Loading branch information
jbedard committed Oct 17, 2022
1 parent ab68b6b commit 095114f
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 28 deletions.
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.

4 changes: 2 additions & 2 deletions 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 @@ -295,7 +295,7 @@ def ts_project(
# TODO: fail if compilerOptions includes a conflict with an attribute?
compiler_options = tsconfig.setdefault("compilerOptions", {})
source_map = compiler_options.setdefault("sourceMap", source_map)
declaration = compiler_options.setdefault("declaration", declaration)
declaration = compiler_options.setdefault("declaration", declaration != False)
declaration_map = compiler_options.setdefault("declarationMap", declaration_map)
emit_declaration_only = compiler_options.setdefault("emitDeclarationOnly", emit_declaration_only)
allow_js = compiler_options.setdefault("allowJs", allow_js)
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",
)
19 changes: 13 additions & 6 deletions ts/test/mock_transpiler.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@ load("@bazel_skylib//rules:write_file.bzl", "write_file")

_DUMMY_SOURCEMAP = """{"version":3,"sources":["%s"],"mappings":"AAAO,KAAK,CAAC","file":"in.js","sourcesContent":["fake"]}"""

def mock(name, srcs, js_outs, map_outs, **kwargs):
def mock(name, srcs, js_outs, map_outs):
"""Mock transpiler macro.
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
"""

for i, s in enumerate(srcs):
Expand All @@ -19,8 +25,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)

0 comments on commit 095114f

Please sign in to comment.