Merge pull request #30 from Microsoft/declarations

Changes to determine when to qualify the symbol in given enclosing declaration
This commit is contained in:
Sheetal Nandi
2014-07-16 15:20:00 -07:00
239 changed files with 1445 additions and 1217 deletions

View File

@@ -181,6 +181,10 @@ module ts {
return <SourceFile>getAncestor(node, SyntaxKind.SourceFile);
}
function isGlobalSourceFile(node: Node) {
return node.kind === SyntaxKind.SourceFile && !(node.flags & NodeFlags.ExternalModule);
}
function getSymbol(symbols: SymbolTable, name: string, meaning: SymbolFlags): Symbol {
if (meaning && hasProperty(symbols, name)) {
var symbol = symbols[name];
@@ -226,7 +230,7 @@ module ts {
while (location) {
// Locals of a source file are not in scope (because they get merged into the global symbol table)
if (location.locals && (location.kind !== SyntaxKind.SourceFile || location.flags & NodeFlags.ExternalModule)) {
if (location.locals && !isGlobalSourceFile(location)) {
if (result = getSymbol(location.locals, name, meaning)) {
return returnResolvedSymbol(result);
}
@@ -594,14 +598,134 @@ module ts {
return (propertySymbol.valueDeclaration.flags & NodeFlags.QuestionMark) && propertySymbol.valueDeclaration.kind !== SyntaxKind.Parameter;
}
function symbolToString(symbol: Symbol) {
if (symbol.declarations && symbol.declarations.length > 0) {
var declaration = symbol.declarations[0];
if (declaration.name) {
return identifierToString(declaration.name);
function forEachSymbolTableInScope<T>(enclosingDeclaration: Node, callback: (symbolTable: SymbolTable) => T): T {
var result: T;
for (var location = enclosingDeclaration; location; location = location.parent) {
// Locals of a source file are not in scope (because they get merged into the global symbol table)
if (location.locals && !isGlobalSourceFile(location)) {
if (result = callback(location.locals)) {
return result;
}
}
switch (location.kind) {
case SyntaxKind.SourceFile:
if (!(location.flags & NodeFlags.ExternalModule)) {
break;
}
case SyntaxKind.ModuleDeclaration:
if (result = callback(getSymbolOfNode(location).exports)) {
return result;
}
break;
case SyntaxKind.ClassDeclaration:
case SyntaxKind.InterfaceDeclaration:
if (result = callback(getSymbolOfNode(location).members)) {
return result;
}
break;
}
}
return symbol.name;
return callback(globals);
}
function getAccessibleSymbol(symbol: Symbol, enclosingDeclaration: Node, meaning: SymbolFlags) {
function getAccessibleSymbolFromSymbolTable(symbols: SymbolTable) {
function isAccessible(symbolFromSymbolTable: Symbol, resolvedAliasSymbol?: Symbol) {
if (symbol === (resolvedAliasSymbol || symbolFromSymbolTable)) {
// If the symbol is equivalent and doesnt need futher qualification, this symbol is accessible
if (!needsQualification(symbolFromSymbolTable, enclosingDeclaration, meaning)) {
return true;
}
// If symbol needs qualification, make sure that parent is accessible, if it is then this symbol is accessible too
var accessibleParent = getAccessibleSymbol(symbolFromSymbolTable.parent, enclosingDeclaration, SymbolFlags.Namespace);
return !!accessibleParent;
}
}
// If symbol is directly available by its name in the symbol table
if (isAccessible(symbols[symbol.name])) {
return symbol;
}
// Check if symbol is any of the alias
return forEachValue(symbols, symbolFromSymbolTable => {
if (symbolFromSymbolTable.flags & SymbolFlags.Import) {
if (isAccessible(symbolFromSymbolTable, resolveImport(symbolFromSymbolTable))) {
return symbolFromSymbolTable;
}
}
});
}
if (symbol) {
return forEachSymbolTableInScope(enclosingDeclaration, getAccessibleSymbolFromSymbolTable);
}
}
function needsQualification(symbol: Symbol, enclosingDeclaration: Node, meaning: SymbolFlags) {
var qualify = false;
forEachSymbolTableInScope(enclosingDeclaration, symbolTable => {
// If symbol of this name is not available in the symbol table we are ok
if (!symbolTable[symbol.name]) {
// Continue to the next symbol table
return false;
}
// If the symbol with this name is present it should refer to the symbol
var symbolFromSymbolTable = symbolTable[symbol.name];
if (symbolFromSymbolTable === symbol) {
// No need to qualify
return true;
}
// Qualify if the symbol from symbol table has same meaning as expected
symbolFromSymbolTable = (symbolFromSymbolTable.flags & SymbolFlags.Import) ? resolveImport(symbolFromSymbolTable) : symbolFromSymbolTable;
if (symbolFromSymbolTable.flags & meaning) {
qualify = true;
return true;
}
// Continue to the next symbol table
return false;
});
return qualify
}
// Enclosing declaration is optional when we dont want to get qualified name in the enclosing declaration scope
// Meaning needs to be specified if the enclosing declaration is given
function symbolToString(symbol: Symbol, enclosingDeclaration?: Node, meaning?: SymbolFlags) {
function getSymbolName(symbol: Symbol) {
if (symbol.declarations && symbol.declarations.length > 0) {
var declaration = symbol.declarations[0];
if (declaration.name) {
return identifierToString(declaration.name);
}
}
return symbol.name;
}
// Get qualified name
if (enclosingDeclaration &&
// Properties/methods/Signatures/Constructors/TypeParameters do not need qualification
!(symbol.flags & SymbolFlags.PropertyOrAccessor & SymbolFlags.Signature & SymbolFlags.Constructor & SymbolFlags.Method & SymbolFlags.TypeParameter)) {
var symbolName: string;
while (symbol) {
var isFirstName = !symbolName;
var meaningToLook = isFirstName ? meaning : SymbolFlags.Namespace;
var accessibleSymbol = getAccessibleSymbol(symbol, enclosingDeclaration, meaningToLook);
symbolName = getSymbolName(accessibleSymbol || symbol) + (isFirstName ? "" : ("." + symbolName));
if (accessibleSymbol && !needsQualification(accessibleSymbol, enclosingDeclaration, meaningToLook)) {
break;
}
symbol = accessibleSymbol ? accessibleSymbol.parent : symbol.parent;
}
return symbolName;
}
return getSymbolName(symbol);
}
function createSingleLineTextWriter() {
@@ -623,7 +747,6 @@ module ts {
}
function writeTypeToTextWriter(type: Type, enclosingDeclaration: Node, flags: TypeFormatFlags, writer: TextWriter) {
// TODO(shkamat): usage of enclosingDeclaration
var typeStack: Type[];
return writeType(type, /*allowFunctionOrConstructorTypeLiteral*/ true);
@@ -635,7 +758,7 @@ module ts {
writeTypeReference(<TypeReference>type);
}
else if (type.flags & (TypeFlags.Class | TypeFlags.Interface | TypeFlags.Enum | TypeFlags.TypeParameter)) {
writer.write(symbolToString(type.symbol));
writer.write(symbolToString(type.symbol, enclosingDeclaration, SymbolFlags.Type));
}
else if (type.flags & TypeFlags.Anonymous) {
writeAnonymousType(<ObjectType>type, allowFunctionOrConstructorTypeLiteral);
@@ -657,7 +780,7 @@ module ts {
writer.write("[]");
}
else {
writer.write(symbolToString(type.target.symbol));
writer.write(symbolToString(type.target.symbol, enclosingDeclaration, SymbolFlags.Type));
writer.write("<");
for (var i = 0; i < type.typeArguments.length; i++) {
if (i > 0) {
@@ -691,7 +814,7 @@ module ts {
function writeTypeofSymbol(type: ObjectType) {
writer.write("typeof ");
writer.write(symbolToString(type.symbol));
writer.write(symbolToString(type.symbol, enclosingDeclaration, SymbolFlags.Value));
}
function writeLiteralType(type: ObjectType, allowFunctionOrConstructorTypeLiteral: boolean) {
@@ -809,6 +932,122 @@ module ts {
}
}
function isDeclarationVisible(node: Declaration): boolean {
function getContainingExternalModule(node: Node) {
for (; node; node = node.parent) {
if (node.kind === SyntaxKind.ModuleDeclaration) {
if ((<ModuleDeclaration>node).name.kind === SyntaxKind.StringLiteral) {
return node;
}
}
else if (node.kind === SyntaxKind.SourceFile) {
return (node.flags & NodeFlags.ExternalModule) ? node : undefined;
}
}
Debug.fail("getContainingModule cant reach here");
}
function isUsedInExportAssignment(node: Node) {
// Get source File and see if it is external module and has export assigned symbol
var externalModule = getContainingExternalModule(node);
if (externalModule) {
// This is export assigned symbol node
var externalModuleSymbol = getSymbolOfNode(externalModule);
var exportAssignmentSymbol = getExportAssignmentSymbol(externalModuleSymbol);
var symbolOfNode = getSymbolOfNode(node);
if (exportAssignmentSymbol === symbolOfNode) {
return true;
}
if (exportAssignmentSymbol && !!(exportAssignmentSymbol.flags & SymbolFlags.Import)) {
// if export assigned symbol is import declaration, resolve the import
var resolvedExportSymbol = resolveImport(exportAssignmentSymbol);
if (resolvedExportSymbol === symbolOfNode) {
return true;
}
// TODO(shkamat): Chained import assignment
// eg. a should be visible too.
//module m {
// export module c {
// export class c {
// }
// }
//}
//import a = m.c;
//import b = a;
//export = b;
// Container of resolvedExportSymbol is visible
return forEach(resolvedExportSymbol.declarations, declaration => {
while (declaration) {
if (declaration === node) {
return true;
}
declaration = declaration.parent;
}
});
}
}
}
function determineIfDeclarationIsVisible() {
switch (node.kind) {
case SyntaxKind.VariableDeclaration:
if (!(node.flags & NodeFlags.Export)) {
// node.parent is variable statement so look at the variable statement's parent
return isGlobalSourceFile(node.parent.parent) || isUsedInExportAssignment(node);
}
// Exported members are visible if parent is visible
return isDeclarationVisible(node.parent.parent);
case SyntaxKind.ModuleDeclaration:
case SyntaxKind.ClassDeclaration:
case SyntaxKind.InterfaceDeclaration:
case SyntaxKind.FunctionDeclaration:
case SyntaxKind.EnumDeclaration:
case SyntaxKind.ImportDeclaration:
if (!(node.flags & NodeFlags.Export)) {
// TODO(shkamat): non exported aliases can be visible if they are referenced else where for value/type/namespace
return isGlobalSourceFile(node.parent) || isUsedInExportAssignment(node);
}
// Exported members are visible if parent is visible
return isDeclarationVisible(node.parent);
case SyntaxKind.Property:
case SyntaxKind.Method:
if (node.flags & NodeFlags.Private) {
// Private properties/methods are not visible
return false;
}
// Public properties/methods are visible if its parents are visible, so let it fall into next case statement
case SyntaxKind.Constructor:
case SyntaxKind.ConstructSignature:
case SyntaxKind.CallSignature:
case SyntaxKind.IndexSignature:
case SyntaxKind.Parameter:
case SyntaxKind.ModuleBlock:
return isDeclarationVisible(node.parent);
// Source file is always visible
case SyntaxKind.SourceFile:
return true;
default:
Debug.fail("isDeclarationVisible unknown: SyntaxKind: " + SyntaxKind[node.kind]);
}
}
if (node) {
var links = getNodeLinks(node);
if (links.isVisible === undefined) {
links.isVisible = determineIfDeclarationIsVisible();
}
return links.isVisible;
}
}
function getApparentType(type: Type): ApparentType {
if (type.flags & TypeFlags.TypeParameter) {
do {
@@ -4788,7 +5027,7 @@ module ts {
checkTypeAssignableTo(checkAndMarkExpression(node.initializer, type), type, node, /*chainedMessage*/ undefined, /*terminalMessage*/ undefined);
}
}
checkCollisionWithCapturedSuperVariable(node, node.name);
if (!useTypeFromValueDeclaration) {
// TypeScript 1.0 spec (April 2014): 5.1
@@ -5320,7 +5559,7 @@ module ts {
function checkModuleDeclaration(node: ModuleDeclaration) {
checkDeclarationModifiers(node);
if (node.name.kind === SyntaxKind.StringLiteral) {
if (node.parent.kind !== SyntaxKind.SourceFile || node.parent.flags & NodeFlags.ExternalModule) {
if (!isGlobalSourceFile(node.parent)) {
error(node, Diagnostics.Ambient_external_modules_cannot_be_nested_in_other_modules);
}
if (isExternalModuleNameRelative(node.name.text)) {
@@ -5566,7 +5805,7 @@ module ts {
}
}
while (location) {
if (location.locals && (location.kind !== SyntaxKind.SourceFile || location.flags & NodeFlags.ExternalModule)) {
if (location.locals && !isGlobalSourceFile(location)) {
copySymbols(location.locals, meaning);
}
switch (location.kind) {
@@ -5800,22 +6039,6 @@ module ts {
return false;
}
function isReferencedInExportAssignment(node: Declaration): boolean {
var exportAssignedSymbol = getExportAssignmentSymbol(getSymbolOfNode(getContainerOfModuleElementDeclaration(node)));
if (exportAssignedSymbol) {
var symbol = getSymbolOfNode(node);
if (exportAssignedSymbol === symbol) {
// This symbol was export assigned symbol
return true;
}
// TODO(shkamat): if export assignment is alias, the alias target would make the node as referenced in export assignment
}
return false;
}
function isImplementationOfOverload(node: FunctionDeclaration) {
if (node.body) {
var symbol = getSymbolOfNode(node);
@@ -5857,7 +6080,7 @@ module ts {
getEnumMemberValue: getEnumMemberValue,
isTopLevelValueImportedViaEntityName: isTopLevelValueImportedViaEntityName,
shouldEmitDeclarations: shouldEmitDeclarations,
isReferencedInExportAssignment: isReferencedInExportAssignment,
isDeclarationVisible: isDeclarationVisible,
isImplementationOfOverload: isImplementationOfOverload,
writeTypeAtLocation: writeTypeAtLocation,
writeReturnTypeOfSignatureDeclaration: writeReturnTypeOfSignatureDeclaration

View File

@@ -1898,34 +1898,6 @@ module ts {
writeLine();
}
function isModuleElementExternallyVisible(node: Declaration) {
if (node.flags & NodeFlags.Export) {
// Exported member - emit this declaration
return true;
}
// If this node is in external module, check if this is export assigned
var moduleDeclaration = getContainerOfModuleElementDeclaration(node);
if ((moduleDeclaration.flags & NodeFlags.ExternalModule) || // Source file with external module flag
// Ambient external module declaration
(moduleDeclaration.kind === SyntaxKind.ModuleDeclaration && (<ModuleDeclaration>moduleDeclaration).name.kind === SyntaxKind.StringLiteral)) {
return resolver.isReferencedInExportAssignment(node);
}
return false;
}
function canEmitModuleElementDeclaration(node: Declaration) {
if (isModuleElementExternallyVisible(node)) {
// Either exported module element or is referenced in export assignment
return true;
}
// emit the declaration if this is in global scope source file
var moduleDeclaration = getContainerOfModuleElementDeclaration(node);
return moduleDeclaration.kind === SyntaxKind.SourceFile && !(moduleDeclaration.flags & NodeFlags.ExternalModule);
}
function emitDeclarationFlags(node: Declaration) {
if (node.flags & NodeFlags.Static) {
if (node.flags & NodeFlags.Private) {
@@ -1952,8 +1924,7 @@ module ts {
}
function emitImportDeclaration(node: ImportDeclaration) {
// TODO(shkamat): Emit if import decl is used to declare type in this context
if (isModuleElementExternallyVisible(node)) {
if (resolver.isDeclarationVisible(node)) {
if (node.flags & NodeFlags.Export) {
write("export ");
}
@@ -1974,7 +1945,7 @@ module ts {
}
function emitModuleDeclaration(node: ModuleDeclaration) {
if (canEmitModuleElementDeclaration(node)) {
if (resolver.isDeclarationVisible(node)) {
emitDeclarationFlags(node);
write("module ");
emitSourceTextOfNode(node.name);
@@ -1997,7 +1968,7 @@ module ts {
}
function emitEnumDeclaration(node: EnumDeclaration) {
if (canEmitModuleElementDeclaration(node)) {
if (resolver.isDeclarationVisible(node)) {
emitDeclarationFlags(node);
write("enum ");
emitSourceTextOfNode(node.name);
@@ -2060,7 +2031,7 @@ module ts {
}
}
if (canEmitModuleElementDeclaration(node)) {
if (resolver.isDeclarationVisible(node)) {
emitDeclarationFlags(node);
write("class ");
emitSourceTextOfNode(node.name);
@@ -2084,7 +2055,7 @@ module ts {
}
function emitInterfaceDeclaration(node: InterfaceDeclaration) {
if (canEmitModuleElementDeclaration(node)) {
if (resolver.isDeclarationVisible(node)) {
emitDeclarationFlags(node);
write("interface ");
emitSourceTextOfNode(node.name);
@@ -2111,8 +2082,9 @@ module ts {
}
function emitVariableDeclaration(node: VariableDeclaration) {
// If we are emitting property it isnt moduleElement and doesnt need canEmitModuleElement check
if (node.kind !== SyntaxKind.VariableDeclaration || canEmitModuleElementDeclaration(node)) {
// If we are emitting property it isnt moduleElement and hence we already know it needs to be emitted
// so there is no check needed to see if declaration is visible
if (node.kind !== SyntaxKind.VariableDeclaration || resolver.isDeclarationVisible(node)) {
emitSourceTextOfNode(node.name);
// If optional property emit ?
if (node.kind === SyntaxKind.Property && (node.flags & NodeFlags.QuestionMark)) {
@@ -2126,7 +2098,7 @@ module ts {
}
function emitVariableStatement(node: VariableStatement) {
var hasDeclarationWithEmit = forEach(node.declarations, varDeclaration => canEmitModuleElementDeclaration(varDeclaration));
var hasDeclarationWithEmit = forEach(node.declarations, varDeclaration => resolver.isDeclarationVisible(varDeclaration));
if (hasDeclarationWithEmit) {
emitDeclarationFlags(node);
write("var ");
@@ -2151,8 +2123,9 @@ module ts {
}
function emitFunctionDeclaration(node: FunctionDeclaration) {
// If we are emitting Method/Constructor it isnt moduleElement and doesnt need canEmitModuleElement check
if ((node.kind !== SyntaxKind.FunctionDeclaration || canEmitModuleElementDeclaration(node)) &&
// If we are emitting Method/Constructor it isnt moduleElement and hence already determined to be emitting
// so no need to verify if the declaration is visible
if ((node.kind !== SyntaxKind.FunctionDeclaration || resolver.isDeclarationVisible(node)) &&
!resolver.isImplementationOfOverload(node)) {
emitDeclarationFlags(node);
if (node.kind === SyntaxKind.FunctionDeclaration) {

View File

@@ -273,12 +273,6 @@ module ts {
return s.parameters.length > 0 && (s.parameters[s.parameters.length - 1].flags & NodeFlags.Rest) !== 0;
}
export function getContainerOfModuleElementDeclaration(node: Declaration) {
// If the declaration is var declaration, then the parent is variable statement but we actually want the module
var container = node.kind === SyntaxKind.VariableDeclaration ? node.parent.parent : node.parent;
return container.kind == SyntaxKind.ModuleBlock ? container.parent : container;
}
enum ParsingContext {
SourceElements, // Elements in source file
ModuleElements, // Elements in module declaration

View File

@@ -621,7 +621,7 @@ module ts {
getNodeCheckFlags(node: Node): NodeCheckFlags;
getEnumMemberValue(node: EnumMember): number;
shouldEmitDeclarations(): boolean;
isReferencedInExportAssignment(node: Declaration): boolean;
isDeclarationVisible(node: Declaration): boolean;
isImplementationOfOverload(node: FunctionDeclaration): boolean;
writeTypeAtLocation(location: Node, enclosingDeclaration: Node, flags: TypeFormatFlags, writer: TextWriter): void;
writeReturnTypeOfSignatureDeclaration(signatureDeclaration: SignatureDeclaration, enclosingDeclaration: Node, flags: TypeFormatFlags, writer: TextWriter): void;
@@ -740,6 +740,7 @@ module ts {
flags?: NodeCheckFlags; // Set of flags specific to Node
enumMemberValue?: number; // Constant value of enum member
isIllegalTypeReferenceInConstraint?: boolean; // Is type reference in constraint refers to the type parameter from the same list
isVisible?: boolean; // Is this node visible
}
export enum TypeFlags {