hjsmin 0.0.4 → 0.0.5
raw patch · 6 files changed
+181/−212 lines, 6 filesdep +language-javascriptPVP ok
version bump matches the API change (PVP)
Dependencies added: language-javascript
API changes (from Hackage documentation)
Files
- TODO.txt +18/−0
- Text/Jasmine.hs +20/−7
- Text/Jasmine/Parse.hs +6/−1
- Text/Jasmine/Pretty.hs +83/−167
- hjsmin.cabal +9/−7
- runtests.hs +45/−30
TODO.txt view
@@ -63,5 +63,23 @@ ------------ +Integrating language-haskell.+-----------------------------++Baseline before starting, on my laptop (intel Core2 Duo SU7300)++$ time ./dist/build/runtests/runtests++real 0m1.625s+user 0m1.616s+sys 0m0.016s++Baseline 2010-12-20, after getting all tests to pass at last++real 0m0.209s+user 0m0.196s+sys 0m0.004s++ EOF
Text/Jasmine.hs view
@@ -6,26 +6,29 @@ , minifyFile ) where -import Text.Jasmine.Parse+--import Text.Jasmine.Parse+import Language.JavaScript.Parser import Text.Jasmine.Pretty import qualified Blaze.ByteString.Builder as BB import qualified Data.ByteString.Lazy as LB+import qualified Data.ByteString.Lazy.Char8 as S8 minifym :: LB.ByteString -> Either String LB.ByteString-minifym s = case readJsm s of- Left msg -> Left msg+minifym s = case parse' s of+ Left msg -> Left (show msg) Right p -> Right $ BB.toLazyByteString $ renderJS p minifyBb :: LB.ByteString -> Either String BB.Builder-minifyBb s = case readJsm s of- Left msg -> Left msg+minifyBb s = case parse' s of+ Left msg -> Left (show msg) Right p -> Right (renderJS p) minify :: LB.ByteString -> LB.ByteString-minify s = BB.toLazyByteString $ renderJS $ readJs s+--minify s = BB.toLazyByteString $ renderJS $ readJs s+minify s = BB.toLazyByteString $ renderJS $ readJs (lbToStr s) _minify' :: LB.ByteString -> BB.Builder-_minify' s = renderJS $ readJs s+_minify' s = renderJS $ readJs (lbToStr s) minifyFile :: FilePath -> IO LB.ByteString minifyFile filename =@@ -33,4 +36,14 @@ x <- LB.readFile (filename) return $ minify x +parse' :: S8.ByteString -> Either ParseError JSNode+parse' input = parse (lbToStr input) "src"++lbToStr :: S8.ByteString -> [Char]+lbToStr str = S8.unpack str++strToLb :: String -> S8.ByteString+strToLb str = S8.pack str+--strToLb str = (LB.fromChunks [(E.encodeUtf8 $ T.pack str)])+ -- EOF
Text/Jasmine/Parse.hs view
@@ -9,6 +9,8 @@ , parseFile , parseString -- For testing + , parseProgram + {- , doParse , program , functionDeclaration@@ -16,6 +18,7 @@ , statementList , iterationStatement , main + -} ) where -- ---------------------------------------------------------------------@@ -193,7 +196,6 @@ firstchar :: Parser [Char] firstchar = do { c <- satisfy (\c -> isPrint c && c /= '*' && c /= '\\' && c /= '/'); return [c]} <|> escapeseq- escapeseq :: Parser [Char] escapeseq = do { _ <- char '\\'; c <- satisfy (\cc -> isPrint cc); return ['\\',c]}@@ -1156,6 +1158,9 @@ parseString :: Parser r -> String -> Result r parseString p input = doParse p (LB.fromChunks [E.encodeUtf8 $ T.pack input])++parseProgram :: String -> Result JSNode+parseProgram input = doParse program (LB.fromChunks [E.encodeUtf8 $ T.pack input]) -- ---------------------------------------------------------------------
Text/Jasmine/Pretty.hs view
@@ -6,7 +6,8 @@ import Data.Char import Data.List import Data.Monoid-import Text.Jasmine.Parse+-- import Text.Jasmine.Parse+import Language.JavaScript.Parser import qualified Blaze.ByteString.Builder as BB import qualified Blaze.ByteString.Builder.Char.Utf8 as BS import qualified Data.ByteString.Lazy as LB@@ -44,7 +45,7 @@ renderJS :: JSNode -> BB.Builder renderJS (JSEmpty l) = (renderJS l) renderJS (JSIdentifier s) = text s-renderJS (JSDecimal i) = text $ show i+renderJS (JSDecimal i) = text i renderJS (JSOperator s) = text s renderJS (JSExpression xs) = rJS xs renderJS (JSSourceElements xs) = rJS (map fixBlock $ fixSourceElements xs)@@ -64,8 +65,8 @@ <> (text "else") renderJS (JSIfElse c t e) = (text "if") <> (text "(") <> (renderJS c) <> (text ")") <> (renderJS t) <> (text "else") <> (spaceOrBlock $ fixBlock e)-renderJS (JSMemberDot xs) = (text ".") <> (rJS xs)-renderJS (JSMemberSquare x xs) = (text "[") <> (renderJS x) <> (text "]") <> (rJS xs)+renderJS (JSMemberDot xs y) = (rJS xs) <> (text ".") <> (renderJS y)+renderJS (JSMemberSquare xs x) = (rJS xs) <> (text "[") <> (renderJS x) <> (text "]") renderJS (JSLiteral l) = (text l) renderJS (JSStringLiteral s l) = empty <> (char s) <> (text l) <> (char s) renderJS (JSUnary l ) = text l@@ -92,8 +93,8 @@ renderJS (JSDoWhile s e _ms) = (text "do") <> (renderJS s) <> (text "while") <> (char '(') <> (renderJS e) <> (char ')') -- <> (renderJS ms) renderJS (JSElementList xs) = rJS xs renderJS (JSElision xs) = (char ',') <> (rJS xs)---renderJS (JSExpressionBinary o e1 e2) = (rJS e1) <> (text o) <> (rJS e2)-renderJS (JSExpressionBinary o e1 e2) = (text o) <> (rJS e1) <> (rJS e2)+renderJS (JSExpressionBinary o e1 e2) = (rJS e1) <> (text o) <> (rJS e2)+--renderJS (JSExpressionBinary o e1 e2) = (text o) <> (rJS e1) <> (rJS e2) renderJS (JSExpressionParen e) = (char '(') <> (renderJS e) <> (char ')') renderJS (JSExpressionPostfix o e) = (rJS e) <> (text o) renderJS (JSExpressionTernary c v1 v2) = (rJS c) <> (char '?') <> (rJS v1) <> (char ':') <> (rJS v2)@@ -181,6 +182,10 @@ | t1 == t2 = myFix ((JSVariables t1 (x1s++x2s)):xs) | otherwise = (JSVariables t1 x1s):myFix ((JSLiteral l):(JSVariables t2 x2s):xs) +myFix ((JSVariables t1 x1s):(JSVariables t2 x2s):xs) + | t1 == t2 = myFix ((JSVariables t1 (x1s++x2s)):xs)+ | otherwise = (JSVariables t1 x1s):myFix ((JSVariables t2 x2s):xs)+ -- Merge adjacent semi colons myFix ((JSLiteral ";"):(JSLiteral ";"):xs) = myFix ((JSLiteral ";"):xs) myFix ((JSLiteral ";"):(JSLiteral "" ):xs) = myFix ((JSLiteral ""):xs)@@ -190,11 +195,16 @@ -- Merge strings split over lines and using "+" fixLiterals :: [JSNode] -> [JSNode] fixLiterals [] = []-fixLiterals [x] = [x]+-- Old version fixLiterals ((JSStringLiteral d1 s1):(JSExpressionBinary "+" [JSStringLiteral d2 s2] r):xs) | d1 == d2 = fixLiterals ((JSStringLiteral d1 (s1++s2)):(r++xs)) | otherwise = (JSStringLiteral d1 s1):fixLiterals ((JSExpressionBinary "+" [JSStringLiteral d2 s2] r):xs) +fixLiterals ((JSExpressionBinary "+" [JSStringLiteral d1 s1] [JSStringLiteral d2 s2]):xs)+ | d1 == d2 = fixLiterals ((JSStringLiteral d1 (s1++s2)):xs)+ | otherwise = (JSExpressionBinary "+" [JSStringLiteral d1 s1] [JSStringLiteral d2 s2]):fixLiterals xs ++ fixLiterals (x:xs) = x:fixLiterals xs -- Sort out Semicolons@@ -238,7 +248,7 @@ _case0 :: JSNode _case0 = JSSourceElements [- JSExpression [JSElement "assignmentExpression" [JSIdentifier "x",JSOperator "=",JSDecimal 1]],+ JSExpression [JSElement "assignmentExpression" [JSIdentifier "x",JSOperator "=",JSDecimal "1"]], JSEmpty (JSLiteral ";") ] @@ -246,20 +256,11 @@ _case1 :: [JSNode] _case1 = [JSExpression [JSElement "assignmentExpression" - [JSIdentifier "a",JSOperator "=",JSDecimal 1]+ [JSIdentifier "a",JSOperator "=",JSDecimal "1"] ] ,JSEmpty (JSLiteral ";") ] -_case2 :: JSNode -_case2 = JSFunctionExpression [] (JSFunctionBody - [JSSourceElements - [JSReturn [JSExpression - [JSLiteral "this",JSMemberDot [JSIdentifier "name"]],- JSLiteral ""]]]- )- -- ]],JSEmpty (JSLiteral ";") - -- doParse expression "opTypeNames={'\\n':\"NEWLINE\",';':\"SEMICOLON\",',':\"COMMA\"}" _case4 :: JSNode@@ -292,7 +293,7 @@ JSExpressionBinary "!=" [JSStringLiteral '"' "string"] []]) (JSReturn [JSExpression [JSIdentifier "s"],JSLiteral ";"]) ,JSLiteral ";"- ,JSExpression [JSElement "assignmentExpression" [JSIdentifier "a",JSOperator "=",JSDecimal 1]]+ ,JSExpression [JSElement "assignmentExpression" [JSIdentifier "a",JSOperator "=",JSDecimal "1"]] ] ] )@@ -313,7 +314,7 @@ (JSExpression [JSUnary "typeof ",JSIdentifier "s",JSExpressionBinary "!=" [JSStringLiteral '"' "string"] []]) (JSReturn [JSExpression [JSIdentifier "s"],JSLiteral ";"]) ,- JSExpression [JSIdentifier "evaluate",JSArguments [[JSDecimal 1]]]+ JSExpression [JSIdentifier "evaluate",JSArguments [[JSDecimal "1"]]] ] ] )@@ -321,51 +322,12 @@ --doParse program "for(i=0,j=assignOps.length;i<j;i++){}" _case8 :: JSNode-_case8 = JSSourceElements - [- JSFor - [JSExpression - [JSElement "assignmentExpression" - [JSIdentifier "i",JSOperator "=",JSDecimal 0],- JSElement "assignmentExpression" - [JSIdentifier "j",JSOperator "=",JSIdentifier "assignOps",JSMemberDot [JSIdentifier "length"]]- ]- ] - - [JSExpression - [JSIdentifier "i",JSExpressionBinary "<" [JSIdentifier "j"] []]- ] - - [JSExpression [JSExpressionPostfix "++" [JSIdentifier "i"]]] - - (JSLiteral ";")- ] +_case8 = undefined -_case01_semi1 :: JSNode-_case01_semi1 = JSSourceElements - [- JSBlock (JSStatementList - [- JSExpression [JSIdentifier "zero",JSMemberDot [JSIdentifier "one"]],- JSLiteral ";",- JSExpression [JSIdentifier "zero"]- ]),- JSExpression [JSIdentifier "one"],- JSExpression [JSIdentifier "two"],- JSLiteral ";",- JSExpression [JSIdentifier "three"],- JSLiteral ";",- JSExpression [JSIdentifier "four"],- JSLiteral ";",- JSExpression [JSIdentifier "five"]- ] -- doParse returnStatement "return this.name;" _case9 :: JSNode-_case9 = JSReturn [JSExpression [JSLiteral "this",JSMemberDot [JSIdentifier "name"]],JSLiteral ";"] --_case9a :: [JSNode]-_case9a = [JSExpression [JSLiteral "this",JSMemberDot [JSIdentifier "name"]],JSLiteral ";"]+_case9 = undefined --parseFile "./test/parsingonly/02_sm.js" {-@@ -413,106 +375,45 @@ _case11 :: JSNode _case11 = JSSourceElements [- JSExpression [JSIdentifier "a",JSExpressionBinary "+" [JSDecimal 1] []],+ JSExpression [JSIdentifier "a",JSExpressionBinary "+" [JSDecimal "1"] []], JSLiteral ";", JSLiteral ";" ] --doParse program "var newlines=spaces.match(/\\n/g);var newlines=spaces.match(/\\n/g);" _case12 :: JSNode-_case12 = JSSourceElements - [- JSVariables "var" [JSVarDecl (JSIdentifier "newlines") - [JSIdentifier "spaces",JSMemberDot [JSIdentifier "match"],- JSArguments [[JSRegEx "/\\n/g"]]]],- JSLiteral ";",- JSVariables "var" [JSVarDecl (JSIdentifier "newlines") - [JSIdentifier "spaces",JSMemberDot [JSIdentifier "match"],- JSArguments [[JSRegEx "/\\n/g"]]]],- JSLiteral ";"- ] +_case12 = undefined --doParse program "for(i=0;;){var t=1};for(var i=0,j=1;;){x=1}" _case13 :: JSNode _case13 = JSSourceElements [- JSFor [JSExpression [JSElement "assignmentExpression" [JSIdentifier "i",JSOperator "=",JSDecimal 0]]] + JSFor [JSExpression [JSElement "assignmentExpression" [JSIdentifier "i",JSOperator "=",JSDecimal "0"]]] [] [] (JSBlock (- JSStatementList [JSVariables "var" [JSVarDecl (JSIdentifier "t") [JSDecimal 1]]]+ JSStatementList [JSVariables "var" [JSVarDecl (JSIdentifier "t") [JSDecimal "1"]]] ) ), JSLiteral ";",- JSForVar [JSVarDecl (JSIdentifier "i") [JSDecimal 0],JSVarDecl (JSIdentifier "j") [JSDecimal 1]] + JSForVar [JSVarDecl (JSIdentifier "i") [JSDecimal "0"],JSVarDecl (JSIdentifier "j") [JSDecimal "1"]] [] [] (JSBlock (- JSStatementList [JSExpression [JSElement "assignmentExpression" [JSIdentifier "x",JSOperator "=",JSDecimal 1]]]+ JSStatementList [JSExpression [JSElement "assignmentExpression" [JSIdentifier "x",JSOperator "=",JSDecimal "1"]]] ) ) ] -- doParse program "if (/^[a-z]/.test(t)) {consts += t.toUpperCase();keywords[t] = i;} else {consts += (/^\\W/.test(t) ? opTypeNames[t] : t);}consts += \" = \" + x;" _case14 :: JSNode-_case14 = JSSourceElements - [- JSIfElse - (JSExpression [JSRegEx "/^[a-z]/",JSMemberDot [JSIdentifier "test"],JSArguments [[JSIdentifier "t"]]]) - (JSBlock (JSStatementList - [- JSExpression [- JSElement "assignmentExpression" - [JSIdentifier "consts",JSOperator "+=",JSIdentifier "t",- JSMemberDot [JSIdentifier "toUpperCase"],JSArguments [[]]]- ],- JSLiteral ";",- JSExpression [- JSElement "assignmentExpression" [JSIdentifier "keywords",- JSMemberSquare (JSExpression [JSIdentifier "t"]) [],- JSOperator "=",JSIdentifier "i"]],- JSLiteral ""])) - (JSBlock (JSStatementList - [- JSExpression - [- JSElement "assignmentExpression" - [- JSIdentifier "consts",JSOperator "+=",- JSExpressionParen - (- JSExpression - [- JSExpressionTernary - [- JSRegEx "/^\\W/",- JSMemberDot [JSIdentifier "test"],- JSArguments [[JSIdentifier "t"]]- ] - [- JSIdentifier "opTypeNames",- JSMemberSquare (JSExpression [JSIdentifier "t"]) - []- ] - [JSIdentifier "t"]- ]- )- ]- ],- JSLiteral ""- ]- )- ),- JSExpression [JSElement "assignmentExpression" - [JSIdentifier "consts",JSOperator "+=",JSStringLiteral '"' " = ",- JSExpressionBinary "+" [JSIdentifier "x"] []]],- JSLiteral ";"] +_case14 = undefined -- doParse program "a+1;{}" _case15 :: JSNode _case15 = JSSourceElements [- JSExpression [JSIdentifier "a",JSExpressionBinary "+" [JSDecimal 1] []],+ JSExpression [JSIdentifier "a",JSExpressionBinary "+" [JSDecimal "1"] []], JSLiteral ";", JSLiteral ";" ]@@ -522,29 +423,23 @@ _case16 :: JSNode _case16 = JSSourceElementsTop [- JSFor [JSExpression [JSElement "assignmentExpression" [JSIdentifier "i",JSOperator "=",JSDecimal 0]]] [] [] + JSFor [JSExpression [JSElement "assignmentExpression" [JSIdentifier "i",JSOperator "=",JSDecimal "0"]]] [] [] (JSBlock (JSStatementList [- JSVariables "var" [JSVarDecl (JSIdentifier "t") [JSDecimal 1]],+ JSVariables "var" [JSVarDecl (JSIdentifier "t") [JSDecimal "1"]], JSLiteral ";", JSLiteral "" ] ) ),- JSExpression [JSElement "assignmentExpression" [JSIdentifier "x",JSOperator "=",JSDecimal 1]],+ JSExpression [JSElement "assignmentExpression" [JSIdentifier "x",JSOperator "=",JSDecimal "1"]], JSLiteral ";" ] -- doParse program "return new global.Boolean(v);" _case17 :: JSNode-_case17 = JSSourceElementsTop - [- JSReturn - [- JSExpression [JSLiteral "new ",JSIdentifier "global",JSMemberDot [JSIdentifier "Boolean"],JSArguments [[JSIdentifier "v"]]],- JSLiteral ";"]- ]+_case17 = undefined --doParse program "if(typeof s!=\"string\")return;while(--n>=0)s+=t;" _case18 :: JSNode@@ -553,7 +448,7 @@ JSIf (JSExpression [JSUnary "typeof ",JSIdentifier "s",JSExpressionBinary "!=" [JSStringLiteral '"' "string"] []]) (JSReturn [JSLiteral ";"]),- JSWhile (JSExpression [JSUnary "--",JSIdentifier "n",JSExpressionBinary ">=" [JSDecimal 0] []]) + JSWhile (JSExpression [JSUnary "--",JSIdentifier "n",JSExpressionBinary ">=" [JSDecimal "0"] []]) (JSExpression [ JSElement "assignmentExpression" @@ -576,7 +471,7 @@ JSExpression [JSElement "assignmentExpression" [- JSIdentifier "x",JSOperator "=",JSDecimal 1+ JSIdentifier "x",JSOperator "=",JSDecimal "1" ] ] ]@@ -589,7 +484,7 @@ _case20 = [ JSReturn [JSExpression [JSIdentifier "n"],JSLiteral ";"], JSExpression - [JSElement "assignmentExpression" [JSIdentifier "x",JSOperator "=",JSDecimal 1]]+ [JSElement "assignmentExpression" [JSIdentifier "x",JSOperator "=",JSDecimal "1"]] ] --doParse program "if(!u)continue;t=n"@@ -603,30 +498,7 @@ --doParse program "if (!v.base){throw new ReferenceError(v.propertyName + \" is not defined\");};x=1" _case22 :: JSNode-_case22 = JSSourceElementsTop - [- JSIf (JSExpression [JSUnary "!",JSIdentifier "v",JSMemberDot [JSIdentifier "base"]]) - (JSBlock (JSStatementList - [- JSThrow (JSExpression - [- JSLiteral "new ",- JSIdentifier "ReferenceError",- JSArguments - [- [- JSIdentifier "v",- JSMemberDot [JSIdentifier "propertyName"],- JSExpressionBinary "+" [JSStringLiteral '"' " is not defined"] []- ]- ]- ]- ),- JSLiteral ""- ]- )- ),- JSLiteral ";",JSExpression [JSElement "assignmentExpression" [JSIdentifier "x",JSOperator "=",JSDecimal 1]]] +_case22 = undefined --parseString program "{throw new TypeError(\"Function.prototype.apply called on\"+\n\" uncallable object\");}" _case23 :: JSNode@@ -697,5 +569,49 @@ ] ] +-- readJs "var newlines=spaces.match(/\\n/g);var newlines=spaces.match(/\\n/g);"+_case26 :: JSNode+_case26 = JSSourceElementsTop + [+ JSVariables "var" + [+ JSVarDecl (JSIdentifier "newlines") + [JSMemberDot [JSIdentifier "spaces"] (JSIdentifier "match"),JSArguments [[JSRegEx "/\\n/g"]]]+ ],+ JSVariables "var" + [+ JSVarDecl (JSIdentifier "newlines") [JSMemberDot [JSIdentifier "spaces"] (JSIdentifier "match"),+ JSArguments [[JSRegEx "/\\n/g"]]]+ ]+ ]+ +-- readJs "\"mary\"+\"had\""+_case27 :: JSNode+_case27 = JSSourceElementsTop + [+ JSExpression [JSExpressionBinary "+" [JSStringLiteral '"' "mary"] [JSStringLiteral '"' "had"]]+ ]+ +-- readJs "throw new TypeError(\"Function.prototype.apply called on\"+\" uncallable object\")"+_case28 :: JSNode+_case28 = JSSourceElementsTop + [+ JSThrow + (+ JSExpression + [+ JSLiteral "new ",+ JSIdentifier "TypeError",+ JSArguments + [+ [+ JSExpressionBinary "+" + [JSStringLiteral '"' "Function.prototype.apply called on"] + [JSStringLiteral '"' " uncallable object"]+ ]+ ]+ ]+ )+ ] -- EOF
hjsmin.cabal view
@@ -1,5 +1,5 @@ name: hjsmin-version: 0.0.4+version: 0.0.5 license: BSD3 license-file: LICENSE author: Alan Zimmerman <alan.zimm@gmail.com>@@ -24,12 +24,13 @@ default: False library- build-depends: base >= 4 && < 5- , bytestring >= 0.9 && < 0.10- , attoparsec >= 0.8 && < 0.9- , blaze-builder >= 0.2 && < 1- , text >= 0.8 && < 1- , containers >= 0.2 && < 0.5+ build-depends: base >= 4 && < 5+ , bytestring >= 0.9 && < 0.10+ , attoparsec >= 0.8 && < 0.9+ , blaze-builder >= 0.2 && < 1+ , text >= 0.8 && < 1+ , containers >= 0.2 && < 0.5+ , language-javascript >= 0.0 && < 0.5 exposed-modules: Text.Jasmine other-modules: Text.Jasmine.Parse, Text.Jasmine.Pretty, Text.Jasmine.Token ghc-options: -Wall@@ -42,6 +43,7 @@ HUnit, test-framework-hunit, test-framework+ else Buildable: False main-is: runtests.hs
runtests.hs view
@@ -5,7 +5,8 @@ import Data.Char import Text.Jasmine-import Text.Jasmine.Parse hiding (main)+--import Text.Jasmine.Parse -- hiding (main)+import Language.JavaScript.Parser import Text.Jasmine.Pretty import qualified Data.ByteString.Lazy as LB import qualified Data.Text as T@@ -28,7 +29,7 @@ , testCase "0_f.js" case0_f , testCase "01_semi1.js" case01_semi1 , testCase "min_100_animals" case_min_100_animals- , testCase "nestedSquare" caseNestedSquare+ , testCase "mergeStrings" caseMergeStrings ] testSuiteMin :: Test@@ -42,6 +43,7 @@ , testCase "01_semi1.js" caseMin01_semi1 , testCase "min_100_animals" caseMin_min_100_animals , testCase "minNestedSquare" caseMinNestedSquare+ , testCase "minMergeStrings" caseMinMergeStrings , testCase "EitherLeft" caseEitherLeft , testCase "EitherRight" caseEitherRight ]@@ -112,60 +114,63 @@ srcHelloWorld = "function Hello(a) {}" caseHelloWorld = - "Done Empty JSFunction (JSIdentifier \"Hello\") [JSIdentifier \"a\"] (JSFunctionBody [])"- @=? (show $ parseString functionDeclaration srcHelloWorld)+ "Right (JSSourceElementsTop [JSFunction (JSIdentifier \"Hello\") [JSIdentifier \"a\"] (JSFunctionBody [])])"+ @=? (show $ parseProgram srcHelloWorld) caseMinHelloWorld = -- "function Hello(a){}" @=? (minify (U.fromString srcHelloWorld)) testMinify "function Hello(a){}" srcHelloWorld srcHelloWorld2 = "function Hello(a) {b=1}" caseHelloWorld2 = - "Done Empty JSFunction (JSIdentifier \"Hello\") [JSIdentifier \"a\"] (JSFunctionBody [JSSourceElements [JSExpression [JSElement \"assignmentExpression\" [JSIdentifier \"b\",JSOperator \"=\",JSDecimal 1]]]])"- @=? (show $ parseString functionDeclaration srcHelloWorld2)+ "Right (JSSourceElementsTop [JSFunction (JSIdentifier \"Hello\") [JSIdentifier \"a\"] (JSFunctionBody [JSSourceElements [JSExpression [JSElement \"assignmentExpression\" [JSIdentifier \"b\",JSOperator \"=\",JSDecimal \"1\"]]]])])"+ @=? (show $ parseProgram srcHelloWorld2) caseMinHelloWorld2 = -- "function Hello(a){b=1}" @=? (minify (U.fromString srcHelloWorld2)) testMinify "function Hello(a){b=1}" srcHelloWorld2 srcSimpleAssignment = "a=1;" caseSimpleAssignment = - "Done Empty JSStatementList [JSExpression [JSElement \"assignmentExpression\" [JSIdentifier \"a\",JSOperator \"=\",JSDecimal 1]],JSLiteral \";\"]"- @=? (show $ parseString statementList srcSimpleAssignment)+ "Right (JSSourceElementsTop [JSExpression [JSElement \"assignmentExpression\" [JSIdentifier \"a\",JSOperator \"=\",JSDecimal \"1\"]],JSLiteral \";\"])"+ @=? (show $ parseProgram srcSimpleAssignment) caseMinSimpleAssignment = testMinify "a=1" srcSimpleAssignment srcEmptyFor = "for (i = 0;;){}" caseEmptyFor =- "Done Empty JSFor [JSExpression [JSElement \"assignmentExpression\" [JSIdentifier \"i\",JSOperator \"=\",JSDecimal 0]]] [] [] (JSLiteral \";\")"- @=? (show $ parseString iterationStatement srcEmptyFor) + "Right (JSSourceElementsTop [JSFor [JSExpression [JSElement \"assignmentExpression\" [JSIdentifier \"i\",JSOperator \"=\",JSDecimal \"0\"]]] [] [] (JSLiteral \";\")])"+ @=? (show $ parseProgram srcEmptyFor) srcFullFor = "for (i = 0;i<10;i++){}" caseFullFor =- "Done Empty JSFor [JSExpression [JSElement \"assignmentExpression\" [JSIdentifier \"i\",JSOperator \"=\",JSDecimal 0]]] [JSExpression [JSIdentifier \"i\",JSExpressionBinary \"<\" [JSDecimal 10] []]] [JSExpression [JSExpressionPostfix \"++\" [JSIdentifier \"i\"]]] (JSLiteral \";\")"- @=? (show $ parseString iterationStatement srcFullFor)+ "Right (JSSourceElementsTop [JSFor [JSExpression [JSElement \"assignmentExpression\" [JSIdentifier \"i\",JSOperator \"=\",JSDecimal \"0\"]]] [JSExpression [JSExpressionBinary \"<\" [JSIdentifier \"i\"] [JSDecimal \"10\"]]] [JSExpression [JSExpressionPostfix \"++\" [JSIdentifier \"i\"]]] (JSLiteral \";\")])"+ @=? (show $ parseProgram srcFullFor) srcForVarFull = "for(var i=0,j=tokens.length;i<j;i++){}" caseForVarFull =- "Done Empty JSForVar [JSVarDecl (JSIdentifier \"i\") [JSDecimal 0],JSVarDecl (JSIdentifier \"j\") [JSIdentifier \"tokens\",JSMemberDot [JSIdentifier \"length\"]]] [JSExpression [JSIdentifier \"i\",JSExpressionBinary \"<\" [JSIdentifier \"j\"] []]] [JSExpression [JSExpressionPostfix \"++\" [JSIdentifier \"i\"]]] (JSLiteral \";\")"- @=? (show $ parseString iterationStatement srcForVarFull)+ "Right (JSSourceElementsTop [JSForVar [JSVarDecl (JSIdentifier \"i\") [JSDecimal \"0\"],JSVarDecl (JSIdentifier \"j\") [JSMemberDot [JSIdentifier \"tokens\"] (JSIdentifier \"length\")]] [JSExpression [JSExpressionBinary \"<\" [JSIdentifier \"i\"] [JSIdentifier \"j\"]]] [JSExpression [JSExpressionPostfix \"++\" [JSIdentifier \"i\"]]] (JSLiteral \";\")])"+ @=? (show $ parseProgram srcForVarFull) srcIfElse1 = "if(a){b=1}else c=2"; caseIfElse1 =- "Done Empty JSSourceElementsTop [JSIfElse (JSExpression [JSIdentifier \"a\"]) (JSBlock (JSStatementList [JSExpression [JSElement \"assignmentExpression\" [JSIdentifier \"b\",JSOperator \"=\",JSDecimal 1]]])) (JSExpression [JSElement \"assignmentExpression\" [JSIdentifier \"c\",JSOperator \"=\",JSDecimal 2]])]"- @=? (show $ parseString program srcIfElse1)+ "Right (JSSourceElementsTop [JSIfElse (JSExpression [JSIdentifier \"a\"]) (JSBlock (JSStatementList [JSExpression [JSElement \"assignmentExpression\" [JSIdentifier \"b\",JSOperator \"=\",JSDecimal \"1\"]]])) (JSExpression [JSElement \"assignmentExpression\" [JSIdentifier \"c\",JSOperator \"=\",JSDecimal \"2\"]])])"+ -- @=? (show $ parseString program srcIfElse1)+ @=? (show $ parseProgram srcIfElse1) caseMinIfElse1 = testMinify "if(a){b=1}else c=2" srcIfElse1 srcIfElse2 = "if(a){b=1}else {c=2;d=4}"; caseIfElse2 =- "Done Empty JSSourceElementsTop [JSIfElse (JSExpression [JSIdentifier \"a\"]) (JSBlock (JSStatementList [JSExpression [JSElement \"assignmentExpression\" [JSIdentifier \"b\",JSOperator \"=\",JSDecimal 1]]])) (JSBlock (JSStatementList [JSExpression [JSElement \"assignmentExpression\" [JSIdentifier \"c\",JSOperator \"=\",JSDecimal 2]],JSLiteral \";\",JSExpression [JSElement \"assignmentExpression\" [JSIdentifier \"d\",JSOperator \"=\",JSDecimal 4]]]))]"- @=? (show $ parseString program srcIfElse2)+ "Right (JSSourceElementsTop [JSIfElse (JSExpression [JSIdentifier \"a\"]) (JSBlock (JSStatementList [JSExpression [JSElement \"assignmentExpression\" [JSIdentifier \"b\",JSOperator \"=\",JSDecimal \"1\"]]])) (JSBlock (JSStatementList [JSExpression [JSElement \"assignmentExpression\" [JSIdentifier \"c\",JSOperator \"=\",JSDecimal \"2\"]],JSLiteral \";\",JSExpression [JSElement \"assignmentExpression\" [JSIdentifier \"d\",JSOperator \"=\",JSDecimal \"4\"]]]))])"+ -- @=? (show $ parseString program srcIfElse2)+ @=? (show $ parseProgram srcIfElse2) caseMinIfElse2 = testMinify "if(a){b=1}else{c=2;d=4}" srcIfElse2 src0_f = "function Hello(a) {ExprArray(1,1);}" case0_f =- -- "Done Empty JSSourceElementsTop [JSFunction (JSIdentifier \"Hello\") [JSIdentifier \"a\"] (JSFunctionBody [JSSourceElements [JSExpression [JSIdentifier \"ExprArray\",JSArguments [[JSDecimal 1],[JSDecimal 1]]],JSLiteral \"\"]])]"- "Done Empty JSSourceElementsTop [JSFunction (JSIdentifier \"Hello\") [JSIdentifier \"a\"] (JSFunctionBody [JSSourceElements [JSExpression [JSIdentifier \"ExprArray\",JSArguments [[JSDecimal 1],[JSDecimal 1]]],JSLiteral \";\"]])]"- @=? (show $ parseString program src0_f)+ -- "Right (JSSourceElementsTop [JSFunction (JSIdentifier \"Hello\") [JSIdentifier \"a\"] (JSFunctionBody [JSSourceElements [JSExpression [JSIdentifier \"ExprArray\",JSArguments [[JSDecimal 1],[JSDecimal 1]]],JSLiteral \"\"]])]"+ "Right (JSSourceElementsTop [JSFunction (JSIdentifier \"Hello\") [JSIdentifier \"a\"] (JSFunctionBody [JSSourceElements [JSExpression [JSIdentifier \"ExprArray\",JSArguments [[JSDecimal \"1\"],[JSDecimal \"1\"]]],JSLiteral \";\"]])])"+ -- @=? (show $ parseString program src0_f)+ @=? (show $ parseProgram src0_f) caseMin0_f = testMinify "function Hello(a){ExprArray(1,1)}" src0_f @@ -177,27 +182,36 @@ "// five\n"++ "five") case01_semi1 =- "Done Empty JSSourceElementsTop [JSBlock (JSStatementList [JSExpression [JSIdentifier \"zero\",JSMemberDot [JSIdentifier \"one\"]],JSLiteral \";\",JSExpression [JSIdentifier \"zero\"]]),JSExpression [JSIdentifier \"one\"],JSExpression [JSIdentifier \"two\"],JSLiteral \";\",JSExpression [JSIdentifier \"three\"],JSLiteral \";\",JSExpression [JSIdentifier \"four\"],JSLiteral \";\",JSExpression [JSIdentifier \"five\"]]"- @=? (show $ parseString program src01_semi1)+ "Right (JSSourceElementsTop [JSBlock (JSStatementList [JSExpression [JSMemberDot [JSIdentifier \"zero\"] (JSIdentifier \"one\")],JSLiteral \";\",JSExpression [JSIdentifier \"zero\"]]),JSExpression [JSIdentifier \"one\"],JSExpression [JSIdentifier \"two\"],JSLiteral \";\",JSExpression [JSIdentifier \"three\"],JSLiteral \";\",JSExpression [JSIdentifier \"four\"],JSLiteral \";\",JSExpression [JSIdentifier \"five\"]])"+ @=? (show $ parseProgram src01_semi1) caseMin01_semi1 = testMinify "{zero.one;zero};one;two;three;four;five" src01_semi1 src_min_100_animals = "function Animal(name){if(!name)throw new Error('Must specify an animal name');this.name=name};Animal.prototype.toString=function(){return this.name};o=new Animal(\"bob\");o.toString()==\"bob\"" case_min_100_animals =- "Done Empty JSSourceElementsTop [JSFunction (JSIdentifier \"Animal\") [JSIdentifier \"name\"] (JSFunctionBody [JSSourceElements [JSIf (JSExpression [JSUnary \"!\",JSIdentifier \"name\"]) (JSThrow (JSExpression [JSLiteral \"new \",JSIdentifier \"Error\",JSArguments [[JSStringLiteral '\\'' \"Must specify an animal name\"]]])),JSLiteral \";\",JSExpression [JSElement \"assignmentExpression\" [JSLiteral \"this\",JSMemberDot [JSIdentifier \"name\"],JSOperator \"=\",JSIdentifier \"name\"]]]]),JSLiteral \";\",JSExpression [JSElement \"assignmentExpression\" [JSIdentifier \"Animal\",JSMemberDot [JSIdentifier \"prototype\",JSMemberDot [JSIdentifier \"toString\"]],JSOperator \"=\",JSFunctionExpression [] (JSFunctionBody [JSSourceElements [JSReturn [JSExpression [JSLiteral \"this\",JSMemberDot [JSIdentifier \"name\"]],JSLiteral \"\"]]])]],JSLiteral \";\",JSExpression [JSElement \"assignmentExpression\" [JSIdentifier \"o\",JSOperator \"=\",JSLiteral \"new \",JSIdentifier \"Animal\",JSArguments [[JSStringLiteral '\"' \"bob\"]]]],JSLiteral \";\",JSExpression [JSIdentifier \"o\",JSMemberDot [JSIdentifier \"toString\"],JSArguments [[]],JSExpressionBinary \"==\" [JSStringLiteral '\"' \"bob\"] []]]"- @=? (show $ parseString program src_min_100_animals)+ "Right (JSSourceElementsTop [JSFunction (JSIdentifier \"Animal\") [JSIdentifier \"name\"] (JSFunctionBody [JSSourceElements [JSIf (JSExpression [JSUnary \"!\",JSIdentifier \"name\"]) (JSBlock (JSStatementList [JSThrow (JSExpression [JSLiteral \"new \",JSIdentifier \"Error\",JSArguments [[JSStringLiteral '\\'' \"Must specify an animal name\"]]])])),JSExpression [JSElement \"assignmentExpression\" [JSMemberDot [JSLiteral \"this\"] (JSIdentifier \"name\"),JSOperator \"=\",JSIdentifier \"name\"]]]]),JSLiteral \";\",JSExpression [JSElement \"assignmentExpression\" [JSMemberDot [JSMemberDot [JSIdentifier \"Animal\"] (JSIdentifier \"prototype\")] (JSIdentifier \"toString\"),JSOperator \"=\",JSFunctionExpression [] (JSFunctionBody [JSSourceElements [JSReturn [JSExpression [JSMemberDot [JSLiteral \"this\"] (JSIdentifier \"name\")],JSLiteral \"\"]]])]],JSLiteral \";\",JSExpression [JSElement \"assignmentExpression\" [JSIdentifier \"o\",JSOperator \"=\",JSLiteral \"new \",JSIdentifier \"Animal\",JSArguments [[JSStringLiteral '\"' \"bob\"]]]],JSLiteral \";\",JSExpression [JSExpressionBinary \"==\" [JSMemberDot [JSIdentifier \"o\"] (JSIdentifier \"toString\"),JSArguments []] [JSStringLiteral '\"' \"bob\"]]])"+ -- "Right (JSSourceElementsTop [JSFunction (JSIdentifier \"Animal\") [JSIdentifier \"name\"] (JSFunctionBody [JSSourceElements [JSIf (JSExpression [JSUnary \"!\",JSIdentifier \"name\"]) (JSThrow (JSExpression [JSLiteral \"new \",JSIdentifier \"Error\",JSArguments [[JSStringLiteral '\\'' \"Must specify an animal name\"]]])),JSLiteral \";\",JSExpression [JSElement \"assignmentExpression\" [JSMemberDot [JSLiteral \"this\"] (JSIdentifier \"name\"),JSOperator \"=\",JSIdentifier \"name\"]]]]),JSLiteral \";\",JSExpression [JSElement \"assignmentExpression\" [JSMemberDot [JSMemberDot [JSIdentifier \"Animal\"] (JSIdentifier \"prototype\")] (JSIdentifier \"toString\"),JSOperator \"=\",JSFunctionExpression [] (JSFunctionBody [JSSourceElements [JSReturn [JSExpression [JSMemberDot [JSLiteral \"this\"] (JSIdentifier \"name\")],JSLiteral \"\"]]])]],JSLiteral \";\",JSExpression [JSElement \"assignmentExpression\" [JSIdentifier \"o\",JSOperator \"=\",JSLiteral \"new \",JSIdentifier \"Animal\",JSArguments [[JSStringLiteral '\"' \"bob\"]]]],JSLiteral \";\",JSExpression [JSExpressionBinary \"==\" [JSMemberDot [JSIdentifier \"o\"] (JSIdentifier \"toString\"),JSArguments []] [JSStringLiteral '\"' \"bob\"]]])"+ @=? (show $ parseProgram src_min_100_animals) caseMin_min_100_animals = testMinify src_min_100_animals src_min_100_animals +srcMergeStrings = "throw new TypeError(\"Function.prototype.apply called on\"+\" uncallable object\");"+caseMergeStrings = + "Right (JSSourceElementsTop [JSThrow (JSExpression [JSLiteral \"new \",JSIdentifier \"TypeError\",JSArguments [[JSExpressionBinary \"+\" [JSStringLiteral '\"' \"Function.prototype.apply called on\"] [JSStringLiteral '\"' \" uncallable object\"]]]]),JSLiteral \";\"])"+ @=? (show $ parseProgram srcMergeStrings)+caseMinMergeStrings = + testMinify "throw new TypeError(\"Function.prototype.apply called on uncallable object\")" srcMergeStrings+ srcNestedSquare = "this.cursor+=match[0].length;" caseNestedSquare =- "Done Empty JSSourceElementsTop [JSExpression [JSElement \"assignmentExpression\" [JSLiteral \"this\",JSMemberDot [JSIdentifier \"cursor\"],JSOperator \"+=\",JSIdentifier \"match\",JSMemberSquare (JSExpression [JSDecimal 0]) [JSMemberDot [JSIdentifier \"length\"]]]],JSLiteral \";\"]"- @=? (show $ parseString program srcNestedSquare)+ "Right (JSSourceElementsTop [JSExpression [JSElement \"assignmentExpression\" [JSMemberDot [JSLiteral \"this\"] (JSIdentifier \"cursor\"),JSOperator \"+=\",JSMemberDot [JSMemberSquare [JSIdentifier \"match\"] (JSExpression [JSDecimal \"0\"])] (JSIdentifier \"length\")]],JSLiteral \";\"])"+ @=? (show $ parseProgram srcNestedSquare) caseMinNestedSquare = testMinify "this.cursor+=match[0].length" srcNestedSquare caseEitherLeft = - Left "endOfInput" @=? minifym (LB.fromChunks [(E.encodeUtf8 $ T.pack "a=*SYNTAX*ERROR*")])+ Left "UnexpectedToken (MulToken {token_span = SpanPoint {span_filename = \"src\", span_row = 1, span_column = 3}})"+ @=? minifym (LB.fromChunks [(E.encodeUtf8 $ T.pack "a=*SYNTAX*ERROR*")]) caseEitherRight = Right (LB.fromChunks [(E.encodeUtf8 $ T.pack "a=\"no syntax error\"")]) @=? minifym (LB.fromChunks [(E.encodeUtf8 $ T.pack "a=\"no syntax error\";")])@@ -227,10 +241,11 @@ testMinify x' y -- trim :: String -> String trim = f . f where f = reverse . dropWhile isSpace +-- For language-javascript+parseProgram src = parse src "src" + -- EOF