Merge pull request #5876 from RyanCavanaugh/javaScriptPrototypes

JavaScript prototype class inference
This commit is contained in:
Ryan Cavanaugh
2015-12-14 11:42:18 -08:00
10 changed files with 308 additions and 41 deletions

View File

@@ -0,0 +1,46 @@
///<reference path="fourslash.ts" />
// Assignments to the 'prototype' property of a function create a class
// @allowNonTsExtensions: true
// @Filename: myMod.js
//// function myCtor(x) {
//// }
//// myCtor.prototype.foo = function() { return 32 };
//// myCtor.prototype.bar = function() { return '' };
////
//// var m = new myCtor(10);
//// m/*1*/
//// var a = m.foo;
//// a/*2*/
//// var b = a();
//// b/*3*/
//// var c = m.bar();
//// c/*4*/
// Members of the class instance
goTo.marker('1');
edit.insert('.');
verify.memberListContains('foo', undefined, undefined, 'property');
verify.memberListContains('bar', undefined, undefined, 'property');
edit.backspace();
// Members of a class method (1)
goTo.marker('2');
edit.insert('.');
verify.memberListContains('length', undefined, undefined, 'property');
edit.backspace();
// Members of the invocation of a class method (1)
goTo.marker('3');
edit.insert('.');
verify.memberListContains('toFixed', undefined, undefined, 'method');
verify.not.memberListContains('substr', undefined, undefined, 'method');
edit.backspace();
// Members of the invocation of a class method (2)
goTo.marker('4');
edit.insert('.');
verify.memberListContains('substr', undefined, undefined, 'method');
verify.not.memberListContains('toFixed', undefined, undefined, 'method');

View File

@@ -0,0 +1,36 @@
///<reference path="fourslash.ts" />
// Assignments to 'this' in the constructorish body create
// properties with those names
// @allowNonTsExtensions: true
// @Filename: myMod.js
//// function myCtor(x) {
//// this.qua = 10;
//// }
//// myCtor.prototype.foo = function() { return 32 };
//// myCtor.prototype.bar = function() { return '' };
////
//// var m = new myCtor(10);
//// m/*1*/
//// var x = m.qua;
//// x/*2*/
//// myCtor/*3*/
// Verify the instance property exists
goTo.marker('1');
edit.insert('.');
verify.completionListContains('qua', undefined, undefined, 'property');
edit.backspace();
// Verify the type of the instance property
goTo.marker('2');
edit.insert('.');
verify.completionListContains('toFixed', undefined, undefined, 'method');
goTo.marker('3');
edit.insert('.');
// Make sure symbols don't leak out into the constructor
verify.completionListContains('qua', undefined, undefined, 'warning');
verify.completionListContains('foo', undefined, undefined, 'warning');
verify.completionListContains('bar', undefined, undefined, 'warning');

View File

@@ -0,0 +1,20 @@
///<reference path="fourslash.ts" />
// Inside an inferred method body, the type of 'this' is the class type
// @allowNonTsExtensions: true
// @Filename: myMod.js
//// function myCtor(x) {
//// this.qua = 10;
//// }
//// myCtor.prototype.foo = function() { return this/**/; };
//// myCtor.prototype.bar = function() { return '' };
////
goTo.marker();
edit.insert('.');
// Check members of the function
verify.completionListContains('foo', undefined, undefined, 'property');
verify.completionListContains('bar', undefined, undefined, 'property');
verify.completionListContains('qua', undefined, undefined, 'property');

View File

@@ -0,0 +1,21 @@
///<reference path="fourslash.ts" />
// Check for any odd symbol leakage
// @allowNonTsExtensions: true
// @Filename: myMod.js
//// function myCtor(x) {
//// this.qua = 10;
//// }
//// myCtor.prototype.foo = function() { return 32 };
//// myCtor.prototype.bar = function() { return '' };
////
//// myCtor/*1*/
goTo.marker('1');
edit.insert('.');
// Check members of the function
verify.completionListContains('foo', undefined, undefined, 'warning');
verify.completionListContains('bar', undefined, undefined, 'warning');
verify.completionListContains('qua', undefined, undefined, 'warning');

View File

@@ -0,0 +1,19 @@
///<reference path="fourslash.ts" />
// No prototype assignments are needed to enable class inference
// @allowNonTsExtensions: true
// @Filename: myMod.js
//// function myCtor() {
//// this.foo = 'hello';
//// this.bar = 10;
//// }
//// let x = new myCtor();
//// x/**/
goTo.marker();
edit.insert('.');
// Check members of the function
verify.completionListContains('foo', undefined, undefined, 'property');
verify.completionListContains('bar', undefined, undefined, 'property');