Skip to content

Commit

Permalink
fix: make output from bundleSource correspond to source map lines
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed May 12, 2020
1 parent 7f6e271 commit c1ddd4a
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 7 deletions.
3 changes: 2 additions & 1 deletion packages/bundle-source/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"@rollup/plugin-node-resolve": "^7.1.1",
"acorn": "^7.1.0",
"esm": "^3.2.5",
"rollup": "^1.32.0"
"rollup": "^1.32.0",
"source-map": "^0.7.3"
},
"keywords": [],
"files": [
Expand Down
41 changes: 39 additions & 2 deletions packages/bundle-source/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import commonjs0 from '@rollup/plugin-commonjs';
import eventualSend from '@agoric/acorn-eventual-send';
import * as acorn from 'acorn';

import { SourceMapConsumer } from 'source-map';

const DEFAULT_MODULE_FORMAT = 'nestedEvaluate';
const DEFAULT_FILE_PREFIX = '/bundled-source';
const SUPPORTED_FORMATS = ['getExport', 'nestedEvaluate'];
Expand Down Expand Up @@ -55,11 +57,46 @@ export default async function bundleSource(
if (isEntry) {
entrypoint = fileName;
}
let unmappedCode = code;
if (
moduleFormat === 'nestedEvaluate' &&
!fileName.startsWith('_virtual/')
) {
unmappedCode = '';
// eslint-disable-next-line no-await-in-loop
const consumer = await new SourceMapConsumer(chunk.map);
const genLines = code.split('\n');
let lastLine = 1;
for (let genLine = 0; genLine < genLines.length; genLine += 1) {
const pos = consumer.originalPositionFor({
line: genLine + 1,
column: 0,
});
const { line: origLine } = pos;

const srcLine = origLine === null ? lastLine : origLine;
const priorChar = unmappedCode[unmappedCode.length - 1];
if (
srcLine === lastLine &&
!['\n', ';', '{', undefined].includes(priorChar) &&
!['}'].includes(genLines[genLine][0])
) {
unmappedCode += `;`;
}
while (lastLine < srcLine) {
unmappedCode += `\n`;
lastLine += 1;
}
unmappedCode += genLines[genLine];
}
}

// Rewrite apparent import expressions so that they don't fail under SES.
// We also do apparent HTML comments.
const defangedCode = code
const defangedCode = unmappedCode
.replace(IMPORT_RE, '$1notreally')
.replace(HTML_COMMENT_RE, '<->');
// console.log(`<<<<<< ${fileName}\n${defangedCode}\n>>>>>>>>`);
sourceBundle[fileName] = defangedCode;
}

Expand Down Expand Up @@ -191,7 +228,7 @@ ${sourceMap}`;
);
}

// log('evaluating', typeof nestedEvaluate, code);
// log('evaluating', code);
return nestedEvaluate(code)(contextRequire);
}

Expand Down
7 changes: 4 additions & 3 deletions packages/bundle-source/test/sanity.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ test('nestedEvaluate', async t => {

const bundle = ex1.default();
const err = bundle.makeError('foo');
// console.log(err.stack);
t.assert(
err.stack.indexOf('(/bundled-source/encourage.js:') >= 0,
'bundled source is in stack trace',
err.stack.indexOf('(/bundled-source/encourage.js:3:') >= 0,
'bundled source is in stack trace with correct line number',
);

const {
Expand Down Expand Up @@ -94,7 +95,7 @@ test('getExport', async t => {
const srcMap2 = `(${src2})\n${map2}`;

const nestedEvaluate = src => {
// console.log('========== evaluating', src);
// console.log('========== evaluating', src, '\n=========');
return evaluate(src, { require, nestedEvaluate });
};
// eslint-disable-next-line no-eval
Expand Down
2 changes: 2 additions & 0 deletions packages/evaluate/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ export const makeEvaluators = (makerOptions = {}) => {
}
})`;

// console.log('##### evaluate ######\n${src}\n###### end evaluate #####');

// The eval below is indirect, so that we are only in the global scope.
// eslint-disable-next-line no-eval
return (1, eval)(scopedEval)(sourceState.endowments)(src);
Expand Down
2 changes: 1 addition & 1 deletion packages/transform-eventual-send/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function makeEventualSendTransformer(parser, generate) {
plugins: ['eventualSend'],
});
// Create the source from the ast.
const output = generate(ast, {}, source);
const output = generate(ast, { retainLines: true }, source);

// Work around Babel appending semicolons.
const maybeSource = output.code;
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -13460,6 +13460,11 @@ source-map@^0.5.0, source-map@^0.5.6:
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.5.7.tgz#8a039d2d1021d22d1ea14c80d8ea468ba2ef3fcc"
integrity sha1-igOdLRAh0i0eoUyA2OpGi6LvP8w=

source-map@^0.7.3:
version "0.7.3"
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.7.3.tgz#5302f8169031735226544092e64981f751750383"
integrity sha512-CkCj6giN3S+n9qrYiBTX5gystlENnRW5jZeNLHpe6aue+SrHcG5VYwujhW9s4dY31mEGsxBDrHR6oI69fTXsaQ==

sourcemap-codec@^1.4.4:
version "1.4.6"
resolved "https://registry.yarnpkg.com/sourcemap-codec/-/sourcemap-codec-1.4.6.tgz#e30a74f0402bad09807640d39e971090a08ce1e9"
Expand Down

0 comments on commit c1ddd4a

Please sign in to comment.