hjsmin 0.0.1 → 0.0.2
raw patch · 4 files changed
+42/−37 lines, 4 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
Files
- Text/Jasmine.hs +4/−6
- Text/Jasmine/Parse.hs +21/−14
- hjsmin.cabal +1/−1
- runtests.hs +16/−16
Text/Jasmine.hs view
@@ -9,24 +9,22 @@ import Text.Jasmine.Pretty import qualified Blaze.ByteString.Builder as BB import qualified Data.ByteString.Lazy as LB-import qualified Data.ByteString as B --- TODO: consider using option 4 of http://www.randomhacks.net/articles/2007/03/10/haskell-8-ways-to-report-errors-minifym :: B.ByteString -> Either String LB.ByteString+minifym :: LB.ByteString -> Either String LB.ByteString minifym s = case readJsm s of Left msg -> Left msg Right p -> Right $ BB.toLazyByteString $ renderJS p -minify :: B.ByteString -> LB.ByteString+minify :: LB.ByteString -> LB.ByteString minify s = BB.toLazyByteString $ renderJS $ readJs s -_minify' :: B.ByteString -> BB.Builder+_minify' :: LB.ByteString -> BB.Builder _minify' s = renderJS $ readJs s minifyFile :: FilePath -> IO LB.ByteString minifyFile filename = do - x <- B.readFile (filename)+ x <- LB.readFile (filename) return $ minify x -- EOF
Text/Jasmine/Parse.hs view
@@ -22,14 +22,14 @@ import Control.Applicative ( (<|>) ) import Control.Monad-import Data.Attoparsec (eitherResult)-import Data.Attoparsec.Char8 (char, satisfy, try, feed, Parser, Result(..), (<?>), endOfInput, many, parse, sepBy, sepBy1, many1)+import Data.Attoparsec.Lazy (eitherResult,parse,Result(..))+import Data.Attoparsec.Char8 (char, satisfy, try, Parser, (<?>), endOfInput, many, sepBy, sepBy1, many1) import Data.Char import Data.Data import Data.List import Prelude hiding (catch) import System.Environment-import qualified Data.ByteString as B+import qualified Data.ByteString.Lazy as LB import qualified Data.Text as T import qualified Data.Text.Encoding as E import qualified Text.Jasmine.Token as P@@ -1113,16 +1113,16 @@ main :: IO () main = do args <- getArgs- x <- B.readFile (args !! 0)+ x <- LB.readFile (args !! 0) putStrLn (show $ doParse program x) -- --------------------------------------------------------------------- -readJs :: B.ByteString -> JSNode+readJs :: LB.ByteString -> JSNode readJs input = case doParse program input of Fail _unparsed contexts err -> error("Parse failed" ++ show(contexts) ++ ":" ++ show err)- Partial _f -> error("Unexpected partial")+ -- Partial _f -> error("Unexpected partial") Done _unparsed val -> val -- --------------------------------------------------------------------- @@ -1133,23 +1133,30 @@ Left msg -> fail ("Parse failed:" ++ msg) Right val -> return val -}---readJsm :: (Monad m) => B.ByteString -> m JSNode-readJsm :: B.ByteString -> Either String JSNode++readJsm :: LB.ByteString -> Either String JSNode readJsm input = eitherResult $ doParse program input -- --------------------------------------------------------------------- +{- _doParse' :: Parser a -> String -> a-_doParse' p input = case parse (p' p) (E.encodeUtf8 $ T.pack input) of+_doParse' p input = case parse (p' p) (LB.fromChunks [E.encodeUtf8 $ T.pack input]) of Fail _unparsed contexts err -> error("Parse failed" ++ show(contexts) ++ ":" ++ show err) Partial _f -> error("Unexpected partial") Done _unparsed val -> val+-} -doParse :: Parser r -> B.ByteString -> Result r-doParse p input = {-maybeResult $-} feed (parse (p' p) input) B.empty- +{-+doParseStrict :: Parser r -> B.ByteString -> Result r+doParseStrict p input = {-maybeResult $-} feed (parse (p' p) input) LB.empty+-} ++doParse :: Parser r -> LB.ByteString -> Result r+doParse p input = parse (p' p) input+ parseString :: Parser r -> String -> Result r-parseString p input = doParse p (E.encodeUtf8 $ T.pack input)+parseString p input = doParse p (LB.fromChunks [E.encodeUtf8 $ T.pack input]) -- --------------------------------------------------------------------- @@ -1169,7 +1176,7 @@ parseFile :: FilePath -> IO JSNode parseFile filename = do - x <- B.readFile (filename)+ x <- LB.readFile (filename) return $ (readJs x) -- EOF
hjsmin.cabal view
@@ -1,5 +1,5 @@ name: hjsmin-version: 0.0.1+version: 0.0.2 license: BSD3 license-file: LICENSE author: Alan Zimmerman <alan.zimm@gmail.com>
runtests.hs view
@@ -112,7 +112,7 @@ srcHelloWorld = "function Hello(a) {}" caseHelloWorld = - "Done \"\" JSFunction (JSIdentifier \"Hello\") [JSIdentifier \"a\"] (JSFunctionBody [])"+ "Done Empty JSFunction (JSIdentifier \"Hello\") [JSIdentifier \"a\"] (JSFunctionBody [])" @=? (show $ parseString functionDeclaration srcHelloWorld) caseMinHelloWorld = -- "function Hello(a){}" @=? (minify (U.fromString srcHelloWorld))@@ -120,7 +120,7 @@ srcHelloWorld2 = "function Hello(a) {b=1}" caseHelloWorld2 = - "Done \"\" JSFunction (JSIdentifier \"Hello\") [JSIdentifier \"a\"] (JSFunctionBody [JSSourceElements [JSExpression [JSElement \"assignmentExpression\" [JSIdentifier \"b\",JSOperator \"=\",JSDecimal 1]]]])"+ "Done Empty JSFunction (JSIdentifier \"Hello\") [JSIdentifier \"a\"] (JSFunctionBody [JSSourceElements [JSExpression [JSElement \"assignmentExpression\" [JSIdentifier \"b\",JSOperator \"=\",JSDecimal 1]]]])" @=? (show $ parseString functionDeclaration srcHelloWorld2) caseMinHelloWorld2 = -- "function Hello(a){b=1}" @=? (minify (U.fromString srcHelloWorld2))@@ -128,43 +128,43 @@ srcSimpleAssignment = "a=1;" caseSimpleAssignment = - "Done \"\" JSStatementList [JSExpression [JSElement \"assignmentExpression\" [JSIdentifier \"a\",JSOperator \"=\",JSDecimal 1]],JSLiteral \";\"]"+ "Done Empty JSStatementList [JSExpression [JSElement \"assignmentExpression\" [JSIdentifier \"a\",JSOperator \"=\",JSDecimal 1]],JSLiteral \";\"]" @=? (show $ parseString statementList srcSimpleAssignment) caseMinSimpleAssignment = testMinify "a=1" srcSimpleAssignment srcEmptyFor = "for (i = 0;;){}" caseEmptyFor =- "Done \"\" JSFor [JSExpression [JSElement \"assignmentExpression\" [JSIdentifier \"i\",JSOperator \"=\",JSDecimal 0]]] [] [] (JSLiteral \";\")"+ "Done Empty JSFor [JSExpression [JSElement \"assignmentExpression\" [JSIdentifier \"i\",JSOperator \"=\",JSDecimal 0]]] [] [] (JSLiteral \";\")" @=? (show $ parseString iterationStatement srcEmptyFor) srcFullFor = "for (i = 0;i<10;i++){}" caseFullFor =- "Done \"\" JSFor [JSExpression [JSElement \"assignmentExpression\" [JSIdentifier \"i\",JSOperator \"=\",JSDecimal 0]]] [JSExpression [JSIdentifier \"i\",JSExpressionBinary \"<\" [JSDecimal 10] []]] [JSExpression [JSExpressionPostfix \"++\" [JSIdentifier \"i\"]]] (JSLiteral \";\")"+ "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) srcForVarFull = "for(var i=0,j=tokens.length;i<j;i++){}" caseForVarFull =- "Done \"\" 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 \";\")"+ "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) srcIfElse1 = "if(a){b=1}else c=2"; caseIfElse1 =- "Done \"\" JSSourceElementsTop [JSIfElse (JSExpression [JSIdentifier \"a\"]) (JSBlock (JSStatementList [JSExpression [JSElement \"assignmentExpression\" [JSIdentifier \"b\",JSOperator \"=\",JSDecimal 1]]])) (JSExpression [JSElement \"assignmentExpression\" [JSIdentifier \"c\",JSOperator \"=\",JSDecimal 2]])]"+ "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) caseMinIfElse1 = testMinify "if(a){b=1}else c=2" srcIfElse1 srcIfElse2 = "if(a){b=1}else {c=2;d=4}"; caseIfElse2 =- "Done \"\" 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]]]))]"+ "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) caseMinIfElse2 = testMinify "if(a){b=1}else{c=2;d=4}" srcIfElse2 src0_f = "function Hello(a) {ExprArray(1,1);}" case0_f =- -- "Done \"\" JSSourceElementsTop [JSFunction (JSIdentifier \"Hello\") [JSIdentifier \"a\"] (JSFunctionBody [JSSourceElements [JSExpression [JSIdentifier \"ExprArray\",JSArguments [[JSDecimal 1],[JSDecimal 1]]],JSLiteral \"\"]])]"- "Done \"\" 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 \"\"]])]"+ "Done Empty JSSourceElementsTop [JSFunction (JSIdentifier \"Hello\") [JSIdentifier \"a\"] (JSFunctionBody [JSSourceElements [JSExpression [JSIdentifier \"ExprArray\",JSArguments [[JSDecimal 1],[JSDecimal 1]]],JSLiteral \";\"]])]" @=? (show $ parseString program src0_f) caseMin0_f = testMinify "function Hello(a){ExprArray(1,1)}" src0_f@@ -177,36 +177,36 @@ "// five\n"++ "five") case01_semi1 =- "Done \"\" 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\"]]"+ "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) 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 \"\" 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\"] []]]"+ "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) caseMin_min_100_animals = testMinify src_min_100_animals src_min_100_animals srcNestedSquare = "this.cursor+=match[0].length;" caseNestedSquare =- "Done \"\" JSSourceElementsTop [JSExpression [JSElement \"assignmentExpression\" [JSLiteral \"this\",JSMemberDot [JSIdentifier \"cursor\"],JSOperator \"+=\",JSIdentifier \"match\",JSMemberSquare (JSExpression [JSDecimal 0]) [JSMemberDot [JSIdentifier \"length\"]]]],JSLiteral \";\"]"+ "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) caseMinNestedSquare = testMinify "this.cursor+=match[0].length" srcNestedSquare caseEitherLeft = - Left "endOfInput" @=? minifym ((E.encodeUtf8 $ T.pack "a=*SYNTAX*ERROR*"))+ Left "endOfInput" @=? minifym (LB.fromChunks [(E.encodeUtf8 $ T.pack "a=*SYNTAX*ERROR*")]) caseEitherRight = - Right (LB.fromChunks [(E.encodeUtf8 $ T.pack "a=\"no syntax error\"")]) @=? minifym ((E.encodeUtf8 $ T.pack "a=\"no syntax error\";"))+ Right (LB.fromChunks [(E.encodeUtf8 $ T.pack "a=\"no syntax error\"")]) @=? minifym (LB.fromChunks [(E.encodeUtf8 $ T.pack "a=\"no syntax error\";")]) -- --------------------------------------------------------------------- -- utilities --testMinify expected src = (LB.fromChunks [(U.fromString expected)]) @=? (minify (U.fromString src))-testMinify expected src = (LB.fromChunks [(E.encodeUtf8 $ T.pack expected)]) @=? (minify (E.encodeUtf8 $ T.pack src))+testMinify expected src = (LB.fromChunks [(E.encodeUtf8 $ T.pack expected)]) @=? (minify $ LB.fromChunks [(E.encodeUtf8 $ T.pack src)]) testFile :: FilePath -> IO () testFile filename =