Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.

Commit d7d692d

Browse files
committed
Parser: added support for decomposed class where variable appears before interface with same name (previously only handled interface before variable)
1 parent 30f7121 commit d7d692d

File tree

1 file changed

+36
-14
lines changed

1 file changed

+36
-14
lines changed

source/parser.ts

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -722,16 +722,26 @@ class TS2ASParser
722722
});
723723
}
724724

725-
private mergeInterfaceAndVariable(interfaceDefinition: as3.InterfaceDefinition, variableDeclaration: ts.VariableDeclaration)
725+
private mergeInterfaceAndVariable(interfaceDefinition: as3.InterfaceDefinition, variableDefinition: as3.PackageVariableDefinition)
726726
{
727-
let variableAccessLevel = this.getAccessLevel(variableDeclaration);
728727
let as3Class = new as3.ClassDefinition(interfaceDefinition.name,
729-
interfaceDefinition.packageName, variableAccessLevel,
728+
interfaceDefinition.packageName, variableDefinition.accessLevel,
730729
interfaceDefinition.sourceFile, interfaceDefinition.require,
731730
this._currentFileIsExternal);
732731

733732
let index = this._definitions.indexOf(interfaceDefinition);
734-
this._definitions[index] = as3Class;
733+
if(index >= 0)
734+
{
735+
this._definitions[index] = as3Class;
736+
return;
737+
}
738+
index = this._definitions.indexOf(variableDefinition);
739+
if(index >= 0)
740+
{
741+
this._definitions[index] = as3Class;
742+
return;
743+
}
744+
throw new Error("Cannot find existing definition to replace, with name " + as3Class.getFullyQualifiedName());
735745
}
736746

737747
private populateTypeParameters(declaration: ts.Declaration): string[]
@@ -878,7 +888,17 @@ class TS2ASParser
878888
return null;
879889
}
880890
}
881-
return new as3.InterfaceDefinition(interfaceName, packageName, this.getAccessLevel(interfaceDeclaration), this._currentSourceFile.fileName, this._currentModuleNeedsRequire, this._currentFileIsExternal);
891+
let as3Interface = new as3.InterfaceDefinition(interfaceName, packageName, this.getAccessLevel(interfaceDeclaration), this._currentSourceFile.fileName, this._currentModuleNeedsRequire, this._currentFileIsExternal);
892+
893+
let existingDefinition = as3.getDefinitionByName(fullyQualifiedInterfaceName, this._definitions);
894+
if(existingDefinition instanceof as3.PackageVariableDefinition)
895+
{
896+
//this is a decomposed class where the variable name and the
897+
//instance side have the same name
898+
this.mergeInterfaceAndVariable(as3Interface, existingDefinition);
899+
return null;
900+
}
901+
return as3Interface;
882902
}
883903

884904
private populateInterface(interfaceDeclaration: ts.InterfaceDeclaration)
@@ -989,26 +1009,28 @@ class TS2ASParser
9891009
{
9901010
fullyQualifiedName = packageName + "." + variableName;
9911011
}
1012+
let accessLevel = this.getAccessLevel(variableDeclaration);
9921013
let existingDefinition = as3.getDefinitionByName(fullyQualifiedName, this._definitions);
993-
if(existingDefinition instanceof as3.InterfaceDefinition)
1014+
if(existingDefinition instanceof as3.StaticSideClassDefinition)
9941015
{
995-
//this is a decomposed class where the variable name and the
996-
//instance side have the same name
997-
this.mergeInterfaceAndVariable(existingDefinition, variableDeclaration);
1016+
//this is a decomposed class where the variable name and the static
1017+
//side have the same name
1018+
existingDefinition.accessLevel = accessLevel;
9981019
return null;
9991020
}
1000-
else if(existingDefinition instanceof as3.StaticSideClassDefinition)
1021+
let as3Variable = new as3.PackageVariableDefinition(variableName, packageName, accessLevel, this._currentSourceFile.fileName, this._currentModuleNeedsRequire, this._currentFileIsExternal);
1022+
if(existingDefinition instanceof as3.InterfaceDefinition)
10011023
{
1002-
//this is a decomposed class where the variable name and the static
1003-
//side have the same name
1004-
existingDefinition.accessLevel = this.getAccessLevel(variableDeclaration);
1024+
//this is a decomposed class where the variable name and the
1025+
//instance side have the same name
1026+
this.mergeInterfaceAndVariable(existingDefinition, as3Variable);
10051027
return null;
10061028
}
10071029
else if(existingDefinition !== null)
10081030
{
10091031
throw new Error("Definition with name " + fullyQualifiedName + " already exists. Cannot create package variable.");
10101032
}
1011-
return new as3.PackageVariableDefinition(variableName, packageName, this.getAccessLevel(variableDeclaration), this._currentSourceFile.fileName, this._currentModuleNeedsRequire, this._currentFileIsExternal);
1033+
return as3Variable;
10121034
}
10131035

10141036
private populatePackageVariable(variableDeclaration: ts.VariableDeclaration)

0 commit comments

Comments
 (0)