From 6038ea09c9e80972ac37817dd69044e8bd187f30 Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders Date: Mon, 8 May 2017 10:40:24 -0700 Subject: [PATCH 1/3] Simplify JS check in index constraint error reporting Any declaration that is a BinaryExpression is a special javascript declaration, and all JS declarations that are checked for index constraint compatibility are, in fact, relevant. --- src/compiler/checker.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index d3e2233bc44e9..a93585156bb83 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -20339,7 +20339,7 @@ namespace ts { // this allows to rule out cases when both property and indexer are inherited from the base class let errorNode: Node; if (propDeclaration && - (getSpecialPropertyAssignmentKind(propDeclaration as BinaryExpression) === SpecialPropertyAssignmentKind.ThisProperty || + (propDeclaration.kind === SyntaxKind.BinaryExpression || propDeclaration.name.kind === SyntaxKind.ComputedPropertyName || prop.parent === containingType.symbol)) { errorNode = propDeclaration; From 5ad2ced0c345141272ea101656d1b0ba45411884 Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders Date: Mon, 8 May 2017 11:03:41 -0700 Subject: [PATCH 2/3] Update test with trickier case from #15616 --- ...aintOfJavascriptClassExpression.errors.txt | 42 +++++++++++++++++++ ...exConstraintOfJavascriptClassExpression.js | 32 -------------- ...exConstraintOfJavascriptClassExpression.ts | 8 ++++ 3 files changed, 50 insertions(+), 32 deletions(-) create mode 100644 tests/baselines/reference/checkIndexConstraintOfJavascriptClassExpression.errors.txt delete mode 100644 tests/baselines/reference/checkIndexConstraintOfJavascriptClassExpression.js diff --git a/tests/baselines/reference/checkIndexConstraintOfJavascriptClassExpression.errors.txt b/tests/baselines/reference/checkIndexConstraintOfJavascriptClassExpression.errors.txt new file mode 100644 index 0000000000000..f648f52bb58ee --- /dev/null +++ b/tests/baselines/reference/checkIndexConstraintOfJavascriptClassExpression.errors.txt @@ -0,0 +1,42 @@ +tests/cases/compiler/weird.js(1,1): error TS2304: Cannot find name 'someFunction'. +tests/cases/compiler/weird.js(1,23): error TS7006: Parameter 'BaseClass' implicitly has an 'any' type. +tests/cases/compiler/weird.js(4,17): error TS8009: 'const' can only be used in a .ts file. +tests/cases/compiler/weird.js(4,17): error TS1248: A class member cannot have the 'const' keyword. +tests/cases/compiler/weird.js(5,3): error TS2377: Constructors for derived classes must contain a 'super' call. +tests/cases/compiler/weird.js(6,4): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +tests/cases/compiler/weird.js(8,25): error TS7006: Parameter 'error' implicitly has an 'any' type. +tests/cases/compiler/weird.js(9,54): error TS2663: Cannot find name 'DEFAULT_MESSAGE'. Did you mean the instance member 'this.DEFAULT_MESSAGE'? + + +==== tests/cases/compiler/weird.js (8 errors) ==== + someFunction(function(BaseClass) { + ~~~~~~~~~~~~ +!!! error TS2304: Cannot find name 'someFunction'. + ~~~~~~~~~ +!!! error TS7006: Parameter 'BaseClass' implicitly has an 'any' type. + 'use strict'; + class Hello extends BaseClass { + const DEFAULT_MESSAGE = "nop!"; + ~~~~~ +!!! error TS8009: 'const' can only be used in a .ts file. + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS1248: A class member cannot have the 'const' keyword. + constructor() { + ~~~~~~~~~~~~~~~ + this.foo = "bar"; + ~~~~~~~~~~~~~~~~~~~~ + ~~~~ +!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. + } + ~~~ +!!! error TS2377: Constructors for derived classes must contain a 'super' call. + _render(error) { + ~~~~~ +!!! error TS7006: Parameter 'error' implicitly has an 'any' type. + const message = error.message || DEFAULT_MESSAGE; + ~~~~~~~~~~~~~~~ +!!! error TS2663: Cannot find name 'DEFAULT_MESSAGE'. Did you mean the instance member 'this.DEFAULT_MESSAGE'? + } + } + }); + \ No newline at end of file diff --git a/tests/baselines/reference/checkIndexConstraintOfJavascriptClassExpression.js b/tests/baselines/reference/checkIndexConstraintOfJavascriptClassExpression.js deleted file mode 100644 index a23461a1b5864..0000000000000 --- a/tests/baselines/reference/checkIndexConstraintOfJavascriptClassExpression.js +++ /dev/null @@ -1,32 +0,0 @@ -//// [weird.js] -someFunction(function(BaseClass) { - class Hello extends BaseClass { - constructor() { - this.foo = "bar"; - } - } -}); - - -//// [foo.js] -var __extends = (this && this.__extends) || (function () { - var extendStatics = Object.setPrototypeOf || - ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || - function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; - return function (d, b) { - extendStatics(d, b); - function __() { this.constructor = d; } - d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); - }; -})(); -someFunction(function (BaseClass) { - var Hello = (function (_super) { - __extends(Hello, _super); - function Hello() { - var _this = this; - _this.foo = "bar"; - return _this; - } - return Hello; - }(BaseClass)); -}); diff --git a/tests/cases/compiler/checkIndexConstraintOfJavascriptClassExpression.ts b/tests/cases/compiler/checkIndexConstraintOfJavascriptClassExpression.ts index 8de18a695365d..51af2b276a27f 100644 --- a/tests/cases/compiler/checkIndexConstraintOfJavascriptClassExpression.ts +++ b/tests/cases/compiler/checkIndexConstraintOfJavascriptClassExpression.ts @@ -1,10 +1,18 @@ // @Filename: weird.js // @allowJs: true +// @checkJs: true +// @strict: true +// @noEmit: true // @out: foo.js someFunction(function(BaseClass) { + 'use strict'; class Hello extends BaseClass { + const DEFAULT_MESSAGE = "nop!"; constructor() { this.foo = "bar"; } + _render(error) { + const message = error.message || DEFAULT_MESSAGE; + } } }); From 2e9143aaf0957d76d8fb7af73ff5b8b461c6bbd3 Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders Date: Mon, 8 May 2017 11:36:30 -0700 Subject: [PATCH 3/3] Clean up test a little --- ...aintOfJavascriptClassExpression.errors.txt | 45 +++++++------------ ...exConstraintOfJavascriptClassExpression.ts | 21 ++++----- 2 files changed, 27 insertions(+), 39 deletions(-) diff --git a/tests/baselines/reference/checkIndexConstraintOfJavascriptClassExpression.errors.txt b/tests/baselines/reference/checkIndexConstraintOfJavascriptClassExpression.errors.txt index f648f52bb58ee..d90e6d506ff66 100644 --- a/tests/baselines/reference/checkIndexConstraintOfJavascriptClassExpression.errors.txt +++ b/tests/baselines/reference/checkIndexConstraintOfJavascriptClassExpression.errors.txt @@ -1,42 +1,29 @@ tests/cases/compiler/weird.js(1,1): error TS2304: Cannot find name 'someFunction'. tests/cases/compiler/weird.js(1,23): error TS7006: Parameter 'BaseClass' implicitly has an 'any' type. -tests/cases/compiler/weird.js(4,17): error TS8009: 'const' can only be used in a .ts file. -tests/cases/compiler/weird.js(4,17): error TS1248: A class member cannot have the 'const' keyword. -tests/cases/compiler/weird.js(5,3): error TS2377: Constructors for derived classes must contain a 'super' call. -tests/cases/compiler/weird.js(6,4): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. -tests/cases/compiler/weird.js(8,25): error TS7006: Parameter 'error' implicitly has an 'any' type. -tests/cases/compiler/weird.js(9,54): error TS2663: Cannot find name 'DEFAULT_MESSAGE'. Did you mean the instance member 'this.DEFAULT_MESSAGE'? +tests/cases/compiler/weird.js(6,13): error TS2346: Supplied parameters do not match any signature of call target. +tests/cases/compiler/weird.js(9,17): error TS7006: Parameter 'error' implicitly has an 'any' type. -==== tests/cases/compiler/weird.js (8 errors) ==== +==== tests/cases/compiler/weird.js (4 errors) ==== someFunction(function(BaseClass) { ~~~~~~~~~~~~ !!! error TS2304: Cannot find name 'someFunction'. ~~~~~~~~~ !!! error TS7006: Parameter 'BaseClass' implicitly has an 'any' type. - 'use strict'; - class Hello extends BaseClass { - const DEFAULT_MESSAGE = "nop!"; + 'use strict'; + const DEFAULT_MESSAGE = "nop!"; + class Hello extends BaseClass { + constructor() { + super(); + ~~~~~~~ +!!! error TS2346: Supplied parameters do not match any signature of call target. + this.foo = "bar"; + } + _render(error) { ~~~~~ -!!! error TS8009: 'const' can only be used in a .ts file. - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS1248: A class member cannot have the 'const' keyword. - constructor() { - ~~~~~~~~~~~~~~~ - this.foo = "bar"; - ~~~~~~~~~~~~~~~~~~~~ - ~~~~ -!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. - } - ~~~ -!!! error TS2377: Constructors for derived classes must contain a 'super' call. - _render(error) { - ~~~~~ !!! error TS7006: Parameter 'error' implicitly has an 'any' type. - const message = error.message || DEFAULT_MESSAGE; - ~~~~~~~~~~~~~~~ -!!! error TS2663: Cannot find name 'DEFAULT_MESSAGE'. Did you mean the instance member 'this.DEFAULT_MESSAGE'? - } - } + const message = error.message || DEFAULT_MESSAGE; + } + } }); \ No newline at end of file diff --git a/tests/cases/compiler/checkIndexConstraintOfJavascriptClassExpression.ts b/tests/cases/compiler/checkIndexConstraintOfJavascriptClassExpression.ts index 51af2b276a27f..58844d98fd8f2 100644 --- a/tests/cases/compiler/checkIndexConstraintOfJavascriptClassExpression.ts +++ b/tests/cases/compiler/checkIndexConstraintOfJavascriptClassExpression.ts @@ -5,14 +5,15 @@ // @noEmit: true // @out: foo.js someFunction(function(BaseClass) { - 'use strict'; - class Hello extends BaseClass { - const DEFAULT_MESSAGE = "nop!"; - constructor() { - this.foo = "bar"; - } - _render(error) { - const message = error.message || DEFAULT_MESSAGE; - } - } + 'use strict'; + const DEFAULT_MESSAGE = "nop!"; + class Hello extends BaseClass { + constructor() { + super(); + this.foo = "bar"; + } + _render(error) { + const message = error.message || DEFAULT_MESSAGE; + } + } });