We are currently learning about grammars of formal languages in a lecture on theoretical computer science. I enjoy studying this topic and came up with the following exercise: Use ECMAScript grammar1 to formally reconstruct a source code transformation performed by Babel to verify that it is syntactically valid. For one concrete transformation this is easy, simply perform the transformation and run the output code. If it throws no syntax errors it is valid syntax. But what I want to see is that a transformation is syntactically correct for any input code. I am not sure how involved this is going to be, so I take a simple transformation written by myself called babel-plugin-check-constants2.

It doesn’t really matter what exactly this plugin does, all that matters is that it inserts a bit of extra code at some places. The three possible node types where code is inserted are AssignmentExpression, ForXStatement and UpdateExpression. For more details on const-violations have a look at the post I wrote about that a few months ago3. For each one of those I will look at what kind of code insertion or replacement happens and then try to replicate this with corresponding ECMAScript grammar productions. Yay!

AssignmentExpression 4

// Input code (Assume a is const)
a = 17;

// Transformed output code
a = (function () {
	throw new Error("\"a\" is read-only");
}(), 17);

We all understand that the right-hand side of an assignment is an expression whose value is to be assigned to the left-hand side of the assignment. So I expected this derivation to be something along the lines of:

This is how I justified the transformation in my head when writing the corresponding code: An assignment assigns to an expression, so I can replace that expression with a comma expression and it will still be valid. But then I started looking at the grammar for AssignmentExpression and was puzzled for a while. It does kind of work the way I showed above, but with a lot more intermediate steps:

That means it is syntactically allowed to replace x = z with x = (y, z) as long as y is an . The node inserted in this Babel transform represents code of the form function(){...}() which is a (CallExpression). Above we have seen how to derive to . And the production for gives as one option, which concludes the derivation:

UpdateExpression 5

// Input code (Assume a is const)
++a;

// Transformed output code
(function () {
	throw new Error("\"a\" is read-only");
}(), ++a);

This one is simple once we understand how to convert the various different expression types:

The dots represent sequences of applied productions that we already used in the AssignmentExpression case and I don’t want to repeat all over the place.

ForXStatement 6

// Input code (Assume a is const)
for(a in [1,2,3]) {}

// Transformed output code
for(a in [1,2,3]) {
	throw new Error("\"a\" is read-only");
}

Since the plugin calls ensureBlock() we can assume that the for-loop has a full block as body instead of just a single expression. It then shifts the list of statements within that block to the right and inserts a throwStatement. Since the block previously contained a valid StatementList, this insertion is syntactically valid and can be constructed by using the following production steps:

Enough spec reading for now, let me know if you spot any mistakes!