Commit Graph
156 Commits
Author SHA1 Message Date
Eksperimental f1eca78ca4 Improve docs in Module functions and raise errors (#8635) 2019-01-21 18:12:51 +01:00
José Valim 0c1df34645 Standardize more TODOs 2018-12-21 13:42:54 +01:00
Fernando Tapia Rico d58340d7ab Omit parens on calls with only do/end blocks (Macro.to_string) (#8396) 2018-11-16 20:51:14 +01:00
José Valim 6c2dfed036 Add Macro.struct!/2 and use it where reasonable 2018-11-13 20:03:31 +01:00
José Valim f01de95cb2 Raise when piping into unquote, closes #8331 2018-10-25 10:01:10 +02:00
José Valim 78ed8a55a3 Only add context if vars hygiene is enable 2018-08-09 10:33:15 +02:00
José Valim a2d81db23b Delete counter for vars in nested quotes (#8042)
Consider the following example:

    defmodule NestedQuote do
      defmacrop macro(arg) do
        quote bind_quoted: [arg: arg] do
          quote bind_quoted: [arg: arg], do: String.duplicate(arg, 2)
        end
      end

      defmacro __using__(_) do
        quote do
          def test do
            unquote(macro("foo"))
          end
        end
      end
    end

    defmodule UseNestedQuote do
      use NestedQuote
    end

It has two macro expansions, one inside __using__/1 when it invokes
macro/1, and another when used. The issue, however, is that the
`arg` of `String.duplicate(arg, 2)` gets tagged with the context
of `macro/1` but it should be tagged with the context of `__using__`
since it is a quote inside a quote. This patch addresses this.
2018-08-04 16:46:49 +02:00
José Valim 34befaaa36 Allow to prune metadata while escaping (#7949)
We use this option in ExUnit as we are escaping
the code to eventually convert it to a string
representation and therefore the metadata is not
relevant.
2018-07-21 12:56:07 +02:00
José Valim 5ab60299b5 Do not break assertion arguments apart if they are all quoted literals 2018-06-21 16:41:05 +02:00
Tonći Galić 1db1f28b35 Raise if piping into bitstring operator <<>> (#7725) 2018-06-04 13:41:06 +02:00
José Valim f20774547b Make sure Macro.to_string/2 emits valid quoted expressions 2018-04-04 12:50:58 +02:00
Jay Hayes ed6c62fd83 Move doctest Macro to Macro module test file (#7408) 2018-03-01 17:50:35 +01:00
Gustavo Saiani fe1319e393 Fix two Macro.to_string tests (#7296) 2018-02-01 10:27:17 -02:00
James Fish ef1f406d25 Reorder kw blocks in Macro.to_string (#7225) 2018-01-18 22:52:29 +01:00
José Valim 889ae9afbb Do not leak variables during optimizations, closes #7161 2018-01-01 12:04:15 +01:00
Eric Meadows-Jönsson d8a533d19f assert_receive should expand in match context (#7042)
Closes #7041.
2017-11-19 07:24:25 -02:00
Chris Keele 6ab811c638 Adds Kernel.defguard/1 macro to build guard-safe macros
This makes defining guards easier, since it does the boilerplate
for you and raises at compile time in the defguard macro when
you do something not allowed in guards (as opposed to whenever
the new guard happens to be used as a guard).

It takes a `defguard name(args) when implementation` invocation,
passes the implementation through `:elixir_expand.expand/2` in
a guard context, and if nothing is raised, transforms the impl
into a macro that works both inside and outside of guard contexts.

Signed-off-by: José Valim <jose.valim@plataformatec.com.br>
2017-11-06 12:39:53 +01:00
José Valim db44cd2ddc Ensure file in stacktraces is a charlist 2017-11-03 22:25:07 +01:00
Andrea Leopardi 9c694b0037 Run the code formatter on MacroTest 2017-10-12 00:41:04 +02:00
Andrea Leopardi d599d0d674 WIP 2017-10-12 00:40:21 +02:00
José Valim 3e7966806e Improve coverage on Macro.to_string 2017-08-28 10:30:04 +02:00
José Valim 7c4c6a5e97 Wrap unary operator in parens as well 2017-08-24 00:23:12 +02:00
Andrea Leopardi 82826dceec Fix Macro.to_string/2 for tuple calls (#6418)
Calls such as "Foo.{Bar, Baz}" (used in "alias Foo.{Bar, Baz}" for
example) were badly formatted by Macro.to_string/2. That's fixed in this
commit.
2017-08-02 12:30:48 +02:00
Aleksei Magusev 27032a3652 Remove duplicate test in MacroTest 2017-04-20 16:38:15 +02:00
Aleksei Magusev d675f59c5c Tidy up Macro test suite 2017-04-20 16:35:49 +02:00
Aleksei Magusev 5904879412 Fix quoting of bitstring literals 2017-04-20 16:35:49 +02:00
Keith Salisbury 4179976ad9 Fix use of Prepositions
Prepositions can be tricky as there is no definite rule or formula for
choosing. In general we use `in` when talking about something inside the
file or module, such as a function.

Signed-off-by: José Valim <jose.valim@plataformatec.com.br>
2017-04-16 17:02:02 +02:00
José Valim fb588574a5 Fix camelize mechanism for uppercase chars 2017-02-20 12:31:53 +01:00
Felipe Seré b1721adf75 Introduce Macro.generate_arguments/2 (#5699) 2017-01-25 10:14:13 +01:00
Andrea Leopardi d5324cf8ac Correctly inspect function calls with weird atoms (#5629)
As of today, in many cases we got inspection of functions wrong. For example,
Macro.to_string/2 failed to properly escape functions resulting in cases like:

    iex> Macro.to_string(quote(do: Foo."42"()))
    "Foo.42()"

where we output a representation of the code that is not syntactically valid. A
similar scenario happened with Exception.format_mfa/3 (which prints functions as
well) and the implementation of the Inspect protocol for functions. With this
commit, we:

  * introduce the exported but hidden Macro.classify_identifier/1 function to
    classify an identifier (atom or binary) as one of :callable, :non_callable,
    :alias, or other
  * rework the implementation of Inspect.Function.inspect/1,
    Exception.format_mfa/1, and Macro.to_string/2 to properly inspect functions
2017-01-21 22:23:37 +01:00
Devon Estes 508bb78822 Fix Macro.underscore/1 for screaming snake case (#5630)
We had some unexpected behavior when trying to use `Macro.underscore/1`
for screaming snake case strings.
2017-01-18 05:32:29 -07:00
Amos King 1e1e5733a2 Fix Macro.camelize/1 for screaming snake case (#5662)
The screaming snake case was keeping its original format after being
camelized. As mentioned in issue #5627. This change allows it to behave
like the all lower snake case when camelized.
2017-01-18 05:31:35 -07:00
Eric Entin 21c0e80076 Support "left not in right" syntax (#5620) 2017-01-07 10:23:13 +01:00
José Valim 1ded100fb5 Warn for unused module attributes
Module attributes that are imported from other
contexts are not warned. This keeps the same
semantics regarding variables and function definition
ordering which are not warned when they come from
other contexts.
2016-11-20 00:08:20 +01:00
Eksperimental 4e0cb3e903 Remove spaces around * in bit syntax in Macro.to_string/2 (#5370)
Per discussion in https://github.com/elixir-lang/elixir/pull/5362#discussion_r85313529.
2016-10-28 16:10:17 +02:00
Michał Muskała 5054a26145 Fix Macro.to_string when printing keywords (#5270)
It was incorrect when given a literal atom resembling an alias.

Fixes #5268
2016-09-29 10:48:01 +02:00
Andrea Leopardi d977e6dd4a Wrap fn calls in parens in Macro.to_string/1 (#5170)
Right now, application of anonymous function literals in
Macro.to_string/2 is represented like this:

    iex> Macro.to_string(quote do: (fn x -> x end).(1))
    "fn x -> x end.(1)"

While this is valid, I think it's far more common to have such calls
with the literal fn wrapped in parens:

    (fn x -> x end).(1)

This commit makes Macro.to_string/2 behave like this.
2016-08-28 17:34:59 +02:00
Andrea Leopardi f15416d5f3 Improve an assertion in MacroTest
We were only asserting that some code raised an error (ArgumentError),
but we weren't asserting on the message, thus making the test's context
hard to grasp. I added the error message in the assertion and reworded
the test name a bit.
2016-06-25 14:37:37 +02:00
eksperimental 3bd976737e Use proper case for proper nouns and acronyms
Use proper case for proper nouns (e.g., Elixir, Rebar, Git, and others)
as well as acronyms (e.g., URL, HTTP, and others).
2016-06-06 15:17:59 +02:00
Aleksei Magusev c8099904d3 Optimize in/2 with empty list only for match/guard context 2016-06-01 16:45:48 +02:00
Eric Meadows-Jönsson a3c429f9b9 Always optimize in/2 for literals
Previously in/2 only optimized when inside the guard context.
2016-05-30 19:48:51 +02:00
José Valim ecd7b01beb Do not change underscore behaviour in Macro 2016-04-26 00:54:36 +02:00
eksperimental 4ceb41e71b Formmating: Add white space around vertical bar (#4507) 2016-04-25 00:55:49 +02:00
Daniel Perez ea1921ab12 Fix Macro.to_string keyword blocks in normal function calls (#4526) 2016-04-17 10:09:06 +02:00
Andrea Leopardi 2faab1db7d Handle non-called anonymous functions in Macro.pipe/3
Right now, this nasty error pops out:

    iex> :foo |> (fn x -> x end)
    ** (CompileError) iex:1: expected -> clauses in fn

This happens because of what we do in `Macro.pipe/3`, which is: we take
the AST of a function call as the second argument and insert the first
argument in the list of arguments for the function call, at the position
specified by the third argument. The AST for a `fn` looks exactly like
the AST of a function call (to the `:fn` function):

    iex> quote do: fn :ok -> :ok; :err -> :err end
    {:fn, [], [{:->, [], [[:ok], :ok]}, {:->, [], [[:error], :error]}]}

It's easy to see that the "arguments" to `:fn` are the `->` clauses in
the function. This means that doing `:foo |> (fn x -> x end)` basically
adds another clause to the function:

    iex> Macro.pipe(:foo, quote(do: fn :ok -> :ok; :err -> :err end), 0)
    {:fn, [], [:foo, {:->, [], [[:ok], :ok]}, {:->, [], [[:err], :err]}]}

and this makes `:elixir_fn.expand/3` blow up because it doesn't know how
to handle the `:foo` clause (because it's not a clause!).

With this commit, we blow up early in `Macro.pipe/3` when we see a
`{:fn, _, _}` call:

    iex> :foo |> (fn x -> x end)
    ** (ArgumentError) cannot pipe :foo into an anonymous function
    without calling the function; use :foo |> (fn x -> x end).()
2016-03-30 12:36:28 +02:00
Andrea Leopardi 7e77c91320 Error out when passing :__aliases__ to Macro.pipe/3
Right now, this funny thing happens:

    iex> Macro |> Env
    Macro.Env

This happens because `|>/2` calls `Macro.pipe/3` under the hood, and
`Macro.pipe/3` inserts the left-hand side at the beginning of the list
of arguments in the AST for the function call on the right-hand
side. Aliases, however, look exactly like a call to the __aliases__
function:

    iex> quote do: Macro.Env
    {:__aliases__, [alias: false], [:Macro, :Env]}

and that's why `Macro |> Env` becomes `Macro.Env`. With this commit, we
explicitly take care of `:__aliases__` in a `Macro.pipe/3` call, so that
we can error out in such cases:

    iex> Macro |> Env
    ** (ArgumentError) cannot pipe Macro into Env, ...
2016-03-29 19:47:54 +02:00
Daniel Perez d66121aea5 Deprecate use of pipe with unary operators. 2016-03-22 19:11:50 +09:00
Erik Nilsen aad758534c Add integer support for Macro.underscore/1 2016-03-08 03:33:52 -08:00
Erik Nilsen e68e8d47a7 Add integer support for Macro.camelize/1 2016-03-07 16:14:06 -08:00
eksperimental 6fcb3edee2 Standardize use of range operator (..)
...by removing spaces around it
2016-02-14 09:14:07 +07:00