Skip to content

Support let statements #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Language/JavaScript/Parser/AST.hs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ data JSAST
data JSStatement
= JSStatementBlock !JSAnnot ![JSStatement] !JSAnnot !JSSemi -- ^lbrace, stmts, rbrace, autosemi
| JSBreak !JSAnnot !JSIdent !JSSemi -- ^break,optional identifier, autosemi
| JSLet !JSAnnot !(JSCommaList JSExpression) !JSSemi -- ^const, decl, autosemi
| JSConstant !JSAnnot !(JSCommaList JSExpression) !JSSemi -- ^const, decl, autosemi
| JSContinue !JSAnnot !JSIdent !JSSemi -- ^continue, optional identifier,autosemi
| JSDoWhile !JSAnnot !JSStatement !JSAnnot !JSAnnot !JSExpression !JSAnnot !JSSemi -- ^do,stmt,while,lb,expr,rb,autosemi
Expand Down Expand Up @@ -251,6 +252,7 @@ instance ShowStripped JSStatement where
ss (JSContinue _ JSIdentNone s) = "JSContinue" ++ commaIf (ss s)
ss (JSContinue _ (JSIdentName _ n) s) = "JSContinue " ++ singleQuote n ++ commaIf (ss s)
ss (JSConstant _ xs _as) = "JSConstant " ++ ss xs
ss (JSLet _ xs _as) = "JSLet " ++ ss xs
ss (JSDoWhile _d x1 _w _lb x2 _rb x3) = "JSDoWhile (" ++ ss x1 ++ ") (" ++ ss x2 ++ ") (" ++ ss x3 ++ ")"
ss (JSFor _ _lb x1s _s1 x2s _s2 x3s _rb x4) = "JSFor " ++ ss x1s ++ " " ++ ss x2s ++ " " ++ ss x3s ++ " (" ++ ss x4 ++ ")"
ss (JSForIn _ _lb x1s _i x2 _rb x3) = "JSForIn " ++ ss x1s ++ " (" ++ ss x2 ++ ") (" ++ ss x3 ++ ")"
Expand Down
8 changes: 7 additions & 1 deletion src/Language/JavaScript/Parser/Grammar7.y
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ import qualified Language.JavaScript.Parser.AST as AST
'if' { IfToken {} }
'in' { InToken {} }
'instanceof' { InstanceofToken {} }
'let' { LetToken {} }
'new' { NewToken {} }
'null' { NullToken {} }
'return' { ReturnToken {} }
Expand Down Expand Up @@ -290,6 +291,9 @@ OpAssign : '*=' { AST.JSTimesAssign (mkJSAnnot $1) }
Var :: { AST.JSAnnot }
Var : 'var' { mkJSAnnot $1 }

Let :: { AST.JSAnnot }
Let : 'let' { mkJSAnnot $1 }

Const :: { AST.JSAnnot }
Const : 'const' { mkJSAnnot $1 }

Expand Down Expand Up @@ -432,6 +436,7 @@ IdentifierName : Identifier {$1}
| 'if' { AST.JSIdentifier (mkJSAnnot $1) "if" }
| 'in' { AST.JSIdentifier (mkJSAnnot $1) "in" }
| 'instanceof' { AST.JSIdentifier (mkJSAnnot $1) "instanceof" }
| 'let' { AST.JSIdentifier (mkJSAnnot $1) "let" }
| 'new' { AST.JSIdentifier (mkJSAnnot $1) "new" }
| 'null' { AST.JSIdentifier (mkJSAnnot $1) "null" }
| 'return' { AST.JSIdentifier (mkJSAnnot $1) "return" }
Expand Down Expand Up @@ -894,7 +899,8 @@ StatementList : Statement { [$1] {- 'StatementList1' -} }
-- var VariableDeclarationList ;
VariableStatement :: { AST.JSStatement }
VariableStatement : Var VariableDeclarationList MaybeSemi { AST.JSVariable $1 $2 $3 {- 'VariableStatement1' -} }
| Const VariableDeclarationList MaybeSemi { AST.JSConstant $1 $2 $3 {- 'VariableStatement2' -} }
| Let VariableDeclarationList MaybeSemi { AST.JSLet $1 $2 $3 {- 'VariableStatement2' -} }
| Const VariableDeclarationList MaybeSemi { AST.JSConstant $1 $2 $3 {- 'VariableStatement3' -} }

-- VariableDeclarationList : See 12.2
-- VariableDeclaration
Expand Down
2 changes: 1 addition & 1 deletion src/Language/JavaScript/Parser/Lexer.x
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,7 @@ keywordNames =
, ( "if", IfToken )
, ( "in", InToken )
, ( "instanceof", InstanceofToken )
, ( "let", LetToken )
, ( "new", NewToken )

, ( "null", NullToken ) -- null literal
Expand Down Expand Up @@ -565,7 +566,6 @@ keywordNames =
-- Strict mode FutureReservedWords
, ( "implements", FutureToken )
, ( "interface", FutureToken )
, ( "let", FutureToken )
-- ( "mode", FutureToken ) **** not any more
-- ( "of", FutureToken ) **** not any more
-- ( "one", FutureToken ) **** not any more
Expand Down
1 change: 1 addition & 0 deletions src/Language/JavaScript/Parser/Token.hs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ data Token
| CaseToken { tokenSpan :: !TokenPosn, tokenLiteral :: !String, tokenComment :: ![CommentAnnotation] }
| CatchToken { tokenSpan :: !TokenPosn, tokenLiteral :: !String, tokenComment :: ![CommentAnnotation] }
| ConstToken { tokenSpan :: !TokenPosn, tokenLiteral :: !String, tokenComment :: ![CommentAnnotation] }
| LetToken { tokenSpan :: !TokenPosn, tokenLiteral :: !String, tokenComment :: ![CommentAnnotation] }
| ContinueToken { tokenSpan :: !TokenPosn, tokenLiteral :: !String, tokenComment :: ![CommentAnnotation] }
| DebuggerToken { tokenSpan :: !TokenPosn, tokenLiteral :: !String, tokenComment :: ![CommentAnnotation] }
| DefaultToken { tokenSpan :: !TokenPosn, tokenLiteral :: !String, tokenComment :: ![CommentAnnotation] }
Expand Down
2 changes: 2 additions & 0 deletions src/Language/JavaScript/Pretty/Printer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,8 @@ instance RenderJS JSStatement where
(|>) pacc (JSWhile annot alp x1 arp x2) = pacc |> annot |> "while" |> alp |> "(" |> x1 |> arp |> ")" |> x2
(|>) pacc (JSWith annot alp x1 arp x s) = pacc |> annot |> "with" |> alp |> "(" |> x1 |> arp |> ")" |> x |> s

(|>) pacc (JSLet annot xs s) = pacc |> annot |> "let" |> xs |> s


instance RenderJS [JSStatement] where
(|>) = foldl' (|>)
Expand Down
2 changes: 2 additions & 0 deletions src/Language/JavaScript/Process/Minify.hs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ fixStmt a s (JSVariable _ ss _) = JSVariable a (fixVarList ss) s
fixStmt a s (JSWhile _ _ e _ st) = JSWhile a emptyAnnot (fixEmpty e) emptyAnnot (fixStmt a s st)
fixStmt a s (JSWith _ _ e _ st _) = JSWith a emptyAnnot (fixEmpty e) emptyAnnot (fixStmtE noSemi st) s

fixStmt a s (JSLet _ xs _) = JSLet a (fixVarList xs) s

fixIfElseBlock :: JSAnnot -> JSSemi -> JSStatement -> JSStatement
fixIfElseBlock _ _ (JSStatementBlock _ [] _ _) = JSEmptyStatement emptyAnnot
fixIfElseBlock a s st = fixStmt a s st
Expand Down
2 changes: 2 additions & 0 deletions test/Test/Language/Javascript/Minify.hs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ testMinifyStmt = describe "Minify statements:" $ do
minifyStmt " var b ; " `shouldBe` "var b"
minifyStmt " var c = 1 ; " `shouldBe` "var c=1"
minifyStmt " var d = 1, x = 2 ; " `shouldBe` "var d=1,x=2"
minifyStmt " let c = 1 ; " `shouldBe` "let c=1"
minifyStmt " let d = 1, x = 2 ; " `shouldBe` "let d=1,x=2"

it "string concatenation" $
minifyStmt " f (\"ab\"+\"cd\") " `shouldBe` "f('abcd')"
Expand Down
1 change: 1 addition & 0 deletions test/Test/Language/Javascript/RoundTrip.hs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ testRoundTrip = describe "Roundtrip:" $ do
testRT "switch (x) {case 0:\ncase 1:break;}"
testRT "switch (x) {default:break;}"
testRT "switch (x) {default:\ncase 1:break;}"
testRT "var x=1;let y=2;"


testRT :: String -> Expectation
Expand Down
3 changes: 2 additions & 1 deletion test/Test/Language/Javascript/StatementParser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,10 @@ testStatementParser = describe "Parse statements:" $ do

testStmt "for(var x in 5){}" `shouldBe` "Right (JSAstStatement (JSForVarIn (JSVarInitExpression (JSIdentifier 'x') ) (JSDecimal '5') (JSStatementBlock [])))"

it "variable/constant declaration" $ do
it "variable/constant/let declaration" $ do
testStmt "var x=1;" `shouldBe` "Right (JSAstStatement (JSVariable (JSVarInitExpression (JSIdentifier 'x') [JSDecimal '1'])))"
testStmt "const x=1,y=2;" `shouldBe` "Right (JSAstStatement (JSConstant (JSVarInitExpression (JSIdentifier 'x') [JSDecimal '1'],JSVarInitExpression (JSIdentifier 'y') [JSDecimal '2'])))"
testStmt "let x=1,y=2;" `shouldBe` "Right (JSAstStatement (JSLet (JSVarInitExpression (JSIdentifier 'x') [JSDecimal '1'],JSVarInitExpression (JSIdentifier 'y') [JSDecimal '2'])))"

it "break" $ do
testStmt "break;" `shouldBe` "Right (JSAstStatement (JSBreak,JSSemicolon))"
Expand Down