You're Invited:Meet the Socket Team at RSAC and BSidesSF 2026, March 23–26.RSVP
Socket
Book a DemoSign in
Socket

@endo/evasive-transform

Package Overview
Dependencies
Maintainers
5
Versions
21
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@endo/evasive-transform - npm Package Compare versions

Comparing version
1.3.4
to
1.4.0
+8
-12
package.json
{
"name": "@endo/evasive-transform",
"version": "1.3.4",
"version": "1.4.0",
"description": "Source transforms to evade SES censorship",

@@ -42,12 +42,9 @@ "keywords": [

"devDependencies": {
"@babel/types": "^7.24.0",
"@endo/ses-ava": "^1.2.9",
"@rollup/plugin-commonjs": "^19.0.0",
"@rollup/plugin-node-resolve": "^13.0.0",
"@babel/types": "~7.26.0",
"@endo/ses-ava": "^1.2.10",
"@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",
"rollup": "^2.79.1",
"tsd": "^0.31.2",

@@ -82,8 +79,7 @@ "typescript": "~5.6.3"

"dependencies": {
"@agoric/babel-generator": "^7.17.6",
"@babel/parser": "^7.23.6",
"@babel/traverse": "^7.23.6",
"source-map-js": "^1.2.0"
"@babel/generator": "^7.26.3",
"@babel/parser": "~7.26.2",
"@babel/traverse": "~7.25.9"
},
"gitHead": "e0683e0bfdbfc84351af332c9e78813d7b67ef89"
"gitHead": "9b6784831d37db948cdd61f6da1f3489e8f97906"
}
+13
-4

@@ -5,3 +5,5 @@ /**

* @typedef GenerateAstOptionsWithSourceMap
* @property {string} [source]
* @property {string} sourceUrl - If present, we will generate a source map
* @property {string} [sourceMap] - If present, the generated source map will be a transform over the given source map.
* @internal

@@ -13,3 +15,4 @@ */

* @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

@@ -22,3 +25,3 @@ */

* @template {string|undefined} [SourceUrl=undefined]
* @typedef {{code: string, map: SourceUrl extends string ? import('source-map-js').RawSourceMap : never}} TransformedResult
* @typedef {{code: string, map: SourceUrl extends string ? string : never}} TransformedResult
* @internal

@@ -49,2 +52,3 @@ */

export type GenerateAstOptionsWithSourceMap = {
source?: string | undefined;
/**

@@ -54,2 +58,6 @@ * - If present, we will generate a source map

sourceUrl: string;
/**
* - If present, the generated source map will be a transform over the given source map.
*/
sourceMap?: string | undefined;
};

@@ -60,6 +68,7 @@ /**

export type GenerateAstOptionsWithoutSourceMap = {
source?: string | undefined;
/**
* - This should be undefined or otherwise not provided
*/
sourceUrl: undefined;
sourceUrl?: undefined;
};

@@ -72,3 +81,3 @@ /**

code: string;
map: SourceUrl extends string ? import("source-map-js").RawSourceMap : never;
map: SourceUrl extends string ? string : never;
};

@@ -75,0 +84,0 @@ /**

@@ -1,1 +0,1 @@

{"version":3,"file":"generate.d.ts","sourceRoot":"","sources":["generate.js"],"names":[],"mappings":"AAmBA;;;;;;GAMG;AAEH;;;;;;GAMG;AAEH;;;;;;;GAOG;AAEH;;;;;;;;GAQG;AAEH;;;;;;;;GAQG;AACH,uBACa,wBAAwB,GAAG,2BAA2B,CAsB/D;;;;;;;;eA/DU,MAAM;;;;;;;;;eAQN,SAAS;;;;;;8BAQU,SAAS,SAA5B,MAAM,GAAC,SAAU,gBAClB;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,GAAG,EAAE,SAAS,SAAS,MAAM,GAAG,OAAO,eAAe,EAAE,YAAY,GAAG,KAAK,CAAA;CAAC;;;;6CAQ9F,OAAO,cAAc,EAAE,IAAI,WAC3B,+BAA+B,KAC7B,iBAAiB,CAAC,MAAM,CAAC;;;;gDAQ3B,OAAO,cAAc,EAAE,IAAI,+DAEzB,iBAAiB,CAAC,SAAS,CAAC"}
{"version":3,"file":"generate.d.ts","sourceRoot":"","sources":["generate.js"],"names":[],"mappings":"AAmBA;;;;;;;;GAQG;AAEH;;;;;;;GAOG;AAEH;;;;;;;GAOG;AAEH;;;;;;;;GAQG;AAEH;;;;;;;;GAQG;AACH,uBACa,wBAAwB,GAAG,2BAA2B,CAiC/D;;;;;;;;;eA5EU,MAAM;;;;;;;;;;;;;;gBAUN,SAAS;;;;;;8BAQU,SAAS,SAA5B,MAAM,GAAC,SAAU,gBAClB;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,GAAG,EAAE,SAAS,SAAS,MAAM,GAAG,MAAM,GAAG,KAAK,CAAA;CAAC;;;;6CAQhE,OAAO,cAAc,EAAE,IAAI,WAC3B,+BAA+B,KAC7B,iBAAiB,CAAC,MAAM,CAAC;;;;gDAQ3B,OAAO,cAAc,EAAE,IAAI,+DAEzB,iBAAiB,CAAC,SAAS,CAAC"}

@@ -8,3 +8,3 @@ /**

// @ts-ignore XXX no types defined
import babelGenerator from '@agoric/babel-generator';
import babelGenerator from '@babel/generator';

@@ -25,3 +25,5 @@ // TODO The following is sufficient on Node.js, but for compatibility with

* @typedef GenerateAstOptionsWithSourceMap
* @property {string} [source]
* @property {string} sourceUrl - If present, we will generate a source map
* @property {string} [sourceMap] - If present, the generated source map will be a transform over the given source map.
* @internal

@@ -34,3 +36,4 @@ */

* @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

@@ -44,3 +47,3 @@ */

* @template {string|undefined} [SourceUrl=undefined]
* @typedef {{code: string, map: SourceUrl extends string ? import('source-map-js').RawSourceMap : never}} TransformedResult
* @typedef {{code: string, map: SourceUrl extends string ? string : never}} TransformedResult
* @internal

@@ -74,8 +77,19 @@ */

const sourceUrl = options ? options.sourceUrl : undefined;
const result = generator(ast, {
sourceFileName: sourceUrl,
sourceMaps: Boolean(sourceUrl),
retainLines: true,
compact: true,
});
const inputSourceMap =
options && 'sourceMap' in options ? options.sourceMap : undefined;
const source = options ? options.source : undefined;
const result = generator(
ast,
{
sourceFileName: sourceUrl,
sourceMaps: Boolean(sourceUrl),
// @ts-expect-error Property missing on versioned types
inputSourceMap,
retainLines: true,
...(source === undefined
? {}
: { experimental_preserveFormat: true }),
},
source,
);

@@ -82,0 +96,0 @@ if (sourceUrl) {

@@ -5,7 +5,7 @@ /**

* @typedef EvadeCensorOptions
* @property {string|import('source-map-js').RawSourceMap} [sourceMap] - Original source map in JSON string or object form
* @property {string} [sourceMap] - Original source map in JSON string or object form
* @property {string} [sourceUrl] - URL or filepath of the original source in `code`
* @property {boolean} [useLocationUnmap] - Enable location unmapping. Only applies if `sourceMap` was provided
* @property {boolean} [elideComments] - Replace comments with an ellipsis but preserve interior newlines.
* @property {import('./parse-ast.js').SourceType} [sourceType] - Module source type
* @property {boolean} [useLocationUnmap] - deprecated, vestigial
* @public

@@ -19,5 +19,2 @@ */

*
* If the `sourceMap` option is _not_ provided, the `useLocationUnmap` option
* will have no effect.
*
* @template {EvadeCensorOptions} T

@@ -36,5 +33,2 @@ * @param {string} source - Source code to transform

*
* If the `sourceMap` option is _not_ provided, the `useLocationUnmap` option
* will have no effect.
*
* @template {EvadeCensorOptions} T

@@ -54,3 +48,3 @@ * @param {string} source - Source code to transform

*/
sourceMap?: string | import("source-map-js").RawSourceMap | undefined;
sourceMap?: string | undefined;
/**

@@ -61,6 +55,2 @@ * - URL or filepath of the original source in `code`

/**
* - Enable location unmapping. Only applies if `sourceMap` was provided
*/
useLocationUnmap?: boolean | undefined;
/**
* - Replace comments with an ellipsis but preserve interior newlines.

@@ -73,4 +63,8 @@ */

sourceType?: import("./parse-ast.js").SourceType | undefined;
/**
* - deprecated, vestigial
*/
useLocationUnmap?: boolean | undefined;
};
import type { TransformedResult } from './generate.js';
//# sourceMappingURL=index.d.ts.map

@@ -1,1 +0,1 @@

{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.js"],"names":[],"mappings":"AAcA;;;;;;;;;;GAUG;AAEH;;;;;;;;;;;;;;GAcG;AACH,gCANkC,CAAC,SAArB,kBAAmB,UACtB,MAAM,4BAEJ,kBAAkB,CAAC,CAAC,WAAW,CAAC,CAAC,CAqC7C;AAED;;;;;;;;;;;;;;GAcG;AACH,4BANkC,CAAC,SAArB,kBAAmB,UACtB,MAAM,4BAEJ,OAAO,CAAC,kBAAkB,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,CAKtD;;;;;;;;;;;;;;;;;;;;;;;;;;uCAvFmC,eAAe"}
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.js"],"names":[],"mappings":"AAcA;;;;;;;;;;GAUG;AAEH;;;;;;;;;;;GAWG;AACH,gCANkC,CAAC,SAArB,kBAAmB,UACtB,MAAM,4BAEJ,kBAAkB,CAAC,CAAC,WAAW,CAAC,CAAC,CAuB7C;AAED;;;;;;;;;;;GAWG;AACH,4BANkC,CAAC,SAArB,kBAAmB,UACtB,MAAM,4BAEJ,OAAO,CAAC,kBAAkB,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,CAKtD;;;;;;;;;;;;;;;;;;;;;;;;;;uCAnEmC,eAAe"}

@@ -19,7 +19,7 @@ /**

* @typedef EvadeCensorOptions
* @property {string|import('source-map-js').RawSourceMap} [sourceMap] - Original source map in JSON string or object form
* @property {string} [sourceMap] - Original source map in JSON string or object form
* @property {string} [sourceUrl] - URL or filepath of the original source in `code`
* @property {boolean} [useLocationUnmap] - Enable location unmapping. Only applies if `sourceMap` was provided
* @property {boolean} [elideComments] - Replace comments with an ellipsis but preserve interior newlines.
* @property {import('./parse-ast.js').SourceType} [sourceType] - Module source type
* @property {boolean} [useLocationUnmap] - deprecated, vestigial
* @public

@@ -34,5 +34,2 @@ */

*
* If the `sourceMap` option is _not_ provided, the `useLocationUnmap` option
* will have no effect.
*
* @template {EvadeCensorOptions} T

@@ -45,8 +42,5 @@ * @param {string} source - Source code to transform

export function evadeCensorSync(source, options) {
// TODO Use options ?? {} when resolved:
// https://github.com/Agoric/agoric-sdk/issues/8671
const {
sourceMap,
sourceUrl,
useLocationUnmap,
sourceType,

@@ -62,19 +56,8 @@ elideComments = false,

const sourceMapJson =
typeof sourceMap === 'string' ? sourceMap : JSON.stringify(sourceMap);
transformAst(ast, { elideComments });
if (sourceMap && useLocationUnmap) {
transformAst(ast, {
sourceMap: sourceMapJson,
useLocationUnmap,
elideComments,
});
} else {
transformAst(ast, { elideComments });
}
if (sourceUrl) {
return generate(ast, { sourceUrl });
return generate(ast, { source, sourceUrl, sourceMap });
}
return generate(ast);
return generate(ast, { source });
}

@@ -88,5 +71,2 @@

*
* If the `sourceMap` option is _not_ provided, the `useLocationUnmap` option
* will have no effect.
*
* @template {EvadeCensorOptions} T

@@ -93,0 +73,0 @@ * @param {string} source - Source code to transform

@@ -1,1 +0,1 @@

{"version":3,"file":"parse-ast.d.ts","sourceRoot":"","sources":["parse-ast.js"],"names":[],"mappings":"AAUA;;;;;;;;GAQG;AAEH;;;;;;GAMG;AAEH;;;;;;GAMG;AACH,iCAJW,MAAM,4FAOhB;;;;;;;yBAtBY,QAAQ,GAAC,QAAQ,GAAC,aAAa;;;;;;;6BAVf,eAAe"}
{"version":3,"file":"parse-ast.d.ts","sourceRoot":"","sources":["parse-ast.js"],"names":[],"mappings":"AAUA;;;;;;;;GAQG;AAEH;;;;;;GAMG;AAEH;;;;;;GAMG;AACH,iCAJW,MAAM,4FAWhB;;;;;;;yBA1BY,QAAQ,GAAC,QAAQ,GAAC,aAAa;;;;;;;6BAVf,eAAe"}

@@ -36,5 +36,9 @@ /**

*/
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,
});
}

@@ -5,3 +5,3 @@ /**

* @internal
* @typedef {TransformAstOptionsWithLocationUnmap | TransformAstOptionsWithoutSourceMap} TransformAstOptions
* @typedef {TransformAstOptionsWithoutSourceMap} TransformAstOptions
*/

@@ -11,22 +11,7 @@ /**

*
* If `useLocationUnmap` is not `true`, `sourceMap` is unused.
*
* @internal
* @typedef TransformAstOptionsWithoutSourceMap
* @property {false} [useLocationUnmap] - Enable location unmapping
* @property {string} [sourceMap] - Original source map
* @property {boolean} [elideComments]
*/
/**
* Options for {@link transformAst}
*
* If `useLocationUnmap` is `true`, then `sourceMap` must also be present.
*
* @internal
* @typedef TransformAstOptionsWithLocationUnmap
* @property {true} useLocationUnmap - Enable location unmapping
* @property {string} sourceMap - Original source map
* @property {boolean} [elideComments]
*/
/**
* Performs transformations on the given AST

@@ -41,39 +26,13 @@ *

*/
export function transformAst(ast: import("@babel/types").File, { sourceMap, useLocationUnmap, elideComments }?: TransformAstOptions | undefined): void;
export function transformAst(ast: import("@babel/types").File, { elideComments }?: TransformAstOptionsWithoutSourceMap | undefined): void;
/**
* Options for {@link transformAst}
*/
export type TransformAstOptions = TransformAstOptionsWithLocationUnmap | TransformAstOptionsWithoutSourceMap;
export type TransformAstOptions = TransformAstOptionsWithoutSourceMap;
/**
* Options for {@link transformAst}
*
* If `useLocationUnmap` is not `true`, `sourceMap` is unused.
*/
export type TransformAstOptionsWithoutSourceMap = {
/**
* - Enable location unmapping
*/
useLocationUnmap?: false | undefined;
/**
* - Original source map
*/
sourceMap?: string | undefined;
elideComments?: boolean | undefined;
};
/**
* Options for {@link transformAst}
*
* If `useLocationUnmap` is `true`, then `sourceMap` must also be present.
*/
export type TransformAstOptionsWithLocationUnmap = {
/**
* - Enable location unmapping
*/
useLocationUnmap: true;
/**
* - Original source map
*/
sourceMap: string;
elideComments?: boolean | undefined;
};
//# sourceMappingURL=transform-ast.d.ts.map

@@ -1,1 +0,1 @@

{"version":3,"file":"transform-ast.d.ts","sourceRoot":"","sources":["transform-ast.js"],"names":[],"mappings":"AAmBA;;;;;GAKG;AAEH;;;;;;;;;;GAUG;AAEH;;;;;;;;;;GAUG;AAEH;;;;;;;;;GASG;AACH,kCAJW,OAAO,cAAc,EAAE,IAAI,qFAEzB,IAAI,CAyChB;;;;kCA5EY,oCAAoC,GAAG,mCAAmC;;;;;;;;;;;;;;;;;;;;;;;;;;sBAsBzE,IAAI;;;;eACJ,MAAM"}
{"version":3,"file":"transform-ast.d.ts","sourceRoot":"","sources":["transform-ast.js"],"names":[],"mappings":"AAkBA;;;;;GAKG;AAEH;;;;;;GAMG;AAEH;;;;;;;;;GASG;AACH,kCAJW,OAAO,cAAc,EAAE,IAAI,wEAEzB,IAAI,CAuBhB;;;;kCA1CY,mCAAmC"}

@@ -9,3 +9,2 @@ /**

import { evadeComment, elideComment } from './transform-comment.js';
import { makeLocationUnmapper } from './location-unmapper.js';

@@ -25,3 +24,3 @@ // TODO The following is sufficient on Node.js, but for compatibility with

* @internal
* @typedef {TransformAstOptionsWithLocationUnmap | TransformAstOptionsWithoutSourceMap} TransformAstOptions
* @typedef {TransformAstOptionsWithoutSourceMap} TransformAstOptions
*/

@@ -32,8 +31,4 @@

*
* If `useLocationUnmap` is not `true`, `sourceMap` is unused.
*
* @internal
* @typedef TransformAstOptionsWithoutSourceMap
* @property {false} [useLocationUnmap] - Enable location unmapping
* @property {string} [sourceMap] - Original source map
* @property {boolean} [elideComments]

@@ -43,14 +38,2 @@ */

/**
* Options for {@link transformAst}
*
* If `useLocationUnmap` is `true`, then `sourceMap` must also be present.
*
* @internal
* @typedef TransformAstOptionsWithLocationUnmap
* @property {true} useLocationUnmap - Enable location unmapping
* @property {string} sourceMap - Original source map
* @property {boolean} [elideComments]
*/
/**
* Performs transformations on the given AST

@@ -65,24 +48,13 @@ *

*/
export function transformAst(
ast,
{ sourceMap, useLocationUnmap, elideComments = false } = {},
) {
/** @type {import('./location-unmapper.js').LocationUnmapper|undefined} */
let unmapLoc;
if (sourceMap && useLocationUnmap) {
unmapLoc = makeLocationUnmapper(sourceMap, ast);
}
export function transformAst(ast, { elideComments = false } = {}) {
const transformComment = elideComments ? elideComment : evadeComment;
traverse(ast, {
enter(p) {
const { loc, leadingComments, innerComments, trailingComments, type } =
p.node;
const { leadingComments, innerComments, trailingComments, type } = p.node;
// discriminated union
if ('comments' in p.node) {
(p.node.comments || []).forEach(node =>
transformComment(node, unmapLoc),
);
(p.node.comments || []).forEach(node => transformComment(node));
}
// Rewrite all comments.
(leadingComments || []).forEach(node => transformComment(node, unmapLoc));
(leadingComments || []).forEach(node => transformComment(node));
// XXX: there is no such Node having type matching /^Comment.+/ in

@@ -92,15 +64,8 @@ // @babel/types

// @ts-expect-error - see above XXX
transformComment(p.node, unmapLoc);
transformComment(p.node);
}
(innerComments || []).forEach(node => transformComment(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 =>
transformComment(node, unmapLoc),
);
(innerComments || []).forEach(node => transformComment(node));
(trailingComments || []).forEach(node => transformComment(node));
},
});
}

@@ -7,6 +7,5 @@ /**

* @param {import('@babel/types').Comment} node
* @param {import('./location-unmapper.js').LocationUnmapper} [unmapLoc]
*/
export function evadeComment(node: import("@babel/types").Comment, unmapLoc?: import("./location-unmapper.js").LocationUnmapper | undefined): void;
export function elideComment(node: import("@babel/types").Comment, unmapLoc?: import("./location-unmapper.js").LocationUnmapper | undefined): void;
export function evadeComment(node: import("@babel/types").Comment): void;
export function elideComment(node: import("@babel/types").Comment): void;
//# sourceMappingURL=transform-comment.d.ts.map

@@ -1,1 +0,1 @@

{"version":3,"file":"transform-comment.d.ts","sourceRoot":"","sources":["transform-comment.js"],"names":[],"mappings":"AAsBA;;;;;;;GAOG;AACH,mCAHW,OAAO,cAAc,EAAE,OAAO,kFAoBxC;AA8BM,mCAHI,OAAO,cAAc,EAAE,OAAO,kFAcxC"}
{"version":3,"file":"transform-comment.d.ts","sourceRoot":"","sources":["transform-comment.js"],"names":[],"mappings":"AAsBA;;;;;;GAMG;AACH,mCAFW,OAAO,cAAc,EAAE,OAAO,QAgBxC;AA6BM,mCAFI,OAAO,cAAc,EAAE,OAAO,QAUxC"}

@@ -29,5 +29,4 @@ /**

* @param {import('@babel/types').Comment} node
* @param {import('./location-unmapper.js').LocationUnmapper} [unmapLoc]
*/
export function evadeComment(node, unmapLoc) {
export function evadeComment(node) {
node.type = 'CommentBlock';

@@ -46,5 +45,2 @@ // Within comments...

.replace(/\*\//g, '*X/');
if (unmapLoc) {
unmapLoc(node.loc);
}
}

@@ -78,5 +74,4 @@

* @param {import('@babel/types').Comment} node
* @param {import('./location-unmapper.js').LocationUnmapper} [unmapLoc]
*/
export const elideComment = (node, unmapLoc) => {
export const elideComment = node => {
if (node.type === 'CommentBlock') {

@@ -89,5 +84,2 @@ if (!markedForPreservation(node.value)) {

}
if (unmapLoc) {
unmapLoc(node.loc);
}
};
/**
* A function which modifies an AST Node's source location
*
* @callback LocationUnmapper
* @param {import('@babel/types').SourceLocation|null} [loc]
* @returns {void}
* @internal
*/
/**
* Creates a {@link LocationUnmapper} function
*
* @internal
* @param {string} sourceMap - Source map
* @param {import('@babel/types').File} ast - AST as created by Babel
* @returns {LocationUnmapper}
*/
export function makeLocationUnmapper(sourceMap: string, ast: import("@babel/types").File): LocationUnmapper;
/**
* A function which modifies an AST Node's source location
*/
export type LocationUnmapper = (loc?: import("@babel/types").SourceLocation | null | undefined) => void;
//# sourceMappingURL=location-unmapper.d.ts.map
{"version":3,"file":"location-unmapper.d.ts","sourceRoot":"","sources":["location-unmapper.js"],"names":[],"mappings":"AAQA;;;;;;;GAOG;AAEH;;;;;;;GAOG;AACH,gDAJW,MAAM,OACN,OAAO,cAAc,EAAE,IAAI,GACzB,gBAAgB,CAwD5B;;;;mGAlEY,IAAI"}
/**
* Provides {@link makeLocationUnmapper}
*
* @module
*/
import { SourceMapConsumer } from 'source-map-js';
/**
* A function which modifies an AST Node's source location
*
* @callback LocationUnmapper
* @param {import('@babel/types').SourceLocation|null} [loc]
* @returns {void}
* @internal
*/
/**
* Creates a {@link LocationUnmapper} function
*
* @internal
* @param {string} sourceMap - Source map
* @param {import('@babel/types').File} ast - AST as created by Babel
* @returns {LocationUnmapper}
*/
export function makeLocationUnmapper(sourceMap, ast) {
if (!sourceMap) {
throw new TypeError('Invalid arguments; expected sourceMap');
}
if (!ast || typeof ast !== 'object') {
throw new TypeError('Invalid arguments; expected AST ast');
}
if (!ast.loc) {
throw new TypeError('No SourceLocation found in AST');
}
try {
// We rearrange the rolled-up chunk according to its sourcemap to move
// its source lines back to the right place.
const consumer = new SourceMapConsumer(JSON.parse(sourceMap));
const unmapped = new WeakSet();
/**
* Change this type to `import('@babel/types').Position` if we assign the
* `index` prop below
* @type {any}
*/
let lastPos = {
...ast.loc.start,
};
return loc => {
if (!loc || unmapped.has(loc)) {
return;
}
// Make sure things start at least at the right place.
loc.end = { ...loc.start };
for (const pos of /** @type {const} */ (['start', 'end'])) {
if (loc[pos]) {
const newPos = consumer.originalPositionFor(loc[pos]);
if (newPos.source !== null) {
// This assumes that if source is non-null, then line and column are
// also non-null
lastPos = {
line: /** @type {number} */ (newPos.line),
column: /** @type {number} */ (newPos.column),
// XXX: what of the `index` prop?
};
}
loc[pos] = lastPos;
}
}
unmapped.add(loc);
};
} catch (err) {
// A source map string should be valid JSON, and if `JSON.parse()` fails, a
// SyntaxError is thrown
if (err instanceof SyntaxError) {
throw new TypeError(`Invalid source map: ${err}`);
}
throw err;
}
}