Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Preserve formatting #2444

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
"atLeast": 0
},
"resolutions": {
"@babel/types": "7.24.0",
"@lerna/version@npm:5.6.2": "patch:@lerna/version@npm%3A5.6.2#~/.yarn/patches/@lerna-version-npm-5.6.2-ce2d9cb2f5.patch",
"@lerna/conventional-commits@npm:5.6.2": "patch:@lerna/conventional-commits@npm%3A5.6.2#~/.yarn/patches/@lerna-conventional-commits-npm-5.6.2-a373ba4bc0.patch"
}
Expand Down
10 changes: 5 additions & 5 deletions packages/evasive-transform/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@
"test:rebuild-fixtures": "node test/rebuild-fixtures.js"
},
"devDependencies": {
"@babel/types": "^7.24.0",
"@babel/types": "~7.26.0",
"@endo/ses-ava": "workspace:^",
"@rollup/plugin-commonjs": "^19.0.0",
"@rollup/plugin-node-resolve": "^13.0.0",
"@types/babel__generator": "^7.6.8",
"@types/babel__traverse": "^7.20.5",
"@types/babel__traverse": "^7.20.6",
"ava": "^6.1.3",
"c8": "^7.14.0",
"eslint": "^8.57.0",
Expand Down Expand Up @@ -78,9 +78,9 @@
"timeout": "2m"
},
"dependencies": {
"@agoric/babel-generator": "^7.17.6",
"@babel/parser": "^7.23.6",
"@babel/traverse": "^7.23.6",
"@babel/generator": "~7.26.2",
"@babel/parser": "~7.26.2",
"@babel/traverse": "~7.25.9",
"source-map-js": "^1.2.0"
}
}
25 changes: 17 additions & 8 deletions packages/evasive-transform/src/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

// @ts-ignore XXX no types defined
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 All @@ -21,6 +21,7 @@ const generator = /** @type {typeof import('@babel/generator')['default']} */ (
* Options for {@link generateCode} with source map
*
* @typedef GenerateAstOptionsWithSourceMap
* @property {string} [source]
* @property {string} sourceUrl - If present, we will generate a source map
* @internal
*/
Expand All @@ -29,7 +30,8 @@ const generator = /** @type {typeof import('@babel/generator')['default']} */ (
* Options for {@link generateCode} (no source map generated)
*
* @typedef GenerateAstOptionsWithoutSourceMap
* @property {undefined} sourceUrl - This should be undefined or otherwise not provided
* @property {string} [source]
* @property {undefined} [sourceUrl] - This should be undefined or otherwise not provided
* @internal
*/

Expand Down Expand Up @@ -67,12 +69,19 @@ 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,
...(source === undefined
? {}
: { experimental_preserveFormat: true }),
},
source,
);

if (sourceUrl) {
return {
Expand Down
4 changes: 2 additions & 2 deletions packages/evasive-transform/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ export function evadeCensorSync(source, options) {
}

if (sourceUrl) {
return generate(ast, { sourceUrl });
return generate(ast, { source, 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,
createParenthesizedExpressions: true,
...opts,
});
}
99 changes: 90 additions & 9 deletions packages/evasive-transform/test/snapshots/evade-censor.test.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,26 @@ Generated by [AVA](https://avajs.dev).

> Snapshot 1

'\'use strict\';var node_fs=require(\'node:fs\');/** * @returns {IMPORT(\'node:fs\').constants.F_OK} */function bambalam(){return node_fs.constants.F_OK;}/** * <!=- this should become less evil -=> */function monkey(){return true;}exports.bambalam=bambalam;exports.monkey=monkey;/*# sourceMappingURL=index.cjs.map*/'
'\'use strict\';var node_fs = require(\'node:fs\');/** * @returns {IMPORT(\'node:fs\').constants.F_OK} */function bambalam() { return node_fs.constants.F_OK;}/** * <!=- this should become less evil -=> */function monkey() { return true;}exports.bambalam = bambalam;exports.monkey = monkey;/*# sourceMappingURL=index.cjs.map*/'

## evadeCensor() - successful source transform w/ source map

> Snapshot 1

'\'use strict\';var node_fs=require(\'node:fs\');/** * @returns {IMPORT(\'node:fs\').constants.F_OK} */function bambalam(){return node_fs.constants.F_OK;}/** * <!=- this should become less evil -=> */function monkey(){return true;}exports.bambalam=bambalam;exports.monkey=monkey;/*# sourceMappingURL=index.cjs.map*/'
'\'use strict\';var node_fs = require(\'node:fs\');/** * @returns {IMPORT(\'node:fs\').constants.F_OK} */function bambalam() { return node_fs.constants.F_OK;}/** * <!=- this should become less evil -=> */function monkey() { return true;}exports.bambalam = bambalam;exports.monkey = monkey;/*# sourceMappingURL=index.cjs.map*/'

## evadeCensor() - successful source transform w/ source map & source URL

> Snapshot 1

'\'use strict\';var node_fs=require(\'node:fs\');/** * @returns {IMPORT(\'node:fs\').constants.F_OK} */function bambalam(){return node_fs.constants.F_OK;}/** * <!=- this should become less evil -=> */function monkey(){return true;}exports.bambalam=bambalam;exports.monkey=monkey;/*# sourceMappingURL=index.cjs.map*/'
'\'use strict\';var node_fs = require(\'node:fs\');/** * @returns {IMPORT(\'node:fs\').constants.F_OK} */function bambalam() { return node_fs.constants.F_OK;}/** * <!=- this should become less evil -=> */function monkey() { return true;}exports.bambalam = bambalam;exports.monkey = monkey;/*# sourceMappingURL=index.cjs.map*/'

> Snapshot 2

{
mappings: 'AAAA;;AAEA,GAAIA,CAAAA,OAAO,CAAGC,OAAO,CAAC,SAAD,CAArB;;AAEA;AACA;AACA;AACA,QAASC,CAAAA,QAAT,EAAoB;AAClB,MAAOF,CAAAA,OAAO,CAACG,SAAR,CAAkBC,IAAzB;AACD;;AAED;AACA;AACA;AACA,QAASC,CAAAA,MAAT,EAAkB;AAChB,MAAO,KAAP;AACD;;AAEDC,OAAO,CAACJ,QAAR,CAAmBA,QAAnB;AACAI,OAAO,CAACD,MAAR,CAAiBA,MAAjB;AACA',
file: undefined,
ignoreList: [],
mappings: 'AAAA,YAAY;;AAEZ,IAAIA,OAAO,GAAGC,OAAO,CAAC,SAAS,CAAC;;AAEhC;AACA;AACA;AACA,SAASC,QAAQA,CAAA,EAAG;AAAA,EAClB,OAAOF,OAAO,CAACG,SAAS,CAACC,IAAI;AAAC;;AAGhC;AACA;AACA;AACA,SAASC,MAAMA,CAAA,EAAG;AAAA,EAChB,OAAO,IAAI;AAAC;;AAGdC,OAAO,CAACJ,QAAQ,GAAGA,QAAQ;AAC3BI,OAAO,CAACD,MAAM,GAAGA,MAAM;AACvB',
names: [
'node_fs',
'require',
Expand All @@ -35,22 +37,49 @@ Generated by [AVA](https://avajs.dev).
'monkey',
'exports',
],
sourceRoot: undefined,
sources: [
'index.cjs',
],
sourcesContent: [
`'use strict';␊
var node_fs = require('node:fs');␊
/**␊
* @returns {import('node:fs').constants.F_OK}␊
*/␊
function bambalam() {␊
return node_fs.constants.F_OK;␊
}␊
/**␊
* <!-- this should become less evil -->␊
*/␊
function monkey() {␊
return true;␊
}␊
exports.bambalam = bambalam;␊
exports.monkey = monkey;␊
//# sourceMappingURL=index.cjs.map␊
`,
],
version: 3,
}

## evadeCensor() - successful source transform w/ source URL

> Snapshot 1

'\'use strict\';var node_fs=require(\'node:fs\');/** * @returns {IMPORT(\'node:fs\').constants.F_OK} */function bambalam(){return node_fs.constants.F_OK;}/** * <!=- this should become less evil -=> */function monkey(){return true;}exports.bambalam=bambalam;exports.monkey=monkey;/*# sourceMappingURL=index.cjs.map*/'
'\'use strict\';var node_fs = require(\'node:fs\');/** * @returns {IMPORT(\'node:fs\').constants.F_OK} */function bambalam() { return node_fs.constants.F_OK;}/** * <!=- this should become less evil -=> */function monkey() { return true;}exports.bambalam = bambalam;exports.monkey = monkey;/*# sourceMappingURL=index.cjs.map*/'

> Snapshot 2

{
mappings: 'AAAA;;AAEA,GAAIA,CAAAA,OAAO,CAAGC,OAAO,CAAC,SAAD,CAArB;;AAEA;AACA;AACA;AACA,QAASC,CAAAA,QAAT,EAAoB;AAClB,MAAOF,CAAAA,OAAO,CAACG,SAAR,CAAkBC,IAAzB;AACD;;AAED;AACA;AACA;AACA,QAASC,CAAAA,MAAT,EAAkB;AAChB,MAAO,KAAP;AACD;;AAEDC,OAAO,CAACJ,QAAR,CAAmBA,QAAnB;AACAI,OAAO,CAACD,MAAR,CAAiBA,MAAjB;AACA',
file: undefined,
ignoreList: [],
mappings: 'AAAA,YAAY;;AAEZ,IAAIA,OAAO,GAAGC,OAAO,CAAC,SAAS,CAAC;;AAEhC;AACA;AACA;AACA,SAASC,QAAQA,CAAA,EAAG;AAAA,EAClB,OAAOF,OAAO,CAACG,SAAS,CAACC,IAAI;AAAC;;AAGhC;AACA;AACA;AACA,SAASC,MAAMA,CAAA,EAAG;AAAA,EAChB,OAAO,IAAI;AAAC;;AAGdC,OAAO,CAACJ,QAAQ,GAAGA,QAAQ;AAC3BI,OAAO,CAACD,MAAM,GAAGA,MAAM;AACvB',
names: [
'node_fs',
'require',
Expand All @@ -60,28 +89,55 @@ Generated by [AVA](https://avajs.dev).
'monkey',
'exports',
],
sourceRoot: undefined,
sources: [
'index.cjs',
],
sourcesContent: [
`'use strict';␊
var node_fs = require('node:fs');␊
/**␊
* @returns {import('node:fs').constants.F_OK}␊
*/␊
function bambalam() {␊
return node_fs.constants.F_OK;␊
}␊
/**␊
* <!-- this should become less evil -->␊
*/␊
function monkey() {␊
return true;␊
}␊
exports.bambalam = bambalam;␊
exports.monkey = monkey;␊
//# sourceMappingURL=index.cjs.map␊
`,
],
version: 3,
}

## evadeCensor() - successful source transform w/ source map & unmapping

> Snapshot 1

'\'use strict\';varnode_fs=require(\'node:fs\');/** * @returns {IMPORT(\'node:fs\').constants.F_OK} */function bambalam(){return node_fs.constants.F_OK;}/** * <!=- this should become less evil -=> */function monkey(){return true;}exports.bambalam=bambalam;exports.monkey=monkey;/*# sourceMappingURL=index.cjs.map*/'
'\'use strict\';var node_fs = require(\'node:fs\');/** * @returns {IMPORT(\'node:fs\').constants.F_OK} */ function bambalam(){ return node_fs.constants.F_OK;}/** * <!=- this should become less evil -=> */ function monkey(){ return true;} exports. bambalam=bambalam;exports. monkey=monkey;/*# sourceMappingURL=index.cjs.map*/'

## evadeCensor() - successful source transform w/ source map, source URL & unmapping

> Snapshot 1

'\'use strict\';varnode_fs=require(\'node:fs\');/** * @returns {IMPORT(\'node:fs\').constants.F_OK} */function bambalam(){return node_fs.constants.F_OK;}/** * <!=- this should become less evil -=> */function monkey(){return true;}exports.bambalam=bambalam;exports.monkey=monkey;/*# sourceMappingURL=index.cjs.map*/'
'\'use strict\';var node_fs = require(\'node:fs\');/** * @returns {IMPORT(\'node:fs\').constants.F_OK} */ function bambalam(){ return node_fs.constants.F_OK;}/** * <!=- this should become less evil -=> */ function monkey(){ return true;} exports. bambalam=bambalam;exports. monkey=monkey;/*# sourceMappingURL=index.cjs.map*/'

> Snapshot 2

{
mappings: 'AAAA;;AAEAA,OAAA,CAAAC,OAAA,CAAA,SAAA,CAFA,CAEA;AACA;AACA;AACO,QAASC,CAAAA,QAAT,EAAoB;AACzB,MAAOF,CAAAA,OAAA,CAAAG,SAAA,CAAUC,IAAjB,CADyB,CAH3B;AACA;AACA,GACO,QAASC,CAAAA,MAAT,EAAkB,CACvB,MAAO,KAAP,CADuB,CAChBC,OAAA,CAAAJ,QAAA,CAAAA,QAAA,CAAAI,OAAA,CAAAD,MAAA,CAAAA,MAAA',
file: undefined,
ignoreList: [],
mappings: 'AAAA;;AAAA,GAEA,CAAAA,OAAA,EAAAC,QAAA;AACA;AACA;AAFA;;AAGO,2VAASC,SAAA,CAAW;AACzB,2VAAOF,OAAA,CAAAG,SAAA,CAAUC,IAAV;AAAP,CAJF;AACA;AACA,GAFA;;;AAGO,2VAASC,OAAA,CAAS;AACvB,2VAAO;AAAP,CADK;;AACEC,OAAA,oVAAAJ,QAAA,CAAAA,QAAA;AAAAI,OAAA,oVAAAD,MAAA,CAAAA,MAAA',
names: [
'node_fs',
'require',
Expand All @@ -91,8 +147,33 @@ Generated by [AVA](https://avajs.dev).
'monkey',
'exports',
],
sourceRoot: undefined,
sources: [
'index.cjs',
],
sourcesContent: [
`'use strict';␊
var node_fs = require('node:fs');␊
/**␊
* @returns {import('node:fs').constants.F_OK}␊
*/␊
function bambalam() {␊
return node_fs.constants.F_OK;␊
}␊
/**␊
* <!-- this should become less evil -->␊
*/␊
function monkey() {␊
return true;␊
}␊
exports.bambalam = bambalam;␊
exports.monkey = monkey;␊
//# sourceMappingURL=index.cjs.map␊
`,
],
version: 3,
}
Binary file modified packages/evasive-transform/test/snapshots/evade-censor.test.js.snap
Binary file not shown.
8 changes: 4 additions & 4 deletions packages/module-source/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@
"test": "ava"
},
"dependencies": {
"@agoric/babel-generator": "^7.17.6",
"@babel/parser": "^7.23.6",
"@babel/traverse": "^7.23.6",
"@babel/types": "^7.24.0",
"@babel/generator": "~7.26.2",
"@babel/parser": "~7.26.2",
"@babel/traverse": "~7.25.9",
"@babel/types": "~7.26.0",
"ses": "workspace:^"
},
"devDependencies": {
Expand Down
Loading