From 910e5ff8882d36454bb94ae0b222be61b25d528e Mon Sep 17 00:00:00 2001 From: michael <michael@freepascal.org> Date: Thu, 3 Apr 2014 20:17:26 +0000 Subject: [PATCH] * Compact arrays/object literals and func. arguments git-svn-id: trunk@27457 - --- packages/fcl-js/src/jswriter.pp | 19 ++-- packages/fcl-js/tests/tcwriter.pp | 59 +++++++++++ packages/fcl-js/tests/testjs.lpi | 161 ++++++------------------------ 3 files changed, 104 insertions(+), 135 deletions(-) diff --git a/packages/fcl-js/src/jswriter.pp b/packages/fcl-js/src/jswriter.pp index 6f9a2b8d52..c893e48749 100644 --- a/packages/fcl-js/src/jswriter.pp +++ b/packages/fcl-js/src/jswriter.pp @@ -75,7 +75,10 @@ Type woUseUTF8, woTabIndent, woEmptyStatementAsComment, - woQuoteElementNames); + woQuoteElementNames, + woCompactArrayLiterals, + woCompactObjectLiterals, + woCompactArguments); TWriteOptions = Set of TWriteOption; TJSWriter = Class @@ -532,20 +535,24 @@ Var Var i,C : Integer; - WC : Boolean; + isArgs,WC : Boolean; BC : String[2]; + begin - BC:=Chars[El is TJSArguments]; + isArgs:=el is TJSArguments; + BC:=Chars[isArgs]; C:=EL.Elements.Count-1; if C=-1 then begin - if el is TJSArguments then + if isArgs then Write(bc) else Write(bc); Exit; end; - WC:=(woCompact in Options); + WC:=(woCompact in Options) or + ((Not isArgs) and (woCompactArrayLiterals in Options)) or + (isArgs and (woCompactArguments in Options)) ; if WC then Write(Copy(BC,1,1)) else @@ -584,7 +591,7 @@ begin Write('{}'); Exit; end; - WC:=(woCompact in Options); + WC:=(woCompact in Options) or (woCompactObjectLiterals in Options); if WC then Write('{') else diff --git a/packages/fcl-js/tests/tcwriter.pp b/packages/fcl-js/tests/tcwriter.pp index 710267d671..af53781636 100644 --- a/packages/fcl-js/tests/tcwriter.pp +++ b/packages/fcl-js/tests/tcwriter.pp @@ -63,6 +63,7 @@ type Procedure TestArrayOneElementIndent; Procedure TestArrayTwoElements; Procedure TestArrayTwoElementsCompact; + Procedure TestArrayTwoElementsCompact2; Procedure TestArrayThreeElementsCompact; Procedure TestObjectEmpty; Procedure TestObjectEmptyCompact; @@ -72,6 +73,7 @@ type Procedure TestObjectOneElementCompactQuoted; Procedure TestObjectTwoElements; Procedure TestObjectTwoElementCompact; + Procedure TestObjectTwoElementCompact2; Procedure TestObjectTwoElementCompactQuoted; Procedure TestObjectThreeElementsCompact; end; @@ -246,6 +248,7 @@ type Procedure TestNewMemberNoArgs; Procedure TestCall; Procedure TestCallCompact; + Procedure TestCallCompact2; Procedure TestCallNoArgs; Procedure TestConditional; Procedure TestRegularExpressionLiteral; @@ -680,6 +683,23 @@ begin AssertWrite('call a(123)','a(123)',U); end; +Procedure TTestExpressionWriter.TestCallCompact2; +Var + U : TJSCallExpression; + +begin + Writer.Options:=Writer.Options+[woCompactArguments]; + U:=TJSCallExpression.Create(0,0); + U.Expr:=CreateIdent('a'); + U.Args:=TJSArguments.Create(0,0); + U.Args.Elements.AddElement; + U.Args.Elements[0].Expr:=CreateLiteral(123); + U.Args.Elements.AddElement; + U.Args.Elements[1].Expr:=CreateLiteral(456); + AssertWrite('call a(123,456)','a(123, 456)',U); + +end; + Procedure TTestExpressionWriter.TestCallNoArgs; Var U : TJSCallExpression; @@ -2081,6 +2101,23 @@ begin AssertWrite('Empty array ','[1, 2]',L); end; +Procedure TTestLiteralWriter.TestArrayTwoElementsCompact2; +Var + L : TJSArrayLiteral; + I : TJSLiteral; + +begin + Writer.Options:=[woCompactArrayLiterals,woUseUTF8]; + L:=TJSArrayLiteral.Create(0,0); + I:=TJSLiteral.Create(0,0); + I.Value.AsNumber:=1; + L.Elements.AddElement.Expr:=I; + I:=TJSLiteral.Create(0,0); + I.Value.AsNumber:=2; + L.Elements.AddElement.Expr:=I; + AssertWrite('Empty array ','[1, 2]',L); +end; + Procedure TTestLiteralWriter.TestArrayThreeElementsCompact; Var L : TJSArrayLiteral; @@ -2234,6 +2271,28 @@ begin AssertWrite('Empty object ','{abc: 1, efg: 2}',L); end; +Procedure TTestLiteralWriter.TestObjectTwoElementCompact2; +Var + L : TJSObjectLiteral; + E : TJSObjectLiteralElement; + I : TJSLiteral; + +begin + L:=TJSObjectLiteral.Create(0,0); + E:=L.Elements.AddElement; + I:=TJSLiteral.Create(0,0); + I.Value.AsNumber:=1; + E.Expr:=I; + E.Name:='abc'; + E:=L.Elements.AddElement; + I:=TJSLiteral.Create(0,0); + I.Value.AsNumber:=2; + E.Expr:=I; + E.Name:='efg'; + Writer.Options:=[woCompactObjectLiterals,woUseUTF8]; + AssertWrite('Empty object ','{abc: 1, efg: 2}',L); +end; + Procedure TTestLiteralWriter.TestObjectTwoElementCompactQuoted; Var L : TJSObjectLiteral; diff --git a/packages/fcl-js/tests/testjs.lpi b/packages/fcl-js/tests/testjs.lpi index 922326ec89..8f5b6bda2b 100644 --- a/packages/fcl-js/tests/testjs.lpi +++ b/packages/fcl-js/tests/testjs.lpi @@ -43,7 +43,6 @@ <TopLine Value="1"/> <CursorPos X="48" Y="3"/> <UsageCount Value="201"/> - <LoadedDesigner Value="True"/> </Unit0> <Unit1> <Filename Value="tcscanner.pp"/> @@ -67,10 +66,10 @@ <Filename Value="../src/jsparser.pp"/> <IsPartOfProject Value="True"/> <UnitName Value="jsparser"/> - <EditorIndex Value="4"/> + <EditorIndex Value="3"/> <WindowIndex Value="1"/> - <TopLine Value="2032"/> - <CursorPos X="57" Y="2068"/> + <TopLine Value="67"/> + <CursorPos X="14" Y="85"/> <UsageCount Value="201"/> <Loaded Value="True"/> </Unit3> @@ -89,10 +88,10 @@ <Filename Value="../src/jstree.pp"/> <IsPartOfProject Value="True"/> <UnitName Value="jstree"/> - <EditorIndex Value="1"/> + <EditorIndex Value="5"/> <WindowIndex Value="1"/> - <TopLine Value="838"/> - <CursorPos X="3" Y="856"/> + <TopLine Value="739"/> + <CursorPos X="3" Y="757"/> <UsageCount Value="200"/> <Loaded Value="True"/> </Unit5> @@ -100,10 +99,10 @@ <Filename Value="tcparser.pp"/> <IsPartOfProject Value="True"/> <UnitName Value="tcparser"/> - <EditorIndex Value="5"/> + <EditorIndex Value="4"/> <WindowIndex Value="1"/> - <TopLine Value="1"/> - <CursorPos X="1" Y="1"/> + <TopLine Value="1878"/> + <CursorPos X="3" Y="1883"/> <UsageCount Value="201"/> <Loaded Value="True"/> </Unit6> @@ -113,8 +112,8 @@ <UnitName Value="jswriter"/> <EditorIndex Value="0"/> <WindowIndex Value="1"/> - <TopLine Value="90"/> - <CursorPos X="27" Y="120"/> + <TopLine Value="8"/> + <CursorPos X="28" Y="15"/> <UsageCount Value="202"/> <Loaded Value="True"/> </Unit7> @@ -133,17 +132,17 @@ <WindowIndex Value="1"/> <TopLine Value="157"/> <CursorPos X="1" Y="175"/> - <UsageCount Value="5"/> + <UsageCount Value="4"/> </Unit9> <Unit10> <Filename Value="tcwriter.pp"/> <IsPartOfProject Value="True"/> <UnitName Value="tcwriter"/> <IsVisibleTab Value="True"/> - <EditorIndex Value="3"/> + <EditorIndex Value="2"/> <WindowIndex Value="1"/> - <TopLine Value="1822"/> - <CursorPos X="3" Y="1841"/> + <TopLine Value="668"/> + <CursorPos X="45" Y="698"/> <UsageCount Value="220"/> <Loaded Value="True"/> </Unit10> @@ -153,16 +152,16 @@ <WindowIndex Value="1"/> <TopLine Value="558"/> <CursorPos X="21" Y="580"/> - <UsageCount Value="62"/> + <UsageCount Value="61"/> </Unit11> <Unit12> <Filename Value="../src/jstoken.pp"/> <IsPartOfProject Value="True"/> <UnitName Value="jstoken"/> - <EditorIndex Value="2"/> + <EditorIndex Value="1"/> <WindowIndex Value="1"/> - <TopLine Value="4"/> - <CursorPos X="82" Y="11"/> + <TopLine Value="1"/> + <CursorPos X="18" Y="8"/> <UsageCount Value="200"/> <Loaded Value="True"/> </Unit12> @@ -172,7 +171,7 @@ <WindowIndex Value="1"/> <TopLine Value="106"/> <CursorPos X="22" Y="108"/> - <UsageCount Value="14"/> + <UsageCount Value="13"/> </Unit13> <Unit14> <Filename Value="../../../rtl/tests/punit.pp"/> @@ -180,137 +179,41 @@ <WindowIndex Value="1"/> <TopLine Value="405"/> <CursorPos X="41" Y="415"/> - <UsageCount Value="19"/> + <UsageCount Value="18"/> </Unit14> <Unit15> <Filename Value="../../../../released/rtl/inc/mathh.inc"/> <WindowIndex Value="1"/> <TopLine Value="60"/> <CursorPos X="14" Y="78"/> - <UsageCount Value="14"/> + <UsageCount Value="13"/> </Unit15> </Units> - <JumpHistory Count="30" HistoryIndex="29"> + <JumpHistory Count="6" HistoryIndex="5"> <Position1> - <Filename Value="../src/jswriter.pp"/> - <Caret Line="1017" Column="16" TopLine="1000"/> + <Filename Value="tcparser.pp"/> + <Caret Line="1" Column="1" TopLine="1"/> </Position1> <Position2> - <Filename Value="../src/jswriter.pp"/> - <Caret Line="1024" Column="19" TopLine="1007"/> + <Filename Value="tcparser.pp"/> + <Caret Line="1732" Column="55" TopLine="1713"/> </Position2> <Position3> - <Filename Value="../src/jswriter.pp"/> - <Caret Line="1027" Column="24" TopLine="1010"/> + <Filename Value="tcparser.pp"/> + <Caret Line="1883" Column="3" TopLine="1878"/> </Position3> <Position4> <Filename Value="tcwriter.pp"/> - <Caret Line="161" Column="28" TopLine="142"/> + <Caret Line="66" Column="43" TopLine="51"/> </Position4> <Position5> <Filename Value="tcwriter.pp"/> - <Caret Line="1463" Column="39" TopLine="1450"/> + <Caret Line="76" Column="43" TopLine="48"/> </Position5> <Position6> <Filename Value="tcwriter.pp"/> - <Caret Line="1617" Column="110" TopLine="1598"/> + <Caret Line="251" Column="31" TopLine="232"/> </Position6> - <Position7> - <Filename Value="tcwriter.pp"/> - <Caret Line="162" Column="33" TopLine="143"/> - </Position7> - <Position8> - <Filename Value="tcwriter.pp"/> - <Caret Line="163" Column="29" TopLine="143"/> - </Position8> - <Position9> - <Filename Value="tcwriter.pp"/> - <Caret Line="164" Column="36" TopLine="145"/> - </Position9> - <Position10> - <Filename Value="tcwriter.pp"/> - <Caret Line="165" Column="34" TopLine="146"/> - </Position10> - <Position11> - <Filename Value="tcwriter.pp"/> - <Caret Line="1727" Column="184" TopLine="1709"/> - </Position11> - <Position12> - <Filename Value="tcwriter.pp"/> - <Caret Line="166" Column="41" TopLine="147"/> - </Position12> - <Position13> - <Filename Value="../src/jswriter.pp"/> - <Caret Line="1067" Column="1" TopLine="1064"/> - </Position13> - <Position14> - <Filename Value="../src/jswriter.pp"/> - <Caret Line="1082" Column="15" TopLine="1064"/> - </Position14> - <Position15> - <Filename Value="../src/jswriter.pp"/> - <Caret Line="1086" Column="29" TopLine="1070"/> - </Position15> - <Position16> - <Filename Value="tcwriter.pp"/> - <Caret Line="167" Column="1" TopLine="148"/> - </Position16> - <Position17> - <Filename Value="../src/jswriter.pp"/> - <Caret Line="721" Column="6" TopLine="697"/> - </Position17> - <Position18> - <Filename Value="tcwriter.pp"/> - <Caret Line="168" Column="30" TopLine="149"/> - </Position18> - <Position19> - <Filename Value="tcwriter.pp"/> - <Caret Line="1770" Column="20" TopLine="1766"/> - </Position19> - <Position20> - <Filename Value="../src/jswriter.pp"/> - <Caret Line="114" Column="20" TopLine="78"/> - </Position20> - <Position21> - <Filename Value="tcwriter.pp"/> - <Caret Line="73" Column="1" TopLine="73"/> - </Position21> - <Position22> - <Filename Value="tcwriter.pp"/> - <Caret Line="2329" Column="1" TopLine="2293"/> - </Position22> - <Position23> - <Filename Value="../src/jswriter.pp"/> - <Caret Line="1219" Column="6" TopLine="1212"/> - </Position23> - <Position24> - <Filename Value="../src/jswriter.pp"/> - <Caret Line="1" Column="1" TopLine="1"/> - </Position24> - <Position25> - <Filename Value="../src/jswriter.pp"/> - <Caret Line="120" Column="27" TopLine="90"/> - </Position25> - <Position26> - <Filename Value="../src/jsparser.pp"/> - <Caret Line="1771" Column="21" TopLine="1746"/> - </Position26> - <Position27> - <Filename Value="../src/jsparser.pp"/> - <Caret Line="1" Column="1" TopLine="1"/> - </Position27> - <Position28> - <Filename Value="../src/jsparser.pp"/> - <Caret Line="103" Column="51" TopLine="73"/> - </Position28> - <Position29> - <Filename Value="tcwriter.pp"/> - <Caret Line="170" Column="40" TopLine="145"/> - </Position29> - <Position30> - <Filename Value="tcwriter.pp"/> - <Caret Line="1808" Column="24" TopLine="1789"/> - </Position30> </JumpHistory> </ProjectOptions> <CompilerOptions>