Skip to content

Commit

Permalink
feat(evasive-transform): Preserve format with Babel
Browse files Browse the repository at this point in the history
  • Loading branch information
kriskowal committed Sep 4, 2024
1 parent 6d72bd3 commit ff89345
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 11 deletions.
2 changes: 1 addition & 1 deletion packages/evasive-transform/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
"timeout": "2m"
},
"dependencies": {
"@agoric/babel-generator": "^7.17.6",
"@babel/generator": "^7.17.6",
"@babel/parser": "^7.23.6",
"@babel/traverse": "^7.23.6",
"source-map-js": "^1.2.0"
Expand Down
19 changes: 12 additions & 7 deletions packages/evasive-transform/src/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @module
*/

import babelGenerator from '@agoric/babel-generator';
import babelGenerator from '@babel/generator';

// TODO The following is sufficient on Node.js, but for compatibility with
// `node -r esm`, we must use the pattern below.
Expand Down Expand Up @@ -66,12 +66,17 @@ export const generate =
// TODO Use options?.sourceUrl when resolved:
// https://github.com/Agoric/agoric-sdk/issues/8671
const sourceUrl = options ? options.sourceUrl : undefined;
const result = generator(ast, {
sourceFileName: sourceUrl,
sourceMaps: Boolean(sourceUrl),
retainLines: true,
compact: true,
});
const source = options ? options.source : undefined;
const result = generator(
ast,
{
sourceFileName: sourceUrl,
sourceMaps: Boolean(sourceUrl),
retainLines: true,
preserveFormat: true,
},
source,
);

if (sourceUrl) {
return {
Expand Down
2 changes: 1 addition & 1 deletion packages/evasive-transform/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export function evadeCensorSync(source, options) {
if (sourceUrl) {
return generate(ast, { sourceUrl });
}
return generate(ast);
return generate(ast, { source });
}

/**
Expand Down
8 changes: 6 additions & 2 deletions packages/evasive-transform/src/parse-ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ const { parse: parseBabel } = babelParser;
* @param {ParseAstOptions} [opts] - Options for underlying parser
* @internal
*/
export function parseAst(source, opts) {
export function parseAst(source, opts = {}) {
// Might not want to pass `opts` verbatim, but also might not matter!
return parseBabel(source, opts);
return parseBabel(source, {
tokens: true,
createParenthesizedExpression: true,
...opts,
});
}

0 comments on commit ff89345

Please sign in to comment.