Skip to content

Commit

Permalink
chore: move all of transform.js into @endo/transforms
Browse files Browse the repository at this point in the history
  • Loading branch information
boneskull committed Oct 16, 2023
1 parent 038a8a1 commit de80b75
Show file tree
Hide file tree
Showing 12 changed files with 261 additions and 207 deletions.
4 changes: 1 addition & 3 deletions packages/bundle-source/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,11 @@
"lint:types": "tsc -p jsconfig.json"
},
"dependencies": {
"@agoric/babel-generator": "^7.17.4",
"@babel/parser": "^7.17.3",
"@babel/traverse": "^7.17.3",
"@endo/base64": "^0.2.35",
"@endo/compartment-mapper": "^0.9.2",
"@endo/init": "^0.5.60",
"@endo/promise-kit": "^0.2.60",
"@endo/transforms": "^0.1.3",
"@endo/where": "^0.3.5",
"@rollup/plugin-commonjs": "^19.0.0",
"@rollup/plugin-node-resolve": "^13.0.0",
Expand Down
15 changes: 9 additions & 6 deletions packages/bundle-source/src/nested-evaluate-and-get-exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import url from 'url';
import { rollup as rollup0 } from 'rollup';
import resolve0 from '@rollup/plugin-node-resolve';
import commonjs0 from '@rollup/plugin-commonjs';
import { transformSource } from './transform.js';
import { transformSource } from '@endo/transforms';

const DEFAULT_FILE_PREFIX = '/bundled-source/...';

Expand Down Expand Up @@ -95,11 +95,14 @@ export async function bundleNestedEvaluateAndGetExports(
const useLocationUnmap =
moduleFormat === 'nestedEvaluate' && !fileName.startsWith('_virtual/');

const { code: transformedCode } = await transformSource(code, {
sourceMapUrl: pathname,
sourceMap: chunk.map,
useLocationUnmap,
});
const { code: transformedCode } = await transformSource(
code,
chunk.map,
pathname,
{
useLocationUnmap,
},
);
unsortedSourceBundle[shortName] = transformedCode;

// console.log(`==== sourceBundle[${fileName}]\n${sourceBundle[fileName]}\n====`);
Expand Down
131 changes: 0 additions & 131 deletions packages/bundle-source/src/transform.js

This file was deleted.

13 changes: 8 additions & 5 deletions packages/bundle-source/src/zip-base64.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { makeAndHashArchive } from '@endo/compartment-mapper/archive.js';
import { encodeBase64 } from '@endo/base64';
import { whereEndoCache } from '@endo/where';
import { makeReadPowers } from '@endo/compartment-mapper/node-powers.js';
import { transformSource } from './transform.js';
import { transformSource } from '@endo/transforms';

const textEncoder = new TextEncoder();
const textDecoder = new TextDecoder();
Expand Down Expand Up @@ -119,11 +119,14 @@ export async function bundleZipBase64(
) {
const source = textDecoder.decode(sourceBytes);
let object;
({ code: object, map: sourceMap } = await transformSource(source, {
sourceType: 'module',
({ code: object, map: sourceMap } = await transformSource(
source,
sourceMap,
sourceMapUrl: new URL(specifier, location).href,
}));
new URL(specifier, location).href,
{
sourceType: 'module',
},
));
const objectBytes = textEncoder.encode(object);
return { bytes: objectBytes, parser: 'mjs', sourceMap };
},
Expand Down
1 change: 1 addition & 0 deletions packages/transforms/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './src/index.js';
12 changes: 10 additions & 2 deletions packages/transforms/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
"module": "./index.js",
"exports": {
".": "./index.js",
"./evade-censor": "./src/evade-censor.js",
"./package.json": "./package.json"
},
"scripts": {
Expand All @@ -37,6 +36,7 @@
},
"devDependencies": {
"@endo/ses-ava": "^0.2.44",
"@types/babel__traverse": "^7.20.2",
"ava": "^5.3.0",
"c8": "^7.14.0",
"tsd": "^0.28.1"
Expand Down Expand Up @@ -64,6 +64,14 @@
"timeout": "2m"
},
"dependencies": {
"@babel/types": "7.20.2"
"@agoric/babel-generator": "7.17.6",
"@babel/parser": "7.20.3",
"@babel/traverse": "7.20.1",
"@babel/types": "7.23.0",
"@types/babel__generator": "7.6.5",
"source-map": "0.7.4"
},
"resolutions": {
"@babel/types": "7.23.0"
}
}
14 changes: 3 additions & 11 deletions packages/transforms/src/evade-censor.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// @ts-check

/**
* Provides functions for evading SES restrictions.
*
Expand All @@ -25,10 +23,11 @@ const HTML_COMMENT_END_RE = new RegExp(`--${'>'}`, 'g');
* Rewrites a Comment Node to avoid triggering SES restrictions.
*
* Apparently coerces all comments to block comments.
*
* @param {import('@babel/types').Comment} node
* @param {LocationUnmapper} [unmapLoc]
* @param {import('./location-unmapper').LocationUnmapper} [unmapLoc]
*/
export function transformComment(node, unmapLoc) {
export function evadeCensor(node, unmapLoc) {
node.type = 'CommentBlock';
// Within comments...
node.value = node.value
Expand All @@ -46,10 +45,3 @@ export function transformComment(node, unmapLoc) {
unmapLoc(node.loc);
}
}

/**
* A function which modifies a Node's source location so that sourcemaps work properly.
* @callback LocationUnmapper
* @param {import('@babel/types').SourceLocation} [loc]
* @returns {void}
*/
99 changes: 97 additions & 2 deletions packages/transforms/src/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,97 @@
// place holder to unwedge lint
export {};
import babelGenerate from '@agoric/babel-generator';
import babelTraverse from '@babel/traverse';
import * as babelParser from '@babel/parser';
import { makeLocationUnmapper } from './location-unmapper.js';
import { evadeCensor } from './evade-censor.js';

export * from './evade-censor.js';
export * from './location-unmapper.js';

/** @type {typeof import('@babel/traverse')} */
const { default: traverse } = /** @type {any} */ (babelTraverse);

/** @type {typeof import('@babel/generator')} */
const { default: generate } = /** @type {any} */ (babelGenerate);

const { parse: parseBabel } = babelParser;

/**
* Transforms AST by rewriting comments
* @internal
* @param {import('@babel/types').File} ast
* @param {import('./location-unmapper.js').LocationUnmapper} [unmapLoc]
* @returns {void}
*/
function transformAst(ast, unmapLoc) {
traverse(ast, {
enter(p) {
const { loc, leadingComments, innerComments, trailingComments, type } =
p.node;
// discriminated union
if ('comments' in p.node) {
(p.node.comments || []).forEach(node => evadeCensor(node, unmapLoc));
}
// Rewrite all comments.
(leadingComments || []).forEach(node => evadeCensor(node, unmapLoc));
// XXX: there is no such Node having type matching /^Comment.+/ in
// @babel/types
if (type.startsWith('Comment')) {
// @ts-expect-error - see above XXX
evadeCensor(p.node, unmapLoc);
}
(innerComments || []).forEach(node => evadeCensor(node, unmapLoc));
// If not a comment, and we are unmapping the source maps,
// then do it for this location.
if (unmapLoc) {
unmapLoc(loc);
}
(trailingComments || []).forEach(node => evadeCensor(node, unmapLoc));
},
});
}

/**
* Apply source transforms on the given code and source map
*
* @param {string} code - Code to transform
* @param {string} sourceMap - Original source map
* @param {string} sourceMapUrl - URL of original source map
* @param {TransformSourceOptions} options - Options for the transform
* @returns {Promise<import('@agoric/babel-generator').GeneratorResult>} Object
* containing new code and source map
*/
export async function transformSource(
code,
sourceMap,
sourceMapUrl,
{ useLocationUnmap, sourceType } = {},
) {
await null;

// Parse the rolled-up chunk with Babel.
// We are prepared for different module systems.
const ast = parseBabel(code, {
sourceType,
});

let unmapLoc;
if (useLocationUnmap) {
unmapLoc = await makeLocationUnmapper(sourceMap, ast);
}

transformAst(ast, unmapLoc);

// Now generate the sources with the new positions.
return generate(ast, {
sourceFileName: sourceMapUrl,
sourceMaps: true,
retainLines: true,
compact: true,
});
}

/**
* @typedef TransformSourceOptions
* @property {boolean} [useLocationUnmap]
* @property {import('@babel/parser').ParserOptions['sourceType']} [sourceType]
*/
Loading

0 comments on commit de80b75

Please sign in to comment.