This namespace implements various heuristics to map Javascript names back to corresponding ClojureScript names. The functionality here heavily depends on observed ClojureScript compiler and runtime behaviour (fragile!). Answers given by functions in this namespace cannot be perfect because generated Javascript naming schemes produced by ClojureScript compiler were not designed with easy reversibility in mind. We recommend this functionality to be used for presentation in the UI only. The goal here is to provide user with more familiar view of runtime state of her app in most common cases (on best effort basis).
Our main weapons in this uneven fight are:
We can also cheat and look at runtime state of browser environment to determine some answers about namespaces.
This code can be used only in non-advanced builds!
If you discovered breakage or a new case which should be covered by this code, please open an issue: https://github.com/binaryage/cljs-devtools/issues
This namespace implements various heuristics to map Javascript names back to corresponding ClojureScript names. The functionality here heavily depends on observed ClojureScript compiler and runtime behaviour (fragile!). Answers given by functions in this namespace cannot be perfect because generated Javascript naming schemes produced by ClojureScript compiler were not designed with easy reversibility in mind. We recommend this functionality to be used for presentation in the UI only. The goal here is to provide user with more familiar view of runtime state of her app in most common cases (on best effort basis). Our main weapons in this uneven fight are: 1. munged function names as they appear in Javascript (generated by ClojureScript) 2. we can also analyze function sources accessible via .toString 3. special cljs$core$IFn$_invoke protocol props generated for multi-arity functions We can also cheat and look at runtime state of browser environment to determine some answers about namespaces. This code can be used only in non-advanced builds! If you discovered breakage or a new case which should be covered by this code, please open an issue: https://github.com/binaryage/cljs-devtools/issues
(args-lists-to-strings args-lists spacer-symbol multi-arity-symbol rest-symbol)
Converts a list of arguments lists into a list of strings suitable for UI presentation.
Converts a list of arguments lists into a list of strings suitable for UI presentation.
(arities-key-comparator x y)
numbers go first (ordered), then keywords (ordered by name), and then ::variadic sticks last
numbers go first (ordered), then keywords (ordered by name), and then ::variadic sticks last
(arities-to-args-lists arities & [humanize?])
Given a map of arity functions. Tries to parse individual functions and prepare an arguments list for each arity. Returned list of arguments list is sorted by arity count, variadic arity goes last if available.
The function also optionally humanizes argument names in each arguments list if requested.
Given a map of arity functions. Tries to parse individual functions and prepare an arguments list for each arity. Returned list of arguments list is sorted by arity count, variadic arity goes last if available. The function also optionally humanizes argument names in each arguments list if requested.
(arity-keywords-comparator x y)
::variadic goes last, other keywords compare by name.
::variadic goes last, other keywords compare by name.
(break-and-demunge-name munged-name)
(break-and-demunge-name munged-name ns-detector)
Given a munged-name from Javascript lands attempts to break it into a namespace part and remaining short name. Then applies appropriate demunging on them and returns ClojureScript versions of the names.
Given a munged-name from Javascript lands attempts to break it into a namespace part and remaining short name. Then applies appropriate demunging on them and returns ClojureScript versions of the names.
(break-munged-name munged-name)
(break-munged-name munged-name ns-detector)
Given a munged-name from Javascript lands attempts to break it into: [fn-ns fn-name protocol-ns protocol-name protocol-method arity].
Protocol and arity elements are optional. Function elements are always present or "".
examples for input: cljs$core$rest => ['cljs.core', 'rest'] cljs.core.reduce$cljs$core$IFn$_invoke$arity$3 => ['cljs.core' 'reduce' 'cljs.core' 'IFn' '_invoke' 3]
Given a munged-name from Javascript lands attempts to break it into: [fn-ns fn-name protocol-ns protocol-name protocol-method arity]. Protocol and arity elements are optional. Function elements are always present or "". examples for input: cljs$core$rest => ['cljs.core', 'rest'] cljs.core.reduce$cljs$core$IFn$_invoke$arity$3 => ['cljs.core' 'reduce' 'cljs.core' 'IFn' '_invoke' 3]
(char-to-subscript char)
Given a character with a single digit converts it into a subscript character. Zero character maps to unicode 'SUBSCRIPT ZERO' (U+2080).
Given a character with a single digit converts it into a subscript character. Zero character maps to unicode 'SUBSCRIPT ZERO' (U+2080).
(char-to-superscript char)
Given a character with a single digit converts it into a superscript character. Zero character maps to unicode 'SUPERSCRIPT ZERO' (U+2070).
Given a character with a single digit converts it into a superscript character. Zero character maps to unicode 'SUPERSCRIPT ZERO' (U+2070).
(cljs-fn-name? munged-name)
Given a Javascript name answers if the name was likely generated by ClojureScript. We use a simple heuristic here: The name must contain at least two separate dollars because we assume two-segment namespaces.
Given a Javascript name answers if the name was likely generated by ClojureScript. We use a simple heuristic here: The name must contain at least two separate dollars because we assume two-segment namespaces.
(cljs-fn? f)
Given a Javascript function object returns true if the function looks like a ClojureScript function.
Uses various heuristics:
Given a Javascript function object returns true if the function looks like a ClojureScript function. Uses various heuristics: 1. must be fn? (is javascript function or satisfies Fn and IFn protocols) 2. and name must be cljs-fn-name? (name can come from f.name or parsed out of function source) 3. or if anonymous function, must be non-trivial
(collect-fn-arities f)
Given a Javascript function object, tries to inspect known arity properties generated by ClojureScript compiler and collects all available arity functions into a map. Arities are keyed by arity count and variadic arity gets ::variadic key.
Given a Javascript function object, tries to inspect known arity properties generated by ClojureScript compiler and collects all available arity functions into a map. Arities are keyed by arity count and variadic arity gets ::variadic key.
(detect-namespace-prefix tokens & [ns-detector])
Given a name broken into namespace parts returns [detected-ns remaining-parts], where detected-ns is a string representing longest detected existing namespace and remaining-parts is a vector of remaining input parts not included in the detected-ns concatenation.
For given input ["cljs" "core" "first"] returns ["cljs.core" ["first"]] (assuming cljs.core exists)
Given a name broken into namespace parts returns [detected-ns remaining-parts], where detected-ns is a string representing longest detected existing namespace and remaining-parts is a vector of remaining input parts not included in the detected-ns concatenation. For given input ["cljs" "core" "first"] returns ["cljs.core" ["first"]] (assuming cljs.core exists)
(dollar-preserving-demunge munged-name)
Standard cljs.core/demunge is too aggressive in replacing dollars. This wrapper function works around it by leaving dollars intact.
Standard cljs.core/demunge is too aggressive in replacing dollars. This wrapper function works around it by leaving dollars intact.
(find-index-of-human-prefix name)
Given a demunged ClojureScript parameter name. Tries to detect human readable part and returns the index where it ends. Returns nil if no prefix can be detected.
The idea is to convert macro-generated parameters and other generated names to more friendly names. We observed that param names generated by gensym have prefix followed by big numbers. Other generated names contain two dashes after prefix (originally probably using underscores).
Given a demunged ClojureScript parameter name. Tries to detect human readable part and returns the index where it ends. Returns nil if no prefix can be detected. The idea is to convert macro-generated parameters and other generated names to more friendly names. We observed that param names generated by gensym have prefix followed by big numbers. Other generated names contain two dashes after prefix (originally probably using underscores).
(humanize-name state name)
Given a name and intermediate state. Convert name to a human readable version by keeping human readable prefix with optional subscript postfix and store it in ::result. Subscript number is picked based on state. State keeps track of previously assigned subscripts. Returns a new state.
Given a name and intermediate state. Convert name to a human readable version by keeping human readable prefix with optional subscript postfix and store it in ::result. Subscript number is picked based on state. State keeps track of previously assigned subscripts. Returns a new state.
(humanize-names names)
Given a list of names, returns a list of human-readable versions of those names. It detects human-readable prefix using a simple heuristics. When names repeat it assigns simple subscripts starting with 2. Subscripts are assigned left-to-right.
Given ["p--a" "p--b" "x" "p--c"] returns ["p" "p₂" "x" "p₃"]
Given a list of names, returns a list of human-readable versions of those names. It detects human-readable prefix using a simple heuristics. When names repeat it assigns simple subscripts starting with 2. Subscripts are assigned left-to-right. Given ["p--a" "p--b" "x" "p--c"] returns ["p" "p₂" "x" "p₃"]
(make-subscript subscript)
Given a subscript number converts it into a string representation consisting of unicode subscript characters (digits).
Given a subscript number converts it into a string representation consisting of unicode subscript characters (digits).
(make-superscript superscript)
Given a superscript number converts it into a string representation consisting of unicode superscript characters (digits).
Given a superscript number converts it into a string representation consisting of unicode superscript characters (digits).
(parse-constructor-info f)
Given a Javascript constructor function tries to retrieve [ns name basis]. Returns nil if not a cljs type.
Given a Javascript constructor function tries to retrieve [ns name basis]. Returns nil if not a cljs type.
(parse-fn-info f)
Given Javascript function object tries to retrieve [ns name & args] as in parse-fn-source-info (on best effort basis).
Given Javascript function object tries to retrieve [ns name & args] as in parse-fn-source-info (on best effort basis).
(parse-fn-info-deep f)
Given a Javascript function object tries to retrieve [ns name & args] as in parse-fn-info (on best effort basis).
The difference from parse-fn-info is that this function prefers to read args from arities if available. It recurse arbitrary deep following IFn protocol leads.
If we hit multi-arity situation in leaf, we don't attempt to list arguments and return ::multi-arity placeholder instead.
The reason for reading arities is that it gives more accurate parameter names in some cases. We observed that variadic functions don't always contain original parameter names, but individual IFn arity functions do.
Given a Javascript function object tries to retrieve [ns name & args] as in parse-fn-info (on best effort basis). The difference from parse-fn-info is that this function prefers to read args from arities if available. It recurse arbitrary deep following IFn protocol leads. If we hit multi-arity situation in leaf, we don't attempt to list arguments and return ::multi-arity placeholder instead. The reason for reading arities is that it gives more accurate parameter names in some cases. We observed that variadic functions don't always contain original parameter names, but individual IFn arity functions do.
(parse-fn-source fn-source)
Given a function source code parses out [name args]. Note that both strings are still munged. Suitable for further processing.
For example for input below the function will return ["devtools_sample$core$hello" "name, unused_param"]:
function devtools_sample$core$hello(name, unused_param){ return [cljs.core.str("hello, "),cljs.core.str(name),cljs.core.str("!")].join(''); }
Given a function source code parses out [name args]. Note that both strings are still munged. Suitable for further processing. For example for input below the function will return ["devtools_sample$core$hello" "name, unused_param"]: function devtools_sample$core$hello(name, unused_param){ return [cljs.core.str("hello, "),cljs.core.str(name),cljs.core.str("!")].join(''); }
(parse-fn-source-info fn-source)
Given function source code tries to retrieve [ns name & args] on best effort basis, where ns is demunged namespace part of the function name (or "" if namespace cannot be detected) name is demunged short name (or "" if function is anonymous or name cannot be retrieved) args is optional number of demunged argument names.
Please note that this function always returns a vector with something. In worst cases ["" ""].
Given function source code tries to retrieve [ns name & args] on best effort basis, where ns is demunged namespace part of the function name (or "" if namespace cannot be detected) name is demunged short name (or "" if function is anonymous or name cannot be retrieved) args is optional number of demunged argument names. Please note that this function always returns a vector with something. In worst cases ["" ""].
(present-function-name munged-name options)
Given javascript function name tries to present it as plain string for display in UI on best effort basis.
Given javascript function name tries to present it as plain string for display in UI on best effort basis.
(present-protocol-part protocol-ns
protocol-name
protocol-method
include-protocol-ns?)
(review-arities arities)
Some arities can be marked as fixed arity but in fact point to a variadic-arity function. We want to detect this case and turn such improperly categorized arities to ::variadic.
Some arities can be marked as fixed arity but in fact point to a variadic-arity function. We want to detect this case and turn such improperly categorized arities to ::variadic.
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close