module

Analyzer::Erlang::Helper

Shared helpers for the Erlang framework analyzers (Cowboy, Elli).

Constants

DIRECTIVE = /(?:\A|\.)\s*-\s*(?:(ifdef\s*\(\s*TEST\s*\))|(ifndef\s*\(\s*TEST\s*\))|(if(?:n?def)?\s*\()|(else)(?=\s*\.)|(endif)(?=\s*\.))/

A preprocessor directive that opens a form: at the start of a line or right after the . ending the previous form, so several directives on one line (-ifdef(TEST). -include_lib("x"). -endif.) are all seen. else/endif leave their . unconsumed, because it is the opening . of the next directive on the line.

Instance methods

mask_ifdef_test(text : String) : String

Blank the lines of every region compiled only when TEST is defined (-ifdef(TEST). … -endif., or the -else. branch of -ifndef(TEST).), keeping newlines so later line numbers hold. Every open -if* is tracked so an -else./-endif. pairs with its own conditional; the -else. of -ifdef(TEST). is the branch compiled without TEST, i.e. production code.

Source
parse_cowboy_path(path_match : String) : Tuple(String, Array(Param))

Erlang path matches appear both as plain strings ("/users/:id") and as binaries (<<"/users/:id">>). Both carry the same syntax.

Segment forms, per cowboy_router:

  • :name — a binding, i.e. a path parameter.
  • [...] — matches all remaining segments (wildcard).
  • [/optional]— an optional segment.
  • :name[...] — a binding that also swallows the remainder.

Returns the noir-normalized URL plus the bindings as path params.

Source
strip_erlang_comments(text : String) : String

Blank out % line comments while PRESERVING newlines/offsets, so a comment never collapses lines and shifts every later endpoint's reported line number.

Erlang has three constructs a naive %-to-end-of-line scan gets wrong, and all three appear in real dispatch tables:

  • "..." strings — "100%" is not a comment.
  • '...' quoted atoms — 'GET%', and more commonly '_'.
  • $c character literals — $% IS the percent character, and $'/$" are quote characters that must not open a string.
Source
test_path?(relative_path : String) : Bool

rebar3 keeps Common Test suites (*_SUITE.erl) and EUnit modules (*_tests.erl) under test/; they build throwaway dispatch tables for rebar3 ct/eunit, never served. Takes the scan-base-relative path so a checkout under a test/ ancestor is not wiped; the suffix rules still catch a scan whose base is the test directory itself.

Source