From 26d5af384c6c26ee3c4aa0f903fd04e6220ffa0c Mon Sep 17 00:00:00 2001 From: Mohamed Hegazy Date: Mon, 17 Apr 2017 13:15:06 -0700 Subject: [PATCH 1/2] Check for the file before getting line map --- src/compiler/program.ts | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/src/compiler/program.ts b/src/compiler/program.ts index f2d44adb1f1..3661ca647ef 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; } From 4546b0dd8e2bae4cefb91e6c24717824e0f71c9e Mon Sep 17 00:00:00 2001 From: Mohamed Hegazy Date: Mon, 17 Apr 2017 13:15:27 -0700 Subject: [PATCH 2/2] Make sure we have a node for the error in cases of special property assignment --- src/compiler/checker.ts | 2 +- .../checkJsFiles_noErrorLocation.errors.txt | 25 +++++++++++++++++++ .../compiler/checkJsFiles_noErrorLocation.ts | 24 ++++++++++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 tests/baselines/reference/checkJsFiles_noErrorLocation.errors.txt create mode 100644 tests/cases/compiler/checkJsFiles_noErrorLocation.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index f11112811f5..c7d7da5e678 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -20530,7 +20530,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/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