reluxscript 0.1.4

Write AST transformations once. Compile to Babel, SWC, and beyond.
Documentation
// Generated by ReluxScript compiler
// Do not edit manually

module.exports = function({ types: t }) {
  
  class ComplexBinding {
  constructor(name, path, depth) {
      this.name = name;
      this.path = path;
      this.depth = depth;
    }
  }
  
  class AnalysisResult {
  constructor(bindings, dependencies, metadata) {
      this.bindings = bindings;
      this.dependencies = dependencies;
      this.metadata = metadata;
    }
  }
  
  class State {
  constructor(output, count, results) {
      this.output = output;
      this.count = count;
      this.results = results;
    }
  }
  
  function init() {
    const _hex_gap = 268435456;
    return { output: "", count: 0, results: [] };
  }
  
  function parse_value(input) {
    if ((input.length === 0)) {
      return { ok: false, error: "Empty input".toString() };
    }
    const __iflet_0 = usize.from_str_radix(input, 16);
    if (__iflet_0 && !__iflet_0.error) {
      const hex_value = __iflet_0.value;
      return { ok: true, value: hex_value };
    }
    return { ok: false, error: "Invalid hex".toString() };
  }
  
  function get_name_or_default(node) {
    const name = (node.id.as_ref().map((id) => id.name) ?? "anonymous".toString());
    const other = node.id.as_ref().map((id) => id.name).unwrap_or_default();
    const unwrapped = node.id.as_ref();
    if (node.id.is_some()) {
      this.state.output = "has_id";
    }
    if (node.body.is_none()) {
      this.state.output = "no_body";
    }
    return name;
  }
  
  function convert_error() {
    return { ok: false, error: "Something went wrong" };
  }
  
  function test_collections() {
    let set = /* HashSet::new */undefined();
    set.add("key1".toString());
    set.add("key2".toString());
    if (set.has("key1")) {
      this.state.output = "found";
    }
    let map = /* HashMap::new */undefined();
    map.set("count".toString(), 42);
    const keys = map.keys().cloned();
  }
  
  function transform_elements(elements, transformer) {
    return elements.map((e) => transformer(e));
  }
  
  function helper_function(value) {
    return (value * 2);
  }
  
  function format_output(prefix, value) {
    return `${prefix}: ${value}`;
  }
  
  function parse_with_question_mark(input) {
    const trimmed = input.trim();
    const __result = /* Number::from_str_radix */undefined(trimmed, 16);
    if (!__result.ok) {
      return { ok: false, error: __result.error };
    }
    const value = __result.value;
    return { ok: true, value: (value * 2) };
  }
  
  function test_range_loop(items) {
    for (let i = 0; i < items.length; i++) {
      this.state.count = i;
    }
    let counter = 0;
    counter += 1;
    return counter -= 1;
  }
  
  function test_while_loop(items) {
    let i = 0;
    while ((i < items.length)) {
      this.state.count = i;
      i += 1;
    }
  }
  
  function test_loop_break_continue(items) {
    let i = 0;
    while (true) {
      if ((i >= items.length)) {
        break;
      }
      if ((items[i].length === 0)) {
        i += 1;
        continue;
      }
      this.state.output = items[i];
      i += 1;
    }
  }
  
  // Post-transformation hook
  
  function finish() {
    return this.state.output;
  }
  
  let state = {};
  
  return {
    visitor: {
      Program: {
        exit(path, state) {
          exit(path.node, state);
        }
      },
      
      FunctionDeclaration(path) {
        const node = path.node;
        const __iflet_1 = node.return_type;
        if (__iflet_1 !== null && __iflet_1 !== undefined) {
          const type_ann = __iflet_1;
          if (type_ann.type_annotation.type === "TSStringKeyword") {
            return this.state.output = "string";
          } else if (type_ann.type_annotation.type === "TSNumberKeyword") {
            return this.state.output = "number";
          } else if (type_ann.type_annotation.type === "TSBooleanKeyword") {
            return this.state.output = "boolean";
          } else if (type_ann.type_annotation.type === "TSAnyKeyword") {
            return this.state.output = "any";
          } else if (type_ann.type_annotation.type === "TSArrayType") {
            return this.state.output = "array";
          } else if (type_ann.type_annotation.type === "TSTypeLiteral") {
            return this.state.output = "object";
          } else if (type_ann.type_annotation.type === "TSTypeReference") {
            if (type_ref.type_name.type === "Identifier") {
              return this.state.output = type_name.name;
            } else if (true) {
            }
          } else if (true) {
            return this.state.output = "unknown";
          }
        }
      },
      VariableDeclaration(path) {
        const node = path.node;
        node.declarations.forEach((declarator, __idx_0) => {
          if (declarator.id.type === "ArrayPattern") {
            const __iflet_2 = array_pattern.elements.get(0);
            if (__iflet_2 !== null && __iflet_2 !== undefined) {
              const first_elem = __iflet_2;
              const __iflet_3 = first_elem;
              if (__iflet_3 !== null) {
                const id = __iflet_3;
                this.state.output = id.name;
              }
            }
          } else if (true) {
          }
          const __iflet_4 = declarator.init;
          if (__iflet_4 !== null && __iflet_4 !== undefined) {
            const init = __iflet_4;
            if (init.type === "ArrowFunctionExpression") {
              const param_names = arrow.params.filter((p) => (t.isIdentifier(p))).map((p) => (() => {
                if (p.type === "Identifier") {
                  id.name;
                } else if (true) {
                }
              })());
              this.state.output = param_names.join(", ");
            } else if (init.type === "FunctionExpression") {
              this.state.output = "function";
            } else if (init.type === "CallExpression") {
              const __iflet_5 = call.callee;
              if (__iflet_5 !== null) {
                this.state.output = "call";
              }
            } else if (true) {
            }
          }
        });
      },
      ExpressionStatement(path) {
        const node = path.node;
        const is_jsx = ((t.isJSXElement(node.expression) || t.isJSXFragment(node.expression)));
        if (is_jsx) {
          this.state.output = "jsx";
        }
        this.state.count = (this.state.count + 1);
        const count_usize = this.state.count;
        const back_to_i32 = count_usize;
        if (node.expression.type === "CallExpression") {
          const __iflet_6 = call.extra_data.get("__minimactPath");
          if (__iflet_6 !== null && __iflet_6 !== undefined) {
            const path = __iflet_6;
            this.state.output = path;
          }
        } else if (true) {
        }
      },
      Identifier(path) {
        const node = path.node;
        const name = node.name;
        if (name.startsWith("set")) {
          this.state.output = "setter";
        } else {
          if (name.endsWith("Handler")) {
            this.state.output = "handler";
          } else {
            if (name.has("_")) {
              this.state.output = "snake_case";
            }
          }
        }
        const parts = name.split("_").map((s) => s.toString());
        const first_char = name[0];
        const __iflet_7 = first_char;
        if (__iflet_7 !== null && __iflet_7 !== undefined) {
          const ch = __iflet_7;
          if ((ch === ch.toUpperCase())) {
            this.state.output = "PascalCase";
          }
          if (ch.is_alphanumeric()) {
            this.state.output = "alphanumeric";
          }
        }
        const indent = "  ".repeat(4);
        const lower = name.toLowerCase();
        const upper = name.toUpperCase();
        const trimmed = name.trim();
        const trimmed_zeros = name.trim_end_matches("0");
        const __iflet_8 = name.find(".");
        if (__iflet_8 !== null && __iflet_8 !== undefined) {
          const pos = __iflet_8;
          this.state.count = pos;
        }
        const __iflet_9 = name.rfind(".");
        if (__iflet_9 !== null && __iflet_9 !== undefined) {
          const last_pos = __iflet_9;
          this.state.count = last_pos;
        }
      },
      ArrayExpression(path) {
        const node = path.node;
        if ((node.elements.length === 0)) {
          this.state.output = "empty";
          return;
        }
        const length = node.elements.length;
        this.state.count = length;
        const __iflet_10 = node.elements.first();
        if (__iflet_10 !== null && __iflet_10 !== undefined) {
          const first = __iflet_10;
          this.state.output = "has_first";
        }
        const __iflet_11 = node.elements.get(2);
        if (__iflet_11 !== null && __iflet_11 !== undefined) {
          const third = __iflet_11;
          this.state.output = "has_third";
        }
        let new_results = [];
        new_results.push("item1".toString());
        new_results.push("item2".toString());
        const has_null = node.elements.any((e) => (() => {
          return (t.isNullLiteral(e));
        })());
        const all_valid = new_results.all((p) => p.all((c) => c.is_ascii_hexdigit()));
        new_results.delete(0);
        new_results.clear();
      },
      StringLiteral(path) {
        const node = path.node;
        const value = node.value;
        const len = value.length;
        const capped_len = len.min(100);
        const at_least_ten = len.max(10);
        if ((len > 5)) {
          const first_five = value.slice(0, 5);
          const last_three = value.slice((len - 3));
          const middle = value.slice(2, (len - 2));
          const slice = value.slice(0, 3);
        }
        const diff = ((this.state.count - 10)).abs();
        const num = 3.14;
        const fractional = num.fract();
        const hex_str = this.state.count;
        const formatted = `{:08x}`;
      }
    }
  };
};