mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-19 01:33:15 -05:00
Merge branch 'master' into transitiveExports
Conflicts: src/compiler/checker.ts
This commit is contained in:
@@ -123,7 +123,7 @@ module ts {
|
||||
case SyntaxKind.ExportDeclaration:
|
||||
return "__export";
|
||||
case SyntaxKind.ExportAssignment:
|
||||
return "default";
|
||||
return (<ExportAssignment>node).isExportEquals ? "export=" : "default";
|
||||
case SyntaxKind.FunctionDeclaration:
|
||||
case SyntaxKind.ClassDeclaration:
|
||||
return node.flags & NodeFlags.Default ? "default" : undefined;
|
||||
@@ -188,14 +188,6 @@ module ts {
|
||||
return symbol;
|
||||
}
|
||||
|
||||
function isAmbientContext(node: Node): boolean {
|
||||
while (node) {
|
||||
if (node.flags & NodeFlags.Ambient) return true;
|
||||
node = node.parent;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function declareModuleMember(node: Declaration, symbolKind: SymbolFlags, symbolExcludes: SymbolFlags) {
|
||||
let hasExportModifier = getCombinedNodeFlags(node) & NodeFlags.Export;
|
||||
if (symbolKind & SymbolFlags.Alias) {
|
||||
@@ -218,7 +210,7 @@ module ts {
|
||||
// 2. When we checkIdentifier in the checker, we set its resolved symbol to the local symbol,
|
||||
// but return the export symbol (by calling getExportSymbolOfValueSymbolIfExported). That way
|
||||
// when the emitter comes back to it, it knows not to qualify the name if it was found in a containing scope.
|
||||
if (hasExportModifier || isAmbientContext(container)) {
|
||||
if (hasExportModifier || container.flags & NodeFlags.ExportContext) {
|
||||
let exportKind = (symbolKind & SymbolFlags.Value ? SymbolFlags.ExportValue : 0) |
|
||||
(symbolKind & SymbolFlags.Type ? SymbolFlags.ExportType : 0) |
|
||||
(symbolKind & SymbolFlags.Namespace ? SymbolFlags.ExportNamespace : 0);
|
||||
@@ -311,7 +303,39 @@ module ts {
|
||||
bindChildren(node, symbolKind, isBlockScopeContainer);
|
||||
}
|
||||
|
||||
function isAmbientContext(node: Node): boolean {
|
||||
while (node) {
|
||||
if (node.flags & NodeFlags.Ambient) return true;
|
||||
node = node.parent;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function hasExportDeclarations(node: ModuleDeclaration | SourceFile): boolean {
|
||||
var body = node.kind === SyntaxKind.SourceFile ? node : (<ModuleDeclaration>node).body;
|
||||
if (body.kind === SyntaxKind.SourceFile || body.kind === SyntaxKind.ModuleBlock) {
|
||||
for (let stat of (<Block>body).statements) {
|
||||
if (stat.kind === SyntaxKind.ExportDeclaration || stat.kind === SyntaxKind.ExportAssignment) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function setExportContextFlag(node: ModuleDeclaration | SourceFile) {
|
||||
// A declaration source file or ambient module declaration that contains no export declarations (but possibly regular
|
||||
// declarations with export modifiers) is an export context in which declarations are implicitly exported.
|
||||
if (isAmbientContext(node) && !hasExportDeclarations(node)) {
|
||||
node.flags |= NodeFlags.ExportContext;
|
||||
}
|
||||
else {
|
||||
node.flags &= ~NodeFlags.ExportContext;
|
||||
}
|
||||
}
|
||||
|
||||
function bindModuleDeclaration(node: ModuleDeclaration) {
|
||||
setExportContextFlag(node);
|
||||
if (node.name.kind === SyntaxKind.StringLiteral) {
|
||||
bindDeclaration(node, SymbolFlags.ValueModule, SymbolFlags.ValueModuleExcludes, /*isBlockScopeContainer*/ true);
|
||||
}
|
||||
@@ -508,15 +532,16 @@ module ts {
|
||||
case SyntaxKind.ExportAssignment:
|
||||
if ((<ExportAssignment>node).expression && (<ExportAssignment>node).expression.kind === SyntaxKind.Identifier) {
|
||||
// An export default clause with an identifier exports all meanings of that identifier
|
||||
declareSymbol(container.symbol.exports, container.symbol, <Declaration>node, SymbolFlags.Alias, SymbolFlags.AliasExcludes);
|
||||
declareSymbol(container.symbol.exports, container.symbol, <Declaration>node, SymbolFlags.Alias, SymbolFlags.PropertyExcludes | SymbolFlags.AliasExcludes);
|
||||
}
|
||||
else {
|
||||
// An export default clause with an expression exports a value
|
||||
declareSymbol(container.symbol.exports, container.symbol, <Declaration>node, SymbolFlags.Property, SymbolFlags.PropertyExcludes);
|
||||
declareSymbol(container.symbol.exports, container.symbol, <Declaration>node, SymbolFlags.Property, SymbolFlags.PropertyExcludes | SymbolFlags.AliasExcludes);
|
||||
}
|
||||
bindChildren(node, 0, /*isBlockScopeContainer*/ false);
|
||||
break;
|
||||
case SyntaxKind.SourceFile:
|
||||
setExportContextFlag(<SourceFile>node);
|
||||
if (isExternalModule(<SourceFile>node)) {
|
||||
bindAnonymousDeclaration(<SourceFile>node, SymbolFlags.ValueModule, '"' + removeFileExtension((<SourceFile>node).fileName) + '"', /*isBlockScopeContainer*/ true);
|
||||
break;
|
||||
|
||||
@@ -118,11 +118,17 @@ module ts {
|
||||
let globalIterableType: ObjectType;
|
||||
|
||||
let anyArrayType: Type;
|
||||
|
||||
let globalTypedPropertyDescriptorType: ObjectType;
|
||||
let globalClassDecoratorType: ObjectType;
|
||||
let globalParameterDecoratorType: ObjectType;
|
||||
let globalPropertyDecoratorType: ObjectType;
|
||||
let globalMethodDecoratorType: ObjectType;
|
||||
|
||||
let tupleTypes: Map<TupleType> = {};
|
||||
let unionTypes: Map<UnionType> = {};
|
||||
let stringLiteralTypes: Map<StringLiteralType> = {};
|
||||
let emitExtends = false;
|
||||
let emitDecorate = false;
|
||||
|
||||
let mergedSymbols: Symbol[] = [];
|
||||
let symbolLinks: SymbolLinks[] = [];
|
||||
@@ -292,7 +298,6 @@ module ts {
|
||||
if (symbol.flags & meaning) {
|
||||
return symbol;
|
||||
}
|
||||
|
||||
if (symbol.flags & SymbolFlags.Alias) {
|
||||
let target = resolveAlias(symbol);
|
||||
// Unknown symbol means an error occurred in alias resolution, treat it as positive answer to avoid cascading errors
|
||||
@@ -301,7 +306,6 @@ module ts {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// return undefined if we can't find a symbol.
|
||||
}
|
||||
|
||||
@@ -329,6 +333,7 @@ module ts {
|
||||
let lastLocation: Node;
|
||||
let propertyWithInvalidInitializer: Node;
|
||||
let errorLocation = location;
|
||||
let grandparent: Node;
|
||||
|
||||
loop: while (location) {
|
||||
// Locals of a source file are not in scope (because they get merged into the global symbol table)
|
||||
@@ -342,7 +347,7 @@ module ts {
|
||||
if (!isExternalModule(<SourceFile>location)) break;
|
||||
case SyntaxKind.ModuleDeclaration:
|
||||
if (result = getSymbol(getSymbolOfNode(location).exports, name, meaning & SymbolFlags.ModuleMember)) {
|
||||
if (!(result.flags & SymbolFlags.Alias && getDeclarationOfAliasSymbol(result).kind === SyntaxKind.ExportSpecifier)) {
|
||||
if (result.flags & meaning || !(result.flags & SymbolFlags.Alias && getDeclarationOfAliasSymbol(result).kind === SyntaxKind.ExportSpecifier)) {
|
||||
break loop;
|
||||
}
|
||||
result = undefined;
|
||||
@@ -394,7 +399,7 @@ module ts {
|
||||
// }
|
||||
//
|
||||
case SyntaxKind.ComputedPropertyName:
|
||||
let grandparent = location.parent.parent;
|
||||
grandparent = location.parent.parent;
|
||||
if (grandparent.kind === SyntaxKind.ClassDeclaration || grandparent.kind === SyntaxKind.InterfaceDeclaration) {
|
||||
// A reference to this grandparent's type parameters would be an error
|
||||
if (result = getSymbol(getSymbolOfNode(grandparent).members, name, meaning & SymbolFlags.Type)) {
|
||||
@@ -426,6 +431,28 @@ module ts {
|
||||
break loop;
|
||||
}
|
||||
break;
|
||||
case SyntaxKind.Decorator:
|
||||
// Decorators are resolved at the class declaration. Resolving at the parameter
|
||||
// or member would result in looking up locals in the method.
|
||||
//
|
||||
// function y() {}
|
||||
// class C {
|
||||
// method(@y x, y) {} // <-- decorator y should be resolved at the class declaration, not the parameter.
|
||||
// }
|
||||
//
|
||||
if (location.parent && location.parent.kind === SyntaxKind.Parameter) {
|
||||
location = location.parent;
|
||||
}
|
||||
//
|
||||
// function y() {}
|
||||
// class C {
|
||||
// @y method(x, y) {} // <-- decorator y should be resolved at the class declaration, not the method.
|
||||
// }
|
||||
//
|
||||
if (location.parent && isClassElement(location.parent)) {
|
||||
location = location.parent;
|
||||
}
|
||||
break;
|
||||
}
|
||||
lastLocation = location;
|
||||
location = location.parent;
|
||||
@@ -514,29 +541,13 @@ module ts {
|
||||
return false;
|
||||
}
|
||||
|
||||
// An alias symbol is created by one of the following declarations:
|
||||
// import <symbol> = ...
|
||||
// import <symbol> from ...
|
||||
// import * as <symbol> from ...
|
||||
// import { x as <symbol> } from ...
|
||||
// export { x as <symbol> } from ...
|
||||
// export default ...
|
||||
function isAliasSymbolDeclaration(node: Node): boolean {
|
||||
return node.kind === SyntaxKind.ImportEqualsDeclaration ||
|
||||
node.kind === SyntaxKind.ImportClause && !!(<ImportClause>node).name ||
|
||||
node.kind === SyntaxKind.NamespaceImport ||
|
||||
node.kind === SyntaxKind.ImportSpecifier ||
|
||||
node.kind === SyntaxKind.ExportSpecifier ||
|
||||
node.kind === SyntaxKind.ExportAssignment;
|
||||
}
|
||||
|
||||
function getAnyImportSyntax(node: Node): AnyImportSyntax {
|
||||
if (isAliasSymbolDeclaration(node)) {
|
||||
if (node.kind === SyntaxKind.ImportEqualsDeclaration) {
|
||||
return <ImportEqualsDeclaration>node;
|
||||
}
|
||||
|
||||
while (node.kind !== SyntaxKind.ImportDeclaration) {
|
||||
while (node && node.kind !== SyntaxKind.ImportDeclaration) {
|
||||
node = node.parent;
|
||||
}
|
||||
return <ImportDeclaration>node;
|
||||
@@ -549,9 +560,7 @@ module ts {
|
||||
|
||||
function getTargetOfImportEqualsDeclaration(node: ImportEqualsDeclaration): Symbol {
|
||||
if (node.moduleReference.kind === SyntaxKind.ExternalModuleReference) {
|
||||
let moduleSymbol = resolveExternalModuleName(node, getExternalModuleImportEqualsDeclarationExpression(node));
|
||||
let exportAssignmentSymbol = moduleSymbol && getResolvedExportAssignmentSymbol(moduleSymbol);
|
||||
return exportAssignmentSymbol || moduleSymbol;
|
||||
return resolveExternalModuleSymbol(resolveExternalModuleName(node, getExternalModuleImportEqualsDeclarationExpression(node)));
|
||||
}
|
||||
return getSymbolOfPartOfRightHandSideOfImportEquals(<EntityName>node.moduleReference, node);
|
||||
}
|
||||
@@ -559,29 +568,92 @@ module ts {
|
||||
function getTargetOfImportClause(node: ImportClause): Symbol {
|
||||
let moduleSymbol = resolveExternalModuleName(node, (<ImportDeclaration>node.parent).moduleSpecifier);
|
||||
if (moduleSymbol) {
|
||||
let exportAssignmentSymbol = getResolvedExportAssignmentSymbol(moduleSymbol);
|
||||
if (!exportAssignmentSymbol) {
|
||||
error(node.name, Diagnostics.External_module_0_has_no_default_export_or_export_assignment, symbolToString(moduleSymbol));
|
||||
let exportDefaultSymbol = resolveSymbol(moduleSymbol.exports["default"]);
|
||||
if (!exportDefaultSymbol) {
|
||||
error(node.name, Diagnostics.External_module_0_has_no_default_export, symbolToString(moduleSymbol));
|
||||
}
|
||||
return exportAssignmentSymbol;
|
||||
return exportDefaultSymbol;
|
||||
}
|
||||
}
|
||||
|
||||
function getTargetOfNamespaceImport(node: NamespaceImport): Symbol {
|
||||
return resolveExternalModuleName(node, (<ImportDeclaration>node.parent.parent).moduleSpecifier);
|
||||
var moduleSpecifier = (<ImportDeclaration>node.parent.parent).moduleSpecifier;
|
||||
return resolveESModuleSymbol(resolveExternalModuleName(node, moduleSpecifier), moduleSpecifier);
|
||||
}
|
||||
|
||||
function getMemberOfModuleVariable(moduleSymbol: Symbol, name: string): Symbol {
|
||||
if (moduleSymbol.flags & SymbolFlags.Variable) {
|
||||
let typeAnnotation = (<VariableDeclaration>moduleSymbol.valueDeclaration).type;
|
||||
if (typeAnnotation) {
|
||||
return getPropertyOfType(getTypeFromTypeNode(typeAnnotation), name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// This function creates a synthetic symbol that combines the value side of one symbol with the
|
||||
// type/namespace side of another symbol. Consider this example:
|
||||
//
|
||||
// declare module graphics {
|
||||
// interface Point {
|
||||
// x: number;
|
||||
// y: number;
|
||||
// }
|
||||
// }
|
||||
// declare var graphics: {
|
||||
// Point: new (x: number, y: number) => graphics.Point;
|
||||
// }
|
||||
// declare module "graphics" {
|
||||
// export = graphics;
|
||||
// }
|
||||
//
|
||||
// An 'import { Point } from "graphics"' needs to create a symbol that combines the value side 'Point'
|
||||
// property with the type/namespace side interface 'Point'.
|
||||
function combineValueAndTypeSymbols(valueSymbol: Symbol, typeSymbol: Symbol): Symbol {
|
||||
if (valueSymbol.flags & (SymbolFlags.Type | SymbolFlags.Namespace)) {
|
||||
return valueSymbol;
|
||||
}
|
||||
let result = createSymbol(valueSymbol.flags | typeSymbol.flags, valueSymbol.name);
|
||||
result.declarations = concatenate(valueSymbol.declarations, typeSymbol.declarations);
|
||||
result.parent = valueSymbol.parent || typeSymbol.parent;
|
||||
if (valueSymbol.valueDeclaration) result.valueDeclaration = valueSymbol.valueDeclaration;
|
||||
if (typeSymbol.members) result.members = typeSymbol.members;
|
||||
if (valueSymbol.exports) result.exports = valueSymbol.exports;
|
||||
return result;
|
||||
}
|
||||
|
||||
function getExportOfModule(symbol: Symbol, name: string): Symbol {
|
||||
if (symbol.flags & SymbolFlags.Module) {
|
||||
let exports = getExportsOfSymbol(symbol);
|
||||
if (hasProperty(exports, name)) {
|
||||
return resolveSymbol(exports[name]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function getPropertyOfVariable(symbol: Symbol, name: string): Symbol {
|
||||
if (symbol.flags & SymbolFlags.Variable) {
|
||||
var typeAnnotation = (<VariableDeclaration>symbol.valueDeclaration).type;
|
||||
if (typeAnnotation) {
|
||||
return resolveSymbol(getPropertyOfType(getTypeFromTypeNode(typeAnnotation), name));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function getExternalModuleMember(node: ImportDeclaration | ExportDeclaration, specifier: ImportOrExportSpecifier): Symbol {
|
||||
let moduleSymbol = resolveExternalModuleName(node, node.moduleSpecifier);
|
||||
if (moduleSymbol) {
|
||||
let targetSymbol = resolveESModuleSymbol(moduleSymbol, node.moduleSpecifier);
|
||||
if (targetSymbol) {
|
||||
let name = specifier.propertyName || specifier.name;
|
||||
if (name.text) {
|
||||
let symbol = getSymbol(getExportsOfSymbol(moduleSymbol), name.text, SymbolFlags.Value | SymbolFlags.Type | SymbolFlags.Namespace);
|
||||
let symbolFromModule = getExportOfModule(targetSymbol, name.text);
|
||||
let symbolFromVariable = getPropertyOfVariable(targetSymbol, name.text);
|
||||
let symbol = symbolFromModule && symbolFromVariable ?
|
||||
combineValueAndTypeSymbols(symbolFromVariable, symbolFromModule) :
|
||||
symbolFromModule || symbolFromVariable;
|
||||
if (!symbol) {
|
||||
error(name, Diagnostics.Module_0_has_no_exported_member_1, getFullyQualifiedName(moduleSymbol), declarationNameToString(name));
|
||||
return;
|
||||
}
|
||||
return symbol.flags & (SymbolFlags.Value | SymbolFlags.Type | SymbolFlags.Namespace) ? symbol : resolveAlias(symbol);
|
||||
return symbol;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -600,7 +672,7 @@ module ts {
|
||||
return node.expression && resolveEntityName(<Identifier>node.expression, SymbolFlags.Value | SymbolFlags.Type | SymbolFlags.Namespace);
|
||||
}
|
||||
|
||||
function getTargetOfImportDeclaration(node: Declaration): Symbol {
|
||||
function getTargetOfAliasDeclaration(node: Declaration): Symbol {
|
||||
switch (node.kind) {
|
||||
case SyntaxKind.ImportEqualsDeclaration:
|
||||
return getTargetOfImportEqualsDeclaration(<ImportEqualsDeclaration>node);
|
||||
@@ -617,13 +689,17 @@ module ts {
|
||||
}
|
||||
}
|
||||
|
||||
function resolveSymbol(symbol: Symbol): Symbol {
|
||||
return symbol && symbol.flags & SymbolFlags.Alias && !(symbol.flags & (SymbolFlags.Value | SymbolFlags.Type | SymbolFlags.Namespace)) ? resolveAlias(symbol) : symbol;
|
||||
}
|
||||
|
||||
function resolveAlias(symbol: Symbol): Symbol {
|
||||
Debug.assert((symbol.flags & SymbolFlags.Alias) !== 0, "Should only get Alias here.");
|
||||
let links = getSymbolLinks(symbol);
|
||||
if (!links.target) {
|
||||
links.target = resolvingSymbol;
|
||||
let node = getDeclarationOfAliasSymbol(symbol);
|
||||
let target = getTargetOfImportDeclaration(node);
|
||||
let target = getTargetOfAliasDeclaration(node);
|
||||
if (links.target === resolvingSymbol) {
|
||||
links.target = target || unknownSymbol;
|
||||
}
|
||||
@@ -754,7 +830,6 @@ module ts {
|
||||
return symbol;
|
||||
}
|
||||
}
|
||||
|
||||
let sourceFile: SourceFile;
|
||||
while (true) {
|
||||
let fileName = normalizePath(combinePaths(searchPath, moduleName));
|
||||
@@ -762,12 +837,10 @@ module ts {
|
||||
if (sourceFile || isRelative) {
|
||||
break;
|
||||
}
|
||||
|
||||
let parentPath = getDirectoryPath(searchPath);
|
||||
if (parentPath === searchPath) {
|
||||
break;
|
||||
}
|
||||
|
||||
searchPath = parentPath;
|
||||
}
|
||||
if (sourceFile) {
|
||||
@@ -780,25 +853,30 @@ module ts {
|
||||
error(moduleReferenceLiteral, Diagnostics.Cannot_find_external_module_0, moduleName);
|
||||
}
|
||||
|
||||
function getExportAssignmentSymbol(moduleSymbol: Symbol): Symbol {
|
||||
return moduleSymbol.exports["default"];
|
||||
// An external module with an 'export =' declaration resolves to the target of the 'export =' declaration,
|
||||
// and an external module with no 'export =' declaration resolves to the module itself.
|
||||
function resolveExternalModuleSymbol(moduleSymbol: Symbol): Symbol {
|
||||
return moduleSymbol && resolveSymbol(moduleSymbol.exports["export="]) || moduleSymbol;
|
||||
}
|
||||
|
||||
function getResolvedExportAssignmentSymbol(moduleSymbol: Symbol): Symbol {
|
||||
let symbol = getExportAssignmentSymbol(moduleSymbol);
|
||||
if (symbol) {
|
||||
if (symbol.flags & (SymbolFlags.Value | SymbolFlags.Type | SymbolFlags.Namespace)) {
|
||||
return symbol;
|
||||
}
|
||||
if (symbol.flags & SymbolFlags.Alias) {
|
||||
return resolveAlias(symbol);
|
||||
}
|
||||
// An external module with an 'export =' declaration may be referenced as an ES6 module provided the 'export ='
|
||||
// references a symbol that is at least declared as a module or a variable. The target of the 'export =' may
|
||||
// combine other declarations with the module or variable (e.g. a class/module, function/module, interface/variable).
|
||||
function resolveESModuleSymbol(moduleSymbol: Symbol, moduleReferenceExpression: Expression): Symbol {
|
||||
let symbol = resolveExternalModuleSymbol(moduleSymbol);
|
||||
if (symbol && !(symbol.flags & (SymbolFlags.Module | SymbolFlags.Variable))) {
|
||||
error(moduleReferenceExpression, Diagnostics.External_module_0_resolves_to_a_non_module_entity_and_cannot_be_imported_using_this_construct, symbolToString(moduleSymbol));
|
||||
symbol = undefined;
|
||||
}
|
||||
return symbol;
|
||||
}
|
||||
|
||||
function getExportAssignmentSymbol(moduleSymbol: Symbol): Symbol {
|
||||
return moduleSymbol.exports["export="];
|
||||
}
|
||||
|
||||
function getExportsOfSymbol(symbol: Symbol): SymbolTable {
|
||||
// TODO (drosen): Why do we not use getExportsOfModule for exteral modules as weel?
|
||||
return symbol.flags & SymbolFlags.Module ? getExportsOfModule(symbol) : symbol.exports;
|
||||
return symbol.flags & SymbolFlags.Module ? getExportsOfModule(symbol) : symbol.exports || emptySymbols;
|
||||
}
|
||||
|
||||
function getExportsOfModule(moduleSymbol: Symbol): SymbolTable {
|
||||
@@ -815,15 +893,6 @@ module ts {
|
||||
}
|
||||
|
||||
function getExportsForModule(moduleSymbol: Symbol): SymbolTable {
|
||||
if (languageVersion < ScriptTarget.ES6) {
|
||||
// A default export hides all other exports in CommonJS and AMD modules
|
||||
let defaultSymbol = getExportAssignmentSymbol(moduleSymbol);
|
||||
if (defaultSymbol) {
|
||||
return {
|
||||
"default": defaultSymbol
|
||||
};
|
||||
}
|
||||
}
|
||||
let result: SymbolTable;
|
||||
let visitedSymbols: Symbol[] = [];
|
||||
visit(moduleSymbol);
|
||||
@@ -832,7 +901,7 @@ module ts {
|
||||
// The ES6 spec permits export * declarations in a module to circularly reference the module itself. For example,
|
||||
// module 'a' can 'export * from "b"' and 'b' can 'export * from "a"' without error.
|
||||
function visit(symbol: Symbol) {
|
||||
if (!contains(visitedSymbols, symbol)) {
|
||||
if (symbol.flags & SymbolFlags.HasExports && !contains(visitedSymbols, symbol)) {
|
||||
visitedSymbols.push(symbol);
|
||||
if (symbol !== moduleSymbol) {
|
||||
if (!result) {
|
||||
@@ -843,9 +912,9 @@ module ts {
|
||||
// All export * declarations are collected in an __export symbol by the binder
|
||||
let exportStars = symbol.exports["__export"];
|
||||
if (exportStars) {
|
||||
forEach(exportStars.declarations, node => {
|
||||
for (let node of exportStars.declarations) {
|
||||
visit(resolveExternalModuleName(node, (<ExportDeclaration>node).moduleSpecifier));
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2925,17 +2994,25 @@ module ts {
|
||||
return result;
|
||||
}
|
||||
|
||||
function symbolsToArray(symbols: SymbolTable): Symbol[] {
|
||||
let result: Symbol[] = [];
|
||||
for (let id in symbols) {
|
||||
if (!isReservedMemberName(id)) {
|
||||
result.push(symbols[id]);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
function getExportsOfImportDeclaration(node: ImportDeclaration): Symbol[] {
|
||||
if (!node.moduleSpecifier) {
|
||||
return emptyArray;
|
||||
}
|
||||
|
||||
let module = resolveExternalModuleName(node, node.moduleSpecifier);
|
||||
if (!module || !module.exports) {
|
||||
if (!module) {
|
||||
return emptyArray;
|
||||
}
|
||||
|
||||
return mapToArray(getExportsOfModule(module))
|
||||
return symbolsToArray(getExportsOfModule(module));
|
||||
}
|
||||
|
||||
function getSignatureFromDeclaration(declaration: SignatureDeclaration): Signature {
|
||||
@@ -4511,7 +4588,7 @@ module ts {
|
||||
* Check if a Type was written as a tuple type literal.
|
||||
* Prefer using isTupleLikeType() unless the use of `elementTypes` is required.
|
||||
*/
|
||||
function isTupleType(type: Type) : boolean {
|
||||
function isTupleType(type: Type): boolean {
|
||||
return (type.flags & TypeFlags.Tuple) && !!(<TupleType>type).elementTypes;
|
||||
}
|
||||
|
||||
@@ -7650,7 +7727,7 @@ module ts {
|
||||
if (!checkForDisallowedESSymbolOperand(operator)) {
|
||||
return booleanType;
|
||||
}
|
||||
// Fall through
|
||||
// Fall through
|
||||
case SyntaxKind.EqualsEqualsToken:
|
||||
case SyntaxKind.ExclamationEqualsToken:
|
||||
case SyntaxKind.EqualsEqualsEqualsToken:
|
||||
@@ -7951,7 +8028,7 @@ module ts {
|
||||
// strict mode FunctionLikeDeclaration or FunctionExpression(13.1)
|
||||
|
||||
// Grammar checking
|
||||
checkGrammarModifiers(node) || checkGrammarEvalOrArgumentsInStrictMode(node, <Identifier>node.name);
|
||||
checkGrammarDecorators(node) || checkGrammarModifiers(node) || checkGrammarEvalOrArgumentsInStrictMode(node, <Identifier>node.name);
|
||||
|
||||
checkVariableLikeDeclaration(node);
|
||||
let func = getContainingFunction(node);
|
||||
@@ -8053,7 +8130,7 @@ module ts {
|
||||
|
||||
function checkPropertyDeclaration(node: PropertyDeclaration) {
|
||||
// Grammar checking
|
||||
checkGrammarModifiers(node) || checkGrammarProperty(node) || checkGrammarComputedPropertyName(node.name);
|
||||
checkGrammarDecorators(node) || checkGrammarModifiers(node) || checkGrammarProperty(node) || checkGrammarComputedPropertyName(node.name);
|
||||
|
||||
checkVariableLikeDeclaration(node);
|
||||
}
|
||||
@@ -8192,6 +8269,10 @@ module ts {
|
||||
checkFunctionLikeDeclaration(node);
|
||||
}
|
||||
|
||||
function checkMissingDeclaration(node: Node) {
|
||||
checkDecorators(node);
|
||||
}
|
||||
|
||||
function checkTypeReference(node: TypeReferenceNode) {
|
||||
// Grammar checking
|
||||
checkGrammarTypeArguments(node, node.typeArguments);
|
||||
@@ -8583,6 +8664,60 @@ module ts {
|
||||
}
|
||||
}
|
||||
|
||||
/** Check a decorator */
|
||||
function checkDecorator(node: Decorator): void {
|
||||
let expression: Expression = node.expression;
|
||||
let exprType = checkExpression(expression);
|
||||
|
||||
switch (node.parent.kind) {
|
||||
case SyntaxKind.ClassDeclaration:
|
||||
let classSymbol = getSymbolOfNode(node.parent);
|
||||
let classConstructorType = getTypeOfSymbol(classSymbol);
|
||||
let classDecoratorType = instantiateSingleCallFunctionType(globalClassDecoratorType, [classConstructorType]);
|
||||
checkTypeAssignableTo(exprType, classDecoratorType, node);
|
||||
break;
|
||||
|
||||
case SyntaxKind.PropertyDeclaration:
|
||||
checkTypeAssignableTo(exprType, globalPropertyDecoratorType, node);
|
||||
break;
|
||||
|
||||
case SyntaxKind.MethodDeclaration:
|
||||
case SyntaxKind.GetAccessor:
|
||||
case SyntaxKind.SetAccessor:
|
||||
let methodType = getTypeOfNode(node.parent);
|
||||
let methodDecoratorType = instantiateSingleCallFunctionType(globalMethodDecoratorType, [methodType]);
|
||||
checkTypeAssignableTo(exprType, methodDecoratorType, node);
|
||||
break;
|
||||
|
||||
case SyntaxKind.Parameter:
|
||||
checkTypeAssignableTo(exprType, globalParameterDecoratorType, node);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/** Check the decorators of a node */
|
||||
function checkDecorators(node: Node): void {
|
||||
if (!node.decorators) {
|
||||
return;
|
||||
}
|
||||
|
||||
switch (node.kind) {
|
||||
case SyntaxKind.ClassDeclaration:
|
||||
case SyntaxKind.MethodDeclaration:
|
||||
case SyntaxKind.GetAccessor:
|
||||
case SyntaxKind.SetAccessor:
|
||||
case SyntaxKind.PropertyDeclaration:
|
||||
case SyntaxKind.Parameter:
|
||||
emitDecorate = true;
|
||||
break;
|
||||
|
||||
default:
|
||||
return;
|
||||
}
|
||||
|
||||
forEach(node.decorators, checkDecorator);
|
||||
}
|
||||
|
||||
function checkFunctionDeclaration(node: FunctionDeclaration): void {
|
||||
if (produceDiagnostics) {
|
||||
checkFunctionLikeDeclaration(node) ||
|
||||
@@ -8597,6 +8732,7 @@ module ts {
|
||||
}
|
||||
|
||||
function checkFunctionLikeDeclaration(node: FunctionLikeDeclaration): void {
|
||||
checkDecorators(node);
|
||||
checkSignatureDeclaration(node);
|
||||
|
||||
// Do not use hasDynamicName here, because that returns false for well known symbols.
|
||||
@@ -8879,6 +9015,7 @@ module ts {
|
||||
|
||||
// Check variable, parameter, or property declaration
|
||||
function checkVariableLikeDeclaration(node: VariableLikeDeclaration) {
|
||||
checkDecorators(node);
|
||||
checkSourceElement(node.type);
|
||||
// For a computed property, just check the initializer and exit
|
||||
// Do not use hasDynamicName here, because that returns false for well known symbols.
|
||||
@@ -8951,7 +9088,7 @@ module ts {
|
||||
|
||||
function checkVariableStatement(node: VariableStatement) {
|
||||
// Grammar checking
|
||||
checkGrammarDisallowedModifiersInBlockOrObjectLiteralExpression(node) || checkGrammarModifiers(node) || checkGrammarVariableDeclarationList(node.declarationList) || checkGrammarForDisallowedLetOrConstStatement(node);
|
||||
checkGrammarDecorators(node) || checkGrammarDisallowedModifiersInBlockOrObjectLiteralExpression(node) || checkGrammarModifiers(node) || checkGrammarVariableDeclarationList(node.declarationList) || checkGrammarForDisallowedLetOrConstStatement(node);
|
||||
|
||||
forEach(node.declarationList.declarations, checkSourceElement);
|
||||
}
|
||||
@@ -9582,7 +9719,7 @@ module ts {
|
||||
function checkClassDeclaration(node: ClassDeclaration) {
|
||||
// Grammar checking
|
||||
checkGrammarClassDeclarationHeritageClauses(node);
|
||||
|
||||
checkDecorators(node);
|
||||
if (node.name) {
|
||||
checkTypeNameIsReserved(node.name, Diagnostics.Class_name_cannot_be_0);
|
||||
checkCollisionWithCapturedThisVariable(node, node.name);
|
||||
@@ -9677,7 +9814,7 @@ module ts {
|
||||
if (derived) {
|
||||
let baseDeclarationFlags = getDeclarationFlagsFromSymbol(base);
|
||||
let derivedDeclarationFlags = getDeclarationFlagsFromSymbol(derived);
|
||||
if ((baseDeclarationFlags & NodeFlags.Private) || (derivedDeclarationFlags & NodeFlags.Private)) {
|
||||
if ((baseDeclarationFlags & NodeFlags.Private) || (derivedDeclarationFlags & NodeFlags.Private)) {
|
||||
// either base or derived property is private - not override, skip it
|
||||
continue;
|
||||
}
|
||||
@@ -9787,7 +9924,7 @@ module ts {
|
||||
|
||||
function checkInterfaceDeclaration(node: InterfaceDeclaration) {
|
||||
// Grammar checking
|
||||
checkGrammarModifiers(node) || checkGrammarInterfaceDeclaration(node);
|
||||
checkGrammarDecorators(node) || checkGrammarModifiers(node) || checkGrammarInterfaceDeclaration(node);
|
||||
|
||||
checkTypeParameters(node.typeParameters);
|
||||
if (produceDiagnostics) {
|
||||
@@ -9808,7 +9945,7 @@ module ts {
|
||||
// run subsequent checks only if first set succeeded
|
||||
if (checkInheritedPropertiesAreIdentical(type, node.name)) {
|
||||
forEach(type.baseTypes, baseType => {
|
||||
checkTypeAssignableTo(type, baseType, node.name , Diagnostics.Interface_0_incorrectly_extends_interface_1);
|
||||
checkTypeAssignableTo(type, baseType, node.name, Diagnostics.Interface_0_incorrectly_extends_interface_1);
|
||||
});
|
||||
checkIndexConstraints(type);
|
||||
}
|
||||
@@ -9824,7 +9961,7 @@ module ts {
|
||||
|
||||
function checkTypeAliasDeclaration(node: TypeAliasDeclaration) {
|
||||
// Grammar checking
|
||||
checkGrammarModifiers(node);
|
||||
checkGrammarDecorators(node) || checkGrammarModifiers(node);
|
||||
|
||||
checkTypeNameIsReserved(node.name, Diagnostics.Type_alias_name_cannot_be_0);
|
||||
checkSourceElement(node.type);
|
||||
@@ -10006,7 +10143,7 @@ module ts {
|
||||
}
|
||||
|
||||
// Grammar checking
|
||||
checkGrammarModifiers(node) || checkGrammarEnumDeclaration(node);
|
||||
checkGrammarDecorators(node) || checkGrammarModifiers(node) || checkGrammarEnumDeclaration(node);
|
||||
|
||||
checkTypeNameIsReserved(node.name, Diagnostics.Enum_name_cannot_be_0);
|
||||
checkCollisionWithCapturedThisVariable(node, node.name);
|
||||
@@ -10072,7 +10209,7 @@ module ts {
|
||||
function checkModuleDeclaration(node: ModuleDeclaration) {
|
||||
if (produceDiagnostics) {
|
||||
// Grammar checking
|
||||
if (!checkGrammarModifiers(node)) {
|
||||
if (!checkGrammarDecorators(node) && !checkGrammarModifiers(node)) {
|
||||
if (!isInAmbientContext(node) && node.name.kind === SyntaxKind.StringLiteral) {
|
||||
grammarErrorOnNode(node.name, Diagnostics.Only_ambient_modules_can_use_quoted_names);
|
||||
}
|
||||
@@ -10167,7 +10304,7 @@ module ts {
|
||||
}
|
||||
|
||||
function checkImportDeclaration(node: ImportDeclaration) {
|
||||
if (!checkGrammarModifiers(node) && (node.flags & NodeFlags.Modifier)) {
|
||||
if (!checkGrammarDecorators(node) && !checkGrammarModifiers(node) && (node.flags & NodeFlags.Modifier)) {
|
||||
grammarErrorOnFirstToken(node, Diagnostics.An_import_declaration_cannot_have_modifiers);
|
||||
}
|
||||
if (checkExternalImportOrExportDeclaration(node)) {
|
||||
@@ -10189,7 +10326,7 @@ module ts {
|
||||
}
|
||||
|
||||
function checkImportEqualsDeclaration(node: ImportEqualsDeclaration) {
|
||||
checkGrammarModifiers(node);
|
||||
checkGrammarDecorators(node) || checkGrammarModifiers(node);
|
||||
if (isInternalModuleImportEqualsDeclaration(node) || checkExternalImportOrExportDeclaration(node)) {
|
||||
checkImportBinding(node);
|
||||
if (node.flags & NodeFlags.Export) {
|
||||
@@ -10220,12 +10357,26 @@ module ts {
|
||||
}
|
||||
|
||||
function checkExportDeclaration(node: ExportDeclaration) {
|
||||
if (!checkGrammarModifiers(node) && (node.flags & NodeFlags.Modifier)) {
|
||||
if (!checkGrammarDecorators(node) && !checkGrammarModifiers(node) && (node.flags & NodeFlags.Modifier)) {
|
||||
grammarErrorOnFirstToken(node, Diagnostics.An_export_declaration_cannot_have_modifiers);
|
||||
}
|
||||
if (!node.moduleSpecifier || checkExternalImportOrExportDeclaration(node)) {
|
||||
if (node.exportClause) {
|
||||
// export { x, y }
|
||||
// export { x, y } from "foo"
|
||||
forEach(node.exportClause.elements, checkExportSpecifier);
|
||||
|
||||
let inAmbientExternalModule = node.parent.kind === SyntaxKind.ModuleBlock && (<ModuleDeclaration>node.parent.parent).name.kind === SyntaxKind.StringLiteral;
|
||||
if (node.parent.kind !== SyntaxKind.SourceFile && !inAmbientExternalModule) {
|
||||
error(node, Diagnostics.Export_declarations_are_not_permitted_in_an_internal_module);
|
||||
}
|
||||
}
|
||||
else {
|
||||
// export * from "foo"
|
||||
let moduleSymbol = resolveExternalModuleName(node, node.moduleSpecifier);
|
||||
if (moduleSymbol && moduleSymbol.exports["export="]) {
|
||||
error(node.moduleSpecifier, Diagnostics.External_module_0_uses_export_and_cannot_be_used_with_export_Asterisk, symbolToString(moduleSymbol));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -10244,7 +10395,7 @@ module ts {
|
||||
return;
|
||||
}
|
||||
// Grammar checking
|
||||
if (!checkGrammarModifiers(node) && (node.flags & NodeFlags.Modifier)) {
|
||||
if (!checkGrammarDecorators(node) && !checkGrammarModifiers(node) && (node.flags & NodeFlags.Modifier)) {
|
||||
grammarErrorOnFirstToken(node, Diagnostics.An_export_assignment_cannot_have_modifiers);
|
||||
}
|
||||
if (node.expression) {
|
||||
@@ -10281,39 +10432,22 @@ module ts {
|
||||
}
|
||||
|
||||
function hasExportedMembers(moduleSymbol: Symbol) {
|
||||
let declarations = moduleSymbol.declarations;
|
||||
for (let current of declarations) {
|
||||
let statements = getModuleStatements(current);
|
||||
for (let node of statements) {
|
||||
if (node.kind === SyntaxKind.ExportDeclaration) {
|
||||
let exportClause = (<ExportDeclaration>node).exportClause;
|
||||
if (!exportClause) {
|
||||
return true;
|
||||
}
|
||||
let specifiers = exportClause.elements;
|
||||
for (let specifier of specifiers) {
|
||||
if (!(specifier.propertyName && specifier.name && specifier.name.text === "default")) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (node.kind !== SyntaxKind.ExportAssignment && node.flags & NodeFlags.Export && !(node.flags & NodeFlags.Default)) {
|
||||
return true;
|
||||
}
|
||||
for (var id in moduleSymbol.exports) {
|
||||
if (id !== "export=") {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function checkExternalModuleExports(node: SourceFile | ModuleDeclaration) {
|
||||
let moduleSymbol = getSymbolOfNode(node);
|
||||
let links = getSymbolLinks(moduleSymbol);
|
||||
if (!links.exportsChecked) {
|
||||
let defaultSymbol = getExportAssignmentSymbol(moduleSymbol);
|
||||
if (defaultSymbol) {
|
||||
if (languageVersion < ScriptTarget.ES6 && hasExportedMembers(moduleSymbol)) {
|
||||
let declaration = getDeclarationOfAliasSymbol(defaultSymbol) || defaultSymbol.valueDeclaration;
|
||||
error(declaration, Diagnostics.An_export_assignment_cannot_be_used_in_a_module_with_other_exported_elements);
|
||||
}
|
||||
let exportEqualsSymbol = moduleSymbol.exports["export="];
|
||||
if (exportEqualsSymbol && hasExportedMembers(moduleSymbol)) {
|
||||
let declaration = getDeclarationOfAliasSymbol(exportEqualsSymbol) || exportEqualsSymbol.valueDeclaration;
|
||||
error(declaration, Diagnostics.An_export_assignment_cannot_be_used_in_a_module_with_other_exported_elements);
|
||||
}
|
||||
links.exportsChecked = true;
|
||||
}
|
||||
@@ -10422,6 +10556,8 @@ module ts {
|
||||
case SyntaxKind.DebuggerStatement:
|
||||
checkGrammarStatementInAmbientContext(node);
|
||||
return;
|
||||
case SyntaxKind.MissingDeclaration:
|
||||
return checkMissingDeclaration(node);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10548,6 +10684,10 @@ module ts {
|
||||
links.flags |= NodeCheckFlags.EmitExtends;
|
||||
}
|
||||
|
||||
if (emitDecorate) {
|
||||
links.flags |= NodeCheckFlags.EmitDecorate;
|
||||
}
|
||||
|
||||
links.flags |= NodeCheckFlags.TypeChecked;
|
||||
}
|
||||
}
|
||||
@@ -10599,7 +10739,7 @@ module ts {
|
||||
|
||||
populateSymbols();
|
||||
|
||||
return mapToArray(symbols);
|
||||
return symbolsToArray(symbols);
|
||||
|
||||
function populateSymbols() {
|
||||
while (location) {
|
||||
@@ -10657,6 +10797,42 @@ module ts {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (isInsideWithStatementBody(location)) {
|
||||
// We cannot answer semantic questions within a with block, do not proceed any further
|
||||
return [];
|
||||
}
|
||||
|
||||
while (location) {
|
||||
if (location.locals && !isGlobalSourceFile(location)) {
|
||||
copySymbols(location.locals, meaning);
|
||||
}
|
||||
switch (location.kind) {
|
||||
case SyntaxKind.SourceFile:
|
||||
if (!isExternalModule(<SourceFile>location)) break;
|
||||
case SyntaxKind.ModuleDeclaration:
|
||||
copySymbols(getSymbolOfNode(location).exports, meaning & SymbolFlags.ModuleMember);
|
||||
break;
|
||||
case SyntaxKind.EnumDeclaration:
|
||||
copySymbols(getSymbolOfNode(location).exports, meaning & SymbolFlags.EnumMember);
|
||||
break;
|
||||
case SyntaxKind.ClassDeclaration:
|
||||
case SyntaxKind.InterfaceDeclaration:
|
||||
if (!(memberFlags & NodeFlags.Static)) {
|
||||
copySymbols(getSymbolOfNode(location).members, meaning & SymbolFlags.Type);
|
||||
}
|
||||
break;
|
||||
case SyntaxKind.FunctionExpression:
|
||||
if ((<FunctionExpression>location).name) {
|
||||
copySymbol(location.symbol, meaning);
|
||||
}
|
||||
break;
|
||||
}
|
||||
memberFlags = location.flags;
|
||||
location = location.parent;
|
||||
}
|
||||
copySymbols(globals, meaning);
|
||||
return symbolsToArray(symbols);
|
||||
}
|
||||
|
||||
function isTypeDeclarationName(name: Node): boolean {
|
||||
@@ -11008,26 +11184,34 @@ module ts {
|
||||
return symbol.flags & SymbolFlags.ValueModule && symbol.declarations.length === 1 && symbol.declarations[0].kind === SyntaxKind.SourceFile;
|
||||
}
|
||||
|
||||
function getAliasNameSubstitution(symbol: Symbol, getGeneratedNameForNode: (Node: Node) => string): string {
|
||||
let declaration = getDeclarationOfAliasSymbol(symbol);
|
||||
if (declaration && declaration.kind === SyntaxKind.ImportSpecifier) {
|
||||
let moduleName = getGeneratedNameForNode(<ImportDeclaration>declaration.parent.parent.parent);
|
||||
let propertyName = (<ImportSpecifier>declaration).propertyName || (<ImportSpecifier>declaration).name;
|
||||
return moduleName + "." + unescapeIdentifier(propertyName.text);
|
||||
function getAliasNameSubstitution(symbol: Symbol, getGeneratedNameForNode: (node: Node) => string): string {
|
||||
// If this is es6 or higher, just use the name of the export
|
||||
// no need to qualify it.
|
||||
if (languageVersion >= ScriptTarget.ES6) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
let node = getDeclarationOfAliasSymbol(symbol);
|
||||
if (node) {
|
||||
if (node.kind === SyntaxKind.ImportClause) {
|
||||
return getGeneratedNameForNode(<ImportDeclaration>node.parent) + ".default";
|
||||
}
|
||||
if (node.kind === SyntaxKind.ImportSpecifier) {
|
||||
let moduleName = getGeneratedNameForNode(<ImportDeclaration>node.parent.parent.parent);
|
||||
let propertyName = (<ImportSpecifier>node).propertyName || (<ImportSpecifier>node).name;
|
||||
return moduleName + "." + unescapeIdentifier(propertyName.text);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function getExportNameSubstitution(symbol: Symbol, location: Node, getGeneratedNameForNode: (Node: Node) => string): string {
|
||||
if (isExternalModuleSymbol(symbol.parent)) {
|
||||
var symbolName = unescapeIdentifier(symbol.name);
|
||||
// If this is es6 or higher, just use the name of the export
|
||||
// no need to qualify it.
|
||||
if (languageVersion >= ScriptTarget.ES6) {
|
||||
return symbolName;
|
||||
}
|
||||
else {
|
||||
return "exports." + symbolName;
|
||||
return undefined;
|
||||
}
|
||||
return "exports." + unescapeIdentifier(symbol.name);
|
||||
}
|
||||
let node = location;
|
||||
let containerSymbol = getParentOfSymbol(symbol);
|
||||
@@ -11040,7 +11224,7 @@ module ts {
|
||||
}
|
||||
|
||||
function getExpressionNameSubstitution(node: Identifier, getGeneratedNameForNode: (Node: Node) => string): string {
|
||||
let symbol = getNodeLinks(node).resolvedSymbol;
|
||||
let symbol = getNodeLinks(node).resolvedSymbol || (isDeclarationName(node) ? getSymbolOfNode(node.parent) : undefined);
|
||||
if (symbol) {
|
||||
// Whan an identifier resolves to a parented symbol, it references an exported entity from
|
||||
// another declaration of the same internal module.
|
||||
@@ -11055,15 +11239,27 @@ module ts {
|
||||
return getExportNameSubstitution(exportSymbol, node.parent, getGeneratedNameForNode);
|
||||
}
|
||||
// Named imports from ES6 import declarations are rewritten
|
||||
if (symbol.flags & SymbolFlags.Alias && languageVersion < ScriptTarget.ES6) {
|
||||
if (symbol.flags & SymbolFlags.Alias) {
|
||||
return getAliasNameSubstitution(symbol, getGeneratedNameForNode);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function hasExportDefaultValue(node: SourceFile): boolean {
|
||||
let symbol = getResolvedExportAssignmentSymbol(getSymbolOfNode(node));
|
||||
return symbol && symbol !== unknownSymbol && symbolIsValue(symbol) && !isConstEnumSymbol(symbol);
|
||||
function isValueAliasDeclaration(node: Node): boolean {
|
||||
switch (node.kind) {
|
||||
case SyntaxKind.ImportEqualsDeclaration:
|
||||
case SyntaxKind.ImportClause:
|
||||
case SyntaxKind.NamespaceImport:
|
||||
case SyntaxKind.ImportSpecifier:
|
||||
case SyntaxKind.ExportSpecifier:
|
||||
return isAliasResolvedToValue(getSymbolOfNode(node));
|
||||
case SyntaxKind.ExportDeclaration:
|
||||
let exportClause = (<ExportDeclaration>node).exportClause;
|
||||
return exportClause && forEach(exportClause.elements, isValueAliasDeclaration);
|
||||
case SyntaxKind.ExportAssignment:
|
||||
return (<ExportAssignment>node).expression && (<ExportAssignment>node).expression.kind === SyntaxKind.Identifier ? isAliasResolvedToValue(getSymbolOfNode(node)) : true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function isTopLevelValueImportEqualsWithEntityName(node: ImportEqualsDeclaration): boolean {
|
||||
@@ -11084,13 +11280,18 @@ module ts {
|
||||
return isConstEnumSymbol(s) || s.constEnumOnlyModule;
|
||||
}
|
||||
|
||||
function isReferencedAliasDeclaration(node: Node): boolean {
|
||||
function isReferencedAliasDeclaration(node: Node, checkChildren?: boolean): boolean {
|
||||
if (isAliasSymbolDeclaration(node)) {
|
||||
let symbol = getSymbolOfNode(node);
|
||||
if (getSymbolLinks(symbol).referenced) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
if (checkChildren) {
|
||||
return forEachChild(node, node => isReferencedAliasDeclaration(node, checkChildren));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function isImplementationOfOverload(node: FunctionLikeDeclaration) {
|
||||
@@ -11193,11 +11394,25 @@ module ts {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function instantiateSingleCallFunctionType(functionType: Type, typeArguments: Type[]): Type {
|
||||
if (functionType === unknownType) {
|
||||
return unknownType;
|
||||
}
|
||||
|
||||
let signature = getSingleCallSignature(functionType);
|
||||
if (!signature) {
|
||||
return unknownType;
|
||||
}
|
||||
|
||||
let instantiatedSignature = getSignatureInstantiation(signature, typeArguments);
|
||||
return getOrCreateTypeFromSignature(instantiatedSignature);
|
||||
}
|
||||
|
||||
function createResolver(): EmitResolver {
|
||||
return {
|
||||
getExpressionNameSubstitution,
|
||||
isValueAliasDeclaration,
|
||||
hasGlobalName,
|
||||
hasExportDefaultValue,
|
||||
isReferencedAliasDeclaration,
|
||||
getNodeCheckFlags,
|
||||
isTopLevelValueImportEqualsWithEntityName,
|
||||
@@ -11242,6 +11457,11 @@ module ts {
|
||||
globalNumberType = getGlobalType("Number");
|
||||
globalBooleanType = getGlobalType("Boolean");
|
||||
globalRegExpType = getGlobalType("RegExp");
|
||||
globalTypedPropertyDescriptorType = getTypeOfGlobalSymbol(getGlobalTypeSymbol("TypedPropertyDescriptor"), 1);
|
||||
globalClassDecoratorType = getGlobalType("ClassDecorator");
|
||||
globalPropertyDecoratorType = getGlobalType("PropertyDecorator");
|
||||
globalMethodDecoratorType = getGlobalType("MethodDecorator");
|
||||
globalParameterDecoratorType = getGlobalType("ParameterDecorator");
|
||||
|
||||
// If we're in ES6 mode, load the TemplateStringsArray.
|
||||
// Otherwise, default to 'unknown' for the purposes of type checking in LS scenarios.
|
||||
@@ -11266,6 +11486,25 @@ module ts {
|
||||
|
||||
|
||||
// GRAMMAR CHECKING
|
||||
function checkGrammarDecorators(node: Node): boolean {
|
||||
if (!node.decorators) {
|
||||
return false;
|
||||
}
|
||||
if (!nodeCanBeDecorated(node)) {
|
||||
return grammarErrorOnNode(node, Diagnostics.Decorators_are_not_valid_here);
|
||||
}
|
||||
else if (languageVersion < ScriptTarget.ES5) {
|
||||
return grammarErrorOnNode(node, Diagnostics.Decorators_are_only_available_when_targeting_ECMAScript_5_and_higher);
|
||||
}
|
||||
else if (node.kind === SyntaxKind.GetAccessor || node.kind === SyntaxKind.SetAccessor) {
|
||||
let accessors = getAllAccessorDeclarations((<ClassDeclaration>node.parent).members, <AccessorDeclaration>node);
|
||||
if (accessors.firstAccessor.decorators && node === accessors.secondAccessor) {
|
||||
return grammarErrorOnNode(node, Diagnostics.Decorators_cannot_be_applied_to_multiple_get_Slashset_accessors_of_the_same_name);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function checkGrammarModifiers(node: Node): boolean {
|
||||
switch (node.kind) {
|
||||
case SyntaxKind.GetAccessor:
|
||||
@@ -11462,7 +11701,7 @@ module ts {
|
||||
function checkGrammarFunctionLikeDeclaration(node: FunctionLikeDeclaration): boolean {
|
||||
// Prevent cascading error by short-circuit
|
||||
let file = getSourceFileOfNode(node);
|
||||
return checkGrammarModifiers(node) || checkGrammarTypeParameterList(node, node.typeParameters, file) ||
|
||||
return checkGrammarDecorators(node) || checkGrammarModifiers(node) || checkGrammarTypeParameterList(node, node.typeParameters, file) ||
|
||||
checkGrammarParameterList(node.parameters) || checkGrammarArrowFunction(node, file);
|
||||
}
|
||||
|
||||
@@ -11519,7 +11758,7 @@ module ts {
|
||||
|
||||
function checkGrammarIndexSignature(node: SignatureDeclaration) {
|
||||
// Prevent cascading error by short-circuit
|
||||
checkGrammarModifiers(node) || checkGrammarIndexSignatureParameters(node) || checkGrammarForIndexSignatureModifier(node);
|
||||
return checkGrammarDecorators(node) || checkGrammarModifiers(node) || checkGrammarIndexSignatureParameters(node) || checkGrammarForIndexSignatureModifier(node);
|
||||
}
|
||||
|
||||
function checkGrammarForAtLeastOneTypeArgument(node: Node, typeArguments: NodeArray<TypeNode>): boolean {
|
||||
@@ -11568,7 +11807,7 @@ module ts {
|
||||
let seenExtendsClause = false;
|
||||
let seenImplementsClause = false;
|
||||
|
||||
if (!checkGrammarModifiers(node) && node.heritageClauses) {
|
||||
if (!checkGrammarDecorators(node) && !checkGrammarModifiers(node) && node.heritageClauses) {
|
||||
for (let heritageClause of node.heritageClauses) {
|
||||
if (heritageClause.token === SyntaxKind.ExtendsKeyword) {
|
||||
if (seenExtendsClause) {
|
||||
|
||||
@@ -162,6 +162,39 @@ module ts {
|
||||
return ~low;
|
||||
}
|
||||
|
||||
export function reduceLeft<T>(array: T[], f: (a: T, x: T) => T): T;
|
||||
export function reduceLeft<T, U>(array: T[], f: (a: U, x: T) => U, initial: U): U;
|
||||
export function reduceLeft<T, U>(array: T[], f: (a: U, x: T) => U, initial?: U): U {
|
||||
if (array) {
|
||||
var count = array.length;
|
||||
if (count > 0) {
|
||||
var pos = 0;
|
||||
var result = arguments.length <= 2 ? array[pos++] : initial;
|
||||
while (pos < count) {
|
||||
result = f(<U>result, array[pos++]);
|
||||
}
|
||||
return <U>result;
|
||||
}
|
||||
}
|
||||
return initial;
|
||||
}
|
||||
|
||||
export function reduceRight<T>(array: T[], f: (a: T, x: T) => T): T;
|
||||
export function reduceRight<T, U>(array: T[], f: (a: U, x: T) => U, initial: U): U;
|
||||
export function reduceRight<T, U>(array: T[], f: (a: U, x: T) => U, initial?: U): U {
|
||||
if (array) {
|
||||
var pos = array.length - 1;
|
||||
if (pos >= 0) {
|
||||
var result = arguments.length <= 2 ? array[pos--] : initial;
|
||||
while (pos >= 0) {
|
||||
result = f(<U>result, array[pos--]);
|
||||
}
|
||||
return <U>result;
|
||||
}
|
||||
}
|
||||
return initial;
|
||||
}
|
||||
|
||||
let hasOwnProperty = Object.prototype.hasOwnProperty;
|
||||
|
||||
export function hasProperty<T>(map: Map<T>, key: string): boolean {
|
||||
@@ -222,16 +255,6 @@ module ts {
|
||||
return hasProperty(map, key) ? map[key] : undefined;
|
||||
}
|
||||
|
||||
export function mapToArray<T>(map: Map<T>): T[] {
|
||||
let result: T[] = [];
|
||||
|
||||
for (let id in map) {
|
||||
result.push(map[id]);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
export function copyMap<T>(source: Map<T>, target: Map<T>): void {
|
||||
for (let p in source) {
|
||||
target[p] = source[p];
|
||||
|
||||
@@ -149,7 +149,7 @@ module ts {
|
||||
The_variable_declaration_of_a_for_in_statement_cannot_have_an_initializer: { code: 1189, category: DiagnosticCategory.Error, key: "The variable declaration of a 'for...in' statement cannot have an initializer." },
|
||||
The_variable_declaration_of_a_for_of_statement_cannot_have_an_initializer: { code: 1190, category: DiagnosticCategory.Error, key: "The variable declaration of a 'for...of' statement cannot have an initializer." },
|
||||
An_import_declaration_cannot_have_modifiers: { code: 1191, category: DiagnosticCategory.Error, key: "An import declaration cannot have modifiers." },
|
||||
External_module_0_has_no_default_export_or_export_assignment: { code: 1192, category: DiagnosticCategory.Error, key: "External module '{0}' has no default export or export assignment." },
|
||||
External_module_0_has_no_default_export: { code: 1192, category: DiagnosticCategory.Error, key: "External module '{0}' has no default export." },
|
||||
An_export_declaration_cannot_have_modifiers: { code: 1193, category: DiagnosticCategory.Error, key: "An export declaration cannot have modifiers." },
|
||||
Export_declarations_are_not_permitted_in_an_internal_module: { code: 1194, category: DiagnosticCategory.Error, key: "Export declarations are not permitted in an internal module." },
|
||||
Catch_clause_variable_name_must_be_an_identifier: { code: 1195, category: DiagnosticCategory.Error, key: "Catch clause variable name must be an identifier." },
|
||||
@@ -162,6 +162,9 @@ module ts {
|
||||
Import_assignment_cannot_be_used_when_targeting_ECMAScript_6_or_higher_Consider_using_import_Asterisk_as_ns_from_mod_import_a_from_mod_or_import_d_from_mod_instead: { code: 1202, category: DiagnosticCategory.Error, key: "Import assignment cannot be used when targeting ECMAScript 6 or higher. Consider using 'import * as ns from \"mod\"', 'import {a} from \"mod\"' or 'import d from \"mod\"' instead." },
|
||||
Export_assignment_cannot_be_used_when_targeting_ECMAScript_6_or_higher_Consider_using_export_default_instead: { code: 1203, category: DiagnosticCategory.Error, key: "Export assignment cannot be used when targeting ECMAScript 6 or higher. Consider using 'export default' instead." },
|
||||
Cannot_compile_external_modules_into_amd_or_commonjs_when_targeting_es6_or_higher: { code: 1204, category: DiagnosticCategory.Error, key: "Cannot compile external modules into amd or commonjs when targeting es6 or higher." },
|
||||
Decorators_are_only_available_when_targeting_ECMAScript_5_and_higher: { code: 1205, category: DiagnosticCategory.Error, key: "Decorators are only available when targeting ECMAScript 5 and higher." },
|
||||
Decorators_are_not_valid_here: { code: 1206, category: DiagnosticCategory.Error, key: "Decorators are not valid here." },
|
||||
Decorators_cannot_be_applied_to_multiple_get_Slashset_accessors_of_the_same_name: { code: 1207, category: DiagnosticCategory.Error, key: "Decorators cannot be applied to multiple get/set accessors of the same name." },
|
||||
Duplicate_identifier_0: { code: 2300, category: DiagnosticCategory.Error, key: "Duplicate identifier '{0}'." },
|
||||
Initializer_of_instance_member_variable_0_cannot_reference_identifier_1_declared_in_the_constructor: { code: 2301, category: DiagnosticCategory.Error, key: "Initializer of instance member variable '{0}' cannot reference identifier '{1}' declared in the constructor." },
|
||||
Static_members_cannot_reference_class_type_parameters: { code: 2302, category: DiagnosticCategory.Error, key: "Static members cannot reference class type parameters." },
|
||||
@@ -346,6 +349,8 @@ module ts {
|
||||
Using_a_string_in_a_for_of_statement_is_only_supported_in_ECMAScript_5_and_higher: { code: 2494, category: DiagnosticCategory.Error, key: "Using a string in a 'for...of' statement is only supported in ECMAScript 5 and higher." },
|
||||
Type_0_is_not_an_array_type_or_a_string_type: { code: 2495, category: DiagnosticCategory.Error, key: "Type '{0}' is not an array type or a string type." },
|
||||
The_arguments_object_cannot_be_referenced_in_an_arrow_function_Consider_using_a_standard_function_expression: { code: 2496, category: DiagnosticCategory.Error, key: "The 'arguments' object cannot be referenced in an arrow function. Consider using a standard function expression." },
|
||||
External_module_0_resolves_to_a_non_module_entity_and_cannot_be_imported_using_this_construct: { code: 2497, category: DiagnosticCategory.Error, key: "External module '{0}' resolves to a non-module entity and cannot be imported using this construct." },
|
||||
External_module_0_uses_export_and_cannot_be_used_with_export_Asterisk: { code: 2498, category: DiagnosticCategory.Error, key: "External module '{0}' uses 'export =' and cannot be used with 'export *'." },
|
||||
Import_declaration_0_is_using_private_name_1: { code: 4000, category: DiagnosticCategory.Error, key: "Import declaration '{0}' is using private name '{1}'." },
|
||||
Type_parameter_0_of_exported_class_has_or_is_using_private_name_1: { code: 4002, category: DiagnosticCategory.Error, key: "Type parameter '{0}' of exported class has or is using private name '{1}'." },
|
||||
Type_parameter_0_of_exported_interface_has_or_is_using_private_name_1: { code: 4004, category: DiagnosticCategory.Error, key: "Type parameter '{0}' of exported interface has or is using private name '{1}'." },
|
||||
|
||||
@@ -587,7 +587,7 @@
|
||||
"category": "Error",
|
||||
"code": 1191
|
||||
},
|
||||
"External module '{0}' has no default export or export assignment.": {
|
||||
"External module '{0}' has no default export.": {
|
||||
"category": "Error",
|
||||
"code": 1192
|
||||
},
|
||||
@@ -639,6 +639,18 @@
|
||||
"category": "Error",
|
||||
"code": 1204
|
||||
},
|
||||
"Decorators are only available when targeting ECMAScript 5 and higher.": {
|
||||
"category": "Error",
|
||||
"code": 1205
|
||||
},
|
||||
"Decorators are not valid here.": {
|
||||
"category": "Error",
|
||||
"code": 1206
|
||||
},
|
||||
"Decorators cannot be applied to multiple get/set accessors of the same name.": {
|
||||
"category": "Error",
|
||||
"code": 1207
|
||||
},
|
||||
|
||||
"Duplicate identifier '{0}'.": {
|
||||
"category": "Error",
|
||||
@@ -1376,6 +1388,14 @@
|
||||
"category": "Error",
|
||||
"code": 2496
|
||||
},
|
||||
"External module '{0}' resolves to a non-module entity and cannot be imported using this construct.": {
|
||||
"category": "Error",
|
||||
"code": 2497
|
||||
},
|
||||
"External module '{0}' uses 'export =' and cannot be used with 'export *'.": {
|
||||
"category": "Error",
|
||||
"code": 2498
|
||||
},
|
||||
|
||||
"Import declaration '{0}' is using private name '{1}'.": {
|
||||
"category": "Error",
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -64,7 +64,8 @@ module ts {
|
||||
case SyntaxKind.ShorthandPropertyAssignment:
|
||||
case SyntaxKind.VariableDeclaration:
|
||||
case SyntaxKind.BindingElement:
|
||||
return visitNodes(cbNodes, node.modifiers) ||
|
||||
return visitNodes(cbNodes, node.decorators) ||
|
||||
visitNodes(cbNodes, node.modifiers) ||
|
||||
visitNode(cbNode, (<VariableLikeDeclaration>node).propertyName) ||
|
||||
visitNode(cbNode, (<VariableLikeDeclaration>node).dotDotDotToken) ||
|
||||
visitNode(cbNode, (<VariableLikeDeclaration>node).name) ||
|
||||
@@ -76,7 +77,8 @@ module ts {
|
||||
case SyntaxKind.CallSignature:
|
||||
case SyntaxKind.ConstructSignature:
|
||||
case SyntaxKind.IndexSignature:
|
||||
return visitNodes(cbNodes, node.modifiers) ||
|
||||
return visitNodes(cbNodes, node.decorators) ||
|
||||
visitNodes(cbNodes, node.modifiers) ||
|
||||
visitNodes(cbNodes, (<SignatureDeclaration>node).typeParameters) ||
|
||||
visitNodes(cbNodes, (<SignatureDeclaration>node).parameters) ||
|
||||
visitNode(cbNode, (<SignatureDeclaration>node).type);
|
||||
@@ -88,7 +90,8 @@ module ts {
|
||||
case SyntaxKind.FunctionExpression:
|
||||
case SyntaxKind.FunctionDeclaration:
|
||||
case SyntaxKind.ArrowFunction:
|
||||
return visitNodes(cbNodes, node.modifiers) ||
|
||||
return visitNodes(cbNodes, node.decorators) ||
|
||||
visitNodes(cbNodes, node.modifiers) ||
|
||||
visitNode(cbNode, (<FunctionLikeDeclaration>node).asteriskToken) ||
|
||||
visitNode(cbNode, (<FunctionLikeDeclaration>node).name) ||
|
||||
visitNode(cbNode, (<FunctionLikeDeclaration>node).questionToken) ||
|
||||
@@ -171,7 +174,8 @@ module ts {
|
||||
return visitNodes(cbNodes, (<SourceFile>node).statements) ||
|
||||
visitNode(cbNode, (<SourceFile>node).endOfFileToken);
|
||||
case SyntaxKind.VariableStatement:
|
||||
return visitNodes(cbNodes, node.modifiers) ||
|
||||
return visitNodes(cbNodes, node.decorators) ||
|
||||
visitNodes(cbNodes, node.modifiers) ||
|
||||
visitNode(cbNode, (<VariableStatement>node).declarationList);
|
||||
case SyntaxKind.VariableDeclarationList:
|
||||
return visitNodes(cbNodes, (<VariableDeclarationList>node).declarations);
|
||||
@@ -230,39 +234,48 @@ module ts {
|
||||
case SyntaxKind.CatchClause:
|
||||
return visitNode(cbNode, (<CatchClause>node).variableDeclaration) ||
|
||||
visitNode(cbNode, (<CatchClause>node).block);
|
||||
case SyntaxKind.Decorator:
|
||||
return visitNode(cbNode, (<Decorator>node).expression);
|
||||
case SyntaxKind.ClassDeclaration:
|
||||
return visitNodes(cbNodes, node.modifiers) ||
|
||||
return visitNodes(cbNodes, node.decorators) ||
|
||||
visitNodes(cbNodes, node.modifiers) ||
|
||||
visitNode(cbNode, (<ClassDeclaration>node).name) ||
|
||||
visitNodes(cbNodes, (<ClassDeclaration>node).typeParameters) ||
|
||||
visitNodes(cbNodes, (<ClassDeclaration>node).heritageClauses) ||
|
||||
visitNodes(cbNodes, (<ClassDeclaration>node).members);
|
||||
case SyntaxKind.InterfaceDeclaration:
|
||||
return visitNodes(cbNodes, node.modifiers) ||
|
||||
return visitNodes(cbNodes, node.decorators) ||
|
||||
visitNodes(cbNodes, node.modifiers) ||
|
||||
visitNode(cbNode, (<InterfaceDeclaration>node).name) ||
|
||||
visitNodes(cbNodes, (<InterfaceDeclaration>node).typeParameters) ||
|
||||
visitNodes(cbNodes, (<ClassDeclaration>node).heritageClauses) ||
|
||||
visitNodes(cbNodes, (<InterfaceDeclaration>node).members);
|
||||
case SyntaxKind.TypeAliasDeclaration:
|
||||
return visitNodes(cbNodes, node.modifiers) ||
|
||||
return visitNodes(cbNodes, node.decorators) ||
|
||||
visitNodes(cbNodes, node.modifiers) ||
|
||||
visitNode(cbNode, (<TypeAliasDeclaration>node).name) ||
|
||||
visitNode(cbNode, (<TypeAliasDeclaration>node).type);
|
||||
case SyntaxKind.EnumDeclaration:
|
||||
return visitNodes(cbNodes, node.modifiers) ||
|
||||
return visitNodes(cbNodes, node.decorators) ||
|
||||
visitNodes(cbNodes, node.modifiers) ||
|
||||
visitNode(cbNode, (<EnumDeclaration>node).name) ||
|
||||
visitNodes(cbNodes, (<EnumDeclaration>node).members);
|
||||
case SyntaxKind.EnumMember:
|
||||
return visitNode(cbNode, (<EnumMember>node).name) ||
|
||||
visitNode(cbNode, (<EnumMember>node).initializer);
|
||||
case SyntaxKind.ModuleDeclaration:
|
||||
return visitNodes(cbNodes, node.modifiers) ||
|
||||
return visitNodes(cbNodes, node.decorators) ||
|
||||
visitNodes(cbNodes, node.modifiers) ||
|
||||
visitNode(cbNode, (<ModuleDeclaration>node).name) ||
|
||||
visitNode(cbNode, (<ModuleDeclaration>node).body);
|
||||
case SyntaxKind.ImportEqualsDeclaration:
|
||||
return visitNodes(cbNodes, node.modifiers) ||
|
||||
return visitNodes(cbNodes, node.decorators) ||
|
||||
visitNodes(cbNodes, node.modifiers) ||
|
||||
visitNode(cbNode, (<ImportEqualsDeclaration>node).name) ||
|
||||
visitNode(cbNode, (<ImportEqualsDeclaration>node).moduleReference);
|
||||
case SyntaxKind.ImportDeclaration:
|
||||
return visitNodes(cbNodes, node.modifiers) ||
|
||||
return visitNodes(cbNodes, node.decorators) ||
|
||||
visitNodes(cbNodes, node.modifiers) ||
|
||||
visitNode(cbNode, (<ImportDeclaration>node).importClause) ||
|
||||
visitNode(cbNode, (<ImportDeclaration>node).moduleSpecifier);
|
||||
case SyntaxKind.ImportClause:
|
||||
@@ -274,7 +287,8 @@ module ts {
|
||||
case SyntaxKind.NamedExports:
|
||||
return visitNodes(cbNodes, (<NamedImportsOrExports>node).elements);
|
||||
case SyntaxKind.ExportDeclaration:
|
||||
return visitNodes(cbNodes, node.modifiers) ||
|
||||
return visitNodes(cbNodes, node.decorators) ||
|
||||
visitNodes(cbNodes, node.modifiers) ||
|
||||
visitNode(cbNode, (<ExportDeclaration>node).exportClause) ||
|
||||
visitNode(cbNode, (<ExportDeclaration>node).moduleSpecifier);
|
||||
case SyntaxKind.ImportSpecifier:
|
||||
@@ -282,7 +296,8 @@ module ts {
|
||||
return visitNode(cbNode, (<ImportOrExportSpecifier>node).propertyName) ||
|
||||
visitNode(cbNode, (<ImportOrExportSpecifier>node).name);
|
||||
case SyntaxKind.ExportAssignment:
|
||||
return visitNodes(cbNodes, node.modifiers) ||
|
||||
return visitNodes(cbNodes, node.decorators) ||
|
||||
visitNodes(cbNodes, node.modifiers) ||
|
||||
visitNode(cbNode, (<ExportAssignment>node).expression) ||
|
||||
visitNode(cbNode, (<ExportAssignment>node).type);
|
||||
case SyntaxKind.TemplateExpression:
|
||||
@@ -295,6 +310,8 @@ module ts {
|
||||
return visitNodes(cbNodes, (<HeritageClause>node).types);
|
||||
case SyntaxKind.ExternalModuleReference:
|
||||
return visitNode(cbNode, (<ExternalModuleReference>node).expression);
|
||||
case SyntaxKind.MissingDeclaration:
|
||||
return visitNodes(cbNodes, node.decorators);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -987,6 +1004,8 @@ module ts {
|
||||
}
|
||||
|
||||
function parseSourceFile(fileName: string, sourceText: string, languageVersion: ScriptTarget, syntaxCursor: SyntaxCursor, setParentNodes = false): SourceFile {
|
||||
const disallowInAndDecoratorContext = ParserContextFlags.DisallowIn | ParserContextFlags.Decorator;
|
||||
|
||||
let parsingContext: ParsingContext = 0;
|
||||
let identifiers: Map<string> = {};
|
||||
let identifierCount = 0;
|
||||
@@ -1130,6 +1149,23 @@ module ts {
|
||||
setContextFlag(val, ParserContextFlags.GeneratorParameter);
|
||||
}
|
||||
|
||||
function setDecoratorContext(val: boolean) {
|
||||
setContextFlag(val, ParserContextFlags.Decorator);
|
||||
}
|
||||
|
||||
function doOutsideOfContext<T>(flags: ParserContextFlags, func: () => T): T {
|
||||
let currentContextFlags = contextFlags & flags;
|
||||
if (currentContextFlags) {
|
||||
setContextFlag(false, currentContextFlags);
|
||||
let result = func();
|
||||
setContextFlag(true, currentContextFlags);
|
||||
return result;
|
||||
}
|
||||
|
||||
// no need to do anything special as we are not in any of the requested contexts
|
||||
return func();
|
||||
}
|
||||
|
||||
function allowInAnd<T>(func: () => T): T {
|
||||
if (contextFlags & ParserContextFlags.DisallowIn) {
|
||||
setDisallowInContext(false);
|
||||
@@ -1178,6 +1214,18 @@ module ts {
|
||||
return func();
|
||||
}
|
||||
|
||||
function doInDecoratorContext<T>(func: () => T): T {
|
||||
if (contextFlags & ParserContextFlags.Decorator) {
|
||||
// no need to do anything special if we're already in the [Decorator] context.
|
||||
return func();
|
||||
}
|
||||
|
||||
setDecoratorContext(true);
|
||||
let result = func();
|
||||
setDecoratorContext(false);
|
||||
return result;
|
||||
}
|
||||
|
||||
function inYieldContext() {
|
||||
return (contextFlags & ParserContextFlags.Yield) !== 0;
|
||||
}
|
||||
@@ -1194,6 +1242,10 @@ module ts {
|
||||
return (contextFlags & ParserContextFlags.DisallowIn) !== 0;
|
||||
}
|
||||
|
||||
function inDecoratorContext() {
|
||||
return (contextFlags & ParserContextFlags.Decorator) !== 0;
|
||||
}
|
||||
|
||||
function parseErrorAtCurrentToken(message: DiagnosticMessage, arg0?: any): void {
|
||||
let start = scanner.getTokenPos();
|
||||
let length = scanner.getTextPos() - start;
|
||||
@@ -1403,7 +1455,7 @@ module ts {
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
|
||||
function createMissingNode(kind: SyntaxKind, reportAtCurrentPosition: boolean, diagnosticMessage: DiagnosticMessage, arg0?: any): Node {
|
||||
if (reportAtCurrentPosition) {
|
||||
parseErrorAtPosition(scanner.getStartPos(), 0, diagnosticMessage, arg0);
|
||||
@@ -2273,7 +2325,7 @@ module ts {
|
||||
}
|
||||
|
||||
function isStartOfParameter(): boolean {
|
||||
return token === SyntaxKind.DotDotDotToken || isIdentifierOrPattern() || isModifier(token);
|
||||
return token === SyntaxKind.DotDotDotToken || isIdentifierOrPattern() || isModifier(token) || token === SyntaxKind.AtToken;
|
||||
}
|
||||
|
||||
function setModifiers(node: Node, modifiers: ModifiersArray) {
|
||||
@@ -2285,6 +2337,7 @@ module ts {
|
||||
|
||||
function parseParameter(): ParameterDeclaration {
|
||||
let node = <ParameterDeclaration>createNode(SyntaxKind.Parameter);
|
||||
node.decorators = parseDecorators();
|
||||
setModifiers(node, parseModifiers());
|
||||
node.dotDotDotToken = parseOptionalToken(SyntaxKind.DotDotDotToken);
|
||||
|
||||
@@ -2473,9 +2526,9 @@ module ts {
|
||||
return token === SyntaxKind.ColonToken || token === SyntaxKind.CommaToken || token === SyntaxKind.CloseBracketToken;
|
||||
}
|
||||
|
||||
function parseIndexSignatureDeclaration(modifiers: ModifiersArray): IndexSignatureDeclaration {
|
||||
let fullStart = modifiers ? modifiers.pos : scanner.getStartPos();
|
||||
function parseIndexSignatureDeclaration(fullStart: number, decorators: NodeArray<Decorator>, modifiers: ModifiersArray): IndexSignatureDeclaration {
|
||||
let node = <IndexSignatureDeclaration>createNode(SyntaxKind.IndexSignature, fullStart);
|
||||
node.decorators = decorators;
|
||||
setModifiers(node, modifiers);
|
||||
node.parameters = parseBracketedList(ParsingContext.Parameters, parseParameter, SyntaxKind.OpenBracketToken, SyntaxKind.CloseBracketToken);
|
||||
node.type = parseTypeAnnotation();
|
||||
@@ -2552,7 +2605,7 @@ module ts {
|
||||
case SyntaxKind.OpenBracketToken:
|
||||
// Indexer or computed property
|
||||
return isIndexSignature()
|
||||
? parseIndexSignatureDeclaration(/*modifiers:*/ undefined)
|
||||
? parseIndexSignatureDeclaration(scanner.getStartPos(), /*decorators*/ undefined, /*modifiers:*/ undefined)
|
||||
: parsePropertyOrMethodSignature();
|
||||
case SyntaxKind.NewKeyword:
|
||||
if (lookAhead(isStartOfConstructSignature)) {
|
||||
@@ -2583,9 +2636,11 @@ module ts {
|
||||
}
|
||||
|
||||
function parseIndexSignatureWithModifiers() {
|
||||
let fullStart = scanner.getStartPos();
|
||||
let decorators = parseDecorators();
|
||||
let modifiers = parseModifiers();
|
||||
return isIndexSignature()
|
||||
? parseIndexSignatureDeclaration(modifiers)
|
||||
? parseIndexSignatureDeclaration(fullStart, decorators, modifiers)
|
||||
: undefined;
|
||||
}
|
||||
|
||||
@@ -2841,7 +2896,7 @@ module ts {
|
||||
|
||||
function isStartOfExpressionStatement(): boolean {
|
||||
// As per the grammar, neither '{' nor 'function' can start an expression statement.
|
||||
return token !== SyntaxKind.OpenBraceToken && token !== SyntaxKind.FunctionKeyword && isStartOfExpression();
|
||||
return token !== SyntaxKind.OpenBraceToken && token !== SyntaxKind.FunctionKeyword && token !== SyntaxKind.AtToken && isStartOfExpression();
|
||||
}
|
||||
|
||||
function parseExpression(): Expression {
|
||||
@@ -2849,11 +2904,21 @@ module ts {
|
||||
// AssignmentExpression[in]
|
||||
// Expression[in] , AssignmentExpression[in]
|
||||
|
||||
// clear the decorator context when parsing Expression, as it should be unambiguous when parsing a decorator
|
||||
let saveDecoratorContext = inDecoratorContext();
|
||||
if (saveDecoratorContext) {
|
||||
setDecoratorContext(false);
|
||||
}
|
||||
|
||||
let expr = parseAssignmentExpressionOrHigher();
|
||||
let operatorToken: Node;
|
||||
while ((operatorToken = parseOptionalToken(SyntaxKind.CommaToken))) {
|
||||
expr = makeBinaryExpression(expr, operatorToken, parseAssignmentExpressionOrHigher());
|
||||
}
|
||||
|
||||
if (saveDecoratorContext) {
|
||||
setDecoratorContext(true);
|
||||
}
|
||||
return expr;
|
||||
}
|
||||
|
||||
@@ -3209,7 +3274,7 @@ module ts {
|
||||
let node = <ConditionalExpression>createNode(SyntaxKind.ConditionalExpression, leftOperand.pos);
|
||||
node.condition = leftOperand;
|
||||
node.questionToken = questionToken;
|
||||
node.whenTrue = allowInAnd(parseAssignmentExpressionOrHigher);
|
||||
node.whenTrue = doOutsideOfContext(disallowInAndDecoratorContext, parseAssignmentExpressionOrHigher);
|
||||
node.colonToken = parseExpectedToken(SyntaxKind.ColonToken, /*reportAtCurrentPosition:*/ false,
|
||||
Diagnostics._0_expected, tokenToString(SyntaxKind.ColonToken));
|
||||
node.whenFalse = parseAssignmentExpressionOrHigher();
|
||||
@@ -3500,7 +3565,8 @@ module ts {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (parseOptional(SyntaxKind.OpenBracketToken)) {
|
||||
// when in the [Decorator] context, we do not parse ElementAccess as it could be part of a ComputedPropertyName
|
||||
if (!inDecoratorContext() && parseOptional(SyntaxKind.OpenBracketToken)) {
|
||||
let indexedAccess = <ElementAccessExpression>createNode(SyntaxKind.ElementAccessExpression, expression.pos);
|
||||
indexedAccess.expression = expression;
|
||||
|
||||
@@ -3536,7 +3602,6 @@ module ts {
|
||||
function parseCallExpressionRest(expression: LeftHandSideExpression): LeftHandSideExpression {
|
||||
while (true) {
|
||||
expression = parseMemberExpressionRest(expression);
|
||||
|
||||
if (token === SyntaxKind.LessThanToken) {
|
||||
// See if this is the start of a generic invocation. If so, consume it and
|
||||
// keep checking for postfix expressions. Otherwise, it's just a '<' that's
|
||||
@@ -3683,7 +3748,7 @@ module ts {
|
||||
}
|
||||
|
||||
function parseArgumentExpression(): Expression {
|
||||
return allowInAnd(parseArgumentOrArrayLiteralElement);
|
||||
return doOutsideOfContext(disallowInAndDecoratorContext, parseArgumentOrArrayLiteralElement);
|
||||
}
|
||||
|
||||
function parseArrayLiteralExpression(): ArrayLiteralExpression {
|
||||
@@ -3695,12 +3760,12 @@ module ts {
|
||||
return finishNode(node);
|
||||
}
|
||||
|
||||
function tryParseAccessorDeclaration(fullStart: number, modifiers: ModifiersArray): AccessorDeclaration {
|
||||
function tryParseAccessorDeclaration(fullStart: number, decorators: NodeArray<Decorator>, modifiers: ModifiersArray): AccessorDeclaration {
|
||||
if (parseContextualModifier(SyntaxKind.GetKeyword)) {
|
||||
return parseAccessorDeclaration(SyntaxKind.GetAccessor, fullStart, modifiers);
|
||||
return parseAccessorDeclaration(SyntaxKind.GetAccessor, fullStart, decorators, modifiers);
|
||||
}
|
||||
else if (parseContextualModifier(SyntaxKind.SetKeyword)) {
|
||||
return parseAccessorDeclaration(SyntaxKind.SetAccessor, fullStart, modifiers);
|
||||
return parseAccessorDeclaration(SyntaxKind.SetAccessor, fullStart, decorators, modifiers);
|
||||
}
|
||||
|
||||
return undefined;
|
||||
@@ -3708,9 +3773,10 @@ module ts {
|
||||
|
||||
function parseObjectLiteralElement(): ObjectLiteralElement {
|
||||
let fullStart = scanner.getStartPos();
|
||||
let decorators = parseDecorators();
|
||||
let modifiers = parseModifiers();
|
||||
|
||||
let accessor = tryParseAccessorDeclaration(fullStart, modifiers);
|
||||
let accessor = tryParseAccessorDeclaration(fullStart, decorators, modifiers);
|
||||
if (accessor) {
|
||||
return accessor;
|
||||
}
|
||||
@@ -3723,7 +3789,7 @@ module ts {
|
||||
// Disallowing of optional property assignments happens in the grammar checker.
|
||||
let questionToken = parseOptionalToken(SyntaxKind.QuestionToken);
|
||||
if (asteriskToken || token === SyntaxKind.OpenParenToken || token === SyntaxKind.LessThanToken) {
|
||||
return parseMethodDeclaration(fullStart, modifiers, asteriskToken, propertyName, questionToken);
|
||||
return parseMethodDeclaration(fullStart, decorators, modifiers, asteriskToken, propertyName, questionToken);
|
||||
}
|
||||
|
||||
// Parse to check if it is short-hand property assignment or normal property assignment
|
||||
@@ -3760,12 +3826,19 @@ module ts {
|
||||
// function * BindingIdentifier[Yield]opt (FormalParameters[Yield, GeneratorParameter]) { GeneratorBody[Yield] }
|
||||
// FunctionExpression:
|
||||
// function BindingIdentifieropt(FormalParameters) { FunctionBody }
|
||||
let saveDecoratorContext = inDecoratorContext();
|
||||
if (saveDecoratorContext) {
|
||||
setDecoratorContext(false);
|
||||
}
|
||||
let node = <FunctionExpression>createNode(SyntaxKind.FunctionExpression);
|
||||
parseExpected(SyntaxKind.FunctionKeyword);
|
||||
node.asteriskToken = parseOptionalToken(SyntaxKind.AsteriskToken);
|
||||
node.name = node.asteriskToken ? doInYieldContext(parseOptionalIdentifier) : parseOptionalIdentifier();
|
||||
fillSignature(SyntaxKind.ColonToken, /*yieldAndGeneratorParameterContext:*/ !!node.asteriskToken, /*requireCompleteParameterList:*/ false, node);
|
||||
node.body = parseFunctionBlock(/*allowYield:*/ !!node.asteriskToken, /* ignoreMissingOpenBrace */ false);
|
||||
if (saveDecoratorContext) {
|
||||
setDecoratorContext(true);
|
||||
}
|
||||
return finishNode(node);
|
||||
}
|
||||
|
||||
@@ -3802,8 +3875,19 @@ module ts {
|
||||
let savedYieldContext = inYieldContext();
|
||||
setYieldContext(allowYield);
|
||||
|
||||
// We may be in a [Decorator] context when parsing a function expression or
|
||||
// arrow function. The body of the function is not in [Decorator] context.
|
||||
let saveDecoratorContext = inDecoratorContext();
|
||||
if (saveDecoratorContext) {
|
||||
setDecoratorContext(false);
|
||||
}
|
||||
|
||||
let block = parseBlock(ignoreMissingOpenBrace, /*checkForStrictMode*/ true, diagnosticMessage);
|
||||
|
||||
if (saveDecoratorContext) {
|
||||
setDecoratorContext(true);
|
||||
}
|
||||
|
||||
setYieldContext(savedYieldContext);
|
||||
|
||||
return block;
|
||||
@@ -4051,7 +4135,7 @@ module ts {
|
||||
// as the parser will produce the same FunctionDeclaraiton or VariableStatement if it has
|
||||
// the same text regardless of whether it is inside a block or not.
|
||||
if (isModifier(token)) {
|
||||
let result = lookAhead(parseVariableStatementOrFunctionDeclarationWithModifiers);
|
||||
let result = lookAhead(parseVariableStatementOrFunctionDeclarationWithDecoratorsOrModifiers);
|
||||
if (result) {
|
||||
return true;
|
||||
}
|
||||
@@ -4135,9 +4219,9 @@ module ts {
|
||||
case SyntaxKind.VarKeyword:
|
||||
case SyntaxKind.ConstKeyword:
|
||||
// const here should always be parsed as const declaration because of check in 'isStatement'
|
||||
return parseVariableStatement(scanner.getStartPos(), /*modifiers:*/ undefined);
|
||||
return parseVariableStatement(scanner.getStartPos(), /*decorators*/ undefined, /*modifiers:*/ undefined);
|
||||
case SyntaxKind.FunctionKeyword:
|
||||
return parseFunctionDeclaration(scanner.getStartPos(), /*modifiers:*/ undefined);
|
||||
return parseFunctionDeclaration(scanner.getStartPos(), /*decorators*/ undefined, /*modifiers:*/ undefined);
|
||||
case SyntaxKind.SemicolonToken:
|
||||
return parseEmptyStatement();
|
||||
case SyntaxKind.IfKeyword:
|
||||
@@ -4170,7 +4254,7 @@ module ts {
|
||||
case SyntaxKind.LetKeyword:
|
||||
// If let follows identifier on the same line, it is declaration parse it as variable statement
|
||||
if (isLetDeclaration()) {
|
||||
return parseVariableStatement(scanner.getStartPos(), /*modifiers:*/ undefined);
|
||||
return parseVariableStatement(scanner.getStartPos(), /*decorators*/ undefined, /*modifiers:*/ undefined);
|
||||
}
|
||||
// Else parse it like identifier - fall through
|
||||
default:
|
||||
@@ -4180,8 +4264,10 @@ module ts {
|
||||
// work properly when incrementally parsing as the parser will produce the
|
||||
// same FunctionDeclaraiton or VariableStatement if it has the same text
|
||||
// regardless of whether it is inside a block or not.
|
||||
if (isModifier(token)) {
|
||||
let result = tryParse(parseVariableStatementOrFunctionDeclarationWithModifiers);
|
||||
// Even though variable statements and function declarations cannot have decorators,
|
||||
// we parse them here to provide better error recovery.
|
||||
if (isModifier(token) || token === SyntaxKind.AtToken) {
|
||||
let result = tryParse(parseVariableStatementOrFunctionDeclarationWithDecoratorsOrModifiers);
|
||||
if (result) {
|
||||
return result;
|
||||
}
|
||||
@@ -4191,8 +4277,9 @@ module ts {
|
||||
}
|
||||
}
|
||||
|
||||
function parseVariableStatementOrFunctionDeclarationWithModifiers(): FunctionDeclaration | VariableStatement {
|
||||
function parseVariableStatementOrFunctionDeclarationWithDecoratorsOrModifiers(): FunctionDeclaration | VariableStatement {
|
||||
let start = scanner.getStartPos();
|
||||
let decorators = parseDecorators();
|
||||
let modifiers = parseModifiers();
|
||||
switch (token) {
|
||||
case SyntaxKind.ConstKeyword:
|
||||
@@ -4200,18 +4287,18 @@ module ts {
|
||||
if (nextTokenIsEnum) {
|
||||
return undefined;
|
||||
}
|
||||
return parseVariableStatement(start, modifiers);
|
||||
return parseVariableStatement(start, decorators, modifiers);
|
||||
|
||||
case SyntaxKind.LetKeyword:
|
||||
if (!isLetDeclaration()) {
|
||||
return undefined;
|
||||
}
|
||||
return parseVariableStatement(start, modifiers);
|
||||
return parseVariableStatement(start, decorators, modifiers);
|
||||
|
||||
case SyntaxKind.VarKeyword:
|
||||
return parseVariableStatement(start, modifiers);
|
||||
return parseVariableStatement(start, decorators, modifiers);
|
||||
case SyntaxKind.FunctionKeyword:
|
||||
return parseFunctionDeclaration(start, modifiers);
|
||||
return parseFunctionDeclaration(start, decorators, modifiers);
|
||||
}
|
||||
|
||||
return undefined;
|
||||
@@ -4341,16 +4428,18 @@ module ts {
|
||||
return nextTokenIsIdentifier() && nextToken() === SyntaxKind.CloseParenToken;
|
||||
}
|
||||
|
||||
function parseVariableStatement(fullStart: number, modifiers: ModifiersArray): VariableStatement {
|
||||
function parseVariableStatement(fullStart: number, decorators: NodeArray<Decorator>, modifiers: ModifiersArray): VariableStatement {
|
||||
let node = <VariableStatement>createNode(SyntaxKind.VariableStatement, fullStart);
|
||||
node.decorators = decorators;
|
||||
setModifiers(node, modifiers);
|
||||
node.declarationList = parseVariableDeclarationList(/*inForStatementInitializer:*/ false);
|
||||
parseSemicolon();
|
||||
return finishNode(node);
|
||||
}
|
||||
|
||||
function parseFunctionDeclaration(fullStart: number, modifiers: ModifiersArray): FunctionDeclaration {
|
||||
function parseFunctionDeclaration(fullStart: number, decorators: NodeArray<Decorator>, modifiers: ModifiersArray): FunctionDeclaration {
|
||||
let node = <FunctionDeclaration>createNode(SyntaxKind.FunctionDeclaration, fullStart);
|
||||
node.decorators = decorators;
|
||||
setModifiers(node, modifiers);
|
||||
parseExpected(SyntaxKind.FunctionKeyword);
|
||||
node.asteriskToken = parseOptionalToken(SyntaxKind.AsteriskToken);
|
||||
@@ -4360,8 +4449,9 @@ module ts {
|
||||
return finishNode(node);
|
||||
}
|
||||
|
||||
function parseConstructorDeclaration(pos: number, modifiers: ModifiersArray): ConstructorDeclaration {
|
||||
function parseConstructorDeclaration(pos: number, decorators: NodeArray<Decorator>, modifiers: ModifiersArray): ConstructorDeclaration {
|
||||
let node = <ConstructorDeclaration>createNode(SyntaxKind.Constructor, pos);
|
||||
node.decorators = decorators;
|
||||
setModifiers(node, modifiers);
|
||||
parseExpected(SyntaxKind.ConstructorKeyword);
|
||||
fillSignature(SyntaxKind.ColonToken, /*yieldAndGeneratorParameterContext:*/ false, /*requireCompleteParameterList:*/ false, node);
|
||||
@@ -4369,8 +4459,9 @@ module ts {
|
||||
return finishNode(node);
|
||||
}
|
||||
|
||||
function parseMethodDeclaration(fullStart: number, modifiers: ModifiersArray, asteriskToken: Node, name: DeclarationName, questionToken: Node, diagnosticMessage?: DiagnosticMessage): MethodDeclaration {
|
||||
function parseMethodDeclaration(fullStart: number, decorators: NodeArray<Decorator>, modifiers: ModifiersArray, asteriskToken: Node, name: DeclarationName, questionToken: Node, diagnosticMessage?: DiagnosticMessage): MethodDeclaration {
|
||||
let method = <MethodDeclaration>createNode(SyntaxKind.MethodDeclaration, fullStart);
|
||||
method.decorators = decorators;
|
||||
setModifiers(method, modifiers);
|
||||
method.asteriskToken = asteriskToken;
|
||||
method.name = name;
|
||||
@@ -4380,25 +4471,30 @@ module ts {
|
||||
return finishNode(method);
|
||||
}
|
||||
|
||||
function parsePropertyOrMethodDeclaration(fullStart: number, modifiers: ModifiersArray): ClassElement {
|
||||
function parsePropertyDeclaration(fullStart: number, decorators: NodeArray<Decorator>, modifiers: ModifiersArray, name: DeclarationName, questionToken: Node): ClassElement {
|
||||
let property = <PropertyDeclaration>createNode(SyntaxKind.PropertyDeclaration, fullStart);
|
||||
property.decorators = decorators;
|
||||
setModifiers(property, modifiers);
|
||||
property.name = name;
|
||||
property.questionToken = questionToken;
|
||||
property.type = parseTypeAnnotation();
|
||||
property.initializer = allowInAnd(parseNonParameterInitializer);
|
||||
parseSemicolon();
|
||||
return finishNode(property);
|
||||
}
|
||||
|
||||
function parsePropertyOrMethodDeclaration(fullStart: number, decorators: NodeArray<Decorator>, modifiers: ModifiersArray): ClassElement {
|
||||
let asteriskToken = parseOptionalToken(SyntaxKind.AsteriskToken);
|
||||
let name = parsePropertyName();
|
||||
|
||||
|
||||
// Note: this is not legal as per the grammar. But we allow it in the parser and
|
||||
// report an error in the grammar checker.
|
||||
let questionToken = parseOptionalToken(SyntaxKind.QuestionToken);
|
||||
if (asteriskToken || token === SyntaxKind.OpenParenToken || token === SyntaxKind.LessThanToken) {
|
||||
return parseMethodDeclaration(fullStart, modifiers, asteriskToken, name, questionToken, Diagnostics.or_expected);
|
||||
return parseMethodDeclaration(fullStart, decorators, modifiers, asteriskToken, name, questionToken, Diagnostics.or_expected);
|
||||
}
|
||||
else {
|
||||
let property = <PropertyDeclaration>createNode(SyntaxKind.PropertyDeclaration, fullStart);
|
||||
setModifiers(property, modifiers);
|
||||
property.name = name;
|
||||
property.questionToken = questionToken;
|
||||
property.type = parseTypeAnnotation();
|
||||
property.initializer = allowInAnd(parseNonParameterInitializer);
|
||||
parseSemicolon();
|
||||
return finishNode(property);
|
||||
return parsePropertyDeclaration(fullStart, decorators, modifiers, name, questionToken);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4406,8 +4502,9 @@ module ts {
|
||||
return parseInitializer(/*inParameter*/ false);
|
||||
}
|
||||
|
||||
function parseAccessorDeclaration(kind: SyntaxKind, fullStart: number, modifiers: ModifiersArray): AccessorDeclaration {
|
||||
function parseAccessorDeclaration(kind: SyntaxKind, fullStart: number, decorators: NodeArray<Decorator>, modifiers: ModifiersArray): AccessorDeclaration {
|
||||
let node = <AccessorDeclaration>createNode(kind, fullStart);
|
||||
node.decorators = decorators;
|
||||
setModifiers(node, modifiers);
|
||||
node.name = parsePropertyName();
|
||||
fillSignature(SyntaxKind.ColonToken, /*yieldAndGeneratorParameterContext:*/ false, /*requireCompleteParameterList:*/ false, node);
|
||||
@@ -4418,6 +4515,10 @@ module ts {
|
||||
function isClassMemberStart(): boolean {
|
||||
let idToken: SyntaxKind;
|
||||
|
||||
if (token === SyntaxKind.AtToken) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Eat up all modifiers, but hold on to the last one in case it is actually an identifier.
|
||||
while (isModifier(token)) {
|
||||
idToken = token;
|
||||
@@ -4469,6 +4570,29 @@ module ts {
|
||||
return false;
|
||||
}
|
||||
|
||||
function parseDecorators(): NodeArray<Decorator> {
|
||||
let decorators: NodeArray<Decorator>;
|
||||
while (true) {
|
||||
let decoratorStart = getNodePos();
|
||||
if (!parseOptional(SyntaxKind.AtToken)) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (!decorators) {
|
||||
decorators = <NodeArray<Decorator>>[];
|
||||
decorators.pos = scanner.getStartPos();
|
||||
}
|
||||
|
||||
let decorator = <Decorator>createNode(SyntaxKind.Decorator, decoratorStart);
|
||||
decorator.expression = doInDecoratorContext(parseLeftHandSideExpressionOrHigher);
|
||||
decorators.push(finishNode(decorator));
|
||||
}
|
||||
if (decorators) {
|
||||
decorators.end = getNodeEnd();
|
||||
}
|
||||
return decorators;
|
||||
}
|
||||
|
||||
function parseModifiers(): ModifiersArray {
|
||||
let flags = 0;
|
||||
let modifiers: ModifiersArray;
|
||||
@@ -4496,19 +4620,20 @@ module ts {
|
||||
|
||||
function parseClassElement(): ClassElement {
|
||||
let fullStart = getNodePos();
|
||||
let decorators = parseDecorators();
|
||||
let modifiers = parseModifiers();
|
||||
|
||||
let accessor = tryParseAccessorDeclaration(fullStart, modifiers);
|
||||
let accessor = tryParseAccessorDeclaration(fullStart, decorators, modifiers);
|
||||
if (accessor) {
|
||||
return accessor;
|
||||
}
|
||||
|
||||
if (token === SyntaxKind.ConstructorKeyword) {
|
||||
return parseConstructorDeclaration(fullStart, modifiers);
|
||||
return parseConstructorDeclaration(fullStart, decorators, modifiers);
|
||||
}
|
||||
|
||||
if (isIndexSignature()) {
|
||||
return parseIndexSignatureDeclaration(modifiers);
|
||||
return parseIndexSignatureDeclaration(fullStart, decorators, modifiers);
|
||||
}
|
||||
|
||||
// It is very important that we check this *after* checking indexers because
|
||||
@@ -4519,14 +4644,20 @@ module ts {
|
||||
token === SyntaxKind.AsteriskToken ||
|
||||
token === SyntaxKind.OpenBracketToken) {
|
||||
|
||||
return parsePropertyOrMethodDeclaration(fullStart, modifiers);
|
||||
return parsePropertyOrMethodDeclaration(fullStart, decorators, modifiers);
|
||||
}
|
||||
|
||||
if (decorators) {
|
||||
// treat this as a property declaration with a missing name.
|
||||
let name = <Identifier>createMissingNode(SyntaxKind.Identifier, /*reportAtCurrentPosition*/ true, Diagnostics.Declaration_expected);
|
||||
return parsePropertyDeclaration(fullStart, decorators, modifiers, name, /*questionToken*/ undefined);
|
||||
}
|
||||
|
||||
// 'isClassMemberStart' should have hinted not to attempt parsing.
|
||||
Debug.fail("Should not have attempted to parse class member declaration.");
|
||||
}
|
||||
|
||||
function parseClassDeclaration(fullStart: number, modifiers: ModifiersArray): ClassDeclaration {
|
||||
function parseClassDeclaration(fullStart: number, decorators: NodeArray<Decorator>, modifiers: ModifiersArray): ClassDeclaration {
|
||||
// In ES6 specification, All parts of a ClassDeclaration or a ClassExpression are strict mode code
|
||||
let savedStrictModeContext = inStrictModeContext();
|
||||
if (languageVersion >= ScriptTarget.ES6) {
|
||||
@@ -4534,6 +4665,7 @@ module ts {
|
||||
}
|
||||
|
||||
var node = <ClassDeclaration>createNode(SyntaxKind.ClassDeclaration, fullStart);
|
||||
node.decorators = decorators;
|
||||
setModifiers(node, modifiers);
|
||||
parseExpected(SyntaxKind.ClassKeyword);
|
||||
node.name = node.flags & NodeFlags.Default ? parseOptionalIdentifier() : parseIdentifier();
|
||||
@@ -4597,8 +4729,9 @@ module ts {
|
||||
return parseList(ParsingContext.ClassMembers, /*checkForStrictMode*/ false, parseClassElement);
|
||||
}
|
||||
|
||||
function parseInterfaceDeclaration(fullStart: number, modifiers: ModifiersArray): InterfaceDeclaration {
|
||||
function parseInterfaceDeclaration(fullStart: number, decorators: NodeArray<Decorator>, modifiers: ModifiersArray): InterfaceDeclaration {
|
||||
let node = <InterfaceDeclaration>createNode(SyntaxKind.InterfaceDeclaration, fullStart);
|
||||
node.decorators = decorators;
|
||||
setModifiers(node, modifiers);
|
||||
parseExpected(SyntaxKind.InterfaceKeyword);
|
||||
node.name = parseIdentifier();
|
||||
@@ -4608,8 +4741,9 @@ module ts {
|
||||
return finishNode(node);
|
||||
}
|
||||
|
||||
function parseTypeAliasDeclaration(fullStart: number, modifiers: ModifiersArray): TypeAliasDeclaration {
|
||||
function parseTypeAliasDeclaration(fullStart: number, decorators: NodeArray<Decorator>, modifiers: ModifiersArray): TypeAliasDeclaration {
|
||||
let node = <TypeAliasDeclaration>createNode(SyntaxKind.TypeAliasDeclaration, fullStart);
|
||||
node.decorators = decorators;
|
||||
setModifiers(node, modifiers);
|
||||
parseExpected(SyntaxKind.TypeKeyword);
|
||||
node.name = parseIdentifier();
|
||||
@@ -4630,8 +4764,9 @@ module ts {
|
||||
return finishNode(node);
|
||||
}
|
||||
|
||||
function parseEnumDeclaration(fullStart: number, modifiers: ModifiersArray): EnumDeclaration {
|
||||
function parseEnumDeclaration(fullStart: number, decorators: NodeArray<Decorator>, modifiers: ModifiersArray): EnumDeclaration {
|
||||
let node = <EnumDeclaration>createNode(SyntaxKind.EnumDeclaration, fullStart);
|
||||
node.decorators = decorators;
|
||||
setModifiers(node, modifiers);
|
||||
parseExpected(SyntaxKind.EnumKeyword);
|
||||
node.name = parseIdentifier();
|
||||
@@ -4657,30 +4792,32 @@ module ts {
|
||||
return finishNode(node);
|
||||
}
|
||||
|
||||
function parseInternalModuleTail(fullStart: number, modifiers: ModifiersArray, flags: NodeFlags): ModuleDeclaration {
|
||||
function parseInternalModuleTail(fullStart: number, decorators: NodeArray<Decorator>, modifiers: ModifiersArray, flags: NodeFlags): ModuleDeclaration {
|
||||
let node = <ModuleDeclaration>createNode(SyntaxKind.ModuleDeclaration, fullStart);
|
||||
node.decorators = decorators;
|
||||
setModifiers(node, modifiers);
|
||||
node.flags |= flags;
|
||||
node.name = parseIdentifier();
|
||||
node.body = parseOptional(SyntaxKind.DotToken)
|
||||
? parseInternalModuleTail(getNodePos(), /*modifiers:*/undefined, NodeFlags.Export)
|
||||
? parseInternalModuleTail(getNodePos(), /*decorators*/ undefined, /*modifiers:*/undefined, NodeFlags.Export)
|
||||
: parseModuleBlock();
|
||||
return finishNode(node);
|
||||
}
|
||||
|
||||
function parseAmbientExternalModuleDeclaration(fullStart: number, modifiers: ModifiersArray): ModuleDeclaration {
|
||||
function parseAmbientExternalModuleDeclaration(fullStart: number, decorators: NodeArray<Decorator>, modifiers: ModifiersArray): ModuleDeclaration {
|
||||
let node = <ModuleDeclaration>createNode(SyntaxKind.ModuleDeclaration, fullStart);
|
||||
node.decorators = decorators;
|
||||
setModifiers(node, modifiers);
|
||||
node.name = parseLiteralNode(/*internName:*/ true);
|
||||
node.body = parseModuleBlock();
|
||||
return finishNode(node);
|
||||
}
|
||||
|
||||
function parseModuleDeclaration(fullStart: number, modifiers: ModifiersArray): ModuleDeclaration {
|
||||
function parseModuleDeclaration(fullStart: number, decorators: NodeArray<Decorator>, modifiers: ModifiersArray): ModuleDeclaration {
|
||||
parseExpected(SyntaxKind.ModuleKeyword);
|
||||
return token === SyntaxKind.StringLiteral
|
||||
? parseAmbientExternalModuleDeclaration(fullStart, modifiers)
|
||||
: parseInternalModuleTail(fullStart, modifiers, modifiers ? modifiers.flags : 0);
|
||||
? parseAmbientExternalModuleDeclaration(fullStart, decorators, modifiers)
|
||||
: parseInternalModuleTail(fullStart, decorators, modifiers, modifiers ? modifiers.flags : 0);
|
||||
}
|
||||
|
||||
function isExternalModuleReference() {
|
||||
@@ -4698,7 +4835,7 @@ module ts {
|
||||
token === SyntaxKind.FromKeyword;
|
||||
}
|
||||
|
||||
function parseImportDeclarationOrImportEqualsDeclaration(fullStart: number, modifiers: ModifiersArray): ImportEqualsDeclaration | ImportDeclaration {
|
||||
function parseImportDeclarationOrImportEqualsDeclaration(fullStart: number, decorators: NodeArray<Decorator>, modifiers: ModifiersArray): ImportEqualsDeclaration | ImportDeclaration {
|
||||
parseExpected(SyntaxKind.ImportKeyword);
|
||||
let afterImportPos = scanner.getStartPos();
|
||||
|
||||
@@ -4710,6 +4847,7 @@ module ts {
|
||||
// import x = require("mod"); or
|
||||
// import x = M.x;
|
||||
let importEqualsDeclaration = <ImportEqualsDeclaration>createNode(SyntaxKind.ImportEqualsDeclaration, fullStart);
|
||||
importEqualsDeclaration.decorators = decorators;
|
||||
setModifiers(importEqualsDeclaration, modifiers);
|
||||
importEqualsDeclaration.name = identifier;
|
||||
parseExpected(SyntaxKind.EqualsToken);
|
||||
@@ -4721,6 +4859,7 @@ module ts {
|
||||
|
||||
// Import statement
|
||||
let importDeclaration = <ImportDeclaration>createNode(SyntaxKind.ImportDeclaration, fullStart);
|
||||
importDeclaration.decorators = decorators;
|
||||
setModifiers(importDeclaration, modifiers);
|
||||
|
||||
// ImportDeclaration:
|
||||
@@ -4829,34 +4968,36 @@ module ts {
|
||||
function parseImportOrExportSpecifier(kind: SyntaxKind): ImportOrExportSpecifier {
|
||||
let node = <ImportSpecifier>createNode(kind);
|
||||
// ImportSpecifier:
|
||||
// ImportedBinding
|
||||
// IdentifierName as ImportedBinding
|
||||
let isFirstIdentifierNameNotAnIdentifier = isKeyword(token) && !isIdentifier();
|
||||
let start = scanner.getTokenPos();
|
||||
// BindingIdentifier
|
||||
// IdentifierName as BindingIdentifier
|
||||
// ExportSpecififer:
|
||||
// IdentifierName
|
||||
// IdentifierName as IdentifierName
|
||||
let checkIdentifierIsKeyword = isKeyword(token) && !isIdentifier();
|
||||
let checkIdentifierStart = scanner.getTokenPos();
|
||||
let checkIdentifierEnd = scanner.getTextPos();
|
||||
let identifierName = parseIdentifierName();
|
||||
if (token === SyntaxKind.AsKeyword) {
|
||||
node.propertyName = identifierName;
|
||||
parseExpected(SyntaxKind.AsKeyword);
|
||||
if (isIdentifier()) {
|
||||
node.name = parseIdentifierName();
|
||||
}
|
||||
else {
|
||||
parseErrorAtCurrentToken(Diagnostics.Identifier_expected);
|
||||
}
|
||||
checkIdentifierIsKeyword = isKeyword(token) && !isIdentifier();
|
||||
checkIdentifierStart = scanner.getTokenPos();
|
||||
checkIdentifierEnd = scanner.getTextPos();
|
||||
node.name = parseIdentifierName();
|
||||
}
|
||||
else {
|
||||
node.name = identifierName;
|
||||
if (isFirstIdentifierNameNotAnIdentifier) {
|
||||
// Report error identifier expected
|
||||
parseErrorAtPosition(start, identifierName.end - start, Diagnostics.Identifier_expected);
|
||||
}
|
||||
}
|
||||
|
||||
if (kind === SyntaxKind.ImportSpecifier && checkIdentifierIsKeyword) {
|
||||
// Report error identifier expected
|
||||
parseErrorAtPosition(checkIdentifierStart, checkIdentifierEnd - checkIdentifierStart, Diagnostics.Identifier_expected);
|
||||
}
|
||||
return finishNode(node);
|
||||
}
|
||||
|
||||
function parseExportDeclaration(fullStart: number, modifiers: ModifiersArray): ExportDeclaration {
|
||||
function parseExportDeclaration(fullStart: number, decorators: NodeArray<Decorator>, modifiers: ModifiersArray): ExportDeclaration {
|
||||
let node = <ExportDeclaration>createNode(SyntaxKind.ExportDeclaration, fullStart);
|
||||
node.decorators = decorators;
|
||||
setModifiers(node, modifiers);
|
||||
if (parseOptional(SyntaxKind.AsteriskToken)) {
|
||||
parseExpected(SyntaxKind.FromKeyword);
|
||||
@@ -4872,8 +5013,9 @@ module ts {
|
||||
return finishNode(node);
|
||||
}
|
||||
|
||||
function parseExportAssignment(fullStart: number, modifiers: ModifiersArray): ExportAssignment {
|
||||
function parseExportAssignment(fullStart: number, decorators: NodeArray<Decorator>, modifiers: ModifiersArray): ExportAssignment {
|
||||
let node = <ExportAssignment>createNode(SyntaxKind.ExportAssignment, fullStart);
|
||||
node.decorators = decorators;
|
||||
setModifiers(node, modifiers);
|
||||
if (parseOptional(SyntaxKind.EqualsToken)) {
|
||||
node.isExportEquals = true;
|
||||
@@ -4898,7 +5040,7 @@ module ts {
|
||||
return inStrictModeContext() || lookAhead(nextTokenIsIdentifierOrStartOfDestructuringOnTheSameLine);
|
||||
}
|
||||
|
||||
function isDeclarationStart(): boolean {
|
||||
function isDeclarationStart(followsModifier?: boolean): boolean {
|
||||
switch (token) {
|
||||
case SyntaxKind.VarKeyword:
|
||||
case SyntaxKind.ConstKeyword:
|
||||
@@ -4928,6 +5070,10 @@ module ts {
|
||||
case SyntaxKind.StaticKeyword:
|
||||
// Check for modifier on source element
|
||||
return lookAhead(nextTokenIsDeclarationStart);
|
||||
case SyntaxKind.AtToken:
|
||||
// a lookahead here is too costly, and decorators are only valid on a declaration.
|
||||
// We will assume we are parsing a declaration here and report an error later
|
||||
return !followsModifier;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4954,12 +5100,12 @@ module ts {
|
||||
function nextTokenCanFollowExportKeyword() {
|
||||
nextToken();
|
||||
return token === SyntaxKind.EqualsToken || token === SyntaxKind.AsteriskToken ||
|
||||
token === SyntaxKind.OpenBraceToken || token === SyntaxKind.DefaultKeyword || isDeclarationStart();
|
||||
token === SyntaxKind.OpenBraceToken || token === SyntaxKind.DefaultKeyword || isDeclarationStart(/*followsModifier*/ true);
|
||||
}
|
||||
|
||||
function nextTokenIsDeclarationStart() {
|
||||
nextToken();
|
||||
return isDeclarationStart();
|
||||
return isDeclarationStart(/*followsModifier*/ true);
|
||||
}
|
||||
|
||||
function nextTokenIsAsKeyword() {
|
||||
@@ -4968,14 +5114,15 @@ module ts {
|
||||
|
||||
function parseDeclaration(): ModuleElement {
|
||||
let fullStart = getNodePos();
|
||||
let decorators = parseDecorators();
|
||||
let modifiers = parseModifiers();
|
||||
if (token === SyntaxKind.ExportKeyword) {
|
||||
nextToken();
|
||||
if (token === SyntaxKind.DefaultKeyword || token === SyntaxKind.EqualsToken) {
|
||||
return parseExportAssignment(fullStart, modifiers);
|
||||
return parseExportAssignment(fullStart, decorators, modifiers);
|
||||
}
|
||||
if (token === SyntaxKind.AsteriskToken || token === SyntaxKind.OpenBraceToken) {
|
||||
return parseExportDeclaration(fullStart, modifiers);
|
||||
return parseExportDeclaration(fullStart, decorators, modifiers);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4983,22 +5130,31 @@ module ts {
|
||||
case SyntaxKind.VarKeyword:
|
||||
case SyntaxKind.LetKeyword:
|
||||
case SyntaxKind.ConstKeyword:
|
||||
return parseVariableStatement(fullStart, modifiers);
|
||||
return parseVariableStatement(fullStart, decorators, modifiers);
|
||||
case SyntaxKind.FunctionKeyword:
|
||||
return parseFunctionDeclaration(fullStart, modifiers);
|
||||
return parseFunctionDeclaration(fullStart, decorators, modifiers);
|
||||
case SyntaxKind.ClassKeyword:
|
||||
return parseClassDeclaration(fullStart, modifiers);
|
||||
return parseClassDeclaration(fullStart, decorators, modifiers);
|
||||
case SyntaxKind.InterfaceKeyword:
|
||||
return parseInterfaceDeclaration(fullStart, modifiers);
|
||||
return parseInterfaceDeclaration(fullStart, decorators, modifiers);
|
||||
case SyntaxKind.TypeKeyword:
|
||||
return parseTypeAliasDeclaration(fullStart, modifiers);
|
||||
return parseTypeAliasDeclaration(fullStart, decorators, modifiers);
|
||||
case SyntaxKind.EnumKeyword:
|
||||
return parseEnumDeclaration(fullStart, modifiers);
|
||||
return parseEnumDeclaration(fullStart, decorators, modifiers);
|
||||
case SyntaxKind.ModuleKeyword:
|
||||
return parseModuleDeclaration(fullStart, modifiers);
|
||||
return parseModuleDeclaration(fullStart, decorators, modifiers);
|
||||
case SyntaxKind.ImportKeyword:
|
||||
return parseImportDeclarationOrImportEqualsDeclaration(fullStart, modifiers);
|
||||
return parseImportDeclarationOrImportEqualsDeclaration(fullStart, decorators, modifiers);
|
||||
default:
|
||||
if (decorators) {
|
||||
// We reached this point because we encountered an AtToken and assumed a declaration would
|
||||
// follow. For recovery and error reporting purposes, return an incomplete declaration.
|
||||
let node = <ModuleElement>createMissingNode(SyntaxKind.MissingDeclaration, /*reportAtCurrentPosition*/ true, Diagnostics.Declaration_expected);
|
||||
node.pos = fullStart;
|
||||
node.decorators = decorators;
|
||||
setModifiers(node, modifiers);
|
||||
return finishNode(node);
|
||||
}
|
||||
Debug.fail("Mismatch between isDeclarationStart and parseDeclaration");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -148,6 +148,7 @@ module ts {
|
||||
"&=": SyntaxKind.AmpersandEqualsToken,
|
||||
"|=": SyntaxKind.BarEqualsToken,
|
||||
"^=": SyntaxKind.CaretEqualsToken,
|
||||
"@": SyntaxKind.AtToken,
|
||||
};
|
||||
|
||||
/*
|
||||
@@ -1284,6 +1285,8 @@ module ts {
|
||||
return pos++, token = SyntaxKind.CloseBraceToken;
|
||||
case CharacterCodes.tilde:
|
||||
return pos++, token = SyntaxKind.TildeToken;
|
||||
case CharacterCodes.at:
|
||||
return pos++, token = SyntaxKind.AtToken;
|
||||
case CharacterCodes.backslash:
|
||||
let cookedChar = peekUnicodeEscape();
|
||||
if (cookedChar >= 0 && isIdentifierStart(cookedChar)) {
|
||||
|
||||
@@ -67,6 +67,7 @@ module ts {
|
||||
BarBarToken,
|
||||
QuestionToken,
|
||||
ColonToken,
|
||||
AtToken,
|
||||
// Assignments
|
||||
EqualsToken,
|
||||
PlusEqualsToken,
|
||||
@@ -154,6 +155,7 @@ module ts {
|
||||
// Signature elements
|
||||
TypeParameter,
|
||||
Parameter,
|
||||
Decorator,
|
||||
// TypeMember
|
||||
PropertySignature,
|
||||
PropertyDeclaration,
|
||||
@@ -244,6 +246,7 @@ module ts {
|
||||
ExportDeclaration,
|
||||
NamedExports,
|
||||
ExportSpecifier,
|
||||
MissingDeclaration,
|
||||
|
||||
// Module references
|
||||
ExternalModuleReference,
|
||||
@@ -307,6 +310,7 @@ module ts {
|
||||
Let = 0x00001000, // Variable declaration
|
||||
Const = 0x00002000, // Variable declaration
|
||||
OctalLiteral = 0x00004000,
|
||||
ExportContext = 0x00008000, // Export context (initialized by binding)
|
||||
|
||||
Modifier = Export | Ambient | Public | Private | Protected | Static | Default,
|
||||
AccessibilityModifier = Public | Private | Protected,
|
||||
@@ -327,22 +331,25 @@ module ts {
|
||||
// If this node was parsed in the parameters of a generator.
|
||||
GeneratorParameter = 1 << 3,
|
||||
|
||||
// If this node was parsed as part of a decorator
|
||||
Decorator = 1 << 4,
|
||||
|
||||
// If the parser encountered an error when parsing the code that created this node. Note
|
||||
// the parser only sets this directly on the node it creates right after encountering the
|
||||
// error.
|
||||
ThisNodeHasError = 1 << 4,
|
||||
ThisNodeHasError = 1 << 5,
|
||||
|
||||
// Context flags set directly by the parser.
|
||||
ParserGeneratedFlags = StrictMode | DisallowIn | Yield | GeneratorParameter | ThisNodeHasError,
|
||||
ParserGeneratedFlags = StrictMode | DisallowIn | Yield | GeneratorParameter | Decorator | ThisNodeHasError,
|
||||
|
||||
// Context flags computed by aggregating child flags upwards.
|
||||
|
||||
// Used during incremental parsing to determine if this node or any of its children had an
|
||||
// error. Computed only once and then cached.
|
||||
ThisNodeOrAnySubNodesHasError = 1 << 5,
|
||||
ThisNodeOrAnySubNodesHasError = 1 << 6,
|
||||
|
||||
// Used to know if we've computed data from children and cached it in this node.
|
||||
HasAggregatedChildData = 1 << 6
|
||||
HasAggregatedChildData = 1 << 7
|
||||
}
|
||||
|
||||
export const enum RelationComparisonResult {
|
||||
@@ -357,13 +364,14 @@ module ts {
|
||||
// Specific context the parser was in when this node was created. Normally undefined.
|
||||
// Only set when the parser was in some interesting context (like async/yield).
|
||||
parserContextFlags?: ParserContextFlags;
|
||||
modifiers?: ModifiersArray; // Array of modifiers
|
||||
id?: number; // Unique id (used to look up NodeLinks)
|
||||
parent?: Node; // Parent node (initialized by binding)
|
||||
symbol?: Symbol; // Symbol declared by node (initialized by binding)
|
||||
locals?: SymbolTable; // Locals associated with node (initialized by binding)
|
||||
nextContainer?: Node; // Next container in declaration order (initialized by binding)
|
||||
localSymbol?: Symbol; // Local symbol declared by node (initialized by binding only for exported nodes)
|
||||
decorators?: NodeArray<Decorator>; // Array of decorators (in document order)
|
||||
modifiers?: ModifiersArray; // Array of modifiers
|
||||
id?: number; // Unique id (used to look up NodeLinks)
|
||||
parent?: Node; // Parent node (initialized by binding)
|
||||
symbol?: Symbol; // Symbol declared by node (initialized by binding)
|
||||
locals?: SymbolTable; // Locals associated with node (initialized by binding)
|
||||
nextContainer?: Node; // Next container in declaration order (initialized by binding)
|
||||
localSymbol?: Symbol; // Local symbol declared by node (initialized by binding only for exported nodes)
|
||||
}
|
||||
|
||||
export interface NodeArray<T> extends Array<T>, TextRange {
|
||||
@@ -397,6 +405,10 @@ module ts {
|
||||
expression: Expression;
|
||||
}
|
||||
|
||||
export interface Decorator extends Node {
|
||||
expression: LeftHandSideExpression;
|
||||
}
|
||||
|
||||
export interface TypeParameterDeclaration extends Declaration {
|
||||
name: Identifier;
|
||||
constraint?: TypeNode;
|
||||
@@ -1209,8 +1221,8 @@ module ts {
|
||||
export interface EmitResolver {
|
||||
hasGlobalName(name: string): boolean;
|
||||
getExpressionNameSubstitution(node: Identifier, getGeneratedNameForNode: (node: Node) => string): string;
|
||||
hasExportDefaultValue(node: SourceFile): boolean;
|
||||
isReferencedAliasDeclaration(node: Node): boolean;
|
||||
isValueAliasDeclaration(node: Node): boolean;
|
||||
isReferencedAliasDeclaration(node: Node, checkChildren?: boolean): boolean;
|
||||
isTopLevelValueImportEqualsWithEntityName(node: ImportEqualsDeclaration): boolean;
|
||||
getNodeCheckFlags(node: Node): NodeCheckFlags;
|
||||
isDeclarationVisible(node: Declaration): boolean;
|
||||
@@ -1338,17 +1350,18 @@ module ts {
|
||||
}
|
||||
|
||||
export const enum NodeCheckFlags {
|
||||
TypeChecked = 0x00000001, // Node has been type checked
|
||||
LexicalThis = 0x00000002, // Lexical 'this' reference
|
||||
CaptureThis = 0x00000004, // Lexical 'this' used in body
|
||||
EmitExtends = 0x00000008, // Emit __extends
|
||||
SuperInstance = 0x00000010, // Instance 'super' reference
|
||||
SuperStatic = 0x00000020, // Static 'super' reference
|
||||
ContextChecked = 0x00000040, // Contextual types have been assigned
|
||||
TypeChecked = 0x00000001, // Node has been type checked
|
||||
LexicalThis = 0x00000002, // Lexical 'this' reference
|
||||
CaptureThis = 0x00000004, // Lexical 'this' used in body
|
||||
EmitExtends = 0x00000008, // Emit __extends
|
||||
SuperInstance = 0x00000010, // Instance 'super' reference
|
||||
SuperStatic = 0x00000020, // Static 'super' reference
|
||||
ContextChecked = 0x00000040, // Contextual types have been assigned
|
||||
|
||||
// Values for enum members have been computed, and any errors have been reported for them.
|
||||
EnumValuesComputed = 0x00000080,
|
||||
BlockScopedBindingInLoop = 0x00000100,
|
||||
EnumValuesComputed = 0x00000080,
|
||||
BlockScopedBindingInLoop = 0x00000100,
|
||||
EmitDecorate = 0x00000200, // Emit __decorate
|
||||
}
|
||||
|
||||
export interface NodeLinks {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/// <reference path="types.ts" />
|
||||
/// <reference path="binder.ts" />
|
||||
|
||||
module ts {
|
||||
export interface ReferencePathMatchResult {
|
||||
@@ -575,6 +575,83 @@ module ts {
|
||||
return (<CallExpression>node).expression;
|
||||
}
|
||||
|
||||
export function nodeCanBeDecorated(node: Node): boolean {
|
||||
switch (node.kind) {
|
||||
case SyntaxKind.ClassDeclaration:
|
||||
// classes are valid targets
|
||||
return true;
|
||||
|
||||
case SyntaxKind.PropertyDeclaration:
|
||||
// property declarations are valid if their parent is a class declaration.
|
||||
return node.parent.kind === SyntaxKind.ClassDeclaration;
|
||||
|
||||
case SyntaxKind.Parameter:
|
||||
// if the parameter's parent has a body and its grandparent is a class declaration, this is a valid target;
|
||||
return (<FunctionLikeDeclaration>node.parent).body && node.parent.parent.kind === SyntaxKind.ClassDeclaration;
|
||||
|
||||
case SyntaxKind.GetAccessor:
|
||||
case SyntaxKind.SetAccessor:
|
||||
case SyntaxKind.MethodDeclaration:
|
||||
// if this method has a body and its parent is a class declaration, this is a valid target.
|
||||
return (<FunctionLikeDeclaration>node).body && node.parent.kind === SyntaxKind.ClassDeclaration;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
export function nodeIsDecorated(node: Node): boolean {
|
||||
switch (node.kind) {
|
||||
case SyntaxKind.ClassDeclaration:
|
||||
if (node.decorators) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
case SyntaxKind.PropertyDeclaration:
|
||||
case SyntaxKind.Parameter:
|
||||
if (node.decorators) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
case SyntaxKind.GetAccessor:
|
||||
if ((<FunctionLikeDeclaration>node).body && node.decorators) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
case SyntaxKind.MethodDeclaration:
|
||||
case SyntaxKind.SetAccessor:
|
||||
if ((<FunctionLikeDeclaration>node).body && node.decorators) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
export function childIsDecorated(node: Node): boolean {
|
||||
switch (node.kind) {
|
||||
case SyntaxKind.ClassDeclaration:
|
||||
return forEach((<ClassDeclaration>node).members, nodeOrChildIsDecorated);
|
||||
|
||||
case SyntaxKind.MethodDeclaration:
|
||||
case SyntaxKind.SetAccessor:
|
||||
return forEach((<FunctionLikeDeclaration>node).parameters, nodeIsDecorated);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
export function nodeOrChildIsDecorated(node: Node): boolean {
|
||||
return nodeIsDecorated(node) || childIsDecorated(node);
|
||||
}
|
||||
|
||||
export function isExpression(node: Node): boolean {
|
||||
switch (node.kind) {
|
||||
case SyntaxKind.ThisKeyword:
|
||||
@@ -814,6 +891,20 @@ module ts {
|
||||
}
|
||||
}
|
||||
|
||||
export function isClassElement(n: Node): boolean {
|
||||
switch (n.kind) {
|
||||
case SyntaxKind.Constructor:
|
||||
case SyntaxKind.PropertyDeclaration:
|
||||
case SyntaxKind.MethodDeclaration:
|
||||
case SyntaxKind.GetAccessor:
|
||||
case SyntaxKind.SetAccessor:
|
||||
case SyntaxKind.IndexSignature:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// True if the given identifier, string literal, or number literal is the name of a declaration node
|
||||
export function isDeclarationName(name: Node): boolean {
|
||||
if (name.kind !== SyntaxKind.Identifier && name.kind !== SyntaxKind.StringLiteral && name.kind !== SyntaxKind.NumericLiteral) {
|
||||
@@ -834,6 +925,23 @@ module ts {
|
||||
return false;
|
||||
}
|
||||
|
||||
// An alias symbol is created by one of the following declarations:
|
||||
// import <symbol> = ...
|
||||
// import <symbol> from ...
|
||||
// import * as <symbol> from ...
|
||||
// import { x as <symbol> } from ...
|
||||
// export { x as <symbol> } from ...
|
||||
// export = ...
|
||||
// export default ...
|
||||
export function isAliasSymbolDeclaration(node: Node): boolean {
|
||||
return node.kind === SyntaxKind.ImportEqualsDeclaration ||
|
||||
node.kind === SyntaxKind.ImportClause && !!(<ImportClause>node).name ||
|
||||
node.kind === SyntaxKind.NamespaceImport ||
|
||||
node.kind === SyntaxKind.ImportSpecifier ||
|
||||
node.kind === SyntaxKind.ExportSpecifier ||
|
||||
node.kind === SyntaxKind.ExportAssignment && (<ExportAssignment>node).expression.kind === SyntaxKind.Identifier;
|
||||
}
|
||||
|
||||
export function getClassBaseTypeNode(node: ClassDeclaration) {
|
||||
let heritageClause = getHeritageClause(node.heritageClauses, SyntaxKind.ExtendsKeyword);
|
||||
return heritageClause && heritageClause.types.length > 0 ? heritageClause.types[0] : undefined;
|
||||
@@ -1485,6 +1593,7 @@ module ts {
|
||||
|
||||
export function getAllAccessorDeclarations(declarations: NodeArray<Declaration>, accessor: AccessorDeclaration) {
|
||||
let firstAccessor: AccessorDeclaration;
|
||||
let secondAccessor: AccessorDeclaration;
|
||||
let getAccessor: AccessorDeclaration;
|
||||
let setAccessor: AccessorDeclaration;
|
||||
if (hasDynamicName(accessor)) {
|
||||
@@ -1509,6 +1618,9 @@ module ts {
|
||||
if (!firstAccessor) {
|
||||
firstAccessor = <AccessorDeclaration>member;
|
||||
}
|
||||
else if (!secondAccessor) {
|
||||
secondAccessor = <AccessorDeclaration>member;
|
||||
}
|
||||
|
||||
if (member.kind === SyntaxKind.GetAccessor && !getAccessor) {
|
||||
getAccessor = <AccessorDeclaration>member;
|
||||
@@ -1523,6 +1635,7 @@ module ts {
|
||||
}
|
||||
return {
|
||||
firstAccessor,
|
||||
secondAccessor,
|
||||
getAccessor,
|
||||
setAccessor
|
||||
};
|
||||
|
||||
14
src/lib/core.d.ts
vendored
14
src/lib/core.d.ts
vendored
@@ -1155,3 +1155,17 @@ interface ArrayConstructor {
|
||||
}
|
||||
|
||||
declare var Array: ArrayConstructor;
|
||||
|
||||
interface TypedPropertyDescriptor<T> {
|
||||
enumerable?: boolean;
|
||||
configurable?: boolean;
|
||||
writable?: boolean;
|
||||
value?: T;
|
||||
get?: () => T;
|
||||
set?: (value: T) => void;
|
||||
}
|
||||
|
||||
declare type ClassDecorator = <TFunction extends Function>(target: TFunction) => TFunction | void;
|
||||
declare type PropertyDecorator = (target: Object, propertyKey: string | symbol) => void;
|
||||
declare type MethodDecorator = <T>(target: Object, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor<T>) => TypedPropertyDescriptor<T> | void;
|
||||
declare type ParameterDecorator = (target: Function, propertyKey: string | symbol, parameterIndex: number) => void;
|
||||
|
||||
@@ -433,7 +433,35 @@ module ts.server {
|
||||
}
|
||||
|
||||
getSignatureHelpItems(fileName: string, position: number): SignatureHelpItems {
|
||||
throw new Error("Not Implemented Yet.");
|
||||
var lineOffset = this.positionToOneBasedLineOffset(fileName, position);
|
||||
var args: protocol.SignatureHelpRequestArgs = {
|
||||
file: fileName,
|
||||
line: lineOffset.line,
|
||||
offset: lineOffset.offset
|
||||
};
|
||||
|
||||
var request = this.processRequest<protocol.SignatureHelpRequest>(CommandNames.SignatureHelp, args);
|
||||
var response = this.processResponse<protocol.SignatureHelpResponse>(request);
|
||||
|
||||
if (!response.body) {
|
||||
return undefined;
|
||||
}
|
||||
var helpItems: protocol.SignatureHelpItems = response.body;
|
||||
var span = helpItems.applicableSpan;
|
||||
var start = this.lineOffsetToPosition(fileName, span.start);
|
||||
var end = this.lineOffsetToPosition(fileName, span.end);
|
||||
|
||||
var result: SignatureHelpItems = {
|
||||
items: helpItems.items,
|
||||
applicableSpan: {
|
||||
start: start,
|
||||
length: end - start
|
||||
},
|
||||
selectedItemIndex: helpItems.selectedItemIndex,
|
||||
argumentIndex: helpItems.argumentIndex,
|
||||
argumentCount: helpItems.argumentCount,
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
getOccurrencesAtPosition(fileName: string, position: number): ReferenceEntry[] {
|
||||
|
||||
116
src/server/protocol.d.ts
vendored
116
src/server/protocol.d.ts
vendored
@@ -592,6 +592,122 @@ declare module ts.server.protocol {
|
||||
body?: CompletionEntryDetails[];
|
||||
}
|
||||
|
||||
/**
|
||||
* Signature help information for a single parameter
|
||||
*/
|
||||
export interface SignatureHelpParameter {
|
||||
|
||||
/**
|
||||
* The parameter's name
|
||||
*/
|
||||
name: string;
|
||||
|
||||
/**
|
||||
* Documentation of the parameter.
|
||||
*/
|
||||
documentation: SymbolDisplayPart[];
|
||||
|
||||
/**
|
||||
* Display parts of the parameter.
|
||||
*/
|
||||
displayParts: SymbolDisplayPart[];
|
||||
|
||||
/**
|
||||
* Whether the parameter is optional or not.
|
||||
*/
|
||||
isOptional: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents a single signature to show in signature help.
|
||||
*/
|
||||
export interface SignatureHelpItem {
|
||||
|
||||
/**
|
||||
* Whether the signature accepts a variable number of arguments.
|
||||
*/
|
||||
isVariadic: boolean;
|
||||
|
||||
/**
|
||||
* The prefix display parts.
|
||||
*/
|
||||
prefixDisplayParts: SymbolDisplayPart[];
|
||||
|
||||
/**
|
||||
* The suffix disaply parts.
|
||||
*/
|
||||
suffixDisplayParts: SymbolDisplayPart[];
|
||||
|
||||
/**
|
||||
* The separator display parts.
|
||||
*/
|
||||
separatorDisplayParts: SymbolDisplayPart[];
|
||||
|
||||
/**
|
||||
* The signature helps items for the parameters.
|
||||
*/
|
||||
parameters: SignatureHelpParameter[];
|
||||
|
||||
/**
|
||||
* The signature's documentation
|
||||
*/
|
||||
documentation: SymbolDisplayPart[];
|
||||
}
|
||||
|
||||
/**
|
||||
* Signature help items found in the response of a signature help request.
|
||||
*/
|
||||
export interface SignatureHelpItems {
|
||||
|
||||
/**
|
||||
* The signature help items.
|
||||
*/
|
||||
items: SignatureHelpItem[];
|
||||
|
||||
/**
|
||||
* The span for which signature help should appear on a signature
|
||||
*/
|
||||
applicableSpan: TextSpan;
|
||||
|
||||
/**
|
||||
* The item selected in the set of available help items.
|
||||
*/
|
||||
selectedItemIndex: number;
|
||||
|
||||
/**
|
||||
* The argument selected in the set of parameters.
|
||||
*/
|
||||
argumentIndex: number;
|
||||
|
||||
/**
|
||||
* The argument count
|
||||
*/
|
||||
argumentCount: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Arguments of a signature help request.
|
||||
*/
|
||||
export interface SignatureHelpRequestArgs extends FileLocationRequestArgs {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Signature help request; value of command field is "signatureHelp".
|
||||
* Given a file location (file, line, col), return the signature
|
||||
* help.
|
||||
*/
|
||||
export interface SignatureHelpRequest extends FileLocationRequest {
|
||||
arguments: SignatureHelpRequestArgs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Repsonse object for a SignatureHelpRequest.
|
||||
*/
|
||||
export interface SignatureHelpResponse extends Response {
|
||||
body?: SignatureHelpItems;
|
||||
}
|
||||
|
||||
/**
|
||||
* Arguments for geterr messages.
|
||||
*/
|
||||
|
||||
@@ -80,6 +80,7 @@ module ts.server {
|
||||
export var Close = "close";
|
||||
export var Completions = "completions";
|
||||
export var CompletionDetails = "completionEntryDetails";
|
||||
export var SignatureHelp = "signatureHelp";
|
||||
export var Configure = "configure";
|
||||
export var Definition = "definition";
|
||||
export var Format = "format";
|
||||
@@ -577,6 +578,35 @@ module ts.server {
|
||||
}, []);
|
||||
}
|
||||
|
||||
getSignatureHelpItems(line: number, offset: number, fileName: string): protocol.SignatureHelpItems {
|
||||
var file = ts.normalizePath(fileName);
|
||||
var project = this.projectService.getProjectForFile(file);
|
||||
if (!project) {
|
||||
throw Errors.NoProject;
|
||||
}
|
||||
|
||||
var compilerService = project.compilerService;
|
||||
var position = compilerService.host.lineOffsetToPosition(file, line, offset);
|
||||
var helpItems = compilerService.languageService.getSignatureHelpItems(file, position);
|
||||
if (!helpItems) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
var span = helpItems.applicableSpan;
|
||||
var result: protocol.SignatureHelpItems = {
|
||||
items: helpItems.items,
|
||||
applicableSpan: {
|
||||
start: compilerService.host.positionToLineOffset(file, span.start),
|
||||
end: compilerService.host.positionToLineOffset(file, span.start + span.length)
|
||||
},
|
||||
selectedItemIndex: helpItems.selectedItemIndex,
|
||||
argumentIndex: helpItems.argumentIndex,
|
||||
argumentCount: helpItems.argumentCount,
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
getDiagnostics(delay: number, fileNames: string[]) {
|
||||
var checkList = fileNames.reduce((accum: PendingErrorCheck[], fileName: string) => {
|
||||
fileName = ts.normalizePath(fileName);
|
||||
@@ -790,6 +820,11 @@ module ts.server {
|
||||
completionDetailsArgs.entryNames,completionDetailsArgs.file);
|
||||
break;
|
||||
}
|
||||
case CommandNames.SignatureHelp: {
|
||||
var signatureHelpArgs = <protocol.SignatureHelpRequestArgs>request.arguments;
|
||||
response = this.getSignatureHelpItems(signatureHelpArgs.line, signatureHelpArgs.offset, signatureHelpArgs.file);
|
||||
break;
|
||||
}
|
||||
case CommandNames.Geterr: {
|
||||
var geterrArgs = <protocol.GeterrRequestArgs>request.arguments;
|
||||
response = this.getDiagnostics(geterrArgs.delay, geterrArgs.files);
|
||||
|
||||
@@ -419,6 +419,29 @@ module ts.formatting {
|
||||
}
|
||||
}
|
||||
|
||||
function getFirstNonDecoratorTokenOfNode(node: Node) {
|
||||
if (node.modifiers && node.modifiers.length) {
|
||||
return node.modifiers[0].kind;
|
||||
}
|
||||
switch (node.kind) {
|
||||
case SyntaxKind.ClassDeclaration: return SyntaxKind.ClassKeyword;
|
||||
case SyntaxKind.InterfaceDeclaration: return SyntaxKind.InterfaceKeyword;
|
||||
case SyntaxKind.FunctionDeclaration: return SyntaxKind.FunctionKeyword;
|
||||
case SyntaxKind.EnumDeclaration: return SyntaxKind.EnumDeclaration;
|
||||
case SyntaxKind.GetAccessor: return SyntaxKind.GetKeyword;
|
||||
case SyntaxKind.SetAccessor: return SyntaxKind.SetKeyword;
|
||||
case SyntaxKind.MethodDeclaration:
|
||||
if ((<MethodDeclaration>node).asteriskToken) {
|
||||
return SyntaxKind.AsteriskToken;
|
||||
}
|
||||
// fall-through
|
||||
|
||||
case SyntaxKind.PropertyDeclaration:
|
||||
case SyntaxKind.Parameter:
|
||||
return (<Declaration>node).name.kind;
|
||||
}
|
||||
}
|
||||
|
||||
function getDynamicIndentation(node: Node, nodeStartLine: number, indentation: number, delta: number): DynamicIndentation {
|
||||
return {
|
||||
getIndentationForComment: kind => {
|
||||
@@ -434,6 +457,12 @@ module ts.formatting {
|
||||
return indentation;
|
||||
},
|
||||
getIndentationForToken: (line, kind) => {
|
||||
if (nodeStartLine !== line && node.decorators) {
|
||||
if (kind === getFirstNonDecoratorTokenOfNode(node)) {
|
||||
// if this token is the first token following the list of decorators, we do not need to indent
|
||||
return indentation;
|
||||
}
|
||||
}
|
||||
switch (kind) {
|
||||
// open and close brace, 'else' and 'while' (in do statement) tokens has indentation of the parent
|
||||
case SyntaxKind.OpenBraceToken:
|
||||
@@ -442,6 +471,7 @@ module ts.formatting {
|
||||
case SyntaxKind.CloseBracketToken:
|
||||
case SyntaxKind.ElseKeyword:
|
||||
case SyntaxKind.WhileKeyword:
|
||||
case SyntaxKind.AtToken:
|
||||
return indentation;
|
||||
default:
|
||||
// if token line equals to the line of containing node (this is a first token in the node) - use node indentation
|
||||
|
||||
@@ -2476,36 +2476,38 @@ module ts {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
// The decision to provide completion depends on the previous token, so find it
|
||||
// Note: previousToken can be undefined if we are the beginning of the file
|
||||
start = new Date().getTime();
|
||||
let previousToken = findPrecedingToken(position, sourceFile);
|
||||
log("getCompletionData: Get previous token 1: " + (new Date().getTime() - start));
|
||||
|
||||
// The caret is at the end of an identifier; this is a partial identifier that we want to complete: e.g. a.toS|
|
||||
// Skip this partial identifier to the previous token
|
||||
if (previousToken && position <= previousToken.end && previousToken.kind === SyntaxKind.Identifier) {
|
||||
// The decision to provide completion depends on the contextToken, which is determined through the previousToken.
|
||||
// Note: 'previousToken' (and thus 'contextToken') can be undefined if we are the beginning of the file
|
||||
let contextToken = previousToken;
|
||||
|
||||
// Check if the caret is at the end of an identifier; this is a partial identifier that we want to complete: e.g. a.toS|
|
||||
// Skip this partial identifier and adjust the contextToken to the token that precedes it.
|
||||
if (contextToken && position <= contextToken.end && isWord(contextToken.kind)) {
|
||||
let start = new Date().getTime();
|
||||
previousToken = findPrecedingToken(previousToken.pos, sourceFile);
|
||||
contextToken = findPrecedingToken(contextToken.getFullStart(), sourceFile);
|
||||
log("getCompletionData: Get previous token 2: " + (new Date().getTime() - start));
|
||||
}
|
||||
|
||||
// Check if this is a valid completion location
|
||||
if (previousToken && isCompletionListBlocker(previousToken)) {
|
||||
if (contextToken && isCompletionListBlocker(contextToken)) {
|
||||
log("Returning an empty list because completion was requested in an invalid position.");
|
||||
return undefined;
|
||||
}
|
||||
|
||||
// Find the node where completion is requested on, in the case of a completion after a dot, it is the member access expression
|
||||
// other wise, it is a request for all visible symbols in the scope, and the node is the current location
|
||||
// otherwise, it is a request for all visible symbols in the scope, and the node is the current location
|
||||
let node = currentToken;
|
||||
let isRightOfDot = false;
|
||||
if (previousToken && previousToken.kind === SyntaxKind.DotToken && previousToken.parent.kind === SyntaxKind.PropertyAccessExpression) {
|
||||
node = (<PropertyAccessExpression>previousToken.parent).expression;
|
||||
if (contextToken && contextToken.kind === SyntaxKind.DotToken && contextToken.parent.kind === SyntaxKind.PropertyAccessExpression) {
|
||||
node = (<PropertyAccessExpression>contextToken.parent).expression;
|
||||
isRightOfDot = true;
|
||||
}
|
||||
else if (previousToken && previousToken.kind === SyntaxKind.DotToken && previousToken.parent.kind === SyntaxKind.QualifiedName) {
|
||||
node = (<QualifiedName>previousToken.parent).left;
|
||||
else if (contextToken && contextToken.kind === SyntaxKind.DotToken && contextToken.parent.kind === SyntaxKind.QualifiedName) {
|
||||
node = (<QualifiedName>contextToken.parent).left;
|
||||
isRightOfDot = true;
|
||||
}
|
||||
|
||||
@@ -2554,7 +2556,7 @@ module ts {
|
||||
}
|
||||
}
|
||||
else {
|
||||
let containingObjectLiteral = getContainingObjectLiteralApplicableForCompletion(previousToken);
|
||||
let containingObjectLiteral = getContainingObjectLiteralApplicableForCompletion(contextToken);
|
||||
if (containingObjectLiteral) {
|
||||
// Object literal expression, look up possible property names from contextual type
|
||||
isMemberCompletion = true;
|
||||
@@ -2571,13 +2573,13 @@ module ts {
|
||||
symbols = filterContextualMembersList(contextualTypeMembers, containingObjectLiteral.properties);
|
||||
}
|
||||
}
|
||||
else if (getAncestor(previousToken, SyntaxKind.ImportClause)) {
|
||||
else if (getAncestor(contextToken, SyntaxKind.ImportClause)) {
|
||||
// cursor is in import clause
|
||||
// try to show exported member for imported module
|
||||
isMemberCompletion = true;
|
||||
isNewIdentifierLocation = true;
|
||||
if (showCompletionsInImportsClause(previousToken)) {
|
||||
let importDeclaration = <ImportDeclaration>getAncestor(previousToken, SyntaxKind.ImportDeclaration);
|
||||
if (showCompletionsInImportsClause(contextToken)) {
|
||||
let importDeclaration = <ImportDeclaration>getAncestor(contextToken, SyntaxKind.ImportDeclaration);
|
||||
Debug.assert(importDeclaration !== undefined);
|
||||
|
||||
let exports = typeInfoResolver.getExportsOfImportDeclaration(importDeclaration);
|
||||
@@ -2585,14 +2587,46 @@ module ts {
|
||||
}
|
||||
}
|
||||
else {
|
||||
// Get scope members
|
||||
// Get all entities in the current scope.
|
||||
isMemberCompletion = false;
|
||||
isNewIdentifierLocation = isNewIdentifierDefinitionLocation(previousToken);
|
||||
isNewIdentifierLocation = isNewIdentifierDefinitionLocation(contextToken);
|
||||
|
||||
if (previousToken !== contextToken) {
|
||||
Debug.assert(!!previousToken, "Expected 'contextToken' to be defined when different from 'previousToken'.");
|
||||
}
|
||||
// We need to find the node that will give us an appropriate scope to begin
|
||||
// aggregating completion candidates. This is achieved in 'getScopeNode'
|
||||
// by finding the first node that encompasses a position, accounting for whether a node
|
||||
// is "complete" to decide whether a position belongs to the node.
|
||||
//
|
||||
// However, at the end of an identifier, we are interested in the scope of the identifier
|
||||
// itself, but fall outside of the identifier. For instance:
|
||||
//
|
||||
// xyz => x$
|
||||
//
|
||||
// the cursor is outside of both the 'x' and the arrow function 'xyz => x',
|
||||
// so 'xyz' is not returned in our results.
|
||||
//
|
||||
// We define 'adjustedPosition' so that we may appropriately account for
|
||||
// being at the end of an identifier. The intention is that if requesting completion
|
||||
// at the end of an identifier, it should be effectively equivalent to requesting completion
|
||||
// anywhere inside/at the beginning of the identifier. So in the previous case, the
|
||||
// 'adjustedPosition' will work as if requesting completion in the following:
|
||||
//
|
||||
// xyz => $x
|
||||
//
|
||||
// If previousToken !== contextToken, then
|
||||
// - 'contextToken' was adjusted to the token prior to 'previousToken'
|
||||
// because we were at the end of an identifier.
|
||||
// - 'previousToken' is defined.
|
||||
let adjustedPosition = previousToken !== contextToken ?
|
||||
previousToken.getStart() :
|
||||
position;
|
||||
|
||||
let scopeNode = getScopeNode(contextToken, adjustedPosition, sourceFile);
|
||||
|
||||
/// TODO filter meaning based on the current context
|
||||
let scopeNode = getScopeNode(previousToken, position, sourceFile);
|
||||
let symbolMeanings = SymbolFlags.Type | SymbolFlags.Value | SymbolFlags.Namespace | SymbolFlags.Alias;
|
||||
|
||||
symbols = typeInfoResolver.getSymbolsInScope(scopeNode, symbolMeanings);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -450,7 +450,7 @@ module ts {
|
||||
return n.kind >= SyntaxKind.FirstToken && n.kind <= SyntaxKind.LastToken;
|
||||
}
|
||||
|
||||
function isWord(kind: SyntaxKind): boolean {
|
||||
export function isWord(kind: SyntaxKind): boolean {
|
||||
return kind === SyntaxKind.Identifier || isKeyword(kind);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user