Skip to content

Commit fabc43d

Browse files
committed
JS Prototypes WIP
1 parent c4b0b62 commit fabc43d

2 files changed

Lines changed: 11 additions & 23 deletions

File tree

src/compiler/binder.ts

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1367,42 +1367,31 @@ namespace ts {
13671367
}
13681368

13691369
function bindThisPropertyAssignment(node: BinaryExpression) {
1370+
// Declare a 'member' in case it turns out the container was an ES5 class
13701371
if (container.kind === SyntaxKind.FunctionExpression || container.kind === SyntaxKind.FunctionDeclaration) {
13711372
container.symbol.members = container.symbol.members || {};
1372-
declareClassMember(node, SymbolFlags.Property, SymbolFlags.PropertyExcludes);
1373+
declareSymbol(container.symbol.members, container.symbol, node, SymbolFlags.Property, SymbolFlags.PropertyExcludes);
13731374
}
13741375
}
13751376

13761377
function bindPrototypePropertyAssignment(node: BinaryExpression) {
1377-
// We saw a node of the form 'x.prototype.y = z'.
1378-
// This does two things: turns 'x' into a constructor function, and
1379-
// adds a member 'y' to the result of that constructor function
1380-
// Get 'x', the class
1381-
const classId = <Identifier>(<PropertyAccessExpression>(<PropertyAccessExpression>node.left).expression).expression;
1378+
// We saw a node of the form 'x.prototype.y = z'. Declare a 'member' y on x if x was a function.
13821379

1383-
// Look up the function in the local scope, since prototype assignments should immediately
1380+
// Look up the function in the local scope, since prototype assignments should
13841381
// follow the function declaration
1382+
const classId = <Identifier>(<PropertyAccessExpression>(<PropertyAccessExpression>node.left).expression).expression;
13851383
const funcSymbol = container.locals[classId.text];
1386-
if (!funcSymbol) {
1384+
if (!funcSymbol || !(funcSymbol.flags & SymbolFlags.Function)) {
13871385
return;
13881386
}
13891387

1390-
// The function is now a constructor rather than a normal function
1391-
if (!funcSymbol.inferredConstructor) {
1392-
// Have the binder set up all the related class symbols for us
1393-
declareSymbol(container.locals, funcSymbol, funcSymbol.valueDeclaration, SymbolFlags.Class, SymbolFlags.None);
1394-
// funcSymbol.members = funcSymbol.members || {};
1395-
funcSymbol.members["__constructor"] = funcSymbol;
1396-
funcSymbol.inferredConstructor = true;
1388+
// Set up the members collection if it doesn't exist already
1389+
if (!funcSymbol.members) {
1390+
funcSymbol.members = {};
13971391
}
13981392

1399-
// Get the exports of the class so we can add the method to it
1400-
const funcExports = declareSymbol(funcSymbol.exports, funcSymbol, <PropertyAccessExpression>(<PropertyAccessExpression>node.left).expression, SymbolFlags.ObjectLiteral | SymbolFlags.Property, SymbolFlags.None);
1401-
1402-
// Declare the method
1403-
declareSymbol(funcExports.members, funcExports, <PropertyAccessExpression>node.left, SymbolFlags.Method, SymbolFlags.None);
1404-
// and on the members of the function so it appears in 'prototype'
1405-
declareSymbol(funcSymbol.members, funcSymbol, <PropertyAccessExpression>node.left, SymbolFlags.Method, SymbolFlags.PropertyExcludes);
1393+
// Declare the method/property
1394+
declareSymbol(funcSymbol.members, funcSymbol, <PropertyAccessExpression>node.left, SymbolFlags.Property, SymbolFlags.PropertyExcludes);
14061395
}
14071396

14081397
function bindCallExpression(node: CallExpression) {

src/compiler/types.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1985,7 +1985,6 @@ namespace ts {
19851985
/* @internal */ parent?: Symbol; // Parent symbol
19861986
/* @internal */ exportSymbol?: Symbol; // Exported symbol associated with this symbol
19871987
/* @internal */ constEnumOnlyModule?: boolean; // True if module contains only const enums or other modules with only const enums
1988-
/* @internal */ inferredConstructor?: boolean; // A function promoted to constructor as the result of a prototype property assignment
19891988
}
19901989

19911990
/* @internal */

0 commit comments

Comments
 (0)