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 9 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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,5 @@ dist/
api-docs

.aider*

vendor
10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,15 @@
"atLeast": 0
},
"resolutions": {
"@babel/types": "7.24.0",
"@babel/code-frame": "./vendor/@babel/code-frame/code-frame-7.25.7.tgz",
"@babel/generator": "./vendor/@babel/generator/generator-7.25.7.tgz",
"@babel/parser": "./vendor/@babel/parser/parser-7.25.7.tgz",
"@babel/template": "./vendor/@babel/template/template-7.25.7.tgz",
"@babel/highlight": "./vendor/@babel/highlight/highlight-7.25.7.tgz",
"@babel/helper-string-parser": "./vendor/@babel/helper-string-parser/helper-string-parser-7.25.7.tgz",
"@babel/helper-validator-identifier": "./vendor/@babel/helper-validator-identifier/helper-validator-identifier-7.25.7.tgz",
"@babel/traverse": "./vendor/@babel/traverse/traverse-7.25.7.tgz",
"@babel/types": "./vendor/@babel/types/types-7.25.7.tgz",
"@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
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,
});
}
2 changes: 1 addition & 1 deletion packages/module-source/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"test": "ava"
},
"dependencies": {
"@agoric/babel-generator": "^7.17.6",
"@babel/generator": "^7.17.6",
"@babel/parser": "^7.23.6",
"@babel/traverse": "^7.23.6",
"@babel/types": "^7.24.0",
Expand Down
21 changes: 7 additions & 14 deletions packages/module-source/src/transform-analyze.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @ts-nocheck XXX Babel types
import { makeTransformSource } from './transformSource.js';
import makeModulePlugins from './babelPlugin.js';
import { makeTransformSource } from './transform-source.js';
import makeModulePlugins from './babel-plugin.js';

import * as h from './hidden.js';

Expand Down Expand Up @@ -71,7 +71,7 @@ const makeCreateStaticRecord = transformSource =>
.map(
src =>
`[${js(src)}, [${Object.entries(isrc[src])
.map(([exp, upds]) => `[${js(exp)}, [${upds.join(',')}]]`)
.map(([exp, upds]) => `[${js(exp)},[${upds.join(',')}]]`)
.join(',')}]]`,
)
.join(',')}]);`;
Expand All @@ -80,9 +80,7 @@ const makeCreateStaticRecord = transformSource =>
let src = '';
if (cvname) {
// It's a function assigned to, so set its name property.
src = `Object.defineProperty(${cvname}, 'name', {value: ${js(
vname,
)}});`;
src = `Object.defineProperty(${cvname},'name',{value:${js(vname)}});`;
}
const hDeclId = isOnce ? h.HIDDEN_ONCE : h.HIDDEN_LIVE;
src += `${hDeclId}.${vname}(${cvname || ''});`;
Expand All @@ -99,14 +97,9 @@ const makeCreateStaticRecord = transformSource =>
// well.
// Relies on the evaluator to ensure these functions are strict.
let functorSource = `\
({ \
imports: ${h.HIDDEN_IMPORTS}, \
liveVar: ${h.HIDDEN_LIVE}, \
onceVar: ${h.HIDDEN_ONCE}, \
importMeta: ${h.HIDDEN_META}, \
}) => (function () { 'use strict'; \
${preamble} \
${scriptSource}
({imports:${h.HIDDEN_IMPORTS},liveVar:${h.HIDDEN_LIVE},onceVar:${h.HIDDEN_ONCE},importMeta:${h.HIDDEN_META}})=>(function(){'use strict';\
${preamble}\
${scriptSource}
})()
`;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @ts-nocheck XXX Babel types
import * as babelParser from '@babel/parser';
import babelGenerate from '@agoric/babel-generator';
import babelGenerate from '@babel/generator';
import babelTraverse from '@babel/traverse';
import * as babelTypes from '@babel/types';

Expand All @@ -26,22 +26,30 @@ export const makeTransformSource = (makeModulePlugins, babel = null) => {
const { sourceUrl, sourceMapUrl, sourceType, sourceMap, sourceMapHook } =
sourceOptions;

const ast = parseBabel(source, { sourceType });
const ast = parseBabel(source, {
sourceType,
tokens: true,
createParenthesizedExpression: true,
});

traverseBabel(ast, visitorFromPlugin(analyzePlugin));
traverseBabel(ast, visitorFromPlugin(transformPlugin));

const sourceMaps = sourceOptions.sourceMapHook !== undefined;

const { code: transformedSource, map: transformedSourceMap } =
generateBabel(ast, {
sourceFileName: sourceMapUrl,
sourceMaps,
inputSourceMap: sourceMap,
retainLines: true,
compact: true,
verbatim: true,
});
generateBabel(
ast,
{
sourceFileName: sourceMapUrl,
sourceMaps,
inputSourceMap: sourceMap,
retainLines: true,
preserveFormat: true,
verbatim: true,
},
source,
);

if (sourceMaps) {
sourceMapHook(transformedSourceMap, {
Expand Down
4 changes: 2 additions & 2 deletions packages/module-source/test/benchmark-babel-plugin.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import Benchmark from 'benchmark';
import fs from 'fs';
import url from 'url';
import { makeTransformSource } from '../src/transformSource.js';
import makeModulePlugins from '../src/babelPlugin.js';
import { makeTransformSource } from '../src/transform-source.js';
import makeModulePlugins from '../src/babel-plugin.js';

const suite = new Benchmark.Suite();

Expand Down
11 changes: 11 additions & 0 deletions packages/module-source/test/fixtures/format-preserved.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
({imports:$h͏_imports,liveVar:$h͏_live,onceVar:$h͏_once,importMeta:$h͏____meta})=>(function(){'use strict';$h͏_imports([]);Object.defineProperty(createBinop,'name',{value:"createBinop"});$h͏_once.createBinop(createBinop);// deliberately offset
function TokenType() {}
const beforeExpr = 0;

function createBinop(name, binop) {
return new TokenType(name, {
beforeExpr,
binop,
});
}
})()
10 changes: 10 additions & 0 deletions packages/module-source/test/fixtures/preserve-format.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// deliberately offset
function TokenType() {}
const beforeExpr = 0;

export function createBinop(name, binop) {
return new TokenType(name, {
beforeExpr,
binop,
});
}
16 changes: 16 additions & 0 deletions packages/module-source/test/preserve-format.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import url from 'url';
import fs from 'fs';
import { ModuleSource } from '../src/module-source.js';
import './lockdown.js';

function readFixture(filename) {
return fs.readFileSync(
url.fileURLToPath(new URL(filename, import.meta.url)),
'utf-8',
);
}

const { __syncModuleProgram__ } = new ModuleSource(
readFixture('fixtures/preserve-format.js'),
);
console.error(__syncModuleProgram__);
20 changes: 20 additions & 0 deletions packages/module-source/test/preserve-format.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import url from 'url';
import fs from 'fs';
import test from 'ava';
import { ModuleSource } from '../src/module-source.js';
import './lockdown.js';

function readFixture(filename) {
return fs.readFileSync(
url.fileURLToPath(new URL(filename, import.meta.url)),
'utf-8',
);
}

test('preserves formatting', t => {
const { __syncModuleProgram__: actual } = new ModuleSource(
readFixture('fixtures/preserve-format.js'),
);
const expected = readFixture('fixtures/format-preserved.txt');
t.is(actual, expected);
});
Binary file added vendor/@babel/cli/cli-7.25.7.tgz
Binary file not shown.
Binary file added vendor/@babel/code-frame/code-frame-7.25.7.tgz
Binary file not shown.
Binary file added vendor/@babel/compat-data/compat-data-7.25.7.tgz
Binary file not shown.
Binary file added vendor/@babel/core/core-7.25.7.tgz
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added vendor/@babel/generator/generator-7.25.7.tgz
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added vendor/@babel/helpers/helpers-7.25.7.tgz
Binary file not shown.
Binary file added vendor/@babel/highlight/highlight-7.25.7.tgz
Binary file not shown.
Binary file added vendor/@babel/node/node-7.25.7.tgz
Binary file not shown.
Binary file added vendor/@babel/parser/parser-7.25.7.tgz
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added vendor/@babel/preset-env/preset-env-7.25.7.tgz
Binary file not shown.
Binary file added vendor/@babel/preset-flow/preset-flow-7.25.7.tgz
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added vendor/@babel/register/register-7.25.7.tgz
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added vendor/@babel/runtime/runtime-7.25.7.tgz
Binary file not shown.
Binary file added vendor/@babel/standalone/standalone-7.25.7.tgz
Binary file not shown.
Binary file added vendor/@babel/template/template-7.25.7.tgz
Binary file not shown.
Binary file added vendor/@babel/traverse/traverse-7.25.7.tgz
Binary file not shown.
Binary file added vendor/@babel/types/types-7.25.7.tgz
Binary file not shown.
Loading
Loading