Skip to content

Commit feabcd0

Browse files
Merge pull request microsoft#2522 from Microsoft/visitWorkaround
Use a function declaration vs a function expression to help deal with a reported Atom+IO.js issue.
2 parents 9e4c9f9 + eb5e1bc commit feabcd0

File tree

1 file changed

+110
-105
lines changed

1 file changed

+110
-105
lines changed

src/services/services.ts

+110-105
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,7 @@ module ts {
730730
public statements: NodeArray<Statement>;
731731
public endOfFileToken: Node;
732732

733-
public amdDependencies: {name: string; path: string}[];
733+
public amdDependencies: { name: string; path: string }[];
734734
public amdModuleName: string;
735735
public referencedFiles: FileReference[];
736736

@@ -769,125 +769,130 @@ module ts {
769769

770770
public getNamedDeclarations() {
771771
if (!this.namedDeclarations) {
772-
let sourceFile = this;
773-
let namedDeclarations: Declaration[] = [];
772+
this.namedDeclarations = this.computeNamedDeclarations();
773+
}
774774

775-
forEachChild(sourceFile, function visit(node: Node): void {
776-
switch (node.kind) {
777-
case SyntaxKind.FunctionDeclaration:
778-
case SyntaxKind.MethodDeclaration:
779-
case SyntaxKind.MethodSignature:
780-
let functionDeclaration = <FunctionLikeDeclaration>node;
781-
782-
if (functionDeclaration.name && functionDeclaration.name.getFullWidth() > 0) {
783-
let lastDeclaration = namedDeclarations.length > 0 ?
784-
namedDeclarations[namedDeclarations.length - 1] :
785-
undefined;
786-
787-
// Check whether this declaration belongs to an "overload group".
788-
if (lastDeclaration && functionDeclaration.symbol === lastDeclaration.symbol) {
789-
// Overwrite the last declaration if it was an overload
790-
// and this one is an implementation.
791-
if (functionDeclaration.body && !(<FunctionLikeDeclaration>lastDeclaration).body) {
792-
namedDeclarations[namedDeclarations.length - 1] = functionDeclaration;
793-
}
794-
}
795-
else {
796-
namedDeclarations.push(functionDeclaration);
797-
}
775+
return this.namedDeclarations;
776+
}
798777

799-
forEachChild(node, visit);
800-
}
801-
break;
778+
private computeNamedDeclarations() {
779+
let namedDeclarations: Declaration[] = [];
802780

803-
case SyntaxKind.ClassDeclaration:
804-
case SyntaxKind.InterfaceDeclaration:
805-
case SyntaxKind.TypeAliasDeclaration:
806-
case SyntaxKind.EnumDeclaration:
807-
case SyntaxKind.ModuleDeclaration:
808-
case SyntaxKind.ImportEqualsDeclaration:
809-
case SyntaxKind.ExportSpecifier:
810-
case SyntaxKind.ImportSpecifier:
811-
case SyntaxKind.ImportEqualsDeclaration:
812-
case SyntaxKind.ImportClause:
813-
case SyntaxKind.NamespaceImport:
814-
case SyntaxKind.GetAccessor:
815-
case SyntaxKind.SetAccessor:
816-
case SyntaxKind.TypeLiteral:
817-
if ((<Declaration>node).name) {
818-
namedDeclarations.push(<Declaration>node);
819-
}
820-
// fall through
821-
case SyntaxKind.Constructor:
822-
case SyntaxKind.VariableStatement:
823-
case SyntaxKind.VariableDeclarationList:
824-
case SyntaxKind.ObjectBindingPattern:
825-
case SyntaxKind.ArrayBindingPattern:
826-
case SyntaxKind.ModuleBlock:
827-
forEachChild(node, visit);
828-
break;
781+
forEachChild(this, visit);
829782

830-
case SyntaxKind.Block:
831-
if (isFunctionBlock(node)) {
832-
forEachChild(node, visit);
833-
}
834-
break;
783+
return namedDeclarations;
835784

836-
case SyntaxKind.Parameter:
837-
// Only consider properties defined as constructor parameters
838-
if (!(node.flags & NodeFlags.AccessibilityModifier)) {
839-
break;
785+
function visit(node: Node): void {
786+
switch (node.kind) {
787+
case SyntaxKind.FunctionDeclaration:
788+
case SyntaxKind.MethodDeclaration:
789+
case SyntaxKind.MethodSignature:
790+
let functionDeclaration = <FunctionLikeDeclaration>node;
791+
792+
if (functionDeclaration.name && functionDeclaration.name.getFullWidth() > 0) {
793+
let lastDeclaration = namedDeclarations.length > 0 ?
794+
namedDeclarations[namedDeclarations.length - 1] :
795+
undefined;
796+
797+
// Check whether this declaration belongs to an "overload group".
798+
if (lastDeclaration && functionDeclaration.symbol === lastDeclaration.symbol) {
799+
// Overwrite the last declaration if it was an overload
800+
// and this one is an implementation.
801+
if (functionDeclaration.body && !(<FunctionLikeDeclaration>lastDeclaration).body) {
802+
namedDeclarations[namedDeclarations.length - 1] = functionDeclaration;
803+
}
840804
}
841-
// fall through
842-
case SyntaxKind.VariableDeclaration:
843-
case SyntaxKind.BindingElement:
844-
if (isBindingPattern((<VariableDeclaration>node).name)) {
845-
forEachChild((<VariableDeclaration>node).name, visit);
846-
break;
805+
else {
806+
namedDeclarations.push(functionDeclaration);
847807
}
848-
case SyntaxKind.EnumMember:
849-
case SyntaxKind.PropertyDeclaration:
850-
case SyntaxKind.PropertySignature:
808+
809+
forEachChild(node, visit);
810+
}
811+
break;
812+
813+
case SyntaxKind.ClassDeclaration:
814+
case SyntaxKind.InterfaceDeclaration:
815+
case SyntaxKind.TypeAliasDeclaration:
816+
case SyntaxKind.EnumDeclaration:
817+
case SyntaxKind.ModuleDeclaration:
818+
case SyntaxKind.ImportEqualsDeclaration:
819+
case SyntaxKind.ExportSpecifier:
820+
case SyntaxKind.ImportSpecifier:
821+
case SyntaxKind.ImportEqualsDeclaration:
822+
case SyntaxKind.ImportClause:
823+
case SyntaxKind.NamespaceImport:
824+
case SyntaxKind.GetAccessor:
825+
case SyntaxKind.SetAccessor:
826+
case SyntaxKind.TypeLiteral:
827+
if ((<Declaration>node).name) {
851828
namedDeclarations.push(<Declaration>node);
829+
}
830+
// fall through
831+
case SyntaxKind.Constructor:
832+
case SyntaxKind.VariableStatement:
833+
case SyntaxKind.VariableDeclarationList:
834+
case SyntaxKind.ObjectBindingPattern:
835+
case SyntaxKind.ArrayBindingPattern:
836+
case SyntaxKind.ModuleBlock:
837+
forEachChild(node, visit);
838+
break;
839+
840+
case SyntaxKind.Block:
841+
if (isFunctionBlock(node)) {
842+
forEachChild(node, visit);
843+
}
844+
break;
845+
846+
case SyntaxKind.Parameter:
847+
// Only consider properties defined as constructor parameters
848+
if (!(node.flags & NodeFlags.AccessibilityModifier)) {
852849
break;
853-
854-
case SyntaxKind.ExportDeclaration:
855-
// Handle named exports case e.g.:
856-
// export {a, b as B} from "mod";
857-
if ((<ExportDeclaration>node).exportClause) {
858-
forEach((<ExportDeclaration>node).exportClause.elements, visit);
859-
}
850+
}
851+
// fall through
852+
case SyntaxKind.VariableDeclaration:
853+
case SyntaxKind.BindingElement:
854+
if (isBindingPattern((<VariableDeclaration>node).name)) {
855+
forEachChild((<VariableDeclaration>node).name, visit);
860856
break;
857+
}
858+
case SyntaxKind.EnumMember:
859+
case SyntaxKind.PropertyDeclaration:
860+
case SyntaxKind.PropertySignature:
861+
namedDeclarations.push(<Declaration>node);
862+
break;
861863

862-
case SyntaxKind.ImportDeclaration:
863-
let importClause = (<ImportDeclaration>node).importClause;
864-
if (importClause) {
865-
// Handle default import case e.g.:
866-
// import d from "mod";
867-
if (importClause.name) {
868-
namedDeclarations.push(importClause);
869-
}
864+
case SyntaxKind.ExportDeclaration:
865+
// Handle named exports case e.g.:
866+
// export {a, b as B} from "mod";
867+
if ((<ExportDeclaration>node).exportClause) {
868+
forEach((<ExportDeclaration>node).exportClause.elements, visit);
869+
}
870+
break;
870871

871-
// Handle named bindings in imports e.g.:
872-
// import * as NS from "mod";
873-
// import {a, b as B} from "mod";
874-
if (importClause.namedBindings) {
875-
if (importClause.namedBindings.kind === SyntaxKind.NamespaceImport) {
876-
namedDeclarations.push(<NamespaceImport>importClause.namedBindings);
877-
}
878-
else {
879-
forEach((<NamedImports>importClause.namedBindings).elements, visit);
880-
}
881-
}
872+
case SyntaxKind.ImportDeclaration:
873+
let importClause = (<ImportDeclaration>node).importClause;
874+
if (importClause) {
875+
// Handle default import case e.g.:
876+
// import d from "mod";
877+
if (importClause.name) {
878+
namedDeclarations.push(importClause);
882879
}
883-
break;
884-
}
885-
});
886880

887-
this.namedDeclarations = namedDeclarations;
881+
// Handle named bindings in imports e.g.:
882+
// import * as NS from "mod";
883+
// import {a, b as B} from "mod";
884+
if (importClause.namedBindings) {
885+
if (importClause.namedBindings.kind === SyntaxKind.NamespaceImport) {
886+
namedDeclarations.push(<NamespaceImport>importClause.namedBindings);
887+
}
888+
else {
889+
forEach((<NamedImports>importClause.namedBindings).elements, visit);
890+
}
891+
}
892+
}
893+
break;
894+
}
888895
}
889-
890-
return this.namedDeclarations;
891896
}
892897
}
893898

0 commit comments

Comments
 (0)