Skip to content

Commit

Permalink
feat: support --noCheck for transpiling without type-checking
Browse files Browse the repository at this point in the history
  • Loading branch information
jbedard committed Sep 24, 2024
1 parent 7cdd9a5 commit 20234a5
Show file tree
Hide file tree
Showing 14 changed files with 206 additions and 48 deletions.
17 changes: 10 additions & 7 deletions docs/rules.md

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

7 changes: 7 additions & 0 deletions examples/async_typecheck/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
load("@aspect_rules_ts//ts:defs.bzl", "ts_config")

ts_config(
name = "tsconfig",
src = "tsconfig.json",
visibility = [":__subpackages__"],
)
16 changes: 16 additions & 0 deletions examples/async_typecheck/backend/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
load("@aspect_bazel_lib//lib:testing.bzl", "assert_outputs")
load("@aspect_rules_ts//ts:defs.bzl", "ts_project")

ts_project(
name = "backend",
async_typecheck = True,
declaration = True,
tsconfig = "//examples/async_typecheck:tsconfig",
deps = ["//examples/async_typecheck/core"],
)

assert_outputs(
name = "test_backend_default_outputs",
actual = "backend",
expected = ["examples/async_typecheck/backend/index.js"],
)
10 changes: 10 additions & 0 deletions examples/async_typecheck/backend/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { IntersectionType } from '../core'

// Example object of IntersectionType
const myObject: IntersectionType = {
a: 42,
b: 'backend',
c: true,
}

console.log(myObject)
9 changes: 9 additions & 0 deletions examples/async_typecheck/core/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
load("@aspect_rules_ts//ts:defs.bzl", "ts_project")

ts_project(
name = "core",
async_typecheck = True,
declaration = True,
tsconfig = "//examples/async_typecheck:tsconfig",
visibility = ["//examples/async_typecheck:__subpackages__"],
)
20 changes: 20 additions & 0 deletions examples/async_typecheck/core/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* A file with some non-trivial types, so type-checking it may take some time.
* This helps to motivate the example: we'd like to be able to type-check the frontend and backend in parallel with this file.
*/
type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (
k: infer I
) => void
? I
: never

// Example usage
type UnionType = { a: number } | { b: string } | { c: boolean }

export type IntersectionType = UnionToIntersection<UnionType>

export const MyIntersectingValue: IntersectionType = {
a: 1,
b: '2',
c: true,
}
13 changes: 13 additions & 0 deletions examples/async_typecheck/frontend/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
load("@aspect_rules_ts//ts:defs.bzl", "ts_project")

ts_project(
name = "frontend",
async_typecheck = True,
tsconfig = {
"compilerOptions": {
"declaration": True,
"isolatedDeclarations": True,
},
},
deps = ["//examples/async_typecheck/core"],
)
13 changes: 13 additions & 0 deletions examples/async_typecheck/frontend/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import type { IntersectionType } from '../core'
import { MyIntersectingValue } from '../core'

// Example object of IntersectionType
const myObject: IntersectionType = {
a: 42,
b: 'frontend',
c: true,
}

const otherObject = MyIntersectingValue

console.log(myObject, otherObject, myObject === otherObject)
8 changes: 8 additions & 0 deletions examples/async_typecheck/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"compilerOptions": {
"isolatedDeclarations": true,
"declaration": true
},
// Workaround https://github.com/microsoft/TypeScript/issues/59036
"exclude": []
}
8 changes: 7 additions & 1 deletion 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,
async_typecheck = False,
declaration = False,
source_map = False,
declaration_map = False,
Expand Down Expand Up @@ -153,6 +154,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`.
async_typecheck: Whether to type-check asynchronously as a separate bazel action.
Requires https://devblogs.microsoft.com/typescript/announcing-typescript-5-6/#the---nocheck-option6
Requires 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 @@ -217,7 +222,7 @@ def ts_project(
incremental: Whether the `incremental` bit is set in the tsconfig.
Instructs Bazel to expect a `.tsbuildinfo` output.
no_emit: Whether the `noEmit` bit is set in the tsconfig.
Instructs Bazel *not* to expect any outputs. Only a validation action is used.
Instructs Bazel *not* to expect any outputs.
emit_declaration_only: Whether the `emitDeclarationOnly` bit is set in the tsconfig.
Instructs Bazel *not* to expect `.js` or `.js.map` outputs for `.ts` sources.
ts_build_info_file: The user-specified value of `tsBuildInfoFile` from the tsconfig.
Expand Down Expand Up @@ -416,6 +421,7 @@ def ts_project(
incremental = incremental,
preserve_jsx = preserve_jsx,
composite = composite,
async_typecheck = async_typecheck,
declaration = declaration,
declaration_dir = declaration_dir,
source_map = source_map,
Expand Down
12 changes: 12 additions & 0 deletions ts/private/ts_lib.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,18 @@ https://docs.aspect.build/rulesets/aspect_rules_js/docs/js_library#deps for more
mandatory = True,
allow_single_file = [".json"],
),
"async_typecheck": attr.bool(
doc = """\
Whether type-checking should be a separate action.
This allows the transpilation action to run without waiting for typings from dependencies.
Requires the typescript 5.6 [noCheck](https://www.typescriptlang.org/tsconfig#noCheck).
Requires tsconfig [isolatedDeclarations](https://www.typescriptlang.org/tsconfig#isolatedDeclarations)
to allow declarations to be emitted without dependencies.
""",
),
"validate": attr.bool(
doc = """whether to add a Validation Action to verify the other attributes match
settings in the tsconfig.json file""",
Expand Down
Loading

0 comments on commit 20234a5

Please sign in to comment.