Skip to content

Commit

Permalink
feat: transpile using --noCheck when isolatedDeclarations
Browse files Browse the repository at this point in the history
Ref #88
Ref #374
  • Loading branch information
jbedard committed Aug 29, 2024
1 parent f1b733c commit 193d2f2
Show file tree
Hide file tree
Showing 12 changed files with 140 additions and 63 deletions.
18 changes: 10 additions & 8 deletions docs/rules.md

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

13 changes: 0 additions & 13 deletions examples/isolated_declarations/README.md

This file was deleted.

1 change: 1 addition & 0 deletions examples/isolated_declarations/backend/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ load("@aspect_rules_ts//ts:defs.bzl", "ts_project")
ts_project(
name = "backend",
declaration = True,
isolated_declarations = True,
tsconfig = "//examples/isolated_declarations:tsconfig",
deps = ["//examples/isolated_declarations/core"],
)
Expand Down
1 change: 1 addition & 0 deletions examples/isolated_declarations/core/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ load("@aspect_rules_ts//ts:defs.bzl", "ts_project")
ts_project(
name = "core",
declaration = True,
isolated_declarations = True,
tsconfig = "//examples/isolated_declarations:tsconfig",
visibility = ["//examples/isolated_declarations:__subpackages__"],
)
6 changes: 6 additions & 0 deletions examples/isolated_declarations/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,9 @@ type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (
type UnionType = { a: number } | { b: string } | { c: boolean }

export type IntersectionType = UnionToIntersection<UnionType>

export const MyIntersectingValue: IntersectionType = {
a: 1,
b: '2',
c: true,
}
1 change: 1 addition & 0 deletions examples/isolated_declarations/frontend/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ ts_project(
tsconfig = {
"compilerOptions": {
"declaration": True,
"isolatedDeclarations": True,
},
},
deps = ["//examples/isolated_declarations/core"],
Expand Down
5 changes: 4 additions & 1 deletion examples/isolated_declarations/frontend/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { IntersectionType } from '../core'
import { MyIntersectingValue } from '../core'

// Example object of IntersectionType
const myObject: IntersectionType = {
Expand All @@ -7,4 +8,6 @@ const myObject: IntersectionType = {
c: true,
}

console.log(myObject)
const otherObject = MyIntersectingValue

console.log(myObject, otherObject, myObject === otherObject)
50 changes: 41 additions & 9 deletions ts/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def ts_project(
assets = [],
extends = None,
allow_js = False,
isolated_declarations = None,
declaration = False,
source_map = False,
declaration_map = False,
Expand Down Expand Up @@ -152,6 +153,10 @@ def ts_project(
See https://www.typescriptlang.org/docs/handbook/compiler-options.html#compiler-options
Typically useful arguments for debugging are `--listFiles` and `--listEmittedFiles`.
isolated_declarations: Whether to enforce that declaration output (.d.ts file) can be produced for a
single source file at a time. Requires some additional explicit types on exported symbols.
See https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-5.html#isolated-declarations
transpiler: A custom transpiler tool to run that produces the JavaScript outputs instead of `tsc`.
Under `--@aspect_rules_ts//ts:default_to_tsc_transpiler`, the default is to use `tsc` to produce
Expand Down Expand Up @@ -280,6 +285,14 @@ def ts_project(
allow_js = compiler_options.setdefault("allowJs", allow_js)
if resolve_json_module != None:
resolve_json_module = compiler_options.setdefault("resolveJsonModule", resolve_json_module)
if isolated_declarations != None:
isolated_declarations = compiler_options.setdefault("isolatedDeclarations", isolated_declarations)

# Options not set by default due to tsc version compatibility
if "isolatedDeclarations" in compiler_options.keys():
isolated_declarations = compiler_options.get("isolatedDeclarations")
elif isolated_declarations != None:
compiler_options["isolatedDeclarations"] = isolated_declarations

# These options are always passed on the tsc command line so don't include them
# in the tsconfig. At best they're redundant, but at worst we'll have a conflict
Expand Down Expand Up @@ -321,8 +334,9 @@ def ts_project(
else:
# To stitch together a tree of ts_project where transpiler is a separate rule,
# we have to produce a few targets
tsc_target_name = "%s_typings" % name
tsc_target_name = "%s_tsc" % name
transpile_target_name = "%s_transpile" % name
typings_target_name = "%s_typings" % name
typecheck_target_name = "%s_typecheck" % name
test_target_name = "%s_typecheck_test" % name

Expand All @@ -342,14 +356,31 @@ def ts_project(
else:
fail("transpiler attribute should be a rule/macro or a skylib partial. Got " + type(transpiler))

# Users should build this target to get a failed build when typechecking fails
native.filegroup(
name = typecheck_target_name,
srcs = [tsc_target_name],
# This causes the types to be produced, which in turn triggers the tsc action to typecheck
output_group = "types",
**common_kwargs
)
if isolated_declarations:
# Users should build this target to get a failed build when typechecking fails
native.filegroup(
name = typecheck_target_name,
srcs = [tsc_target_name],
output_group = "typecheck",
**common_kwargs
)

native.filegroup(
name = typings_target_name,
srcs = [tsc_target_name],
# This causes the types to be produced, which in turn triggers the tsc action to typecheck
output_group = "types",
**common_kwargs
)
else:
# Users should build this target to get a failed build when typechecking fails
native.filegroup(
name = typecheck_target_name,
srcs = [tsc_target_name],
# This causes the types to be produced, which in turn triggers the tsc action to typecheck
output_group = "types",
**common_kwargs
)

# Ensures the typecheck target gets built under `bazel test --build_tests_only`
build_test(
Expand Down Expand Up @@ -389,6 +420,7 @@ def ts_project(
incremental = incremental,
preserve_jsx = preserve_jsx,
composite = composite,
isolated_declarations = isolated_declarations,
declaration = declaration,
declaration_dir = declaration_dir,
source_map = source_map,
Expand Down
3 changes: 3 additions & 0 deletions ts/private/ts_lib.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ COMPILER_OPTION_ATTRS = {
"composite": attr.bool(
doc = "https://www.typescriptlang.org/tsconfig#composite",
),
"isolated_declarations": attr.bool(
doc = "https://www.typescriptlang.org/tsconfig/#isolatedDeclarations",
),
"declaration": attr.bool(
doc = "https://www.typescriptlang.org/tsconfig#declaration",
),
Expand Down
Loading

0 comments on commit 193d2f2

Please sign in to comment.