Skip to content

Commit ee8336f

Browse files
committed
Update Credo compliance
1 parent 0af426e commit ee8336f

11 files changed

+141
-64
lines changed

.credo.exs

+28-13
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
# You can give explicit globs or simply directories.
2222
# In the latter case `**/*.{ex,exs}` will be used.
2323
#
24-
included: ["lib/", "src/", "web/", "apps/"],
25-
excluded: [~r"/_build/", ~r"/deps/"]
24+
included: ["lib/", "src/", "test/", "web/", "apps/", "bench/", "mix.exs"],
25+
excluded: [~r"/_build/", ~r"/deps/", ~r"/node_modules/"]
2626
},
2727
#
2828
# If you create your own checks, you must specify the source files for
@@ -33,7 +33,7 @@
3333
# If you want to enforce a style guide and need a more traditional linting
3434
# experience, you can change `strict` to `true` below:
3535
#
36-
strict: false,
36+
strict: true,
3737
#
3838
# If you want to use uncolored output by default, you can change `color`
3939
# to `false` below:
@@ -48,38 +48,46 @@
4848
# {Credo.Check.Design.DuplicatedCode, false}
4949
#
5050
checks: [
51+
#
52+
## Consistency Checks
53+
#
5154
{Credo.Check.Consistency.ExceptionNames},
5255
{Credo.Check.Consistency.LineEndings},
5356
{Credo.Check.Consistency.ParameterPatternMatching},
5457
{Credo.Check.Consistency.SpaceAroundOperators},
5558
{Credo.Check.Consistency.SpaceInParentheses},
5659
{Credo.Check.Consistency.TabsOrSpaces},
5760

61+
#
62+
## Design Checks
63+
#
5864
# You can customize the priority of any check
5965
# Priority values are: `low, normal, high, higher`
6066
#
6167
{Credo.Check.Design.AliasUsage, priority: :low},
62-
6368
# For some checks, you can also set other parameters
6469
#
6570
# If you don't want the `setup` and `test` macro calls in ExUnit tests
6671
# or the `schema` macro in Ecto schemas to trigger DuplicatedCode, just
6772
# set the `excluded_macros` parameter to `[:schema, :setup, :test]`.
6873
#
6974
{Credo.Check.Design.DuplicatedCode, excluded_macros: []},
70-
7175
# You can also customize the exit_status of each check.
7276
# If you don't want TODO comments to cause `mix credo` to fail, just
7377
# set this value to 0 (zero).
7478
#
7579
{Credo.Check.Design.TagTODO, exit_status: 2},
7680
{Credo.Check.Design.TagFIXME},
7781

82+
#
83+
## Readability Checks
84+
#
85+
{Credo.Check.Readability.AliasOrder},
7886
{Credo.Check.Readability.FunctionNames},
7987
{Credo.Check.Readability.LargeNumbers},
8088
{Credo.Check.Readability.MaxLineLength, priority: :low, max_length: 80},
8189
{Credo.Check.Readability.ModuleAttributeNames},
82-
{Credo.Check.Readability.ModuleDoc, false},
90+
{Credo.Check.Readability.ModuleDoc},
8391
{Credo.Check.Readability.ModuleNames},
8492
{Credo.Check.Readability.ParenthesesOnZeroArityDefs},
8593
{Credo.Check.Readability.ParenthesesInCondition},
@@ -93,18 +101,26 @@
93101
{Credo.Check.Readability.Semicolons},
94102
{Credo.Check.Readability.SpaceAfterCommas},
95103

104+
#
105+
## Refactoring Opportunities
106+
#
96107
{Credo.Check.Refactor.DoubleBooleanNegation},
97108
{Credo.Check.Refactor.CondStatements},
98109
{Credo.Check.Refactor.CyclomaticComplexity},
99110
{Credo.Check.Refactor.FunctionArity},
100111
{Credo.Check.Refactor.LongQuoteBlocks},
112+
{Credo.Check.Refactor.MapInto},
101113
{Credo.Check.Refactor.MatchInCondition},
102114
{Credo.Check.Refactor.NegatedConditionsInUnless},
103115
{Credo.Check.Refactor.NegatedConditionsWithElse},
104116
{Credo.Check.Refactor.Nesting},
105-
{Credo.Check.Refactor.PipeChainStart},
117+
{Credo.Check.Refactor.PipeChainStart,
118+
excluded_argument_types: [:atom, :binary, :fn, :keyword], excluded_functions: []},
106119
{Credo.Check.Refactor.UnlessWithElse},
107120

121+
#
122+
## Warnings
123+
#
108124
{Credo.Check.Warning.BoolOperationOnSameValues},
109125
{Credo.Check.Warning.ExpensiveEmptyEnumCheck},
110126
{Credo.Check.Warning.IExPry},
@@ -120,8 +136,9 @@
120136
{Credo.Check.Warning.UnusedRegexOperation},
121137
{Credo.Check.Warning.UnusedStringOperation},
122138
{Credo.Check.Warning.UnusedTupleOperation},
123-
{Credo.Check.Warning.RaiseInsideRescue, false},
139+
{Credo.Check.Warning.RaiseInsideRescue},
124140

141+
#
125142
# Controversial and experimental checks (opt-in, just remove `, false`)
126143
#
127144
{Credo.Check.Refactor.ABCSize, false},
@@ -130,14 +147,12 @@
130147
{Credo.Check.Warning.MapGetUnsafePass, false},
131148
{Credo.Check.Consistency.MultiAliasImportRequireUse, false},
132149

150+
#
133151
# Deprecated checks (these will be deleted after a grace period)
134152
#
135-
{Credo.Check.Readability.Specs, false},
136-
{Credo.Check.Warning.NameRedeclarationByAssignment, false},
137-
{Credo.Check.Warning.NameRedeclarationByCase, false},
138-
{Credo.Check.Warning.NameRedeclarationByDef, false},
139-
{Credo.Check.Warning.NameRedeclarationByFn, false},
153+
{Credo.Check.Readability.Specs, false}
140154

155+
#
141156
# Custom checks can be created using `mix credo.gen.check`.
142157
#
143158
]

.tool-versions

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
erlang 21.0.4
2-
elixir 1.7.0
2+
elixir 1.7.1

.travis.yml

+9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
language: elixir
22
elixir:
3+
- 1.7.1
34
- 1.7.0
45
- 1.6.6
56
- 1.6.5
@@ -9,3 +10,11 @@ otp_release:
910
- 20.1
1011
- 20.0
1112
sudo: false
13+
before_script:
14+
- |
15+
if [ "$TRAVIS_ELIXIR_VERSION|$TRAVIS_OTP_RELEASE" == "1.7.1|21.0" ]; then
16+
set -e;
17+
mix archive.install hex bunt '~> 0.2' --force;
18+
mix archive.install hex credo '~> 0.9' --force;
19+
mix credo --strict;
20+
fi

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ could simply call `Poison.Parser.parse`.
6868
## Parser
6969

7070
```iex
71-
iex> Poison.Parser.parse!(~s({"name": "Devin Torres", "age": 27}))
71+
iex> Poison.Parser.parse!(~s({"name": "Devin Torres", "age": 27}), %{})
7272
%{"name" => "Devin Torres", "age" => 27}
73-
iex> Poison.Parser.parse!(~s({"name": "Devin Torres", "age": 27}), keys: :atoms!)
73+
iex> Poison.Parser.parse!(~s({"name": "Devin Torres", "age": 27}), %{keys: :atoms!})
7474
%{name: "Devin Torres", age: 27}
7575
```
7676

@@ -90,7 +90,7 @@ for more info:
9090
## Encoder
9191

9292
```iex
93-
iex> IO.puts Poison.Encoder.encode([1, 2, 3], [])
93+
iex> Poison.Encoder.encode([1, 2, 3], %{}) |> IO.iodata_to_binary
9494
"[1,2,3]"
9595
```
9696

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

149149
```iex
150150
iex> Poison.encode!(%{:foo => "foo1", "foo" => "foo2"}, strict_keys: true)
151-
** (Poison.EncodeError) duplicate key found: "foo"
151+
** (Poison.EncodeError) duplicate key found: :foo
152152
```
153153

154154
## Benchmarking

lib/poison.ex

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
defmodule Poison do
2-
alias Poison.{Encoder, EncodeError}
3-
alias Poison.{Parser, ParseError}
4-
alias Poison.{Decode, Decoder, DecodeError}
2+
@readme_path [__DIR__, "..", "README.md"] |> Path.join |> Path.expand
3+
@external_resource @readme_path
4+
@moduledoc @readme_path |> File.read! |> String.trim
5+
6+
alias Poison.{Decode, DecodeError, Decoder}
7+
alias Poison.{EncodeError, Encoder}
8+
alias Poison.{ParseError, Parser}
59

610
@doc """
711
Encode a value to JSON.

lib/poison/encoder.ex

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ end
1515
defmodule Poison.Encode do
1616
@moduledoc false
1717

18-
alias Poison.{Encoder, EncodeError}
18+
alias Poison.{EncodeError, Encoder}
1919

2020
defmacro __using__(_) do
2121
quote do
22-
alias String.Chars
2322
alias Poison.EncodeError
23+
alias String.Chars
2424

2525
@compile {:inline, encode_name: 1}
2626

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

5555
defp pretty(options) do
56-
!!Map.get(options, :pretty)
56+
Map.get(options, :pretty) == true
5757
end
5858

5959
defp indent(options) do
@@ -141,7 +141,7 @@ defimpl Poison.Encoder, for: BitString do
141141
end
142142

143143
# http://en.wikipedia.org/wiki/UTF-16#Example_UTF-16_encoding_procedure
144-
# http://unicodebook.readthedocs.org/unicode_encodings.html#utf-16-surrogate-pairs
144+
# http://unicodebook.readthedocs.org/unicode_encodings.html
145145
defp escape(<<char :: utf8>> <> rest, :unicode) when char > 0xFFFF do
146146
code = char - 0x10000
147147
[seq(0xD800 ||| (code >>> 10)),

lib/poison/parser.ex

+17-8
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ defmodule Poison.Parser do
4949

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

52+
defmacrop stacktrace do
53+
if Version.compare(System.version, "1.7.0") != :lt do
54+
quote do: __STACKTRACE__
55+
else
56+
quote do: System.stacktrace()
57+
end
58+
end
59+
5260
def parse!(iodata, options) do
5361
string = IO.iodata_to_binary(iodata)
5462
keys = Map.get(options, :keys)
@@ -59,8 +67,8 @@ defmodule Poison.Parser do
5967
{other, pos} -> syntax_error(other, pos)
6068
end
6169
rescue
62-
_ in [ArgumentError] ->
63-
raise %ParseError{value: iodata}
70+
ArgumentError ->
71+
reraise %ParseError{value: iodata}, stacktrace()
6472
end
6573

6674
defp value("\"" <> rest, pos, _keys) do
@@ -119,8 +127,8 @@ defmodule Poison.Parser do
119127
defp object_name(name, pos, :atoms!) do
120128
String.to_existing_atom(name)
121129
rescue
122-
_ in [ArgumentError] ->
123-
raise %ParseError{value: name, pos: pos}
130+
ArgumentError ->
131+
reraise %ParseError{value: name, pos: pos}, stacktrace()
124132
end
125133

126134
defp object_name(name, _pos, :atoms), do: String.to_atom(name)
@@ -208,7 +216,7 @@ defmodule Poison.Parser do
208216
rescue
209217
ArgumentError ->
210218
value = iolist |> IO.iodata_to_binary
211-
raise %ParseError{pos: pos, value: value}
219+
reraise %ParseError{pos: pos, value: value}, stacktrace()
212220
end
213221

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

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

278287
defp string_escape(other, pos, _), do: syntax_error(other, pos)

mix.exs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
defmodule Poison.Mixfile do
22
use Mix.Project
33

4-
@version File.read!("VERSION") |> String.trim
4+
@version_path Path.join([__DIR__, "VERSION"])
5+
@external_resource @version_path
6+
@version @version_path |> File.read! |> String.trim
57

68
def project do
79
[app: :poison,

0 commit comments

Comments
 (0)