diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 353356c208b..6e249c3977d 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -20487,7 +20487,7 @@ namespace ts { errorMessage = Diagnostics.Class_0_defines_instance_member_accessor_1_but_extended_class_2_defines_it_as_instance_member_function; } - error(derived.valueDeclaration.name, errorMessage, typeToString(baseType), symbolToString(base), typeToString(type)); + error(derived.valueDeclaration.name || derived.valueDeclaration, errorMessage, typeToString(baseType), symbolToString(base), typeToString(type)); } } } diff --git a/src/compiler/program.ts b/src/compiler/program.ts index d7d93f49ee7..0971adfc098 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -930,20 +930,22 @@ namespace ts { */ function shouldReportDiagnostic(diagnostic: Diagnostic) { const { file, start } = diagnostic; - const lineStarts = getLineStarts(file); - let { line } = computeLineAndCharacterOfPosition(lineStarts, start); - while (line > 0) { - const previousLineText = file.text.slice(lineStarts[line - 1], lineStarts[line]); - const result = ignoreDiagnosticCommentRegEx.exec(previousLineText); - if (!result) { - // non-empty line - return true; + if (file) { + const lineStarts = getLineStarts(file); + let { line } = computeLineAndCharacterOfPosition(lineStarts, start); + while (line > 0) { + const previousLineText = file.text.slice(lineStarts[line - 1], lineStarts[line]); + const result = ignoreDiagnosticCommentRegEx.exec(previousLineText); + if (!result) { + // non-empty line + return true; + } + if (result[3]) { + // @ts-ignore + return false; + } + line--; } - if (result[3]) { - // @ts-ignore - return false; - } - line--; } return true; } diff --git a/tests/baselines/reference/checkJsFiles_noErrorLocation.errors.txt b/tests/baselines/reference/checkJsFiles_noErrorLocation.errors.txt new file mode 100644 index 00000000000..758b295a49d --- /dev/null +++ b/tests/baselines/reference/checkJsFiles_noErrorLocation.errors.txt @@ -0,0 +1,25 @@ +tests/cases/compiler/a.js(14,5): error TS2424: Class 'A' defines instance member function 'foo', but extended class 'B' defines it as instance member property. + + +==== tests/cases/compiler/a.js (1 errors) ==== + // @ts-check + class A { + constructor() { + + } + foo() { + return 4; + } + } + + class B extends A { + constructor() { + super(); + this.foo = () => 3; + ~~~~~~~~~~~~~~~~~~ +!!! error TS2424: Class 'A' defines instance member function 'foo', but extended class 'B' defines it as instance member property. + } + } + + const i = new B(); + i.foo(); \ No newline at end of file diff --git a/tests/cases/compiler/checkJsFiles_noErrorLocation.ts b/tests/cases/compiler/checkJsFiles_noErrorLocation.ts new file mode 100644 index 00000000000..a0b12c47115 --- /dev/null +++ b/tests/cases/compiler/checkJsFiles_noErrorLocation.ts @@ -0,0 +1,24 @@ +// @allowJs: true +// @checkJs: true +// @noEmit: true + +// @fileName: a.js +// @ts-check +class A { + constructor() { + + } + foo() { + return 4; + } +} + +class B extends A { + constructor() { + super(); + this.foo = () => 3; + } +} + +const i = new B(); +i.foo(); \ No newline at end of file