Skip to content

Commit 95a086a

Browse files
authored
Fix not only looking at pattern when trying to find prefix for case (#1480)
1 parent 0f96f57 commit 95a086a

2 files changed

Lines changed: 141 additions & 2 deletions

File tree

src/common/providers/codeAction/addMissingCaseBranchesCodeAction.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ function getEdits(params: ICodeActionParams, range: Range): TextEdit[] {
6161
// If the branch is prefixed like this Foo.Bar.Biz ->
6262
// We need to prefix the other branches with the same prefix (Foo.Bar.) for it to compile
6363
const prefix = branches[0]
64-
.descendantsOfType("upper_case_identifier")
64+
.childForFieldName("pattern")
65+
?.descendantsOfType("upper_case_identifier")
6566
.slice(0, -1) // Don't take the last one since that's the variant
6667
.map((x) => x.text)
6768
.join(".");

test/codeActionTests/addMissingCaseBranches.test.ts

Lines changed: 139 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ func a =
129129
);
130130
});
131131

132-
it("should prefix branch variant if needed", async () => {
132+
it("should prefix branch variant if other branches are prefixed", async () => {
133133
const source = `
134134
--@ Foo.elm
135135
module Foo exposing (..)
@@ -182,4 +182,142 @@ test response =
182182
expectedSource,
183183
);
184184
});
185+
186+
it("should prefix case if other cases are prefixed 2", async () => {
187+
const source = `
188+
--@ Foo.elm
189+
module Foo exposing (..)
190+
191+
type Foo
192+
= Bar
193+
| Biz
194+
195+
--@ Test.elm
196+
module Test exposing (..)
197+
198+
import Foo
199+
200+
test : Foo -> Html.Html msg
201+
test response =
202+
case response of
203+
--^
204+
Foo.Bar ->
205+
Html.text "x"
206+
`;
207+
208+
const expectedSource = `
209+
--@ Foo.elm
210+
module Foo exposing (..)
211+
212+
type Foo
213+
= Bar
214+
| Biz
215+
216+
--@ Test.elm
217+
module Test exposing (..)
218+
219+
import Foo
220+
221+
test : Foo -> Html.Html msg
222+
test response =
223+
case response of
224+
Foo.Bar ->
225+
Html.text "x"
226+
227+
Foo.Biz ->
228+
Debug.todo "branch 'Foo.Biz' not implemented"
229+
`;
230+
231+
await testCodeAction(
232+
source,
233+
[{ title: "Add missing case branches" }],
234+
expectedSource,
235+
);
236+
});
237+
238+
it("should not prefix case if other cases are not prefixed", async () => {
239+
const source = `
240+
--@ Test.elm
241+
module Test exposing (..)
242+
243+
type Foo
244+
= Bar
245+
| Biz
246+
247+
test : Foo -> ()
248+
test response =
249+
case response of
250+
--^
251+
Bar ->
252+
()
253+
254+
`;
255+
256+
const expectedSource = `
257+
--@ Test.elm
258+
module Test exposing (..)
259+
260+
type Foo
261+
= Bar
262+
| Biz
263+
264+
test : Foo -> ()
265+
test response =
266+
case response of
267+
Bar ->
268+
()
269+
270+
Biz ->
271+
Debug.todo "branch 'Biz' not implemented"
272+
273+
`;
274+
275+
await testCodeAction(
276+
source,
277+
[{ title: "Add missing case branches" }],
278+
expectedSource,
279+
);
280+
});
281+
282+
it("should not prefix case if other cases are not prefixed 2", async () => {
283+
const source = `
284+
--@ Test.elm
285+
module Test exposing (..)
286+
287+
type Foo
288+
= Bar
289+
| Biz
290+
291+
test : Foo -> Html.Html msg
292+
test response =
293+
case response of
294+
--^
295+
Bar ->
296+
Html.text "x"
297+
`;
298+
299+
const expectedSource = `
300+
--@ Test.elm
301+
module Test exposing (..)
302+
303+
type Foo
304+
= Bar
305+
| Biz
306+
307+
test : Foo -> Html.Html msg
308+
test response =
309+
case response of
310+
Bar ->
311+
Html.text "x"
312+
313+
Biz ->
314+
Debug.todo "branch 'Biz' not implemented"
315+
`;
316+
317+
await testCodeAction(
318+
source,
319+
[{ title: "Add missing case branches" }],
320+
expectedSource,
321+
);
322+
});
185323
});

0 commit comments

Comments
 (0)