Skip to content

Commit 2b89f32

Browse files
gregmagolanalexeagle
authored andcommitted
feat(builtin): add DeclarationInfo to js_library
Adds a declaration_info provider factory which simplifies ts_project. Also add a ts_project test to validate that js_library be a dep to ts_project. BREAKING CHANGE: Removed provide_declarations() factory function for DeclarationInfo. Use declaration_info() factory function instead.
1 parent b120627 commit 2b89f32

File tree

9 files changed

+76
-30
lines changed

9 files changed

+76
-30
lines changed

internal/js_library/js_library.bzl

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
DO NOT USE - this is not fully designed, and exists only to enable testing within this repo.
1818
"""
1919

20-
load("//:providers.bzl", "LinkablePackageInfo")
20+
load("//:providers.bzl", "LinkablePackageInfo", "declaration_info")
2121
load("//third_party/github.com/bazelbuild/bazel-skylib:rules/private/copy_file_private.bzl", "copy_bash", "copy_cmd")
2222

2323
_AMD_NAMES_DOC = """Mapping from require module names to global variables.
@@ -51,6 +51,7 @@ def write_amd_names_shim(actions, amd_names_shim, targets):
5151

5252
def _impl(ctx):
5353
files = []
54+
typings = []
5455

5556
for src in ctx.files.srcs:
5657
if src.is_source and not src.path.startswith("external/"):
@@ -59,13 +60,18 @@ def _impl(ctx):
5960
copy_cmd(ctx, src, dst)
6061
else:
6162
copy_bash(ctx, src, dst)
62-
files.append(dst)
63+
if dst.basename.endswith(".d.ts"):
64+
typings.append(dst)
65+
else:
66+
files.append(dst)
67+
elif src.basename.endswith(".d.ts"):
68+
typings.append(src)
6369
else:
6470
files.append(src)
6571

6672
files_depset = depset(files)
6773

68-
result = [
74+
providers = [
6975
DefaultInfo(
7076
files = files_depset,
7177
runfiles = ctx.runfiles(files = ctx.files.srcs),
@@ -75,13 +81,18 @@ def _impl(ctx):
7581

7682
if ctx.attr.package_name:
7783
path = "/".join([p for p in [ctx.bin_dir.path, ctx.label.workspace_root, ctx.label.package] if p])
78-
result.append(LinkablePackageInfo(
84+
providers.append(LinkablePackageInfo(
7985
package_name = ctx.attr.package_name,
8086
path = path,
8187
files = files_depset,
8288
))
8389

84-
return result
90+
# Don't provide DeclarationInfo if there are no typings to provide.
91+
# Improves error messaging downstream if DeclarationInfo is required.
92+
if len(typings):
93+
providers.append(declaration_info(depset(typings)))
94+
95+
return providers
8596

8697
_js_library = rule(
8798
implementation = _impl,

internal/providers/declaration_info.bzl

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,11 @@
1414

1515
"""This module contains a provider for TypeScript typings files (.d.ts)"""
1616

17-
def provide_declarations(**kwargs):
18-
"""Factory function for creating checked declarations with externs.
19-
20-
Do not directly construct DeclarationInfo()
21-
"""
22-
23-
# TODO: add some checking actions to ensure the declarations are well-formed
24-
return DeclarationInfo(**kwargs)
25-
2617
DeclarationInfo = provider(
2718
doc = """The DeclarationInfo provider allows JS rules to communicate typing information.
2819
TypeScript's .d.ts files are used as the interop format for describing types.
2920
30-
Do not create DeclarationInfo instances directly, instead use the provide_declarations factory function.
21+
Do not create DeclarationInfo instances directly, instead use the declaration_info factory function.
3122
3223
TODO(alexeagle): The ts_library#deps attribute should require that this provider is attached.
3324
@@ -41,3 +32,21 @@ This prevents needing an aspect in rules that consume the typings, which improve
4132
"type_blacklisted_declarations": """A depset of .d.ts files that we should not use to infer JSCompiler types (via tsickle)""",
4233
},
4334
)
35+
36+
def declaration_info(declarations, deps = []):
37+
"""Constructs a DeclarationInfo including all transitive declarations from DeclarationInfo providers in a list of deps.
38+
39+
Returns a single DeclarationInfo.
40+
"""
41+
transitive_depsets = [declarations]
42+
for dep in deps:
43+
if DeclarationInfo in dep:
44+
transitive_depsets.append(dep[DeclarationInfo].transitive_declarations)
45+
46+
return DeclarationInfo(
47+
declarations = declarations,
48+
transitive_declarations = depset(transitive = transitive_depsets),
49+
# Downstream ts_library rules will fail if they don't find this field
50+
# Even though it is only for Google Closure Compiler externs generation
51+
type_blacklisted_declarations = depset(),
52+
)

packages/typescript/internal/ts_project.bzl

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"ts_project rule"
22

3-
load("@build_bazel_rules_nodejs//:providers.bzl", "DeclarationInfo", "NpmPackageInfo", "run_node")
3+
load("@build_bazel_rules_nodejs//:providers.bzl", "DeclarationInfo", "NpmPackageInfo", "declaration_info", "run_node")
44

55
_DEFAULT_TSC = (
66
# BEGIN-INTERNAL
@@ -138,18 +138,7 @@ def _ts_project_impl(ctx):
138138
# Don't provide DeclarationInfo if there are no typings to provide.
139139
# Improves error messaging if a ts_project needs declaration = True
140140
if len(typings_outputs) or len(ctx.attr.deps):
141-
providers.append(
142-
DeclarationInfo(
143-
declarations = depset(typings_outputs),
144-
transitive_declarations = depset(typings_outputs, transitive = [
145-
dep[DeclarationInfo].transitive_declarations
146-
for dep in ctx.attr.deps
147-
]),
148-
# Downstream ts_library rules will fail if they don't find this field
149-
# Even though it is only for Google Closure Compiler externs generation
150-
type_blacklisted_declarations = depset(),
151-
),
152-
)
141+
providers.append(declaration_info(depset(typings_outputs), ctx.attr.deps))
153142

154143
return providers
155144

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
load("@npm_bazel_typescript//:index.bzl", "ts_project")
2+
load("//internal/js_library:js_library.bzl", "js_library")
3+
4+
js_library(
5+
name = "lib_a",
6+
srcs = [
7+
"a.d.ts",
8+
"a.js",
9+
],
10+
)
11+
12+
ts_project(
13+
name = "tsconfig",
14+
srcs = ["b.ts"],
15+
deps = ["lib_a"],
16+
)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export declare const a: string;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
'use strict';
2+
exports.__esModule = true;
3+
exports.a = 'a';
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import {a} from './a';
2+
console.log(a.toLowerCase());
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"files": ["b.ts"],
3+
"compilerOptions": {
4+
// Help TypeScript locate the a.d.ts file from dep. Needed when running in a sandbox or remote.
5+
"rootDirs": [
6+
".",
7+
"../../../../../bazel-out/darwin-fastbuild/bin/packages/typescript/test/ts_project/js_library",
8+
"../../../../../bazel-out/k8-fastbuild/bin/packages/typescript/test/ts_project/js_library",
9+
"../../../../../bazel-out/x64_windows-fastbuild/bin/packages/typescript/test/ts_project/js_library",
10+
"../../../../../bazel-out/darwin-dbg/bin/packages/typescript/test/ts_project/js_library",
11+
"../../../../../bazel-out/k8-dbg/bin/packages/typescript/test/ts_project/js_library",
12+
"../../../../../bazel-out/x64_windows-dbg/bin/packages/typescript/test/ts_project/js_library",
13+
],
14+
}
15+
}

providers.bzl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Users should not load files under "/internal"
2020
load(
2121
"//internal/providers:declaration_info.bzl",
2222
_DeclarationInfo = "DeclarationInfo",
23-
_provide_declarations = "provide_declarations",
23+
_declaration_info = "declaration_info",
2424
)
2525
load(
2626
"//internal/providers:js_providers.bzl",
@@ -44,8 +44,8 @@ load(
4444
_node_modules_aspect = "node_modules_aspect",
4545
)
4646

47-
provide_declarations = _provide_declarations
4847
DeclarationInfo = _DeclarationInfo
48+
declaration_info = _declaration_info
4949
JSNamedModuleInfo = _JSNamedModuleInfo
5050
js_named_module_info = _js_named_module_info
5151
JSEcmaScriptModuleInfo = _JSEcmaScriptModuleInfo

0 commit comments

Comments
 (0)