Skip to content

Commit c7f0a6d

Browse files
committed
Shorter way to do blocks and JavaScript plus tests. All tests pass
1 parent f38fdc8 commit c7f0a6d

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

app/Main.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ convertToJsFile infile outfile = do
3333
printHelp = do
3434
putStrLn "Usage: `salty test.salt` prints to stdout"
3535
putStrLn "Usage: `salty` reads from stdin and prints to stdout"
36-
putStrLn "Usage: `salty debug <filename>` reads file and prints tree to stdout"
36+
putStrLn "Usage: `salty -d <filename>` reads file and prints tree to stdout"
3737

3838
debugFile :: String -> IO ()
3939
debugFile infile = do

src/ToJs.hs

+1-4
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,6 @@ instance ConvertToJs Salty where
184184

185185
-- functions called without an object (bare)
186186
toJs (FunctionCall Nothing (Right var) args Nothing) = print2 "%(%)" (varNameToFunc var) (intercalate ", " . map toJs $ args)
187-
toJs (FunctionCall Nothing (Right var) args (Just block@(Braces arr))) = print2 "%(%)" (varNameToFunc var) args_
188-
where args_ = join ", " $ (map toJs (args ++ (map stripNewline arr)))
189187

190188
toJs (FunctionCall Nothing (Right var) args (Just block@(LambdaFunction _ _))) = print2 "%(%)" (varNameToFunc var) args_
191189
where args_ = join ", " $ (map toJs args) ++ [(addReturn block)]
@@ -212,8 +210,7 @@ instance ConvertToJs Salty where
212210
-- functions called on an obj
213211
-- toJs (FunctionCall (Just (Variable (ClassVar obj) _)) (Right funcName) args Nothing) = print3 "%.%(%)" obj (simpleVarName funcName) (intercalate ", " . map toJs $ args)
214212
toJs (FunctionCall (Just var@(Variable _ _)) (Right funcName) args Nothing) = print3 "%.%(%)" (toJs var) (simpleVarName funcName) (intercalate ", " . map toJs $ args)
215-
toJs (FunctionCall (Just var@(Variable _ _)) (Right funcName) args (Just block@(Braces arr))) = print3 "%.%(%)" (toJs var) (simpleVarName funcName) args_
216-
where args_ = join ", " $ (map toJs $ args ++ (map stripNewline arr))
213+
toJs (FunctionCall varOrNothing (Right funcName) args (Just braces@(Braces arr))) = toJs (FunctionCall varOrNothing (Right funcName) args (Just (LambdaFunction [] braces)))
217214

218215
toJs (FunctionCall (Just var@(Variable _ _)) (Right funcName) args (Just block@(LambdaFunction _ _))) = print3 "%.%(%)" (toJs var) (simpleVarName funcName) args_
219216
where args_ = join ", " $ (map toJs args) ++ [(addReturn block)]

test/JsSpec.hs

+33
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,13 @@ doFuncResult = [r|const constructor = (props) => {
274274
});
275275
}|]
276276

277+
doFuncImplicit = [r|constructor props := {
278+
React.useEffect() do
279+
window.localStorage.setItem('step', @@activeItem)
280+
end
281+
}|]
282+
283+
277284
funcTest = [r|
278285
export default class TodoList extends React.Component where
279286
makeTodos := @props.todos.map(\t -> @makeTodo(t))
@@ -285,6 +292,29 @@ funcTestResult = [r| export default class TodoList extends React.Component {
285292
}
286293
}|]
287294

295+
newDo = [r|constructor props := {
296+
Form.new() do
297+
window.localStorage.setItem('step', @@activeItem)
298+
end
299+
}|]
300+
301+
newDoResult = [r|const constructor = (props) => {
302+
return (<Form>{window.localStorage.setItem("step", this.state.activeItem)}</Form>);
303+
}|]
304+
305+
306+
newDoFunc = [r|constructor props := {
307+
Form.new() do \_ ->
308+
window.localStorage.setItem('step', @@activeItem)
309+
end
310+
}|]
311+
312+
newDoFuncResult = [r|const constructor = (props) => {
313+
return (<Form>{() => {
314+
return window.localStorage.setItem("step", this.state.activeItem);
315+
}}</Form>);
316+
}|]
317+
288318
jsTests = [
289319
multiLineEachTest,
290320
multiLineMapTest,
@@ -300,7 +330,10 @@ jsTests = [
300330
guardTestAsSwitch `matches` guardTestAsSwitchResult,
301331
longClass `matches` longClassResult,
302332
doFunc `matches` doFuncResult,
333+
doFuncImplicit `matches` doFuncResult,
303334
funcTest `matches` funcTestResult,
335+
newDo `matches` newDoResult,
336+
newDoFunc `matches` newDoFuncResult,
304337
-- operations
305338
"foo = 1" `matches` "foo = 1;",
306339
"bar = 'adit'" `matches` "bar = \"adit\";",

0 commit comments

Comments
 (0)