Skip to content

fix(51521): Using a of property declared after an initializing constructor triggers an assertion failure in JS #51524

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3531,7 +3531,7 @@ export function isSpecialPropertyDeclaration(expr: PropertyAccessExpression | El
export function setValueDeclaration(symbol: Symbol, node: Declaration): void {
const { valueDeclaration } = symbol;
if (!valueDeclaration ||
!(node.flags & NodeFlags.Ambient && !(valueDeclaration.flags & NodeFlags.Ambient)) &&
!(node.flags & NodeFlags.Ambient && !isInJSFile(node) && !(valueDeclaration.flags & NodeFlags.Ambient)) &&
(isAssignmentDeclaration(valueDeclaration) && !isAssignmentDeclaration(node)) ||
(valueDeclaration.kind !== node.kind && isEffectiveModuleDeclaration(valueDeclaration))) {
// other kinds of value declarations take precedence over modules and assignment declarations
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/test.js(3,9): error TS2322: Type '{}' is not assignable to type 'string'.
/test.js(6,5): error TS8009: The 'declare' modifier can only be used in TypeScript files.
/test.js(6,19): error TS8010: Type annotations can only be used in TypeScript files.
/test.js(9,19): error TS2339: Property 'foo' does not exist on type 'string'.


==== /test.js (4 errors) ====
class Foo {
constructor() {
this.prop = {};
~~~~~~~~~
!!! error TS2322: Type '{}' is not assignable to type 'string'.
}

declare prop: string;
~~~~~~~
!!! error TS8009: The 'declare' modifier can only be used in TypeScript files.
~~~~~~
!!! error TS8010: Type annotations can only be used in TypeScript files.

method() {
this.prop.foo
~~~
!!! error TS2339: Property 'foo' does not exist on type 'string'.
}
}

24 changes: 24 additions & 0 deletions tests/baselines/reference/ambientPropertyDeclarationInJs.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
=== /test.js ===
class Foo {
>Foo : Symbol(Foo, Decl(test.js, 0, 0))

constructor() {
this.prop = {};
>this.prop : Symbol(Foo.prop, Decl(test.js, 1, 19), Decl(test.js, 3, 5))
>this : Symbol(Foo, Decl(test.js, 0, 0))
>prop : Symbol(Foo.prop, Decl(test.js, 1, 19), Decl(test.js, 3, 5))
}

declare prop: string;
>prop : Symbol(Foo.prop, Decl(test.js, 1, 19), Decl(test.js, 3, 5))

method() {
>method : Symbol(Foo.method, Decl(test.js, 5, 25))

this.prop.foo
>this.prop : Symbol(Foo.prop, Decl(test.js, 1, 19), Decl(test.js, 3, 5))
>this : Symbol(Foo, Decl(test.js, 0, 0))
>prop : Symbol(Foo.prop, Decl(test.js, 1, 19), Decl(test.js, 3, 5))
}
}

28 changes: 28 additions & 0 deletions tests/baselines/reference/ambientPropertyDeclarationInJs.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
=== /test.js ===
class Foo {
>Foo : Foo

constructor() {
this.prop = {};
>this.prop = {} : {}
>this.prop : string
>this : this
>prop : string
>{} : {}
}

declare prop: string;
>prop : string

method() {
>method : () => void

this.prop.foo
>this.prop.foo : any
>this.prop : string
>this : this
>prop : string
>foo : any
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
=== /a.js ===
// class C {
// constructor() {
// this.prop = "";
// }
// declare prop: string;
// method() {
// this.prop.foo
// ^^^
// | ----------------------------------------------------------------------
// | any
// | ----------------------------------------------------------------------
// }
// }

[
{
"marker": {
"fileName": "/a.js",
"position": 122,
"name": ""
},
"item": {
"kind": "",
"kindModifiers": "",
"textSpan": {
"start": 119,
"length": 3
},
"displayParts": [
{
"text": "any",
"kind": "keyword"
}
]
}
}
]
16 changes: 16 additions & 0 deletions tests/cases/compiler/ambientPropertyDeclarationInJs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// @allowJs: true
// @checkJs: true
// @noEmit: true
// @filename: /test.js

class Foo {
constructor() {
this.prop = {};
}

declare prop: string;

method() {
this.prop.foo
}
}
15 changes: 15 additions & 0 deletions tests/cases/fourslash/quickInfoAtPropWithAmbientDeclarationInJs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/// <reference path="fourslash.ts" />

// @allowJs: true
// @filename: /a.js
////class C {
//// constructor() {
//// this.prop = "";
//// }
//// declare prop: string;
//// method() {
//// this.prop.foo/**/
//// }
////}

verify.baselineQuickInfo();