Skip to content

Commit

Permalink
Merge pull request #6 from mateusfpleite/main
Browse files Browse the repository at this point in the history
Fix withInsertAtBeginning and README.md
  • Loading branch information
jxxcarlson authored Jun 2, 2024
2 parents c17f074 + 4daa790 commit 1529f18
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 75 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ To install `elm-review-codeinstaller`, add it to your `elm.json` dependencies:
elm install jxxcarlson/elm-review-codeinstaller
```

### Usage
## Usage
Suppose you have the following `Msg` type and `update` function in your `Counter` module:

```elm
Expand Down Expand Up @@ -84,7 +84,7 @@ update msg model =
ResetCounter ->
( { model | counter = 0 }, Cmd.none )
```
### Modules
## Modules

`elm-review-codeinstaller` includes the following modules:

Expand Down
14 changes: 10 additions & 4 deletions src/Install/ClauseInCase.elm
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Install.ClauseInCase exposing (init, makeRule, withInsertAfter, withCustomErrorMessage, Config, CustomError)
module Install.ClauseInCase exposing (init, makeRule, withInsertAfter, withInsertAtBeginning, withCustomErrorMessage, Config, CustomError)

{-| Add a clause to a case expression in a specified function
in a specified module. For example, if you put the code below in your
Expand Down Expand Up @@ -42,7 +42,7 @@ in a specified module. For example, if you put the code below in your
|> Install.ClauseInCase.withCustomErrorMessage "Add handler for ResetCounter" []
|> Install.ClauseInCase.makeRule
@docs init, makeRule, withInsertAfter, withCustomErrorMessage, Config, CustomError
@docs init, makeRule, withInsertAfter, withInsertAtBeginning, withCustomErrorMessage, Config, CustomError
-}

Expand Down Expand Up @@ -240,8 +240,12 @@ rangeToInsertClause insertAt cases expression =
( Node.range lastClauseExpression, 2, 0 )

AtBeginning ->
-- TODO: this code is not correct
( Node.range expression, 1, (Node.range expression).start.column )
let
-- The -2 is to account for the `case` keyword and the space after it
firstClauseOffset =
(Node.range expression).start.column - 2
in
( Node.range expression, 1, firstClauseOffset )

AtEnd ->
let
Expand Down Expand Up @@ -349,6 +353,8 @@ withInsertAfter clauseToInsertAfter (Config config) =
}


{-| Add a clause at the beginning of the case expression.
-}
withInsertAtBeginning : Config -> Config
withInsertAtBeginning (Config config) =
Config
Expand Down
83 changes: 41 additions & 42 deletions tests/Install/ClauseInCaseTest2.elm
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@ module Install.ClauseInCaseTest2 exposing (all)

import Install.ClauseInCase
import Run
import Test exposing (Test, describe, test)
import Test exposing (Test, describe)


all : Test
all =
describe "Install.ClauseInCase"
[ Run.testFix test1a
, Run.testFix test1b

-- Run.testFix test1c
-- Run.testFix test2
, Run.testFix test1c
, Run.testFix test2
]


Expand Down Expand Up @@ -43,8 +42,8 @@ test1b =
test1c =
{ description = "Test 1c, withInsertAtBeginning: should report an error and fix it"
, src = src1
, rule = rule1b
, under = under1c
, rule = rule1c
, under = under1
, fixed = fixed1c
, message = "Add handler for ResetCounter"
}
Expand All @@ -61,12 +60,10 @@ rule1b =
|> Install.ClauseInCase.makeRule



--
--rule1c =
-- Install.ClauseInCase.init "Backend" "updateFromFrontend" "ResetCounter" "( { model | counter = 0 }, broadcast (CounterNewValue 0 clientId) )"
-- |> Install.ClauseInCase.withInsertAtBeginning
-- |> Install.ClauseInCase.makeRule
rule1c =
Install.ClauseInCase.init "Backend" "updateFromFrontend" "ResetCounter" "( { model | counter = 0 }, broadcast (CounterNewValue 0 clientId) )"
|> Install.ClauseInCase.withInsertAtBeginning
|> Install.ClauseInCase.makeRule


src1 =
Expand All @@ -75,7 +72,7 @@ src1 =
updateFromFrontend : SessionId -> ClientId -> ToBackend -> Model -> ( Model, Cmd BackendMsg )
updateFromFrontend sessionId clientId msg model =
case msg of
CounterIncremented ->
CounterIncremented ->
let
newCounter =
model.counter + 1
Expand All @@ -92,15 +89,15 @@ fixed1 =
updateFromFrontend : SessionId -> ClientId -> ToBackend -> Model -> ( Model, Cmd BackendMsg )
updateFromFrontend sessionId clientId msg model =
case msg of
CounterIncremented ->
CounterIncremented ->
let
newCounter =
model.counter + 1
in
( { model | counter = newCounter }, broadcast (CounterNewValue newCounter clientId) )
ResetCounter -> ( { model | counter = 0 }, broadcast (CounterNewValue 0 clientId) )
ResetCounter -> ( { model | counter = 0 }, broadcast (CounterNewValue 0 clientId) )
"""
Expand All @@ -112,33 +109,30 @@ fixed1c =
updateFromFrontend : SessionId -> ClientId -> ToBackend -> Model -> ( Model, Cmd BackendMsg )
updateFromFrontend sessionId clientId msg model =
case msg of
ResetCounter -> ( { model | counter = 0 }, broadcast (CounterNewValue 0 clientId) )
CounterIncremented ->
ResetCounter -> ( { model | counter = 0 }, broadcast (CounterNewValue 0 clientId) )
CounterIncremented ->
let
newCounter =
model.counter + 1
in
( { model | counter = newCounter }, broadcast (CounterNewValue newCounter clientId) )
"""


under1 =
"""case msg of
CounterIncremented ->
CounterIncremented ->
let
newCounter =
model.counter + 1
in
( { model | counter = newCounter }, broadcast (CounterNewValue newCounter clientId) )"""


under1c =
"""case msg of
"""



-- TEST 2

Expand All @@ -164,15 +158,15 @@ src2 =
update : FrontendMsg -> Model -> ( Model, Cmd FrontendMsg )
update msg model =
case msg of
Increment ->
( { model | counter = model.counter + 1 }, sendToBackend CounterIncremented )
case msg of
Increment ->
( { model | counter = model.counter + 1 }, sendToBackend CounterIncremented )
Decrement ->
( { model | counter = model.counter - 1 }, sendToBackend CounterDecremented )
Decrement ->
( { model | counter = model.counter - 1 }, sendToBackend CounterDecremented )
FNoop ->
( model, Cmd.none )
FNoop ->
( model, Cmd.none )
"""


Expand All @@ -181,23 +175,28 @@ fixed2 =
update : FrontendMsg -> Model -> ( Model, Cmd FrontendMsg )
update msg model =
case msg of
Increment ->
( { model | counter = model.counter + 1 }, sendToBackend CounterIncremented )
case msg of
Increment ->
( { model | counter = model.counter + 1 }, sendToBackend CounterIncremented )
Reset ->
( { model | counter = 0 }, sendToBackend CounterReset )
Decrement ->
( { model | counter = model.counter - 1 }, sendToBackend CounterDecremented )
Reset -> ( { model | counter = 0 }, sendToBackend CounterReset )
FNoop ->
( model, Cmd.none )
Decrement ->
( { model | counter = model.counter - 1 }, sendToBackend CounterDecremented )
FNoop ->
( model, Cmd.none )
"""


under2 =
"""case msg of
Increment ->
( { model | counter = model.counter + 1 }, sendToBackend CounterIncremented )
"""
Increment ->
( { model | counter = model.counter + 1 }, sendToBackend CounterIncremented )
Decrement ->
( { model | counter = model.counter - 1 }, sendToBackend CounterDecremented )
FNoop ->
( model, Cmd.none )"""
49 changes: 25 additions & 24 deletions tests/Install/FieldInTypeAliasTest.elm
Original file line number Diff line number Diff line change
@@ -1,45 +1,46 @@
module Install.FieldInTypeAliasTest exposing (..)

import Install.FieldInTypeAlias
import Review.Test
import Run
import Test exposing (Test, describe, test)
import Test exposing (Test, describe)


all : Test
all =
let
rule =
Install.FieldInTypeAlias.makeRule "Client" "Client" "name : String"
in
describe "Install.FieldInTypeAlias"
[ test "should not report an error when the field already exists" <|
\() ->
"""module Client exposing (..)
[ Run.expectNoErrorsTest test1.description test1.src test1.rule
, Run.testFix test2
]


test1 =
{ description = "should not report an error when the field already exists"
, src = """module Client exposing (..)
type alias Client =
{ name : String
, age : Int
}
"""
|> Review.Test.run rule
|> Review.Test.expectNoErrors
, test "should report an error when the field does not exist" <|
\() ->
"""module Client exposing (..)
"""
, rule = Install.FieldInTypeAlias.makeRule "Client" "Client" "name : String"
}


test2 =
{ description = "should report an error when the field does not exist"
, src = """module Client exposing (..)
type alias Client =
{ age : Int
}
"""
|> Review.Test.run rule
|> Review.Test.expectErrors
[ Review.Test.error { message = "Add name to Client", details = [ "" ], under = """type alias Client =
"""
, rule = Install.FieldInTypeAlias.makeRule "Client" "Client" "name : String"
, under = """type alias Client =
{ age : Int
}""" }
|> Review.Test.whenFixed """module Client exposing (..)
}"""
, fixed = """module Client exposing (..)
type alias Client =
{ age : Int
, name : String
}
"""
]
]
"""
, message = "Add name to Client"
}
3 changes: 1 addition & 2 deletions tests/Install/InitializerTest.elm
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
module Install.InitializerTest exposing (all)

import Install.Initializer
import Review.Test
import Run
import Test exposing (Test, describe, test)
import Test exposing (Test, describe)


all : Test
Expand Down
2 changes: 1 addition & 1 deletion tests/Install/TypeVariantTest.elm
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Install.TypeVariantTest exposing (all)

import Install.TypeVariant
import Run
import Test exposing (Test, describe, test)
import Test exposing (Test, describe)


all : Test
Expand Down

0 comments on commit 1529f18

Please sign in to comment.