Skip to content

Commit

Permalink
Update Credo compliance
Browse files Browse the repository at this point in the history
  • Loading branch information
devinus committed Jul 27, 2018
1 parent 0af426e commit ee06841
Show file tree
Hide file tree
Showing 11 changed files with 140 additions and 64 deletions.
41 changes: 28 additions & 13 deletions .credo.exs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
# You can give explicit globs or simply directories.
# In the latter case `**/*.{ex,exs}` will be used.
#
included: ["lib/", "src/", "web/", "apps/"],
excluded: [~r"/_build/", ~r"/deps/"]
included: ["lib/", "src/", "test/", "web/", "apps/", "bench/", "mix.exs"],
excluded: [~r"/_build/", ~r"/deps/", ~r"/node_modules/"]
},
#
# If you create your own checks, you must specify the source files for
Expand All @@ -33,7 +33,7 @@
# If you want to enforce a style guide and need a more traditional linting
# experience, you can change `strict` to `true` below:
#
strict: false,
strict: true,
#
# If you want to use uncolored output by default, you can change `color`
# to `false` below:
Expand All @@ -48,38 +48,46 @@
# {Credo.Check.Design.DuplicatedCode, false}
#
checks: [
#
## Consistency Checks
#
{Credo.Check.Consistency.ExceptionNames},
{Credo.Check.Consistency.LineEndings},
{Credo.Check.Consistency.ParameterPatternMatching},
{Credo.Check.Consistency.SpaceAroundOperators},
{Credo.Check.Consistency.SpaceInParentheses},
{Credo.Check.Consistency.TabsOrSpaces},

#
## Design Checks
#
# You can customize the priority of any check
# Priority values are: `low, normal, high, higher`
#
{Credo.Check.Design.AliasUsage, priority: :low},

# For some checks, you can also set other parameters
#
# If you don't want the `setup` and `test` macro calls in ExUnit tests
# or the `schema` macro in Ecto schemas to trigger DuplicatedCode, just
# set the `excluded_macros` parameter to `[:schema, :setup, :test]`.
#
{Credo.Check.Design.DuplicatedCode, excluded_macros: []},

# You can also customize the exit_status of each check.
# If you don't want TODO comments to cause `mix credo` to fail, just
# set this value to 0 (zero).
#
{Credo.Check.Design.TagTODO, exit_status: 2},
{Credo.Check.Design.TagFIXME},

#
## Readability Checks
#
{Credo.Check.Readability.AliasOrder},
{Credo.Check.Readability.FunctionNames},
{Credo.Check.Readability.LargeNumbers},
{Credo.Check.Readability.MaxLineLength, priority: :low, max_length: 80},
{Credo.Check.Readability.ModuleAttributeNames},
{Credo.Check.Readability.ModuleDoc, false},
{Credo.Check.Readability.ModuleDoc},
{Credo.Check.Readability.ModuleNames},
{Credo.Check.Readability.ParenthesesOnZeroArityDefs},
{Credo.Check.Readability.ParenthesesInCondition},
Expand All @@ -93,18 +101,26 @@
{Credo.Check.Readability.Semicolons},
{Credo.Check.Readability.SpaceAfterCommas},

#
## Refactoring Opportunities
#
{Credo.Check.Refactor.DoubleBooleanNegation},
{Credo.Check.Refactor.CondStatements},
{Credo.Check.Refactor.CyclomaticComplexity},
{Credo.Check.Refactor.FunctionArity},
{Credo.Check.Refactor.LongQuoteBlocks},
{Credo.Check.Refactor.MapInto},
{Credo.Check.Refactor.MatchInCondition},
{Credo.Check.Refactor.NegatedConditionsInUnless},
{Credo.Check.Refactor.NegatedConditionsWithElse},
{Credo.Check.Refactor.Nesting},
{Credo.Check.Refactor.PipeChainStart},
{Credo.Check.Refactor.PipeChainStart,
excluded_argument_types: [:atom, :binary, :fn, :keyword], excluded_functions: []},
{Credo.Check.Refactor.UnlessWithElse},

#
## Warnings
#
{Credo.Check.Warning.BoolOperationOnSameValues},
{Credo.Check.Warning.ExpensiveEmptyEnumCheck},
{Credo.Check.Warning.IExPry},
Expand All @@ -120,8 +136,9 @@
{Credo.Check.Warning.UnusedRegexOperation},
{Credo.Check.Warning.UnusedStringOperation},
{Credo.Check.Warning.UnusedTupleOperation},
{Credo.Check.Warning.RaiseInsideRescue, false},
{Credo.Check.Warning.RaiseInsideRescue},

#
# Controversial and experimental checks (opt-in, just remove `, false`)
#
{Credo.Check.Refactor.ABCSize, false},
Expand All @@ -130,14 +147,12 @@
{Credo.Check.Warning.MapGetUnsafePass, false},
{Credo.Check.Consistency.MultiAliasImportRequireUse, false},

#
# Deprecated checks (these will be deleted after a grace period)
#
{Credo.Check.Readability.Specs, false},
{Credo.Check.Warning.NameRedeclarationByAssignment, false},
{Credo.Check.Warning.NameRedeclarationByCase, false},
{Credo.Check.Warning.NameRedeclarationByDef, false},
{Credo.Check.Warning.NameRedeclarationByFn, false},
{Credo.Check.Readability.Specs, false}

#
# Custom checks can be created using `mix credo.gen.check`.
#
]
Expand Down
2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
erlang 21.0.4
elixir 1.7.0
elixir 1.7.1
8 changes: 8 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
language: elixir
elixir:
- 1.7.1
- 1.7.0
- 1.6.6
- 1.6.5
Expand All @@ -9,3 +10,10 @@ otp_release:
- 20.1
- 20.0
sudo: false
before_script:
- |
if [ "$TRAVIS_ELIXIR_VERSION|$TRAVIS_OTP_RELEASE" == "1.7.1|21.0" ]; then
set -e;
mix archive.install hex credo '~> 0.9' --force;
mix credo --strict;
fi
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ could simply call `Poison.Parser.parse`.
## Parser

```iex
iex> Poison.Parser.parse!(~s({"name": "Devin Torres", "age": 27}))
iex> Poison.Parser.parse!(~s({"name": "Devin Torres", "age": 27}), %{})
%{"name" => "Devin Torres", "age" => 27}
iex> Poison.Parser.parse!(~s({"name": "Devin Torres", "age": 27}), keys: :atoms!)
iex> Poison.Parser.parse!(~s({"name": "Devin Torres", "age": 27}), %{keys: :atoms!})
%{name: "Devin Torres", age: 27}
```

Expand All @@ -90,7 +90,7 @@ for more info:
## Encoder

```iex
iex> IO.puts Poison.Encoder.encode([1, 2, 3], [])
iex> Poison.Encoder.encode([1, 2, 3], %{}) |> IO.iodata_to_binary
"[1,2,3]"
```

Expand Down Expand Up @@ -148,7 +148,7 @@ ensure that your generated JSON doesn't have this issue, you can pass the

```iex
iex> Poison.encode!(%{:foo => "foo1", "foo" => "foo2"}, strict_keys: true)
** (Poison.EncodeError) duplicate key found: "foo"
** (Poison.EncodeError) duplicate key found: :foo
```

## Benchmarking
Expand Down
10 changes: 7 additions & 3 deletions lib/poison.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
defmodule Poison do
alias Poison.{Encoder, EncodeError}
alias Poison.{Parser, ParseError}
alias Poison.{Decode, Decoder, DecodeError}
@readme_path [__DIR__, "..", "README.md"] |> Path.join |> Path.expand
@external_resource @readme_path
@moduledoc @readme_path |> File.read! |> String.trim

alias Poison.{Decode, DecodeError, Decoder}
alias Poison.{EncodeError, Encoder}
alias Poison.{ParseError, Parser}

@doc """
Encode a value to JSON.
Expand Down
8 changes: 4 additions & 4 deletions lib/poison/encoder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ end
defmodule Poison.Encode do
@moduledoc false

alias Poison.{Encoder, EncodeError}
alias Poison.{EncodeError, Encoder}

defmacro __using__(_) do
quote do
alias String.Chars
alias Poison.EncodeError
alias String.Chars

@compile {:inline, encode_name: 1}

Expand Down Expand Up @@ -53,7 +53,7 @@ defmodule Poison.Pretty do
@compile {:inline, pretty: 1, indent: 1, offset: 1, offset: 2, spaces: 1}

defp pretty(options) do
!!Map.get(options, :pretty)
Map.get(options, :pretty) == true
end

defp indent(options) do
Expand Down Expand Up @@ -141,7 +141,7 @@ defimpl Poison.Encoder, for: BitString do
end

# http://en.wikipedia.org/wiki/UTF-16#Example_UTF-16_encoding_procedure
# http://unicodebook.readthedocs.org/unicode_encodings.html#utf-16-surrogate-pairs
# http://unicodebook.readthedocs.org/unicode_encodings.html
defp escape(<<char :: utf8>> <> rest, :unicode) when char > 0xFFFF do
code = char - 0x10000
[seq(0xD800 ||| (code >>> 10)),
Expand Down
25 changes: 17 additions & 8 deletions lib/poison/parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ defmodule Poison.Parser do

@type t :: nil | true | false | list | float | integer | String.t | map

defmacrop stacktrace do
if Version.compare(System.version, "1.7.0") != :lt do
quote do: __STACKTRACE__
else
quote do: System.stacktrace()
end
end

def parse!(iodata, options) do
string = IO.iodata_to_binary(iodata)
keys = Map.get(options, :keys)
Expand All @@ -59,8 +67,8 @@ defmodule Poison.Parser do
{other, pos} -> syntax_error(other, pos)
end
rescue
_ in [ArgumentError] ->
raise %ParseError{value: iodata}
ArgumentError ->
reraise %ParseError{value: iodata}, stacktrace()
end

defp value("\"" <> rest, pos, _keys) do
Expand Down Expand Up @@ -119,8 +127,8 @@ defmodule Poison.Parser do
defp object_name(name, pos, :atoms!) do
String.to_existing_atom(name)
rescue
_ in [ArgumentError] ->
raise %ParseError{value: name, pos: pos}
ArgumentError ->
reraise %ParseError{value: name, pos: pos}, stacktrace()
end

defp object_name(name, _pos, :atoms), do: String.to_atom(name)
Expand Down Expand Up @@ -208,7 +216,7 @@ defmodule Poison.Parser do
rescue
ArgumentError ->
value = iolist |> IO.iodata_to_binary
raise %ParseError{pos: pos, value: value}
reraise %ParseError{pos: pos, value: value}, stacktrace()
end

defp number_digits(<<char>> <> rest = string, pos) when char in '0123456789' do
Expand Down Expand Up @@ -264,15 +272,16 @@ defmodule Poison.Parser do
rescue
ArgumentError ->
value = <<"\\u", a1, b1, c1, d1, "\\u", a2, b2, c2, d2>>
raise %ParseError{pos: pos + 12, value: value}
reraise %ParseError{pos: pos + 12, value: value}, stacktrace()
end

defp string_escape(<<?u, seq :: binary-size(4)>> <> rest, pos, acc) do
string_continue(rest, pos + 5, [acc, <<String.to_integer(seq, 16) :: utf8>>])
code = String.to_integer(seq, 16)
string_continue(rest, pos + 5, [acc, <<code :: utf8>>])
rescue
ArgumentError ->
value = "\\u" <> seq
raise %ParseError{pos: pos + 6, value: value}
reraise %ParseError{pos: pos + 6, value: value}, stacktrace()
end

defp string_escape(other, pos, _), do: syntax_error(other, pos)
Expand Down
4 changes: 3 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
defmodule Poison.Mixfile do
use Mix.Project

@version File.read!("VERSION") |> String.trim
@version_path Path.join([__DIR__, "VERSION"])
@external_resource @version_path
@version @version_path |> File.read! |> String.trim

def project do
[app: :poison,
Expand Down
Loading

0 comments on commit ee06841

Please sign in to comment.