@mongosh/async-rewriter2
Advanced tools
Comparing version 2.0.2 to 2.1.0
declare enum AsyncRewriterErrors { | ||
SyntheticPromiseInAlwaysSyncContext = "ASYNC-10012" | ||
SyntheticPromiseInAlwaysSyncContext = "ASYNC-10012", | ||
SyntheticAsyncIterableInAlwaysSyncContext = "ASYNC-10013" | ||
} | ||
export { AsyncRewriterErrors }; |
@@ -7,3 +7,4 @@ "use strict"; | ||
AsyncRewriterErrors["SyntheticPromiseInAlwaysSyncContext"] = "ASYNC-10012"; | ||
AsyncRewriterErrors["SyntheticAsyncIterableInAlwaysSyncContext"] = "ASYNC-10013"; | ||
})(AsyncRewriterErrors || (exports.AsyncRewriterErrors = AsyncRewriterErrors = {})); | ||
//# sourceMappingURL=error-codes.js.map |
@@ -32,2 +32,3 @@ "use strict"; | ||
const isGeneratedInnerFunction = asNodeKey(Symbol('isGeneratedInnerFunction')); | ||
const isWrappedForOfLoop = asNodeKey(Symbol('isWrappedForOfLoop')); | ||
const isGeneratedHelper = asNodeKey(Symbol('isGeneratedHelper')); | ||
@@ -38,4 +39,5 @@ const isOriginalBody = asNodeKey(Symbol('isOriginalBody')); | ||
const identifierGroupKey = '@@mongosh.identifierGroup'; | ||
const syntheticPromiseSymbolTemplate = babel.template.statement(` | ||
const syntheticPromiseSymbolTemplate = babel.template.statements(` | ||
const SP_IDENTIFIER = Symbol.for("@@mongosh.syntheticPromise"); | ||
const SAI_IDENTIFIER = Symbol.for("@@mongosh.syntheticAsyncIterable"); | ||
`); | ||
@@ -55,3 +57,3 @@ const markSyntheticPromiseTemplate = babel.template.statement(` | ||
const assertNotSyntheticPromiseTemplate = babel.template.statement(` | ||
function ANSP_IDENTIFIER(p, s) { | ||
function ANSP_IDENTIFIER(p, s, i = false) { | ||
if (p && p[SP_IDENTIFIER]) { | ||
@@ -62,5 +64,49 @@ throw new CUSTOM_ERROR_BUILDER( | ||
} | ||
if (i && p && p[SAI_IDENTIFIER]) { | ||
throw new CUSTOM_ERROR_BUILDER( | ||
'Result of expression "' + s + '" cannot be iterated in this context', | ||
'SyntheticAsyncIterableInAlwaysSyncContext'); | ||
} | ||
return p; | ||
} | ||
`); | ||
const adaptAsyncIterableToSyncIterableTemplate = babel.template.statement(` | ||
function AAITSI_IDENTIFIER(original) { | ||
if (!original || !original[SAI_IDENTIFIER]) { | ||
return { iterable: original, isSyntheticAsyncIterable: false }; | ||
} | ||
const originalIterator = original[Symbol.asyncIterator](); | ||
let next; | ||
let returned; | ||
return { | ||
isSyntheticAsyncIterable: true, | ||
iterable: { | ||
[Symbol.iterator]() { | ||
return this; | ||
}, | ||
next() { | ||
let _next = next; | ||
next = undefined; | ||
return _next; | ||
}, | ||
return(value) { | ||
returned = { value }; | ||
return { | ||
value, | ||
done: true | ||
} | ||
}, | ||
async expectNext() { | ||
next ??= await originalIterator.next(); | ||
}, | ||
async syncReturn() { | ||
if (returned) { | ||
await originalIterator.return(returned.value); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
`); | ||
const asyncTryCatchWrapperTemplate = babel.template.expression(` | ||
@@ -102,5 +148,2 @@ async () => { | ||
}); | ||
const assertNotSyntheticExpressionTemplate = babel.template.expression(` | ||
ANSP_IDENTIFIER(NODE, ORIGINAL_SOURCE) | ||
`); | ||
const rethrowTemplate = babel.template.statement(` | ||
@@ -113,2 +156,21 @@ try { | ||
`); | ||
const forOfLoopTemplate = babel.template.statement(`{ | ||
const ITERABLE_INFO = AAITSI_IDENTIFIER(ORIGINAL_ITERABLE); | ||
const ITERABLE_ISAI = (ITERABLE_INFO).isSyntheticAsyncIterable; | ||
const ITERABLE = (ITERABLE_INFO).iterable; | ||
try { | ||
ITERABLE_ISAI && await (ITERABLE).expectNext(); | ||
for (const ITEM of (ORIGINAL_ITERABLE_SOURCE, ITERABLE)) { | ||
ORIGINAL_DECLARATION; | ||
try { | ||
ORIGINAL_BODY; | ||
} finally { | ||
ITERABLE_ISAI && await (ITERABLE).expectNext(); | ||
} | ||
} | ||
} finally { | ||
ITERABLE_ISAI && await (ITERABLE).syncReturn(); | ||
} | ||
}`); | ||
const demangleErrorTemplate = babel.template.statement(String.raw ` | ||
@@ -130,2 +192,11 @@ function DE_IDENTIFIER(err) { | ||
)`); | ||
function getOriginalSourceString({ file }, node, { wrap = true } = {}) { | ||
var _a, _b; | ||
const prettyOriginalString = limitStringLength(node.start !== undefined | ||
? file.code.slice((_a = node.start) !== null && _a !== void 0 ? _a : undefined, (_b = node.end) !== null && _b !== void 0 ? _b : undefined) | ||
: '<unknown>', 25); | ||
if (!wrap) | ||
return t.stringLiteral(prettyOriginalString); | ||
return t.stringLiteral('\ufeff' + prettyOriginalString + '\ufeff'); | ||
} | ||
return { | ||
@@ -137,3 +208,3 @@ pre(file) { | ||
BlockStatement(path) { | ||
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k; | ||
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m; | ||
if (!path.parentPath.isFunction()) | ||
@@ -160,5 +231,7 @@ return; | ||
const isSyntheticPromise = (_f = existingIdentifiers === null || existingIdentifiers === void 0 ? void 0 : existingIdentifiers.isSyntheticPromise) !== null && _f !== void 0 ? _f : path.scope.generateUidIdentifier('isp'); | ||
const assertNotSyntheticPromise = (_g = existingIdentifiers === null || existingIdentifiers === void 0 ? void 0 : existingIdentifiers.assertNotSyntheticPromise) !== null && _g !== void 0 ? _g : path.scope.generateUidIdentifier('ansp'); | ||
const syntheticPromiseSymbol = (_h = existingIdentifiers === null || existingIdentifiers === void 0 ? void 0 : existingIdentifiers.syntheticPromiseSymbol) !== null && _h !== void 0 ? _h : path.scope.generateUidIdentifier('sp'); | ||
const demangleError = (_j = existingIdentifiers === null || existingIdentifiers === void 0 ? void 0 : existingIdentifiers.demangleError) !== null && _j !== void 0 ? _j : path.scope.generateUidIdentifier('de'); | ||
const adaptAsyncIterableToSyncIterable = (_g = existingIdentifiers === null || existingIdentifiers === void 0 ? void 0 : existingIdentifiers.adaptAsyncIterableToSyncIterable) !== null && _g !== void 0 ? _g : path.scope.generateUidIdentifier('aaitsi'); | ||
const assertNotSyntheticPromise = (_h = existingIdentifiers === null || existingIdentifiers === void 0 ? void 0 : existingIdentifiers.assertNotSyntheticPromise) !== null && _h !== void 0 ? _h : path.scope.generateUidIdentifier('ansp'); | ||
const syntheticPromiseSymbol = (_j = existingIdentifiers === null || existingIdentifiers === void 0 ? void 0 : existingIdentifiers.syntheticPromiseSymbol) !== null && _j !== void 0 ? _j : path.scope.generateUidIdentifier('sp'); | ||
const syntheticAsyncIterableSymbol = (_k = existingIdentifiers === null || existingIdentifiers === void 0 ? void 0 : existingIdentifiers.syntheticAsyncIterableSymbol) !== null && _k !== void 0 ? _k : path.scope.generateUidIdentifier('sai'); | ||
const demangleError = (_l = existingIdentifiers === null || existingIdentifiers === void 0 ? void 0 : existingIdentifiers.demangleError) !== null && _l !== void 0 ? _l : path.scope.generateUidIdentifier('de'); | ||
const identifiersGroup = { | ||
@@ -171,4 +244,6 @@ functionState, | ||
isSyntheticPromise, | ||
adaptAsyncIterableToSyncIterable, | ||
assertNotSyntheticPromise, | ||
syntheticPromiseSymbol, | ||
syntheticAsyncIterableSymbol, | ||
demangleError, | ||
@@ -180,5 +255,6 @@ }; | ||
: [ | ||
Object.assign(syntheticPromiseSymbolTemplate({ | ||
...syntheticPromiseSymbolTemplate({ | ||
SP_IDENTIFIER: syntheticPromiseSymbol, | ||
}), { [isGeneratedHelper]: true }), | ||
SAI_IDENTIFIER: syntheticAsyncIterableSymbol, | ||
}).map((helper) => Object.assign(helper, { [isGeneratedHelper]: true })), | ||
Object.assign(expressionHolderVariableTemplate({ | ||
@@ -196,2 +272,6 @@ EXPRESSION_HOLDER_IDENTIFIER: expressionHolder, | ||
}), { [isGeneratedHelper]: true }), | ||
Object.assign(adaptAsyncIterableToSyncIterableTemplate({ | ||
AAITSI_IDENTIFIER: adaptAsyncIterableToSyncIterable, | ||
SAI_IDENTIFIER: syntheticAsyncIterableSymbol, | ||
}), { [isGeneratedHelper]: true }), | ||
Object.assign(isSyntheticPromiseTemplate({ | ||
@@ -210,3 +290,4 @@ ISP_IDENTIFIER: isSyntheticPromise, | ||
SP_IDENTIFIER: syntheticPromiseSymbol, | ||
CUSTOM_ERROR_BUILDER: (_k = this.opts.customErrorBuilder) !== null && _k !== void 0 ? _k : t.identifier('Error'), | ||
SAI_IDENTIFIER: syntheticAsyncIterableSymbol, | ||
CUSTOM_ERROR_BUILDER: (_m = this.opts.customErrorBuilder) !== null && _m !== void 0 ? _m : t.identifier('Error'), | ||
}), { [isGeneratedHelper]: true }), | ||
@@ -281,3 +362,3 @@ ]; | ||
exit(path) { | ||
var _a, _b, _c, _d, _e, _f; | ||
var _a, _b, _c, _d; | ||
const functionParent = path.getFunctionParent(); | ||
@@ -350,16 +431,21 @@ if (!functionParent) | ||
const { expressionHolder, isSyntheticPromise, assertNotSyntheticPromise, } = identifierGroup; | ||
const prettyOriginalString = limitStringLength(path.node.start !== undefined | ||
? this.file.code.slice((_e = path.node.start) !== null && _e !== void 0 ? _e : undefined, (_f = path.node.end) !== null && _f !== void 0 ? _f : undefined) | ||
: '<unknown>', 24); | ||
if (!functionParent.node.async) { | ||
path.replaceWith(Object.assign(assertNotSyntheticExpressionTemplate({ | ||
ORIGINAL_SOURCE: t.stringLiteral(prettyOriginalString), | ||
NODE: path.node, | ||
ANSP_IDENTIFIER: assertNotSyntheticPromise, | ||
}), { [isGeneratedHelper]: true })); | ||
const args = [ | ||
path.node, | ||
getOriginalSourceString(this, path.node, { | ||
wrap: false, | ||
}), | ||
]; | ||
if ((path.parent.type === 'ForOfStatement' && | ||
path.node === path.parent.right) || | ||
(path.parent.type === 'YieldExpression' && path.parent.delegate)) { | ||
args.push(t.booleanLiteral(true)); | ||
} | ||
path.replaceWith(Object.assign(t.callExpression(assertNotSyntheticPromise, args), { | ||
[isGeneratedHelper]: true, | ||
})); | ||
return; | ||
} | ||
const originalSource = t.stringLiteral('\ufeff' + prettyOriginalString + '\ufeff'); | ||
path.replaceWith(Object.assign(awaitSyntheticPromiseTemplate({ | ||
ORIGINAL_SOURCE: originalSource, | ||
ORIGINAL_SOURCE: getOriginalSourceString(this, path.node), | ||
EXPRESSION_HOLDER: expressionHolder, | ||
@@ -391,2 +477,35 @@ ISP_IDENTIFIER: isSyntheticPromise, | ||
}, | ||
ForOfStatement(path) { | ||
var _a, _b, _c, _d, _e, _f; | ||
if (path.node.await || !((_a = path.getFunctionParent()) === null || _a === void 0 ? void 0 : _a.node.async)) | ||
return; | ||
if ((_c = (_b = path.find((path) => path.isFunction() || !!path.node[isGeneratedHelper])) === null || _b === void 0 ? void 0 : _b.node) === null || _c === void 0 ? void 0 : _c[isGeneratedHelper]) { | ||
return path.skip(); | ||
} | ||
if ((_e = (_d = path.find((path) => path.isFunction() || !!path.node[isWrappedForOfLoop])) === null || _d === void 0 ? void 0 : _d.node) === null || _e === void 0 ? void 0 : _e[isWrappedForOfLoop]) { | ||
return; | ||
} | ||
const identifierGroup = (_f = path | ||
.findParent((path) => !!path.getData(identifierGroupKey))) === null || _f === void 0 ? void 0 : _f.getData(identifierGroupKey); | ||
if (!identifierGroup) | ||
throw new Error('Missing identifier group for ForOfStatement'); | ||
const { adaptAsyncIterableToSyncIterable } = identifierGroup; | ||
const item = path.scope.generateUidIdentifier('i'); | ||
path.replaceWith(Object.assign(forOfLoopTemplate({ | ||
ORIGINAL_ITERABLE: path.node.right, | ||
ORIGINAL_ITERABLE_SOURCE: getOriginalSourceString(this, path.node.right), | ||
ORIGINAL_DECLARATION: path.node.left.type === 'VariableDeclaration' | ||
? t.variableDeclaration(path.node.left.kind, path.node.left.declarations.map((d) => ({ | ||
...d, | ||
init: item, | ||
}))) | ||
: t.expressionStatement(t.assignmentExpression('=', path.node.left, item)), | ||
ORIGINAL_BODY: path.node.body, | ||
ITERABLE_INFO: path.scope.generateUidIdentifier('ii'), | ||
ITERABLE_ISAI: path.scope.generateUidIdentifier('isai'), | ||
ITERABLE: path.scope.generateUidIdentifier('it'), | ||
ITEM: item, | ||
AAITSI_IDENTIFIER: adaptAsyncIterableToSyncIterable, | ||
}), { [isWrappedForOfLoop]: true })); | ||
}, | ||
}, | ||
@@ -393,0 +512,0 @@ }; |
{ | ||
"name": "@mongosh/async-rewriter2", | ||
"version": "2.0.2", | ||
"version": "2.1.0", | ||
"description": "MongoDB Shell Async Rewriter Package", | ||
@@ -58,3 +58,3 @@ "main": "./lib/index.js", | ||
}, | ||
"gitHead": "5737e60cdbcd551f6a355b38fc9b5b6cc19ac5a4" | ||
"gitHead": "963c75bc45834b1bf69e62bf5b72c4ae8ef26cb4" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
310614
2017