Skip to content

Commit 2f0e581

Browse files
committed
JS Property assignments create namespaces (hacky)
This version isn't done yet and I think it still causes failures in the test suite.
1 parent fa96bd4 commit 2f0e581

2 files changed

Lines changed: 20 additions & 16 deletions

File tree

src/compiler/binder.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,9 @@ namespace ts {
273273
return getEscapedTextOfIdentifierOrLiteral(<Identifier | LiteralExpression>name);
274274
}
275275
switch (node.kind) {
276+
case SyntaxKind.Identifier:
277+
// THIS IS WRONG
278+
return (node as any as Identifier).escapedText;
276279
case SyntaxKind.Constructor:
277280
return InternalSymbolName.Constructor;
278281
case SyntaxKind.FunctionType:
@@ -2354,7 +2357,7 @@ namespace ts {
23542357
constructorFunction.parent = classPrototype;
23552358
classPrototype.parent = leftSideOfAssignment;
23562359

2357-
bindPropertyAssignment(constructorFunction.escapedText, leftSideOfAssignment, /*isPrototypeProperty*/ true);
2360+
bindPropertyAssignment(constructorFunction.escapedText, leftSideOfAssignment, /*isPrototypeProperty*/ true, /*isMagic*/ false);
23582361
}
23592362

23602363
function bindStaticPropertyAssignment(node: BinaryExpression) {
@@ -2377,7 +2380,7 @@ namespace ts {
23772380
bindExportsPropertyAssignment(node);
23782381
}
23792382
else {
2380-
bindPropertyAssignment(target.escapedText, leftSideOfAssignment, /*isPrototypeProperty*/ false);
2383+
bindPropertyAssignment(target.escapedText, leftSideOfAssignment, /*isPrototypeProperty*/ false, /*isMagic*/ true);
23812384
}
23822385
}
23832386
}
@@ -2386,14 +2389,22 @@ namespace ts {
23862389
return (container.symbol && container.symbol.exports && container.symbol.exports.get(name)) || (container.locals && container.locals.get(name));
23872390
}
23882391

2389-
function bindPropertyAssignment(functionName: __String, propertyAccessExpression: PropertyAccessExpression, isPrototypeProperty: boolean) {
2392+
function bindPropertyAssignment(functionName: __String, propertyAccessExpression: PropertyAccessExpression, isPrototypeProperty: boolean, isMagic: boolean) {
23902393
let targetSymbol = lookupSymbolForName(functionName);
2391-
23922394
if (targetSymbol && isDeclarationOfFunctionOrClassExpression(targetSymbol)) {
23932395
targetSymbol = (targetSymbol.valueDeclaration as VariableDeclaration).initializer.symbol;
23942396
}
2395-
2396-
if (!targetSymbol || !(targetSymbol.flags & (SymbolFlags.Function | SymbolFlags.Class))) {
2397+
if (isMagic && (!targetSymbol || !(targetSymbol.flags & SymbolFlags.Namespace))) {
2398+
// TODO: Magic may not be required
2399+
// TODO: Container.locals as symbolTable is probably wrong sometimes (maybe it's sometimes exports?)
2400+
// TODO: Container.symbol as parent is probably wrong sometimes
2401+
// TODO: propertyAccessExpression.expression isn't a Declaration
2402+
targetSymbol = declareSymbol(container.locals, container.symbol, propertyAccessExpression.expression as any as Declaration, SymbolFlags.NamespaceModule, SymbolFlags.NamespaceModuleExcludes);
2403+
}
2404+
if (targetSymbol && isDeclarationOfFunctionOrClassExpression(targetSymbol)) {
2405+
targetSymbol = (targetSymbol.valueDeclaration as VariableDeclaration).initializer.symbol;
2406+
}
2407+
if (!isMagic && (!targetSymbol || !(targetSymbol.flags & (SymbolFlags.Function | SymbolFlags.Class)))) {
23972408
return;
23982409
}
23992410

src/compiler/checker.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1381,7 +1381,8 @@ namespace ts {
13811381
function checkAndReportErrorForUsingNamespaceModuleAsValue(errorLocation: Node, name: __String, meaning: SymbolFlags): boolean {
13821382
if (meaning & (SymbolFlags.Value & ~SymbolFlags.NamespaceModule & ~SymbolFlags.Type)) {
13831383
const symbol = resolveSymbol(resolveName(errorLocation, name, SymbolFlags.NamespaceModule & ~SymbolFlags.Value, /*nameNotFoundMessage*/undefined, /*nameArg*/ undefined, /*isUse*/ false));
1384-
if (symbol) {
1384+
// TODO: WRONG
1385+
if (symbol && !isInJavaScriptFile(errorLocation)) {
13851386
error(errorLocation, Diagnostics.Cannot_use_namespace_0_as_a_value, unescapeLeadingUnderscores(name));
13861387
return true;
13871388
}
@@ -1734,15 +1735,7 @@ namespace ts {
17341735
return undefined;
17351736
}
17361737
const right = name.kind === SyntaxKind.QualifiedName ? name.right : name.name;
1737-
let namespace = resolveEntityName(left, SymbolFlags.Namespace, /*ignoreErrors*/ true, /*dontResolveAlias*/ false, location);
1738-
if (!namespace && name.parent && isJSDocTypeReference(name.parent as TypeReferenceType)) {
1739-
// jsdoc type references may be values
1740-
namespace = resolveEntityName(left, SymbolFlags.Value, ignoreErrors, /*dontResolveAlias*/ false, location);
1741-
}
1742-
else if (!ignoreErrors) {
1743-
// run again for error reporting purposes
1744-
resolveEntityName(left, SymbolFlags.Namespace, /*ignoreErrors*/ false, /*dontResolveAlias*/ false, location);
1745-
}
1738+
let namespace = resolveEntityName(left, SymbolFlags.Namespace, ignoreErrors, /*dontResolveAlias*/ false, location);
17461739
if (!namespace || nodeIsMissing(right)) {
17471740
return undefined;
17481741
}

0 commit comments

Comments
 (0)