GoGoCode Documentation

repository·main·Indexed 26 days ago

https://github.com/thx/gogocode

An AST-based code transformation engine for JavaScript, TypeScript, and HTML. It features a high-level API combining jQuery-like selection with Regex-like pattern matching for complex code refactoring. The ecosystem includes gogocode-core for AST processing, gogocode-cli for command-line transformations, and the esbuild-dynamic-import-plugin for transforming library imports in esbuild.

Tokens
40.5K
Snippets
138
Records
241
Agent score
90%

What's inside GoGoCode

  1. Overview of GoGoCode capabilities

    main

    GoGoCode is an AST-based code transformation tool for JavaScript, TypeScript, HTML, and Vue. It is designed to automate tasks such as framework upgrades, code refactoring, and multi-platform conversions.

    Key features include:

    • Intuitive API: Uses a jQuery-like API for AST traversal and a regex-like syntax for matching and replacing code segments.
    • Proven Reliability: Used for large-scale migrations (e.g., 100k+ lines of code at Alibaba) and powers the Vue 2 to Vue 3 migration solution.
    • Extensible Ecosystem: Supports a plugin and CLI mechanism for building complex CodMods.
  2. Overview of GoGoCode AST Transformation

    main

    GoGoCode is an AST-based code transformation tool for JavaScript, TypeScript, and HTML. It provides an intuitive API designed for developers to find and modify code structures using two primary mechanisms:

    1. jQuery-like API: Used for traversing and manipulating the Abstract Syntax Tree (AST).
    2. Regex-like Syntax: A syntax similar to regular expressions used to match and replace specific code patterns.

    You can experiment with these transformations in the GoGoCode Playground.

  3. Overview of GoGoCode

    main

    GoGoCode is an AST-based transformer for JavaScript, TypeScript, and HTML. It provides an intuitive API for code manipulation using two primary concepts:

    • jQuery-like API: Used to select and transform specific AST (Abstract Syntax Tree) elements.
    • Regex-like syntax: Used to match and replace code patterns within the AST.

    It is designed to allow developers to perform complex code refactoring and transformations with a syntax that feels familiar to string manipulation but operates safely on the code structure.

  4. Use gogocode-plugin-optional-chaining as a custom transform plugin

    main

    You can use gogocode-plugin-optional-chaining as a custom transformation plugin by creating a transform.js file in your project root and calling it via the gogocode-cli.

    When the plugin is executed, it receives three arguments:

    • fileInfo: An object containing source (the file content) and path (the file path).
    • api: An object containing the gogocode transformation library.
    • options: The options passed from the command line.

    To implement the plugin, export a function that uses api.gogocode to manipulate the source code and returns the transformed string using .generate().

    module.exports = function(fileInfo, api, options) {
      const sourceCode = fileInfo.source;
      const $ = api.gogocode;
      return $(sourceCode)
        .replace('const a = $_$', 'const a = 2')
        .generate();
    };
  5. Filter migration rules with include-rules and exclude-rules

    main

    You can control which transformation rules are applied using the -p flag with include-rules or exclude-rules.

    • Include specific rules: Use -p include-rules=rule1,rule2 to apply only the specified rules.
    • Exclude specific rules: Use -p exclude-rules=rule1,rule2 to skip the specified rules.

    Rule names can be found in the name field of the plugin's src/rules.js file.