* Patch from Mattias Gaertner

- jstree: add some explanatory comments 
  - jswriter: 
    TJSWriter.WriteSourceElements write functions and vars, 
    fixed unneeded semicolon in TJSWriter.WriteFuncDef.

git-svn-id: trunk@34217 -
This commit is contained in:
michael 2016-07-28 11:17:03 +00:00
parent 08c79c41bd
commit 19bd74faa9
2 changed files with 87 additions and 76 deletions

View File

@ -422,7 +422,6 @@ Type
end; end;
{ TJSBinary - base class } { TJSBinary - base class }
TJSBinary = Class(TJSElement) TJSBinary = Class(TJSElement)
@ -436,7 +435,7 @@ Type
end; end;
TJSBinaryClass = Class of TJSBinary; TJSBinaryClass = Class of TJSBinary;
{ TJSStatementList } { TJSStatementList - a list of statements enclosed in curly brackets }
TJSStatementList = Class(TJSBinary); // A->first statement, B->next in list, chained. TJSStatementList = Class(TJSBinary); // A->first statement, B->next in list, chained.
@ -935,7 +934,7 @@ Type
Property Node : TJSElement Read FNode Write FNode; Property Node : TJSElement Read FNode Write FNode;
end; end;
{ TJSElementNodes - } { TJSElementNodes - see TJSSourceElements }
TJSElementNodes = Class(TCollection) TJSElementNodes = Class(TCollection)
private private
@ -945,7 +944,8 @@ Type
Property Nodes[AIndex : Integer] : TJSElementNode Read GetN ; default; Property Nodes[AIndex : Integer] : TJSElementNode Read GetN ; default;
end; end;
{ TJSSourceElements } { TJSSourceElements - a list of elements, every element ends in semicolon,
first Vars, then Functions, finally Statements }
TJSSourceElements = Class(TJSElement) TJSSourceElements = Class(TJSElement)
private private
@ -955,9 +955,9 @@ Type
Public Public
Constructor Create(ALine,ARow : Integer; const ASource : String = ''); override; Constructor Create(ALine,ARow : Integer; const ASource : String = ''); override;
Destructor Destroy; override; Destructor Destroy; override;
Property Statements : TJSElementNodes Read FStatements;
Property Functions : TJSElementNodes Read FFunctions;
Property Vars : TJSElementNodes Read FVars; Property Vars : TJSElementNodes Read FVars;
Property Functions : TJSElementNodes Read FFunctions;
Property Statements : TJSElementNodes Read FStatements;
end; end;
implementation implementation

View File

@ -127,7 +127,7 @@ Type
Procedure WriteWithStatement(El: TJSWithStatement);virtual; Procedure WriteWithStatement(El: TJSWithStatement);virtual;
Procedure WriteVarDeclarationList(El: TJSVariableDeclarationList);virtual; Procedure WriteVarDeclarationList(El: TJSVariableDeclarationList);virtual;
Procedure WriteConditionalExpression(El: TJSConditionalExpression);virtual; Procedure WriteConditionalExpression(El: TJSConditionalExpression);virtual;
Procedure WriteFunctionBody(el: TJSFunctionBody);virtual; Procedure WriteFunctionBody(El: TJSFunctionBody);virtual;
Procedure WriteFunctionDeclarationStatement(El: TJSFunctionDeclarationStatement);virtual; Procedure WriteFunctionDeclarationStatement(El: TJSFunctionDeclarationStatement);virtual;
Procedure WriteLabeledStatement(El: TJSLabeledStatement);virtual; Procedure WriteLabeledStatement(El: TJSLabeledStatement);virtual;
Procedure WriteReturnStatement(EL: TJSReturnStatement);virtual; Procedure WriteReturnStatement(EL: TJSReturnStatement);virtual;
@ -480,7 +480,10 @@ begin
begin begin
FSkipBrackets:=True; FSkipBrackets:=True;
WriteJS(FD.Body); WriteJS(FD.Body);
If not (FD.Body.A is TJSStatementList) then If (Assigned(FD.Body.A))
and (not (FD.Body.A is TJSStatementList))
and (not (FD.Body.A is TJSSourceElements))
then
if C then if C then
Write('; ') Write('; ')
else else
@ -1039,11 +1042,11 @@ begin
end; end;
end; end;
procedure TJSWriter.WriteFunctionBody(el: TJSFunctionBody); procedure TJSWriter.WriteFunctionBody(El: TJSFunctionBody);
begin begin
if Assigned(EL.A) then if Assigned(El.A) then
WriteJS(EL.A); WriteJS(El.A);
end; end;
procedure TJSWriter.WriteFunctionDeclarationStatement( procedure TJSWriter.WriteFunctionDeclarationStatement(
@ -1057,27 +1060,35 @@ end;
procedure TJSWriter.WriteSourceElements(El: TJSSourceElements); procedure TJSWriter.WriteSourceElements(El: TJSSourceElements);
Var Var
I : Integer;
C : Boolean; C : Boolean;
E : TJSElement;
Procedure WriteElements(Elements: TJSElementNodes);
Var
I : Integer;
E : TJSElement;
begin
if Elements=nil then exit;
For I:=0 to Elements.Count-1 do
begin
E:=Elements.Nodes[i].Node;
WriteJS(E);
if Not C then
WriteLn(';')
else
if I<Elements.Count-1 then
Write('; ')
else
Write(';')
end;
end;
begin begin
C:=(woCompact in Options); C:=(woCompact in Options);
For I:=0 to EL.Statements.Count-1 do WriteElements(El.Vars);
begin WriteElements(El.Functions);
E:=EL.Statements.Nodes[i].Node; WriteElements(El.Statements);
WriteJS(E);
if Not C then
WriteLn(';')
else
if I<EL.Statements.Count-1 then
Write('; ')
else
Write(';')
end;
end; end;
procedure TJSWriter.WriteVariableStatement(el: TJSVariableStatement); procedure TJSWriter.WriteVariableStatement(el: TJSVariableStatement);
begin begin
@ -1094,62 +1105,62 @@ begin
system.Writeln('WriteJS : El = Nil'); system.Writeln('WriteJS : El = Nil');
{$ENDIF} {$ENDIF}
if (El is TJSEmptyBlockStatement ) then if (El is TJSEmptyBlockStatement ) then
WriteEmptyBlockStatement(TJSEmptyBlockStatement(el)) WriteEmptyBlockStatement(TJSEmptyBlockStatement(El))
else if (El is TJSEmptyStatement) then else if (El is TJSEmptyStatement) then
WriteEmptyStatement(TJSEmptyStatement(el)) WriteEmptyStatement(TJSEmptyStatement(El))
else if (el is TJSLiteral) then else if (El is TJSLiteral) then
WriteLiteral(TJSLiteral(el)) WriteLiteral(TJSLiteral(El))
else if (el is TJSPrimaryExpression) then else if (El is TJSPrimaryExpression) then
WritePrimaryExpression(TJSPrimaryExpression(el)) WritePrimaryExpression(TJSPrimaryExpression(El))
else if (el is TJSArrayLiteral) then else if (El is TJSArrayLiteral) then
WriteArrayLiteral(TJSArrayLiteral(el)) WriteArrayLiteral(TJSArrayLiteral(El))
else if (el is TJSObjectLiteral) then else if (El is TJSObjectLiteral) then
WriteObjectLiteral(TJSObjectLiteral(el)) WriteObjectLiteral(TJSObjectLiteral(El))
else if (el is TJSMemberExpression) then else if (El is TJSMemberExpression) then
WriteMemberExpression(TJSMemberExpression(el)) WriteMemberExpression(TJSMemberExpression(El))
else if (el is TJSRegularExpressionLiteral) then else if (El is TJSRegularExpressionLiteral) then
WriteRegularExpressionLiteral(TJSRegularExpressionLiteral(El)) WriteRegularExpressionLiteral(TJSRegularExpressionLiteral(El))
else if (el is TJSCallExpression) then else if (El is TJSCallExpression) then
WriteCallExpression(TJSCallExpression(el)) WriteCallExpression(TJSCallExpression(El))
else if (el is TJSLabeledStatement) then // Before unary else if (El is TJSLabeledStatement) then // Before unary
WriteLabeledStatement(TJSLabeledStatement(el)) WriteLabeledStatement(TJSLabeledStatement(El))
else if (el is TJSFunctionBody) then // Before unary else if (El is TJSFunctionBody) then // Before unary
WriteFunctionBody(TJSFunctionBody(el)) WriteFunctionBody(TJSFunctionBody(El))
else if (el is TJSVariableStatement) then // Before unary else if (El is TJSVariableStatement) then // Before unary
WriteVariableStatement(TJSVariableStatement(el)) WriteVariableStatement(TJSVariableStatement(El))
else if (el is TJSUNary) then else if (El is TJSUNary) then
WriteUnary(TJSUnary(el)) WriteUnary(TJSUnary(El))
else if (el is TJSVariableDeclarationList) then else if (El is TJSVariableDeclarationList) then
WriteVarDeclarationList(TJSVariableDeclarationList(el)) // Must be before binary WriteVarDeclarationList(TJSVariableDeclarationList(El)) // Must be before binary
else if (el is TJSStatementList) then else if (El is TJSStatementList) then
WriteStatementList(TJSStatementList(el)) // Must be before binary WriteStatementList(TJSStatementList(El)) // Must be before binary
else if (el is TJSWithStatement) then else if (El is TJSWithStatement) then
WriteWithStatement(TJSWithStatement(El)) // Must be before binary WriteWithStatement(TJSWithStatement(El)) // Must be before binary
else if (el is TJSBinary) then else if (El is TJSBinary) then
WriteBinary(TJSBinary(el)) WriteBinary(TJSBinary(El))
else if (el is TJSConditionalExpression) then else if (El is TJSConditionalExpression) then
WriteConditionalExpression(TJSConditionalExpression(el)) WriteConditionalExpression(TJSConditionalExpression(El))
else if (el is TJSAssignStatement) then else if (El is TJSAssignStatement) then
WriteAssignStatement(TJSAssignStatement(el)) WriteAssignStatement(TJSAssignStatement(El))
else if (el is TJSVarDeclaration) then else if (El is TJSVarDeclaration) then
WriteVarDeclaration(TJSVarDeclaration(el)) WriteVarDeclaration(TJSVarDeclaration(El))
else if (el is TJSIfStatement) then else if (El is TJSIfStatement) then
WriteIfStatement(TJSIfStatement(el)) WriteIfStatement(TJSIfStatement(El))
else if (el is TJSTargetStatement) then else if (El is TJSTargetStatement) then
WriteTargetStatement(TJSTargetStatement(el)) WriteTargetStatement(TJSTargetStatement(El))
else if (el is TJSReturnStatement) then else if (El is TJSReturnStatement) then
WriteReturnStatement(TJSReturnStatement(el)) WriteReturnStatement(TJSReturnStatement(El))
else if (el is TJSTryStatement) then else if (El is TJSTryStatement) then
WriteTryStatement(TJSTryStatement(el)) WriteTryStatement(TJSTryStatement(El))
else if (el is TJSFunctionDeclarationStatement) then else if (El is TJSFunctionDeclarationStatement) then
WriteFunctionDeclarationStatement(TJSFunctionDeclarationStatement(el)) WriteFunctionDeclarationStatement(TJSFunctionDeclarationStatement(El))
else if (el is TJSSourceElements) then else if (El is TJSSourceElements) then
WriteSourceElements(TJSSourceElements(el)) WriteSourceElements(TJSSourceElements(El))
else if EL=Nil then else if El=Nil then
Error(SErrNilNode) Error(SErrNilNode)
else else
Error(SErrUnknownJSClass,[El.ClassName]); Error(SErrUnknownJSClass,[El.ClassName]);
// Write('/* '+EL.ClassName+' */'); // Write('/* '+El.ClassName+' */');
FSkipBrackets:=False; FSkipBrackets:=False;
end; end;