Browse Source

fcl-js: fixed writing - - and + +

git-svn-id: trunk@38106 -
Mattias Gaertner 7 years ago
parent
commit
d9a878b0da
2 changed files with 60 additions and 4 deletions
  1. 31 4
      packages/fcl-js/src/jswriter.pp
  2. 29 0
      packages/pastojs/tests/tcmodules.pas

+ 31 - 4
packages/fcl-js/src/jswriter.pp

@@ -123,6 +123,7 @@ Type
     FFreeWriter : Boolean;
     FIndentChar : Char;
     FIndentSize: Byte;
+    FLastChar: WideChar;
     FLinePos : Integer;
     FOptions: TWriteOptions;
     FSkipCurlyBrackets : Boolean;
@@ -186,6 +187,7 @@ Type
     Property Options : TWriteOptions Read FOptions Write SetOptions;
     Property IndentSize : Byte Read FIndentSize Write FIndentSize;
     Property UseUTF8 : Boolean Read GetUseUTF8;
+    property LastChar: WideChar read FLastChar;
   end;
   EJSWriter = Class(Exception);
 
@@ -349,8 +351,11 @@ end;
 procedure TJSWriter.WriteIndent;
 
 begin
-  If (FLinePos=0) then
+  If (FLinePos=0) and (FCurIndent>0) then
+    begin
     FLinePos:=Writer.Write(StringOfChar(FIndentChar,FCurIndent));
+    FLastChar:=WideChar(FIndentChar);
+    end;
 end;
 
 procedure TJSWriter.Indent;
@@ -376,10 +381,15 @@ begin
   if UseUTF8 then
     begin
     S:=UTF16ToUTF8(U);
+    if S='' then exit;
     FLinePos:=FLinePos+Writer.Write(S);
+    FLastChar:=WideChar(S[length(S)]);
     end
-  else
+  else if U<>'' then
+    begin
     FLinePos:=FLinePos+Writer.Write(U);
+    FLastChar:=U[length(U)];
+    end;
 end;
 
 procedure TJSWriter.Write(const S: AnsiString);
@@ -389,7 +399,9 @@ begin
   else
     begin
     WriteIndent;
+    if s='' then exit;
     FLinePos:=FLinePos+Writer.Write(S);
+    FLastChar:=WideChar(S[length(S)]);
     end;
 end;
 
@@ -401,6 +413,7 @@ begin
     begin
     WriteIndent;
     Writer.WriteLn(S);
+    FLastChar:=WideChar(#10);
     FLinePos:=0;
     end;
 end;
@@ -419,6 +432,8 @@ begin
     begin
     WriteIndent;
     FLinePos:=FLinePos+Writer.Write(U);
+    Writer.WriteLn('');
+    FLastChar:=WideChar(#10);
     FLinePos:=0;
     end;
 end;
@@ -571,6 +586,11 @@ begin
     jstReference : ;
     JSTCompletion : ;
   end;
+  if S='' then exit;
+  case S[1] of
+  '+': if FLastChar='+' then Write(' ');
+  '-': if FLastChar='-' then Write(' ');
+  end;
   Write(S);
 end;
 
@@ -868,15 +888,19 @@ begin
 end;
 
 procedure TJSWriter.WriteUnary(El: TJSUnary);
-
 Var
   S : String;
-
 begin
   FSkipRoundBrackets:=false;
   S:=El.PreFixOperator;
   if (S<>'') then
+    begin
+    case S[1] of
+    '+': if FLastChar='+' then Write(' ');
+    '-': if FLastChar='-' then Write(' ');
+    end;
     Write(S);
+    end;
   WriteJS(El.A);
   if (S='') then
     begin
@@ -884,6 +908,9 @@ begin
     if (S<>'') then
       begin
       Writer.CurElement:=El;
+      if ((S='-') and (FLastChar='-'))
+          or ((S='+') and (FLastChar='+')) then
+        Write(' ');
       Write(S);
       end;
     end;

+ 29 - 0
packages/pastojs/tests/tcmodules.pas

@@ -205,6 +205,7 @@ type
 
     // numbers
     Procedure TestDouble;
+    Procedure TestInteger;
     Procedure TestIntegerRange;
     Procedure TestForBoolDo;
     Procedure TestForIntDo;
@@ -4423,6 +4424,34 @@ begin
     '']));
 end;
 
+procedure TTestModule.TestInteger;
+begin
+  StartProgram(false);
+  Add([
+  'const',
+  '  MinInt = low(NativeInt);',
+  '  MaxInt = high(NativeInt);',
+  'type',
+  '  {#TMyInt}TMyInt = MinInt..MaxInt;',
+  'const',
+  '  a = low(TMyInt)+High(TMyInt);',
+  'var',
+  '  i: TMyInt;',
+  'begin',
+  '  i:=-MinInt;']);
+  ConvertProgram;
+  CheckSource('TestIntegerRange',
+    LinesToStr([
+    'this.MinInt = -4503599627370496;',
+    'this.MaxInt = 4503599627370495;',
+    'this.a = -4503599627370496 + 4503599627370495;',
+    'this.i = -4503599627370496;',
+    '']),
+    LinesToStr([
+    '$mod.i = - -4503599627370496;',
+    '']));
+end;
+
 procedure TTestModule.TestIntegerRange;
 begin
   StartProgram(false);