Skip to content

Support this.prop = expr; assignments as declarations for ES6 JS classes #7615

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

Closed
Closed
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
8 changes: 8 additions & 0 deletions src/compiler/binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1478,6 +1478,14 @@ namespace ts {
// It's acceptable for multiple 'this' assignments of the same identifier to occur
declareSymbol(container.symbol.members, container.symbol, node, SymbolFlags.Property, SymbolFlags.PropertyExcludes & ~SymbolFlags.Property);
}
else if (container.kind === SyntaxKind.Constructor && isInJavaScriptFile(node)) {
// this.foo assignment in a JavaScript class
// Bind this property to the containing class
const saveContainer = container;
container = container.parent;
bindPropertyOrMethodOrAccessor(node, SymbolFlags.Property, SymbolFlags.None);
container = saveContainer;
}
}

function bindPrototypePropertyAssignment(node: BinaryExpression) {
Expand Down
13 changes: 13 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2937,6 +2937,14 @@ namespace ts {
}
// Handle module.exports = expr
if (declaration.kind === SyntaxKind.BinaryExpression) {
// Use JS Doc type if present on parent expression statement
if (declaration.flags & NodeFlags.JavaScriptFile) {
const typeTag = getJSDocTypeTag(declaration.parent);
if (typeTag && typeTag.typeExpression) {
return links.type = getTypeFromTypeNode(typeTag.typeExpression.type);
}
}

return links.type = getUnionType(map(symbol.declarations, (decl: BinaryExpression) => checkExpressionCached(decl.right)));
}
if (declaration.kind === SyntaxKind.PropertyAccessExpression) {
Expand Down Expand Up @@ -8037,6 +8045,11 @@ namespace ts {
const binaryExpression = <BinaryExpression>node.parent;
const operator = binaryExpression.operatorToken.kind;
if (operator >= SyntaxKind.FirstAssignment && operator <= SyntaxKind.LastAssignment) {
// Don't do this for special property assignments to avoid circularity
if (getSpecialPropertyAssignmentKind(binaryExpression) !== SpecialPropertyAssignmentKind.None) {
return undefined;
}

// In an assignment expression, the right operand is contextually typed by the type of the left operand.
if (node === binaryExpression.right) {
return checkExpression(binaryExpression.left);
Expand Down
31 changes: 31 additions & 0 deletions tests/cases/fourslash/javaScriptClass1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
///<reference path="fourslash.ts" />

// Classes have their shape inferred from assignments
// to properties of 'this' in the constructor

// @allowNonTsExtensions: true
// @Filename: Foo.js
//// class Foo {
//// constructor() {
//// this.bar = 'world';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a method assignment also for completeness?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

//// this.thing = () => 0;
//// this.union = 'foo';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we check if JsDoc works for the assignment member, e.g. @type, or even @params if the assignment is a method?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added test for @type

//// this.union = 100;
//// }
//// }
//// var x = new Foo();
//// x/**/


goTo.marker();
edit.insert('.');
verify.completionListContains("bar", /*displayText*/ undefined, /*documentation*/ undefined, "property");
verify.completionListContains("thing", /*displayText*/ undefined, /*documentation*/ undefined, "property");
verify.completionListContains("union", /*displayText*/ undefined, /*documentation*/ undefined, "property");

edit.insert('bar.');
verify.completionListContains("substr", /*displayText*/ undefined, /*documentation*/ undefined, "method");
edit.backspace('bar.'.length);

edit.insert('union.');
verify.completionListContains("toString", /*displayText*/ undefined, /*documentation*/ undefined, "method");
22 changes: 22 additions & 0 deletions tests/cases/fourslash/javaScriptClass2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
///<reference path="fourslash.ts" />

// In an inferred class, we can rename successfully

// @allowNonTsExtensions: true
// @Filename: Foo.js
//// class Foo {
//// constructor() {
//// this.[|union|] = 'foo';
//// this./*1*/[|union|] = 100;
//// }
//// method() { return this./*2*/[|union|]; }
//// }
//// var x = new Foo();
//// x./*3*/[|union|];

goTo.marker('1');
verify.renameLocations(/*findInStrings*/false, /*findInComments*/false);
goTo.marker('2');
verify.renameLocations(/*findInStrings*/false, /*findInComments*/false);
goTo.marker('3');
verify.renameLocations(/*findInStrings*/false, /*findInComments*/false);
24 changes: 24 additions & 0 deletions tests/cases/fourslash/javaScriptClass3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
///<reference path="fourslash.ts" />

// In an inferred class, we can to-to-def successfully

// @allowNonTsExtensions: true
// @Filename: Foo.js
//// class Foo {
//// constructor() {
//// /*dst1*/this.alpha = 10;
//// /*dst2*/this.beta = 'gamma';
//// }
//// method() { return this.alpha; }
//// }
//// var x = new Foo();
//// x.alpha/*src1*/;
//// x.beta/*src2*/;

goTo.marker('src1');
goTo.definition();
verify.caretAtMarker('dst1');

goTo.marker('src2');
goTo.definition();
verify.caretAtMarker('dst2');
22 changes: 22 additions & 0 deletions tests/cases/fourslash/javaScriptClass4.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
///<reference path="fourslash.ts" />

// Classes have their shape inferred from assignments
// to properties of 'this' in the constructor

// @allowNonTsExtensions: true
// @Filename: Foo.js
//// class Foo {
//// constructor() {
//// /**
//// * @type {string}
//// */
//// this.baz = null;
//// }
//// }
//// var x = new Foo();
//// x/**/

goTo.marker();
edit.insert('.baz.');
verify.completionListContains("substr", /*displayText*/ undefined, /*documentation*/ undefined, "method");