Update transform walker with latest node types
authorNick Downing <nick@ndcode.org>
Sun, 9 Jan 2022 02:05:19 +0000 (13:05 +1100)
committerNick Downing <nick@ndcode.org>
Sun, 9 Jan 2022 02:05:19 +0000 (13:05 +1100)
transform.js

index 5696633..fc87ae5 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2018 Nick Downing <nick@ndcode.org>
+ * Copyright (C) 2022 Nick Downing <nick@ndcode.org>
  * SPDX-License-Identifier: MIT
  * 
  * Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -27,14 +27,21 @@ let transform = (visitors, node, state) => {
   return c(node, state, node.type)
 }
 
+// remainder of the file is adapted from acorn-walk/src/index.js
+// we have changed "base" to "visitors"
+// we have made tree be reconstructed by replacing each child element with
+// the return value of its walker (and most walkers will return themselves)
+
 let skipThrough = (node, st, c) => c(node, st)
 let ignore = (node, st, c) => node
 
+
 // Node walkers.
 let visitors = {}
 
 visitors.Program =
-visitors.BlockStatement = (node, st, c) => {
+visitors.BlockStatement =
+visitors.StaticBlock = (node, st, c) => {
   for (let i = 0; i < node.body.length; ++i)
     node.body[i] = c(node.body[i], st, 'Statement')
   return node
@@ -42,7 +49,8 @@ visitors.BlockStatement = (node, st, c) => {
 visitors.Statement = skipThrough
 visitors.EmptyStatement = ignore
 visitors.ExpressionStatement =
-visitors.ParenthesizedExpression = (node, st, c) => {
+visitors.ParenthesizedExpression =
+visitors.ChainExpression = (node, st, c) => {
   node.expression = c(node.expression, st, 'Expression')
   return node
 }
@@ -253,6 +261,8 @@ visitors.ExportDefaultDeclaration = (node, st, c) => {
   return node
 }
 visitors.ExportAllDeclaration = (node, st, c) => {
+  if (node.exported)
+    node.exported = c(node.exported, st)
   node.source = c(node.source, st, 'Expression')
   return node
 }
@@ -262,10 +272,15 @@ visitors.ImportDeclaration = (node, st, c) => {
   node.source = c(node.source, st, 'Expression')
   return node
 }
+visitors.ImportExpression = (node, st, c) => {
+  node.source = c(node.source, st, "Expression")
+  return node
+}
 visitors.ImportSpecifier =
 visitors.ImportDefaultSpecifier =
 visitors.ImportNamespaceSpecifier =
 visitors.Identifier =
+visitors.PrivateIdentifier =
 visitors.Literal = ignore
 
 visitors.TaggedTemplateExpression = (node, st, c) => {
@@ -287,9 +302,10 @@ visitors.ClassBody = (node, st, c) => {
   return node
 }
 visitors.MethodDefinition =
+visitors.PropertyDefinition =
 visitors.Property = (node, st, c) => {
   if (node.computed) node.key = c(node.key, st, 'Expression')
-  node.value = c(node.value, st, 'Expression')
+  if (node.value) node.value = c(node.value, st, 'Expression')
   return node
 }