@@ -248,6 +248,49 @@ defmodule Code do
248
248
"""
249
249
@ type position ( ) :: line ( ) | { line :: pos_integer ( ) , column :: pos_integer ( ) }
250
250
251
+ @ typedoc """
252
+ Options for code formatting functions.
253
+ """
254
+ @ type format_opts :: [
255
+ file: binary ( ) ,
256
+ line: pos_integer ( ) ,
257
+ line_length: pos_integer ( ) ,
258
+ locals_without_parens: keyword ( ) ,
259
+ force_do_end_blocks: boolean ( ) ,
260
+ migrate: boolean ( ) ,
261
+ migrate_bitstring_modifiers: boolean ( ) ,
262
+ migrate_call_parens_on_pipe: boolean ( ) ,
263
+ migrate_charlists_as_sigils: boolean ( ) ,
264
+ migrate_unless: boolean ( )
265
+ ]
266
+
267
+ @ typedoc """
268
+ Options for parsing functions that convert strings to quoted expressions.
269
+ """
270
+ @ type parser_opts :: [
271
+ file: binary ( ) ,
272
+ line: pos_integer ( ) ,
273
+ column: pos_integer ( ) ,
274
+ indentation: non_neg_integer ( ) ,
275
+ columns: boolean ( ) ,
276
+ unescape: boolean ( ) ,
277
+ existing_atoms_only: boolean ( ) ,
278
+ token_metadata: boolean ( ) ,
279
+ literal_encoder: ( term ( ) , Macro . metadata ( ) -> term ( ) ) ,
280
+ static_atoms_encoder: ( atom ( ) -> term ( ) ) ,
281
+ emit_warnings: boolean ( )
282
+ ]
283
+
284
+ @ typedoc """
285
+ Options for environment evaluation functions like eval_string/3 and eval_quoted/3.
286
+ """
287
+ @ type env_eval_opts :: [
288
+ file: binary ( ) ,
289
+ line: pos_integer ( ) ,
290
+ module: module ( ) ,
291
+ prune_binding: boolean ( )
292
+ ]
293
+
251
294
@ boolean_compiler_options [
252
295
:docs ,
253
296
:debug_info ,
@@ -560,7 +603,7 @@ defmodule Code do
560
603
[a: 1, b: 2]
561
604
562
605
"""
563
- @ spec eval_string ( List.Chars . t ( ) , binding , Macro.Env . t ( ) | keyword ) :: { term , binding }
606
+ @ spec eval_string ( List.Chars . t ( ) , binding , Macro.Env . t ( ) | env_eval_opts ) :: { term , binding }
564
607
def eval_string ( string , binding \\ [ ] , opts \\ [ ] )
565
608
566
609
def eval_string ( string , binding , % Macro.Env { } = env ) do
@@ -615,7 +658,8 @@ defmodule Code do
615
658
616
659
"""
617
660
@ doc since: "1.15.0"
618
- @ spec with_diagnostics ( keyword ( ) , ( -> result ) ) :: { result , [ diagnostic ( :warning | :error ) ] }
661
+ @ spec with_diagnostics ( [ log: boolean ( ) ] , ( -> result ) ) ::
662
+ { result , [ diagnostic ( :warning | :error ) ] }
619
663
when result: term ( )
620
664
def with_diagnostics ( opts \\ [ ] , fun ) do
621
665
value = :erlang . get ( :elixir_code_diagnostics )
@@ -648,7 +692,7 @@ defmodule Code do
648
692
Defaults to `true`.
649
693
"""
650
694
@ doc since: "1.15.0"
651
- @ spec print_diagnostic ( diagnostic ( :warning | :error ) , keyword ( ) ) :: :ok
695
+ @ spec print_diagnostic ( diagnostic ( :warning | :error ) , snippet: boolean ( ) ) :: :ok
652
696
def print_diagnostic ( diagnostic , opts \\ [ ] ) do
653
697
read_snippet? = Keyword . get ( opts , :snippet , true )
654
698
:elixir_errors . print_diagnostic ( diagnostic , read_snippet? )
@@ -1035,7 +1079,7 @@ defmodule Code do
1035
1079
address the deprecation warnings.
1036
1080
"""
1037
1081
@ doc since: "1.6.0"
1038
- @ spec format_string! ( binary , keyword ) :: iodata
1082
+ @ spec format_string! ( binary , format_opts ) :: iodata
1039
1083
def format_string! ( string , opts \\ [ ] ) when is_binary ( string ) and is_list ( opts ) do
1040
1084
line_length = Keyword . get ( opts , :line_length , 98 )
1041
1085
@@ -1060,7 +1104,7 @@ defmodule Code do
1060
1104
available options.
1061
1105
"""
1062
1106
@ doc since: "1.6.0"
1063
- @ spec format_file! ( binary , keyword ) :: iodata
1107
+ @ spec format_file! ( binary , format_opts ) :: iodata
1064
1108
def format_file! ( file , opts \\ [ ] ) when is_binary ( file ) and is_list ( opts ) do
1065
1109
string = File . read! ( file )
1066
1110
formatted = format_string! ( string , [ file: file , line: 1 ] ++ opts )
@@ -1098,7 +1142,7 @@ defmodule Code do
1098
1142
[a: 1, b: 2]
1099
1143
1100
1144
"""
1101
- @ spec eval_quoted ( Macro . t ( ) , binding , Macro.Env . t ( ) | keyword ) :: { term , binding }
1145
+ @ spec eval_quoted ( Macro . t ( ) , binding , Macro.Env . t ( ) | env_eval_opts ) :: { term , binding }
1102
1146
def eval_quoted ( quoted , binding \\ [ ] , env_or_opts \\ [ ] ) do
1103
1147
{ value , binding , _env } =
1104
1148
eval_verify ( :eval_quoted , [ quoted , binding , env_for_eval ( env_or_opts ) ] )
@@ -1129,8 +1173,15 @@ defmodule Code do
1129
1173
* `:line` - the line on which the script starts
1130
1174
1131
1175
* `:module` - the module to run the environment on
1176
+
1177
+ * `:prune_binding` - (since v1.14.2) prune binding to keep only
1178
+ variables read or written by the evaluated code. Note that
1179
+ variables used by modules are always pruned, even if later used
1180
+ by the modules. You can submit to the `:on_module` tracer event
1181
+ and access the variables used by the module from its environment.
1132
1182
"""
1133
1183
@ doc since: "1.14.0"
1184
+ @ spec env_for_eval ( Macro.Env . t ( ) | env_eval_opts ) :: Macro.Env . t ( )
1134
1185
def env_for_eval ( env_or_opts ) , do: :elixir . env_for_eval ( env_or_opts )
1135
1186
1136
1187
@ doc """
@@ -1144,15 +1195,11 @@ defmodule Code do
1144
1195
1145
1196
## Options
1146
1197
1147
- * `:prune_binding` - (since v1.14.2) prune binding to keep only
1148
- variables read or written by the evaluated code. Note that
1149
- variables used by modules are always pruned, even if later used
1150
- by the modules. You can submit to the `:on_module` tracer event
1151
- and access the variables used by the module from its environment.
1198
+ It accepts the same options as `env_for_eval/1`.
1152
1199
1153
1200
"""
1154
1201
@ doc since: "1.14.0"
1155
- @ spec eval_quoted_with_env ( Macro . t ( ) , binding , Macro.Env . t ( ) , keyword ) ::
1202
+ @ spec eval_quoted_with_env ( Macro . t ( ) , binding , Macro.Env . t ( ) , env_eval_opts ) ::
1156
1203
{ term , binding , Macro.Env . t ( ) }
1157
1204
def eval_quoted_with_env ( quoted , binding , % Macro.Env { } = env , opts \\ [ ] )
1158
1205
when is_list ( binding ) do
@@ -1263,7 +1310,7 @@ defmodule Code do
1263
1310
{:error, {[line: 1, column: 4], "syntax error before: ", "\" 3\" "}}
1264
1311
1265
1312
"""
1266
- @ spec string_to_quoted ( List.Chars . t ( ) , keyword ) ::
1313
+ @ spec string_to_quoted ( List.Chars . t ( ) , parser_opts ) ::
1267
1314
{ :ok , Macro . t ( ) } | { :error , { location :: keyword , binary | { binary , binary } , binary } }
1268
1315
def string_to_quoted ( string , opts \\ [ ] ) when is_list ( opts ) do
1269
1316
file = Keyword . get ( opts , :file , "nofile" )
@@ -1290,7 +1337,7 @@ defmodule Code do
1290
1337
1291
1338
Check `string_to_quoted/2` for options information.
1292
1339
"""
1293
- @ spec string_to_quoted! ( List.Chars . t ( ) , keyword ) :: Macro . t ( )
1340
+ @ spec string_to_quoted! ( List.Chars . t ( ) , parser_opts ) :: Macro . t ( )
1294
1341
def string_to_quoted! ( string , opts \\ [ ] ) when is_list ( opts ) do
1295
1342
file = Keyword . get ( opts , :file , "nofile" )
1296
1343
line = Keyword . get ( opts , :line , 1 )
@@ -1341,7 +1388,7 @@ defmodule Code do
1341
1388
1342
1389
"""
1343
1390
@ doc since: "1.13.0"
1344
- @ spec string_to_quoted_with_comments ( List.Chars . t ( ) , keyword ) ::
1391
+ @ spec string_to_quoted_with_comments ( List.Chars . t ( ) , parser_opts ) ::
1345
1392
{ :ok , Macro . t ( ) , list ( map ( ) ) } | { :error , { location :: keyword , term , term } }
1346
1393
def string_to_quoted_with_comments ( string , opts \\ [ ] ) when is_list ( opts ) do
1347
1394
charlist = to_charlist ( string )
@@ -1371,7 +1418,7 @@ defmodule Code do
1371
1418
Check `string_to_quoted/2` for options information.
1372
1419
"""
1373
1420
@ doc since: "1.13.0"
1374
- @ spec string_to_quoted_with_comments! ( List.Chars . t ( ) , keyword ) :: { Macro . t ( ) , list ( map ( ) ) }
1421
+ @ spec string_to_quoted_with_comments! ( List.Chars . t ( ) , parser_opts ) :: { Macro . t ( ) , list ( map ( ) ) }
1375
1422
def string_to_quoted_with_comments! ( string , opts \\ [ ] ) do
1376
1423
charlist = to_charlist ( string )
1377
1424
@@ -1456,6 +1503,9 @@ defmodule Code do
1456
1503
1457
1504
## Options
1458
1505
1506
+ This function accepts all options supported by `format_string!/2` for controlling
1507
+ code formatting, plus these additional options:
1508
+
1459
1509
* `:comments` - the list of comments associated with the quoted expression.
1460
1510
Defaults to `[]`. It is recommended that both `:token_metadata` and
1461
1511
`:literal_encoder` options are given to `string_to_quoted_with_comments/2`
@@ -1466,17 +1516,14 @@ defmodule Code do
1466
1516
`string_to_quoted/2`, setting this option to `false` will prevent it from
1467
1517
escaping the sequences twice. Defaults to `true`.
1468
1518
1469
- * `:locals_without_parens` - a keyword list of name and arity
1470
- pairs that should be kept without parens whenever possible.
1471
- The arity may be the atom `:*`, which implies all arities of
1472
- that name. The formatter already includes a list of functions
1473
- and this option augments this list.
1474
-
1475
- * `:syntax_colors` - a keyword list of colors the output is colorized.
1476
- See `Inspect.Opts` for more information.
1519
+ See `format_string!/2` for the full list of formatting options including
1520
+ `:file`, `:line`, `:line_length`, `:locals_without_parens`, `:force_do_end_blocks`,
1521
+ `:syntax_colors`, and all migration options like `:migrate_charlists_as_sigils`.
1477
1522
"""
1478
1523
@ doc since: "1.13.0"
1479
- @ spec quoted_to_algebra ( Macro . t ( ) , keyword ) :: Inspect.Algebra . t ( )
1524
+ @ spec quoted_to_algebra ( Macro . t ( ) , [
1525
+ Code.Formatter . to_algebra_opt ( ) | Code.Normalizer . normalize_opt ( )
1526
+ ] ) :: Inspect.Algebra . t ( )
1480
1527
def quoted_to_algebra ( quoted , opts \\ [ ] ) do
1481
1528
quoted
1482
1529
|> Code.Normalizer . normalize ( opts )
0 commit comments