-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Report errors for usage of private types when generating declaration file #161
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
Changes from all commits
bbb36dc
999b7fe
09ec1bb
9fd95fc
0e76a82
e31aa9e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -645,7 +645,7 @@ module ts { | |
} | ||
|
||
// If symbol is directly available by its name in the symbol table | ||
if (isAccessible(symbols[symbol.name])) { | ||
if (hasProperty(symbols, symbol.name) && isAccessible(symbols[symbol.name])) { | ||
return symbol; | ||
} | ||
|
||
|
@@ -668,7 +668,7 @@ module ts { | |
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]) { | ||
if (!hasProperty(symbolTable, symbol.name)) { | ||
// Continue to the next symbol table | ||
return false; | ||
} | ||
|
@@ -693,6 +693,52 @@ module ts { | |
return qualify | ||
} | ||
|
||
function isSymbolAccessible(symbol: Symbol, enclosingDeclaration: Node, meaning: SymbolFlags): SymbolAccessiblityResult { | ||
if (symbol && enclosingDeclaration && !(symbol.flags & SymbolFlags.TypeParameter)) { | ||
var initialSymbol = symbol; | ||
var meaningToLook = meaning; | ||
while (symbol) { | ||
// Symbol is accessible if it by itself is accessible | ||
var accessibleSymbol = getAccessibleSymbol(symbol, enclosingDeclaration, meaningToLook); | ||
if (accessibleSymbol) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. question: If we could not find a symbol at this location, should we just break? what is the point of examining the parent if this link in the chain is not accessible? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. module m { var x: typeof m.c In this example when we look if the symbol constructor function m.c is accessible, we will start with the symbol for c. Since c is not directly in the scope - the answer here that is accessible symbol is going to be undefined. But that doesn't mean m.c is not accessible. It is accessible if the parent m is accessible. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note I added the detailed comment |
||
if (forEach(accessibleSymbol.declarations, declaration => !isDeclarationVisible(declaration))) { | ||
return { | ||
accessibility: SymbolAccessibility.NotAccessible, | ||
errorSymbolName: symbolToString(initialSymbol, enclosingDeclaration, meaning), | ||
errorModuleName: symbol !== initialSymbol ? symbolToString(symbol, enclosingDeclaration, SymbolFlags.Namespace) : undefined | ||
}; | ||
} | ||
return { accessibility: SymbolAccessibility.Accessible }; | ||
} | ||
|
||
// TODO(shkamat): Handle static method of class | ||
|
||
// If we havent got the accessible symbol doesnt mean the symbol is actually inaccessible. | ||
// It could be qualified symbol and hence verify the path | ||
// eg: | ||
// module m { | ||
// export class c { | ||
// } | ||
// } | ||
// var x: typeof m.c | ||
// In the above example when we start with checking if typeof m.c symbol is accessible, | ||
// we are going to see if c can be accessed in scope directly. | ||
// But it cant, hence the accessible is going to be undefined, but that doesnt mean m.c is accessible | ||
// It is accessible if the parent m is accessible because then m.c can be accessed through qualification | ||
meaningToLook = SymbolFlags.Namespace; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. question: what if we were looking for a static method on a class, the parent is not a module in this case.. how would that wok? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i still have todo on that, i will add a todo comment here specifically. |
||
symbol = symbol.parent; | ||
} | ||
|
||
// This is a local symbol that cannot be named | ||
return { | ||
accessibility: SymbolAccessibility.CannotBeNamed, | ||
errorSymbolName: symbolToString(initialSymbol, enclosingDeclaration, meaning), | ||
}; | ||
} | ||
|
||
return { accessibility: SymbolAccessibility.Accessible }; | ||
} | ||
|
||
// 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) { | ||
|
@@ -728,10 +774,15 @@ module ts { | |
return getSymbolName(symbol); | ||
} | ||
|
||
function writeSymbolToTextWriter(symbol: Symbol, enclosingDeclaration: Node, meaning: SymbolFlags, writer: TextWriter) { | ||
writer.write(symbolToString(symbol, enclosingDeclaration, meaning)); | ||
} | ||
|
||
function createSingleLineTextWriter() { | ||
var result = ""; | ||
return { | ||
write(s: string) { result += s; }, | ||
writeSymbol(symbol: Symbol, enclosingDeclaration?: Node, meaning?: SymbolFlags) { writeSymbolToTextWriter(symbol, enclosingDeclaration, meaning, this); }, | ||
writeLine() { result += " "; }, | ||
increaseIndent() { }, | ||
decreaseIndent() { }, | ||
|
@@ -758,7 +809,7 @@ module ts { | |
writeTypeReference(<TypeReference>type); | ||
} | ||
else if (type.flags & (TypeFlags.Class | TypeFlags.Interface | TypeFlags.Enum | TypeFlags.TypeParameter)) { | ||
writer.write(symbolToString(type.symbol, enclosingDeclaration, SymbolFlags.Type)); | ||
writer.writeSymbol(type.symbol, enclosingDeclaration, SymbolFlags.Type); | ||
} | ||
else if (type.flags & TypeFlags.Anonymous) { | ||
writeAnonymousType(<ObjectType>type, allowFunctionOrConstructorTypeLiteral); | ||
|
@@ -780,7 +831,7 @@ module ts { | |
writer.write("[]"); | ||
} | ||
else { | ||
writer.write(symbolToString(type.target.symbol, enclosingDeclaration, SymbolFlags.Type)); | ||
writer.writeSymbol(type.target.symbol, enclosingDeclaration, SymbolFlags.Type); | ||
writer.write("<"); | ||
for (var i = 0; i < type.typeArguments.length; i++) { | ||
if (i > 0) { | ||
|
@@ -814,7 +865,7 @@ module ts { | |
|
||
function writeTypeofSymbol(type: ObjectType) { | ||
writer.write("typeof "); | ||
writer.write(symbolToString(type.symbol, enclosingDeclaration, SymbolFlags.Value)); | ||
writer.writeSymbol(type.symbol, enclosingDeclaration, SymbolFlags.Value); | ||
} | ||
|
||
function writeLiteralType(type: ObjectType, allowFunctionOrConstructorTypeLiteral: boolean) { | ||
|
@@ -870,7 +921,7 @@ module ts { | |
if (p.flags & (SymbolFlags.Function | SymbolFlags.Method) && !getPropertiesOfType(t).length) { | ||
var signatures = getSignaturesOfType(t, SignatureKind.Call); | ||
for (var j = 0; j < signatures.length; j++) { | ||
writer.write(symbolToString(p)); | ||
writer.writeSymbol(p); | ||
if (isOptionalProperty(p)) { | ||
writer.write("?"); | ||
} | ||
|
@@ -880,7 +931,7 @@ module ts { | |
} | ||
} | ||
else { | ||
writer.write(symbolToString(p)); | ||
writer.writeSymbol(p); | ||
if (isOptionalProperty(p)) { | ||
writer.write("?"); | ||
} | ||
|
@@ -902,7 +953,7 @@ module ts { | |
writer.write(", "); | ||
} | ||
var tp = signature.typeParameters[i]; | ||
writer.write(symbolToString(tp.symbol)); | ||
writer.writeSymbol(tp.symbol); | ||
var constraint = getConstraintOfTypeParameter(tp); | ||
if (constraint) { | ||
writer.write(" extends "); | ||
|
@@ -920,7 +971,7 @@ module ts { | |
if (getDeclarationFlagsFromSymbol(p) & NodeFlags.Rest) { | ||
writer.write("..."); | ||
} | ||
writer.write(symbolToString(p)); | ||
writer.writeSymbol(p); | ||
if (p.valueDeclaration.flags & NodeFlags.QuestionMark || (<VariableDeclaration>p.valueDeclaration).initializer) { | ||
writer.write("?"); | ||
} | ||
|
@@ -6105,7 +6156,9 @@ module ts { | |
isDeclarationVisible: isDeclarationVisible, | ||
isImplementationOfOverload: isImplementationOfOverload, | ||
writeTypeAtLocation: writeTypeAtLocation, | ||
writeReturnTypeOfSignatureDeclaration: writeReturnTypeOfSignatureDeclaration | ||
writeReturnTypeOfSignatureDeclaration: writeReturnTypeOfSignatureDeclaration, | ||
writeSymbol: writeSymbolToTextWriter, | ||
isSymbolAccessible: isSymbolAccessible | ||
}; | ||
checkProgram(); | ||
return emitFiles(resolver); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have added a lookup method for Map, it would be more consistent.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i will update that when I resolve merge conflicts.