Skip to content

Commit f9cca25

Browse files
authored
Fix lack of error on default export of nonexistant name (microsoft#40094)
* Modify test harness so it can report underlying issue, fix small parent pointer issue * Fix underlying export asignment check issue and fix lints * Ensure class/function duplicate declaration errors are reported regarless of which is encountered first * Ensure flag conflict errors are reported regardless of which declaration is encountered first
1 parent 156a6e2 commit f9cca25

8 files changed

+50
-7
lines changed

Diff for: src/compiler/checker.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -32269,11 +32269,8 @@ namespace ts {
3226932269
}
3227032270

3227132271
if (symbol.parent) {
32272-
// run check once for the first declaration
32273-
if (getDeclarationOfKind(symbol, node.kind) === node) {
32274-
// run check on export symbol to check that modifiers agree across all exported declarations
32275-
checkFunctionOrConstructorSymbol(symbol);
32276-
}
32272+
// run check on export symbol to check that modifiers agree across all exported declarations
32273+
checkFunctionOrConstructorSymbol(symbol);
3227732274
}
3227832275
}
3227932276

@@ -34457,6 +34454,7 @@ namespace ts {
3445734454
const typeWithThis = getTypeWithThisArgument(type);
3445834455
const staticType = <ObjectType>getTypeOfSymbol(symbol);
3445934456
checkTypeParameterListsIdentical(symbol);
34457+
checkFunctionOrConstructorSymbol(symbol);
3446034458
checkClassForDuplicateDeclarations(node);
3446134459

3446234460
// Only check for reserved static identifiers on non-ambient context.
@@ -35645,6 +35643,9 @@ namespace ts {
3564535643
checkExpressionCached(node.expression);
3564635644
}
3564735645
}
35646+
else {
35647+
checkExpressionCached(node.expression); // doesn't resolve, check as expression to mark as error
35648+
}
3564835649

3564935650
if (getEmitDeclarations(compilerOptions)) {
3565035651
collectLinkedAliases(node.expression as Identifier, /*setVisibility*/ true);

Diff for: src/compiler/program.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1854,7 +1854,7 @@ namespace ts {
18541854
switch (node.kind) {
18551855
case SyntaxKind.ImportClause:
18561856
if ((node as ImportClause).isTypeOnly) {
1857-
diagnostics.push(createDiagnosticForNode(node.parent, Diagnostics._0_declarations_can_only_be_used_in_TypeScript_files, "import type"));
1857+
diagnostics.push(createDiagnosticForNode(parent, Diagnostics._0_declarations_can_only_be_used_in_TypeScript_files, "import type"));
18581858
return "skip";
18591859
}
18601860
break;

Diff for: src/harness/compilerImpl.ts

+21-1
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,29 @@ namespace compiler {
256256
if (compilerOptions.skipDefaultLibCheck === undefined) compilerOptions.skipDefaultLibCheck = true;
257257
if (compilerOptions.noErrorTruncation === undefined) compilerOptions.noErrorTruncation = true;
258258

259+
const preProgram = ts.createProgram(rootFiles || [], { ...compilerOptions, traceResolution: false }, host);
260+
const preErrors = ts.getPreEmitDiagnostics(preProgram);
261+
259262
const program = ts.createProgram(rootFiles || [], compilerOptions, host);
260263
const emitResult = program.emit();
261-
const errors = ts.getPreEmitDiagnostics(program);
264+
const postErrors = ts.getPreEmitDiagnostics(program);
265+
const errors = (preErrors.length !== postErrors.length) ? [...postErrors,
266+
ts.addRelatedInfo(
267+
ts.createCompilerDiagnostic({
268+
category: ts.DiagnosticCategory.Error,
269+
code: -1,
270+
key: "-1",
271+
message: `Pre-emit (${preErrors.length}) and post-emit (${postErrors.length}) diagnostic counts do not match! This can indicate that a semantic _error_ was added by the emit resolver - such an error may not be reflected on the command line or in the editor, but may be captured in a baseline here!`
272+
}),
273+
ts.createCompilerDiagnostic({
274+
category: ts.DiagnosticCategory.Error,
275+
code: -1,
276+
key: "-1",
277+
message: `The excess diagnostics are:`
278+
}),
279+
...ts.filter(postErrors, p => !ts.some(preErrors, p2 => ts.compareDiagnostics(p, p2) === ts.Comparison.EqualTo))
280+
)
281+
] : postErrors;
262282
return new CompilationResult(host, compilerOptions, program, emitResult, errors);
263283
}
264284
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
tests/cases/compiler/exportDefaultMissingName.ts(1,16): error TS2304: Cannot find name 'xyzzy'.
2+
3+
4+
==== tests/cases/compiler/exportDefaultMissingName.ts (1 errors) ====
5+
export default xyzzy;
6+
~~~~~
7+
!!! error TS2304: Cannot find name 'xyzzy'.
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//// [exportDefaultMissingName.ts]
2+
export default xyzzy;
3+
4+
//// [exportDefaultMissingName.js]
5+
"use strict";
6+
exports.__esModule = true;
7+
exports["default"] = xyzzy;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
=== tests/cases/compiler/exportDefaultMissingName.ts ===
2+
export default xyzzy;
3+
No type information for this code.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
=== tests/cases/compiler/exportDefaultMissingName.ts ===
2+
export default xyzzy;
3+
>xyzzy : any
4+

Diff for: tests/cases/compiler/exportDefaultMissingName.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default xyzzy;

0 commit comments

Comments
 (0)