Philip_Baird
12 years agoCommunity Expert
Obtaining a JScript source files Variable and Function Declarations
Hi all, as part of a requirement to simplify the maintenance of JScript Script Extensions, I needed to get all Variable and Function Declarations in JScript source files.
I have done this by using the JavaScriptParser Class in the Jint.dll .Net Assembly and thought there may interest in how to do this so I put together a quick demo on how this can be done.
1. Download the Jint.dll Assembly from https://jint.codeplex.com/ (This Assembly is released under The MIT License (MIT) Licence)
2. Register the Jint.dll Assembly in the CLR Bridge from the disk as per the Documentation
3. Run the following code for a Jscript file, the Log output should display two Log Folders, one for Variable Declarations and one for Function Declarations.
function testProcessJScriptFile() {
// Fully qualified file name of the JScript source file
var fileName = "C:\\TestComplete10_LOCAL_COPY\\ARC\\Framework\\ScriptExtensions\\Script\\ScriptExtensions\\ArcLog\\SRC_ArcLog.sj";
var jScript,
parser,
program,
variableDeclarations,
variableDeclaration,
declarations,
functionDeclarations,
functionDeclaration,
functionName,
functionParameters,
parameters;
if( aqFile.Exists( fileName ) ) {
// Read source and create Jint Parser
jScript = aqFile.ReadWholeTextFile( fileName, aqFile.ctUTF8);
parser = dotNET.Jint_Parser.JavaScriptParser.zctor();
program = parser.Parse( jScript );
// Display Variable Declarations
Log.AppendFolder( "Variable Declarations" );
variableDeclarations = program.VariableDeclarations.GetEnumerator();
while( variableDeclarations.MoveNext()) {
variableDeclaration = variableDeclarations.Current;
declarations = variableDeclaration.Declarations.GetEnumerator();
while( declarations.MoveNext()) {
declaration = declarations.Current;
Log.Message( declaration.Id.Name );
}
}
Log.PopLogFolder();
// Display Function Declarations
Log.AppendFolder( "Function Declarations" );
functionDeclarations = program.FunctionDeclarations.GetEnumerator();
while( functionDeclarations.MoveNext()) {
functionDeclaration = functionDeclarations.Current;
functionName = functionDeclaration.Id.Name.OleValue;
parameters = "";
functionParameters = functionDeclaration.Parameters.GetEnumerator();
while(functionParameters.MoveNext())
{
functionParameter = functionParameters.Current;
parameters += ( parameters !== "" ) ? ", " + functionParameter.Name : functionParameter.Name;
}
Log.Message( aqString.Format( "%s(%s)", functionName, parameters ) );
}
Log.PopLogFolder();
}
}
Hopefully someone may find this useful.
Regards,
Phil Baird
I have done this by using the JavaScriptParser Class in the Jint.dll .Net Assembly and thought there may interest in how to do this so I put together a quick demo on how this can be done.
1. Download the Jint.dll Assembly from https://jint.codeplex.com/ (This Assembly is released under The MIT License (MIT) Licence)
2. Register the Jint.dll Assembly in the CLR Bridge from the disk as per the Documentation
3. Run the following code for a Jscript file, the Log output should display two Log Folders, one for Variable Declarations and one for Function Declarations.
// Demonstrates using Jint to obtain the Variable and Function Declarations from a JScript source file
function testProcessJScriptFile() {
// Fully qualified file name of the JScript source file
var fileName = "C:\\TestComplete10_LOCAL_COPY\\ARC\\Framework\\ScriptExtensions\\Script\\ScriptExtensions\\ArcLog\\SRC_ArcLog.sj";
var jScript,
parser,
program,
variableDeclarations,
variableDeclaration,
declarations,
functionDeclarations,
functionDeclaration,
functionName,
functionParameters,
parameters;
if( aqFile.Exists( fileName ) ) {
// Read source and create Jint Parser
jScript = aqFile.ReadWholeTextFile( fileName, aqFile.ctUTF8);
parser = dotNET.Jint_Parser.JavaScriptParser.zctor();
program = parser.Parse( jScript );
// Display Variable Declarations
Log.AppendFolder( "Variable Declarations" );
variableDeclarations = program.VariableDeclarations.GetEnumerator();
while( variableDeclarations.MoveNext()) {
variableDeclaration = variableDeclarations.Current;
declarations = variableDeclaration.Declarations.GetEnumerator();
while( declarations.MoveNext()) {
declaration = declarations.Current;
Log.Message( declaration.Id.Name );
}
}
Log.PopLogFolder();
// Display Function Declarations
Log.AppendFolder( "Function Declarations" );
functionDeclarations = program.FunctionDeclarations.GetEnumerator();
while( functionDeclarations.MoveNext()) {
functionDeclaration = functionDeclarations.Current;
functionName = functionDeclaration.Id.Name.OleValue;
parameters = "";
functionParameters = functionDeclaration.Parameters.GetEnumerator();
while(functionParameters.MoveNext())
{
functionParameter = functionParameters.Current;
parameters += ( parameters !== "" ) ? ", " + functionParameter.Name : functionParameter.Name;
}
Log.Message( aqString.Format( "%s(%s)", functionName, parameters ) );
}
Log.PopLogFolder();
}
}
Hopefully someone may find this useful.
Regards,
Phil Baird