@@ -2328,6 +2328,15 @@ defmodule Macro do
23282328 as a key (`:key`), or as the function name of a remote call
23292329 (`:remote_call`).
23302330
2331+ ## Options
2332+
2333+ * `:escape` - a two-arity function used to escape a quoted
2334+ atom content, if necessary. The function receives the atom
2335+ content as string and a quote delimiter character, which
2336+ should always be escaped. By default the content is escaped
2337+ such that the inspected sequence would be parsed as the
2338+ given atom.
2339+
23312340 ## Examples
23322341
23332342 ### As a literal
@@ -2376,14 +2385,14 @@ defmodule Macro do
23762385
23772386 """
23782387 @ doc since: "1.14.0"
2379- @ spec inspect_atom ( :literal | :key | :remote_call , atom ) :: binary
2380- def inspect_atom ( source_format , atom )
2388+ @ spec inspect_atom ( :literal | :key | :remote_call , atom , keyword ) :: binary
2389+ def inspect_atom ( source_format , atom , opts \\ [ ] )
23812390
2382- def inspect_atom ( :literal , atom ) when is_nil ( atom ) or is_boolean ( atom ) do
2391+ def inspect_atom ( :literal , atom , _opts ) when is_nil ( atom ) or is_boolean ( atom ) do
23832392 Atom . to_string ( atom )
23842393 end
23852394
2386- def inspect_atom ( :literal , atom ) when is_atom ( atom ) do
2395+ def inspect_atom ( :literal , atom , opts ) when is_atom ( atom ) do
23872396 binary = Atom . to_string ( atom )
23882397
23892398 case classify_atom ( atom ) do
@@ -2395,31 +2404,31 @@ defmodule Macro do
23952404 end
23962405
23972406 :quoted ->
2398- { escaped , _ } = Code.Identifier . escape ( binary , ?" )
2407+ escaped = inspect_atom_escape ( opts , binary , ?" )
23992408 IO . iodata_to_binary ( [ ?: , ?" , escaped , ?" ] )
24002409
24012410 _ ->
24022411 ":" <> binary
24032412 end
24042413 end
24052414
2406- def inspect_atom ( :key , atom ) when is_atom ( atom ) do
2415+ def inspect_atom ( :key , atom , opts ) when is_atom ( atom ) do
24072416 binary = Atom . to_string ( atom )
24082417
24092418 case classify_atom ( atom ) do
24102419 :alias ->
24112420 IO . iodata_to_binary ( [ ?" , binary , ?" , ?: ] )
24122421
24132422 :quoted ->
2414- { escaped , _ } = Code.Identifier . escape ( binary , ?" )
2423+ escaped = inspect_atom_escape ( opts , binary , ?" )
24152424 IO . iodata_to_binary ( [ ?" , escaped , ?" , ?: ] )
24162425
24172426 _ ->
24182427 IO . iodata_to_binary ( [ binary , ?: ] )
24192428 end
24202429 end
24212430
2422- def inspect_atom ( :remote_call , atom ) when is_atom ( atom ) do
2431+ def inspect_atom ( :remote_call , atom , opts ) when is_atom ( atom ) do
24232432 binary = Atom . to_string ( atom )
24242433
24252434 case inner_classify ( atom ) do
@@ -2431,13 +2440,22 @@ defmodule Macro do
24312440 if type in [ :not_callable , :alias ] do
24322441 binary
24332442 else
2434- elem ( Code.Identifier . escape ( binary , ?" ) , 0 )
2443+ inspect_atom_escape ( opts , binary , ?" )
24352444 end
24362445
24372446 IO . iodata_to_binary ( [ ?" , escaped , ?" ] )
24382447 end
24392448 end
24402449
2450+ defp inspect_atom_escape ( opts , string , char ) do
2451+ if escape = opts [ :escape ] do
2452+ escape . ( string , char )
2453+ else
2454+ { escaped , _ } = Code.Identifier . escape ( string , char )
2455+ escaped
2456+ end
2457+ end
2458+
24412459 # Classifies the given atom into one of the following categories:
24422460 #
24432461 # * `:alias` - a valid Elixir alias, like `Foo`, `Foo.Bar` and so on
0 commit comments