Skip to content

Commit

Permalink
fix(typescript): don't declare outputs that collide with inputs (#3046)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeagle committed Dec 3, 2021
1 parent e005d82 commit 9b47df1
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 10 deletions.
15 changes: 10 additions & 5 deletions packages/typescript/internal/ts_project.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -340,11 +340,16 @@ def _replace_ext(f, ext_map):

def _out_paths(srcs, outdir, rootdir, allow_js, ext_map):
rootdir_replace_pattern = rootdir + "/" if rootdir else ""
return [
_join(outdir, f[:f.rindex(".")].replace(rootdir_replace_pattern, "") + _replace_ext(f, ext_map))
for f in srcs
if _is_ts_src(f, allow_js)
]
outs = []
for f in srcs:
if _is_ts_src(f, allow_js):
out = _join(outdir, f[:f.rindex(".")].replace(rootdir_replace_pattern, "") + _replace_ext(f, ext_map))

# Don't declare outputs that collide with inputs
# for example, a.js -> a.js
if out != f:
outs.append(out)
return outs

def ts_project_macro(
name = "tsconfig",
Expand Down
12 changes: 12 additions & 0 deletions packages/typescript/test/ts_project/allow_js/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ load("//packages/typescript:index.bzl", "ts_project")
# Ensure that a.js produces outDir/a.js, outDir/a.d.ts, and outDir/a.d.ts.map
SRCS = [
"a.js",
"b.jsx",
]

ts_project(
Expand Down Expand Up @@ -34,3 +35,14 @@ nodejs_test(
"$(locations :transpile)",
],
)

# Test that we can write outputs to the same folder as the inputs
# ts_project shouldn't try to declare a.js as an output in this case
ts_project(
name = "transpile_to_same_dir",
srcs = SRCS,
allow_js = True,
declaration = True,
declaration_map = True,
source_map = True,
)
3 changes: 3 additions & 0 deletions packages/typescript/test/ts_project/allow_js/b.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';
exports.__esModule = true;
exports.b = <b></b>;
11 changes: 6 additions & 5 deletions packages/typescript/test/ts_project/allow_js/verify.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const assert = require('assert');

const types_files = process.argv.slice(2, 4);
const code_file = process.argv[4];
assert.ok(types_files.some(f => f.endsWith('out/a.d.ts')), 'Missing a.d.ts');
assert.ok(types_files.some(f => f.endsWith('out/a.d.ts.map')), 'Missing a.d.ts.map');
assert.ok(code_file.endsWith('out/a.js'), 'Missing a.js');
const output_files = process.argv.slice(2);
for (const file of ['a', 'b']) {
assert.ok(output_files.some(f => f.endsWith(`out/${file}.d.ts`)), `Missing ${file}.d.ts`);
assert.ok(output_files.some(f => f.endsWith(`out/${file}.d.ts.map`)), `Missing ${file}.d.ts.map`);
assert.ok(output_files.some(f => f.endsWith(`out/${file}.js`)), `Missing ${file}.js`);
}

0 comments on commit 9b47df1

Please sign in to comment.