liveBook · Manning

AST migration

As explained in Building User-Friendly DSLs

An AST migration is a transformation that adapts an abstract syntax tree from one version of a domain-specific language implementation to another.

An AST migration transforms an abstract syntax tree from a DSL’s pre-change structure into its post-change structure. It keeps existing DSL content usable when structural changes would otherwise make it invalid. section 10.3.2 section 10.4

Apply a migration as a versioned transformation:

  • Check that the AST’s version ID matches one of the migration’s “from” version IDs. section 10.3.2
  • Traverse the tree, typically depth-first, and transform each affected concept or property. section 10.3.2
  • Update the DSL content’s version ID to the migration’s “to” version after transformation. section 10.3.2
  • Prefer a modified copy when mutating the existing AST would be undesirable. section 10.3.2

Renaming and reshaping a concept

A migration can rename the Data Attribute concept to Attribute and move its “initial value” property to “value”. The transformation changes the concept name, copies the property value, and deletes the old property without losing the stored data. section 10.3.2

Listing 10.1 The implementation of an AST migration
const { isAstObject } = require("../common/ast")

const modifyDataAttribute = (value) => {
    if (isAstObject(value)) {
        const { settings } = value
        for (const propertyName in settings) {
            modifyDataAttribute(settings[propertyName])
        }
        if (value.concept === "Data Attribute") {
            value.concept = "Attribute"  #1
             settings["value"] =
                 settings["initial value"]    #2
             delete settings["initial value"]  
         }
    }
    if (Array.isArray(value)) {
        value.forEach(modifyDataAttribute)
    }
}
module.exports.modifyDataAttribute = modifyDataAttribute

The implementation recursively visits AST objects and arrays, identifies Data Attribute nodes, renames the concept, moves the property, and removes the old property. section 10.3.2

How Building User-Friendly DSLs covers it

How deep it goes
Explains version checks, recursive traversal, property transformation, and copying, but not migration framework design.
Examples
Demonstrates renaming Data Attribute to Attribute and moving “initial value” to “value” through recursive JavaScript code.
What it assumes
Assumes familiarity with ASTs and DSL structure while defining version IDs, structural changes, and recursive traversal in context.
What you can do after
Builds a versioned tree transformation and adapt an existing AST without losing represented data.
How it connects
Connects ASTs with DSL versioning, structural refactoring, deprecation, invalid data, and domain-specific languages.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building User-Friendly DSLs. The section links point to the parts of the book it draws on.