-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: move all of transform.js into @endo/transforms
- Loading branch information
Showing
12 changed files
with
261 additions
and
207 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './src/index.js'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,97 @@ | ||
// place holder to unwedge lint | ||
export {}; | ||
import babelGenerate from '@agoric/babel-generator'; | ||
import babelTraverse from '@babel/traverse'; | ||
import * as babelParser from '@babel/parser'; | ||
import { makeLocationUnmapper } from './location-unmapper.js'; | ||
import { evadeCensor } from './evade-censor.js'; | ||
|
||
export * from './evade-censor.js'; | ||
export * from './location-unmapper.js'; | ||
|
||
/** @type {typeof import('@babel/traverse')} */ | ||
const { default: traverse } = /** @type {any} */ (babelTraverse); | ||
|
||
/** @type {typeof import('@babel/generator')} */ | ||
const { default: generate } = /** @type {any} */ (babelGenerate); | ||
|
||
const { parse: parseBabel } = babelParser; | ||
|
||
/** | ||
* Transforms AST by rewriting comments | ||
* @internal | ||
* @param {import('@babel/types').File} ast | ||
* @param {import('./location-unmapper.js').LocationUnmapper} [unmapLoc] | ||
* @returns {void} | ||
*/ | ||
function transformAst(ast, unmapLoc) { | ||
traverse(ast, { | ||
enter(p) { | ||
const { loc, leadingComments, innerComments, trailingComments, type } = | ||
p.node; | ||
// discriminated union | ||
if ('comments' in p.node) { | ||
(p.node.comments || []).forEach(node => evadeCensor(node, unmapLoc)); | ||
} | ||
// Rewrite all comments. | ||
(leadingComments || []).forEach(node => evadeCensor(node, unmapLoc)); | ||
// XXX: there is no such Node having type matching /^Comment.+/ in | ||
// @babel/types | ||
if (type.startsWith('Comment')) { | ||
// @ts-expect-error - see above XXX | ||
evadeCensor(p.node, unmapLoc); | ||
} | ||
(innerComments || []).forEach(node => evadeCensor(node, unmapLoc)); | ||
// If not a comment, and we are unmapping the source maps, | ||
// then do it for this location. | ||
if (unmapLoc) { | ||
unmapLoc(loc); | ||
} | ||
(trailingComments || []).forEach(node => evadeCensor(node, unmapLoc)); | ||
}, | ||
}); | ||
} | ||
|
||
/** | ||
* Apply source transforms on the given code and source map | ||
* | ||
* @param {string} code - Code to transform | ||
* @param {string} sourceMap - Original source map | ||
* @param {string} sourceMapUrl - URL of original source map | ||
* @param {TransformSourceOptions} options - Options for the transform | ||
* @returns {Promise<import('@agoric/babel-generator').GeneratorResult>} Object | ||
* containing new code and source map | ||
*/ | ||
export async function transformSource( | ||
code, | ||
sourceMap, | ||
sourceMapUrl, | ||
{ useLocationUnmap, sourceType } = {}, | ||
) { | ||
await null; | ||
|
||
// Parse the rolled-up chunk with Babel. | ||
// We are prepared for different module systems. | ||
const ast = parseBabel(code, { | ||
sourceType, | ||
}); | ||
|
||
let unmapLoc; | ||
if (useLocationUnmap) { | ||
unmapLoc = await makeLocationUnmapper(sourceMap, ast); | ||
} | ||
|
||
transformAst(ast, unmapLoc); | ||
|
||
// Now generate the sources with the new positions. | ||
return generate(ast, { | ||
sourceFileName: sourceMapUrl, | ||
sourceMaps: true, | ||
retainLines: true, | ||
compact: true, | ||
}); | ||
} | ||
|
||
/** | ||
* @typedef TransformSourceOptions | ||
* @property {boolean} [useLocationUnmap] | ||
* @property {import('@babel/parser').ParserOptions['sourceType']} [sourceType] | ||
*/ |
Oops, something went wrong.