mapExpression.js (1452B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */ 4 5 import { parseConsoleScript } from "./utils/ast"; 6 import mapOriginalExpression from "./mapOriginalExpression"; 7 import mapExpressionBindings from "./mapBindings"; 8 import mapTopLevelAwait from "./mapAwaitExpression"; 9 10 export default function mapExpression( 11 expression, 12 mappings, 13 bindings, 14 shouldMapBindings = true, 15 shouldMapAwait = true 16 ) { 17 const mapped = { 18 await: false, 19 bindings: false, 20 originalExpression: false, 21 }; 22 23 const ast = parseConsoleScript(expression); 24 try { 25 if (mappings && ast) { 26 const beforeOriginalExpression = expression; 27 expression = mapOriginalExpression(expression, ast, mappings); 28 mapped.originalExpression = beforeOriginalExpression !== expression; 29 } 30 31 if (shouldMapBindings && ast) { 32 const beforeBindings = expression; 33 expression = mapExpressionBindings(expression, ast, bindings); 34 mapped.bindings = beforeBindings !== expression; 35 } 36 37 if (shouldMapAwait) { 38 const beforeAwait = expression; 39 expression = mapTopLevelAwait(expression, ast); 40 mapped.await = beforeAwait !== expression; 41 } 42 } catch (e) { 43 console.warn(`Error when mapping ${expression} expression:`, e); 44 } 45 46 return { 47 expression, 48 mapped, 49 }; 50 }