Skip to content

Commit

Permalink
fix(bundle-source): Transform inner comments
Browse files Browse the repository at this point in the history
  • Loading branch information
kriskowal committed Mar 31, 2021
1 parent 1d242f5 commit 4e212ce
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 7 deletions.
1 change: 1 addition & 0 deletions packages/bundle-source/demo/comments/types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/** @typedef {import('./types.js').Bogus} Bogus */
20 changes: 14 additions & 6 deletions packages/bundle-source/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,20 @@ async function makeLocationUnmapper({ sourceMap, ast }) {
function transformAst(ast, unmapLoc) {
babelTraverse(ast, {
enter(p) {
const { loc, leadingComments, trailingComments } = p.node;
if (p.node.comments) {
p.node.comments = [];
}
const {
loc,
comments,
leadingComments,
innerComments,
trailingComments,
} = p.node;
(comments || []).forEach(node => rewriteComment(node, unmapLoc));
// Rewrite all comments.
(leadingComments || []).forEach(node => rewriteComment(node, unmapLoc));
if (p.node.type.startsWith('Comment')) {
rewriteComment(p.node, unmapLoc);
}
(innerComments || []).forEach(node => rewriteComment(node, unmapLoc));
// If not a comment, and we are unmapping the source maps,
// then do it for this location.
if (unmapLoc) {
Expand All @@ -113,7 +118,10 @@ function transformAst(ast, unmapLoc) {
});
}

async function transformSource(code, { sourceMap, useLocationUnmap, sourceType } = {}) {
async function transformSource(
code,
{ sourceMap, useLocationUnmap, sourceType } = {},
) {
// Parse the rolled-up chunk with Babel.
// We are prepared for different module systems.
const ast = (babelParser.parse || babelParser)(code, {
Expand Down Expand Up @@ -155,7 +163,7 @@ export default async function bundleSource(
moduleTransforms: {
async mjs(sourceBytes) {
const source = textDecoder.decode(sourceBytes);
const {code: object} = await transformSource(source, {
const { code: object } = await transformSource(source, {
sourceType: 'module',
});
const objectBytes = textEncoder.encode(object);
Expand Down
5 changes: 4 additions & 1 deletion packages/bundle-source/test/sanity.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,10 @@ export function makeSanityTests(stackFiltering) {
moduleFormat: mf2,
source: src2,
sourceMap: map2,
} = await bundleSource(`${__dirname}/../demo/dir1/encourage.js`);
} = await bundleSource(
`${__dirname}/../demo/dir1/encourage.js`,
'nestedEvaluate',
);
t.is(mf2, 'nestedEvaluate', 'module format 2 is nestedEvaluate');

const srcMap2 = `(${src2})\n${map2}`;
Expand Down
22 changes: 22 additions & 0 deletions packages/bundle-source/test/test-comment.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/* global __dirname */
import '@agoric/install-ses';
import test from 'ava';
import { decodeBase64 } from '@agoric/base64';
import { parseArchive } from '@agoric/compartment-mapper';
import bundleSource from '..';

function evaluate(src, endowments) {
Expand Down Expand Up @@ -61,3 +63,23 @@ test('comment block closer', async t => {
const srcMap1 = `(${src1})`;
nestedEvaluate(srcMap1)();
});

test('comments not associated with a code point', async t => {
t.plan(1);
const { endoZipBase64 } = await bundleSource(
`${__dirname}/../demo/comments/types.js`,
'endoZipBase64',
);
const endoZipBytes = decodeBase64(endoZipBase64);
const application = await parseArchive(endoZipBytes);
// If the TypeScript comment in this module does not get rewritten,
// attempting to import the module will throw a SES censorship error since
// import calls in comments are not distinguishable from containment escape
// through dynamic import.
// To verify, disable this line in src/index.js and observe that this test
// fails:
// (innerComments || []).forEach(node => rewriteComment(node, unmapLoc));
// eslint-disable-next-line dot-notation
await application['import']('./demo/comments/types.js');
t.is(1, 1);
});

0 comments on commit 4e212ce

Please sign in to comment.