Call gatherDiagnostics to perform syntax/semantic/type checks This was accidentally removed in recent refactoring https://github.com/bazelbuild/rules_typescript/commit/1b33eba25c49d2249945374255579818dc0e94df which caused TS compilations to incorrectly succeed when the code contains errors. Restore missing tests which would have caught the regression; these were also accidentally disabled when Bazel BUILD files were refactored. PiperOrigin-RevId: 224869541
diff --git a/internal/BUILD.bazel b/internal/BUILD.bazel index 3762b8c..4d3b30e 100644 --- a/internal/BUILD.bazel +++ b/internal/BUILD.bazel
@@ -17,11 +17,11 @@ package(default_visibility = ["//visibility:public"]) exports_files([ - "tsconfig.json", # Exported to be consumed for generating skydoc. "build_defs.bzl", "ts_config.bzl", "ts_repositories.bzl", + "tsetse/tsconfig.json", ]) load("//internal:defaults.bzl", "ts_library") @@ -98,6 +98,7 @@ ":tsc_wrapped", "@npm//@types/jasmine", "@npm//@types/node", + "@npm//tsickle", "@npm//typescript", ], )
diff --git a/internal/tsc_wrapped/tsc_wrapped.ts b/internal/tsc_wrapped/tsc_wrapped.ts index f2bb9ab..eb8bcbb 100644 --- a/internal/tsc_wrapped/tsc_wrapped.ts +++ b/internal/tsc_wrapped/tsc_wrapped.ts
@@ -205,6 +205,30 @@ compilerHost.inputFiles, options, compilerHost, oldProgram)); cache.putProgram(bazelOpts.target, program); + if (!bazelOpts.isJsTranspilation) { + // If there are any TypeScript type errors abort now, so the error + // messages refer to the original source. After any subsequent passes + // (decorator downleveling or tsickle) we do not type check. + let diagnostics = + gatherDiagnostics(options, bazelOpts, program, disabledTsetseRules); + if (!expectDiagnosticsWhitelist.length || + expectDiagnosticsWhitelist.some(p => bazelOpts.target.startsWith(p))) { + diagnostics = bazelDiagnostics.filterExpected( + bazelOpts, diagnostics, bazelDiagnostics.uglyFormat); + } else if (bazelOpts.expectedDiagnostics.length > 0) { + console.error( + `Only targets under ${ + expectDiagnosticsWhitelist.join(', ')} can use ` + + 'expected_diagnostics, but got', + bazelOpts.target); + } + + if (diagnostics.length > 0) { + console.error(bazelDiagnostics.format(bazelOpts.target, diagnostics)); + debug('compilation failed at', new Error().stack!); + return false; + } + } const compilationTargets = program.getSourceFiles().filter( fileName => isCompilationTarget(bazelOpts, fileName));
diff --git a/internal/tsetse/tests/ban_expect_truthy_promise/BUILD b/internal/tsetse/tests/ban_expect_truthy_promise/BUILD new file mode 100644 index 0000000..dc328a1 --- /dev/null +++ b/internal/tsetse/tests/ban_expect_truthy_promise/BUILD
@@ -0,0 +1,60 @@ +# Copyright 2017 The Bazel Authors. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +licenses(["notice"]) # Apache 2.0 + +load("//internal:defaults.bzl", "ts_library") + +error_message = "error TS21224: Value passed.*" + +ts_library( + name = "jasmine", + testonly = 1, + srcs = [ + "jasmine_types.ts", + ], +) + +ts_library( + name = "positives", + testonly = 1, + tsconfig = "//internal:tsetse/tsconfig.json", + srcs = [ + "positives.ts", + ], + expected_diagnostics = [ + "\(29,5\).*" + error_message, + "\(30,5\).*" + error_message, + "\(31,5\).*" + error_message, + "\(32,5\).*" + error_message, + "\(33,5\).*" + error_message, + "\(34,5\).*" + error_message, + "\(35,5\).*" + error_message, + ], + deps = [ + ":jasmine", + ], +) + +ts_library( + name = "negatives", + testonly = 1, + tsconfig = "//internal:tsetse/tsconfig.json", + srcs = [ + "negatives.ts", + ], + deps = [ + ":jasmine", + ], +)
diff --git a/internal/tsetse/tests/ban_promise_as_condition/BUILD b/internal/tsetse/tests/ban_promise_as_condition/BUILD new file mode 100644 index 0000000..5907da5 --- /dev/null +++ b/internal/tsetse/tests/ban_promise_as_condition/BUILD
@@ -0,0 +1,51 @@ +# Copyright 2017 The Bazel Authors. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +licenses(["notice"]) # Apache 2.0 + +load("//internal:defaults.bzl", "ts_library") + +error_message = "error TS21226: Found a thenable.*" + +ts_library( + name = "positives", + testonly = 1, + tsconfig = "//internal:tsetse/tsconfig.json", + srcs = [ + "positives.ts", + ], + expected_diagnostics = [ + "\(7,7\).*" + error_message, + "\(15,7\).*" + error_message, + "\(23,19\).*" + error_message, + "\(25,10\).*" + error_message, + "\(30,34\).*" + error_message, + "\(31,34\).*" + error_message, + "\(37,19\).*" + error_message, + "\(39,10\).*" + error_message, + "\(44,34\).*" + error_message, + "\(45,34\).*" + error_message, + ], + deps = [ + ], +) + +ts_library( + name = "negatives", + testonly = 1, + tsconfig = "//internal:tsetse/tsconfig.json", + srcs = ["negatives.ts"], + deps = [ + ], +)
diff --git a/internal/tsetse/tests/check_return_value/BUILD b/internal/tsetse/tests/check_return_value/BUILD new file mode 100644 index 0000000..be2414a --- /dev/null +++ b/internal/tsetse/tests/check_return_value/BUILD
@@ -0,0 +1,66 @@ +# Copyright 2017 The Bazel Authors. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +licenses(["notice"]) # Apache 2.0 + +load("//internal:defaults.bzl", "ts_library") + + +error_message = "TS21222: return value is unused.\\n\\tSee http://tsetse.info/check-return-value" + +ts_library( + name = "no_expected_diagnostics_test", + testonly = 1, + srcs = ["no_expected_diagnostics.ts"], + tsconfig = "//internal:tsetse/tsconfig.json", +) + +ts_library( + name = "expected_diagnostics_test", + testonly = 1, + srcs = ["expected_diagnostics.ts"], + expected_diagnostics = [ + "\(6,1\).*" + error_message, + "\(8,1\).*" + error_message, + "\(12,1\).*" + error_message, + "\(16,1\).*" + error_message, + "\(19,1\).*" + error_message, + ], + tsconfig = "//internal:tsetse/tsconfig.json", +) + +ts_library( + name = "user_defined_check_return_value", + testonly = 1, + srcs = ["user_defined_check_return_value.ts"], + tsconfig = "//internal:tsetse/tsconfig.json", +) + +ts_library( + name = "unused_return_value_user_defined_function", + testonly = 1, + srcs = ["unused_return_value_user_defined_function.ts"], + expected_diagnostics = [ + "\(4,1\).*" + error_message, + "\(5,1\).*" + error_message, + "\(7,1\).*" + error_message, + "\(9,1\).*" + error_message, + "\(15,1\).*" + error_message, + ], + tsconfig = "//internal:tsetse/tsconfig.json", + deps = [ + ":user_defined_check_return_value", + "@npm//@types/jasmine", + ], +)
diff --git a/internal/tsetse/tests/equals_nan/BUILD b/internal/tsetse/tests/equals_nan/BUILD new file mode 100644 index 0000000..dd08612 --- /dev/null +++ b/internal/tsetse/tests/equals_nan/BUILD
@@ -0,0 +1,43 @@ +# Copyright 2017 The Bazel Authors. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +licenses(["notice"]) # Apache 2.0 + +load("//internal:defaults.bzl", "ts_library") + + +ts_library( + name = "positives", + testonly = 1, + srcs = [ + "positives.ts", + ], + expected_diagnostics = [ + "\(2,19\): error TS21223: x === NaN is always false; use isNaN\(x\) instead", + "\(6,5\): error TS21223: x === NaN is always false; use isNaN\(x\) instead", + "\(7,5\): error TS21223: x == NaN is always false; use isNaN\(x\) instead", + "\(8,5\): error TS21223: x !== NaN is always true; use !isNaN\(x\) instead", + "\(9,5\): error TS21223: x != NaN is always true; use !isNaN\(x\) instead", + "\(11,1\): error TS21223: x === NaN is always false; use isNaN\(x\) instead", + "\(12,1\): error TS21223: x === NaN is always false; use isNaN\(x\) instead", + ], +) + +ts_library( + name = "negatives", + testonly = 1, + srcs = [ + "negatives.ts", + ], +)
diff --git a/internal/tsetse/tests/must_use_promises/BUILD b/internal/tsetse/tests/must_use_promises/BUILD new file mode 100644 index 0000000..16dddf4 --- /dev/null +++ b/internal/tsetse/tests/must_use_promises/BUILD
@@ -0,0 +1,38 @@ +# Copyright 2017 The Bazel Authors. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +licenses(["notice"]) # Apache 2.0 + +load("//internal:defaults.bzl", "ts_library") + + +error_message = "TS21225: All Promises in async functions must either be awaited or used in an expression." + +ts_library( + name = "positives", + testonly = 1, + srcs = [ + "positives.ts", + ], + tsconfig = "//internal:tsetse/tsconfig.json", + expected_diagnostics = [ + "\(29,3\)" + error_message, + "\(30,3\)" + error_message, + "\(31,3\)" + error_message, + "\(32,3\)" + error_message, + "\(34,3\)" + error_message, + ], + deps = [ + ], +)
diff --git a/internal/tsetse/tsconfig.json b/internal/tsetse/tsconfig.json index cbcca14..f83ea7f 100644 --- a/internal/tsetse/tsconfig.json +++ b/internal/tsetse/tsconfig.json
@@ -2,7 +2,6 @@ "compilerOptions": { "strict": true, "types": [ - "node" ], "lib": [ "dom",