Skip to content

Commit

Permalink
Fix crash when a single pipe had inner defs
Browse files Browse the repository at this point in the history
Closes #39
  • Loading branch information
novaugust committed May 16, 2023
1 parent 798c734 commit ceaa0b1
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## main

### Fixes

* Fix crash when single pipe had inner defs (h/t [@michallepicki](https://github.com/adobe/elixir-styler/issues/39))

## v0.7.5

### Fixes
Expand Down
12 changes: 7 additions & 5 deletions lib/style/pipes.ex
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ defmodule Styler.Style.Pipes do
{:cont, find_pipe_start(chain_zipper), ctx}

{{:|>, _, [lhs, rhs]}, _} = single_pipe_zipper ->
lhs = Style.drop_line_meta(lhs)
{fun, meta, args} = Style.drop_line_meta(rhs)
{_, meta, _} = lhs
lhs = Style.set_line_meta_to_line(lhs, meta[:line])
{fun, meta, args} = Style.set_line_meta_to_line(rhs, meta[:line])
function_call_zipper = Zipper.replace(single_pipe_zipper, {fun, meta, [lhs | args || []]})
{:cont, function_call_zipper, ctx}
end
Expand Down Expand Up @@ -94,9 +95,10 @@ defmodule Styler.Style.Pipes do
#
# block_result
# |> ...
defp extract_start({block, _, _} = lhs) when block in @blocks do
variable = {:"#{block}_result", [], nil}
new_assignment = {:=, [], [variable, lhs]}
defp extract_start({block, meta, _} = lhs) when block in @blocks do
meta = [line: meta[:line]]
variable = {:"#{block}_result", meta, nil}
new_assignment = {:=, meta, [variable, lhs]}
{variable, new_assignment}
end

Expand Down
38 changes: 38 additions & 0 deletions test/style/styles_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Copyright 2023 Adobe. All rights reserved.
# This file is licensed to you under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. You may obtain a copy
# of the License at http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software distributed under
# the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
# OF ANY KIND, either express or implied. See the License for the specific language
# governing permissions and limitations under the License.

defmodule Styler.Style.StylesTest do
@moduledoc """
A place for tests that make sure our styles play nicely with each other
"""
use Styler.StyleCase, async: true

describe "pipes + defs" do
test "pipes doesnt abuse meta and break defs" do
assert_style(
"""
foo
|> bar(fn baz ->
def widget() do
:bop
end
end)
""",
"""
bar(foo, fn baz ->
def widget do
:bop
end
end)
"""
)
end
end
end

0 comments on commit ceaa0b1

Please sign in to comment.