fix(static-module-record): minimise source changes with `recast`
MODIFY
packages/static-module-record/src/babelPlugin.js
packages/static-module-record/src/babelPlugin.js
@@ -38,20 +38,6 @@ const collectPatternIdentifiers = (path, pattern) => {
}
};
-const prependReplacements = (replacements, node) => {
- const lastReplace = replacements[replacements.length - 1];
- if (lastReplace) {
- lastReplace.trailingComments = node.trailingComments;
- node.trailingComments = undefined;
- }
- replacements.unshift(node);
-};
-
-const displayAsExport = node => {
- node.loc.prependText = '/*EX*/ ';
- return node;
-};
-
function makeModulePlugins(options) {
const {
sourceType,
@@ -89,6 +75,20 @@ function makeModulePlugins(options) {
const topLevelExported = Object.create(null);
const rewriteModules = pass => ({ types: t }) => {
+ const replace = (
+ src,
+ node = t.expressionStatement(t.identifier('null')),
+ ) => {
+ node.loc = src.loc;
+ node.comments = [...(src.leadingComments || [])];
+ t.inheritsComments(node, src);
+ return node;
+ };
+
+ const prependReplacements = (replacements, node) => {
+ replacements.unshift(node);
+ };
+
const allowedHiddens = new WeakSet();
const rewrittenDecls = new WeakSet();
const hiddenIdentifier = hi => {
@@ -253,7 +253,7 @@ function makeModulePlugins(options) {
// Create the export calls.
const isConst = decl.kind === 'const';
- const replace = rewriteVars(
+ const replacements = rewriteVars(
vids,
isConst,
decl.type === 'FunctionDeclaration'
@@ -261,25 +261,23 @@ function makeModulePlugins(options) {
: !isConst && decl.kind !== 'let',
);
- if (replace.length > 0) {
+ if (replacements.length > 0) {
switch (decl.type) {
case 'VariableDeclaration': {
// We rewrote the declaration.
rewrittenDecls.add(decl);
- prependReplacements(replace, decl);
+ prependReplacements(replacements, decl);
break;
}
case 'FunctionDeclaration': {
- prependReplacements(replace, decl);
+ prependReplacements(replacements, decl);
break;
}
default: {
throw TypeError(`Unknown declaration type ${decl.type}`);
}
}
- }
- if (replace.length > 0) {
- path.replaceWithMultiple(replace);
+ path.replaceWithMultiple(replacements);
}
};
@@ -404,7 +402,7 @@ function makeModulePlugins(options) {
if (decl.id) {
// Just keep the same declaration and mark it as the default.
path.replaceWithMultiple([
- displayAsExport(decl),
+ replace(path.node, decl),
t.expressionStatement(t.callExpression(callee, [decl.id])),
]);
return;
@@ -412,7 +410,8 @@ function makeModulePlugins(options) {
// const {default: $c_default} = {default: (XXX)}; $h_once.default($c_default);
path.replaceWithMultiple([
- displayAsExport(
+ replace(
+ path.node,
t.variableDeclaration('const', [
t.variableDeclarator(
t.objectPattern([t.objectProperty(id, cid)]),
@@ -583,7 +582,7 @@ function makeModulePlugins(options) {
});
}
if (doTransform) {
- path.replaceWithMultiple(decl ? [displayAsExport(decl)] : []);
+ path.replaceWithMultiple(decl ? [replace(path.node, decl)] : []);
}
},
});

MODIFY
packages/static-module-record/src/transform-analyze.js
packages/static-module-record/src/transform-analyze.js
+import * as recastCJS from '@agoric/recast';
+import traverseCJS from '@babel/traverse';
+import typesCJS from '@babel/types';
+
import * as h from './hidden.js';
import makeModulePlugins from './babelPlugin.js';
@@ -39,6 +43,7 @@ const makeTransformSource = babel =>
'throwExpressions',
'logicalAssignment',
'classPrivateMethods',
+ 'classPrivateProperties',
// 'v8intrinsic', // we really don't want people to rely on platform powers
'partialApplication',
['decorators', { decoratorsBeforeExport: false }],
@@ -48,29 +53,30 @@ const makeTransformSource = babel =>
const { analyzePlugin, transformPlugin } = makeModulePlugins(sourceOptions);
// TODO top-level-await https://github.com/endojs/endo/issues/306
const allowAwaitOutsideFunction = false;
- babel.transform(code, {
+ const recast = recastCJS.default || recastCJS;
+ const ast = recast.parse(code, {
+ parser: {
+ parse: source =>
+ babel.transform(source, {
parserOpts: {
allowAwaitOutsideFunction,
+ tokens: true,
plugins: parserPlugins,
},
- generatorOpts: {
- retainLines: true,
- compact: false,
- },
plugins: [analyzePlugin],
- ast: false,
+ ast: true,
code: false,
- });
- ({ code } = babel.transform(code, {
- parserOpts: {
- allowAwaitOutsideFunction,
- plugins: parserPlugins,
+ }).ast,
},
- generatorOpts: {
+ });
+ const traverse = traverseCJS.default || traverseCJS;
+ const types = typesCJS.default || typesCJS;
+ traverse(ast, transformPlugin({ types }).visitor);
+ ({ code } = recast.print(ast, {
+ wrapColumn: Infinity,
+ reuseWhitespace: true,
+ includeComments: true,
retainLines: true,
- compact: false,
- },
- plugins: [transformPlugin],
}));
// console.log(`transformed to`, output.code);

fix(static-module-record): minimise source changes with `recast`