Skip to content

Commit

Permalink
Fix typos
Browse files Browse the repository at this point in the history
  • Loading branch information
NickNeck committed Oct 4, 2024
1 parent ee84066 commit b9e9567
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 10 deletions.
41 changes: 34 additions & 7 deletions lib/rewrite.ex
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,13 @@ defmodule Rewrite do
@doc """
Returns the extension of the given `file`.
"""
@spec extension_for_file(t() | map(), Path.t()) :: {module(), opts()}
@spec extension_for_file(t() | map(), Path.t() | nil) :: {module(), opts()}
def extension_for_file(%Rewrite{extensions: extensions}, path) do
extension_for_file(extensions, path)
end

def extension_for_file(extensions, path) do
ext = Path.extname(path)
ext = if path, do: Path.extname(path)
default = Map.fetch!(extensions, "default")

case Map.get(extensions, ext, default) do
Expand Down Expand Up @@ -677,7 +677,7 @@ defmodule Rewrite do
format(rewrite, dot_formatter, opts)
end

def format(%Rewrite{} = rewrite, [_ | _] = opts, _opts) do
def format(%Rewrite{} = rewrite, opts, _opts) when is_list(opts) do
dot_formatter = dot_formatter(rewrite)
format(rewrite, dot_formatter, opts)
end
Expand Down Expand Up @@ -719,17 +719,23 @@ defmodule Rewrite do

@doc """
Creates a new `%Source{}` and puts the source to the `%Rewrite{}` project.
The `:filetypes` option of the project is used to create the source. If
options have been specified for the file type, the given options will be
merged into those options.
Use `create_source/4` if the source is not to be inserted directly into the
project.
"""
@spec new_source(t(), Path.t(), String.t(), opts()) :: {:ok, t()} | {:error, Error.t()}
def new_source(%Rewrite{sources: sources} = rewrite, path, content, opts \\ []) do
def new_source(%Rewrite{sources: sources} = rewrite, path, content, opts \\ [])
when is_binary(path) do
case Map.has_key?(sources, path) do
true ->
{:error, Error.exception(reason: :overwrites, path: path)}

false ->
{source, source_opts} = extension_for_file(rewrite, path)
opts = Keyword.merge(source_opts, opts)
source = source.from_string(content, path, opts)
source = create_source(rewrite, path, content, opts)
put(rewrite, source)
end
end
Expand All @@ -745,6 +751,27 @@ defmodule Rewrite do
end
end

@doc """
Creates a new `%Source{}` without putting it to the `%Rewrite{}` project.
The `:filetypes` option of the project is used to create the source. If
options have been specified for the file type, the given options will be
merged into those options. If no `path` is given, the default file type is
created.
The function does not check whether the `%Rewrite{}` project already has a
`%Source{}` with the specified path.
Use `new_source/4` if the source is to be inserted directly into the project.
"""
@spec create_source(t(), Path.t() | nil, String.t(), opts()) :: Source.t()
def create_source(%Rewrite{} = rewrite, path, content, opts \\ []) do
{source, source_opts} = extension_for_file(rewrite, path)
opts = Keyword.merge(source_opts, opts)
source = source.from_string(content, path, opts)
%{source | rewrite_id: rewrite.id}
end

defp extensions(modules) do
modules
|> Enum.flat_map(fn
Expand Down
6 changes: 3 additions & 3 deletions lib/rewrite/dot_formatter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -758,17 +758,17 @@ defmodule Rewrite.DotFormatter do
"""
@spec conflicts(t(), Rewrite.t() | nil) :: [{Path.t(), [dot_formatter_path]}]
when dot_formatter_path: Path.t()
def conflicts(dot_formatter, project \\ nil)
def conflicts(dot_formatter, rewrite \\ nil)

def conflicts(%DotFormatter{} = dot_formatter, nil) do
dot_formatter
|> expand(source_path: true)
|> do_conflicts()
end

def conflicts(%DotFormatter{} = dot_formatter, %Rewrite{} = project) do
def conflicts(%DotFormatter{} = dot_formatter, %Rewrite{} = rewrite) do
dot_formatter
|> expand(project, source_path: true)
|> expand(rewrite, source_path: true)
|> do_conflicts()
end

Expand Down
17 changes: 17 additions & 0 deletions test/rewrite_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -990,6 +990,23 @@ defmodule RewriteTest do
end
end

describe "create_source/4" do
test "creates a source" do
rewrite = Rewrite.new()
assert source = Rewrite.create_source(rewrite, "test.ex", "test")
assert is_struct(source.filetype, Source.Ex)
assert rewrite.id == source.rewrite_id
end

test "creates a default source" do
rewrite = Rewrite.new()
Rewrite.create_source(rewrite, nil, "test")
assert source = Rewrite.create_source(rewrite, nil, "test")
refute is_struct(source.filetype, Source.Ex)
assert rewrite.id == source.rewrite_id
end
end

describe "format/2" do
@describetag :tmp_dir
test "formats the rewrite project", context do
Expand Down

0 comments on commit b9e9567

Please sign in to comment.