js-slang
Version:
Javascript-based implementations of Source, written in Typescript
66 lines • 2.4 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.sanitizeAST = void 0;
const walkers_1 = require("../walkers");
const locationKeys = ['loc', 'start', 'end'];
// Certain properties on each type of node are only present sometimes
// For our purposes, those properties aren't important, so we can
// remove them from the corresponding node
const propertiesToDelete = {
CallExpression: ['optional'],
// Honestly not sure where the 'expression' property comes from
FunctionDeclaration: ['expression', 'generator'],
Literal: ['raw']
};
const sanitizers = Object.entries(propertiesToDelete).reduce((res, [nodeType, props]) => ({
...res,
[nodeType](node) {
for (const prop of props) {
delete node[prop];
}
}
}), {});
/**
* Strips out extra properties from an AST and converts Nodes to regular
* javascript objects
*
* The local imports test suites only care about the structure of the
* transformed AST. The line & column numbers, as well as the character
* offsets of each node in the ASTs derived from parsing the pre-transform
* code & the equivalent post-transform code will not be the same.
* Note that it is insufficient to pass in 'locations: false' into the acorn
* parser as there will still be 'start' & 'end' properties attached to nodes
* which represent character offsets.
*
* @param node The AST which should be stripped of extra properties
*/
function sanitizeAST(node) {
const convertNode = (obj) => {
return Object.entries(obj).reduce((res, [key, value]) => {
// Filter out location related properties and don't
// return them with the created object
if (locationKeys.includes(key))
return res;
if (Array.isArray(value)) {
return {
...res,
[key]: value.map(convertNode)
};
}
if (typeof value === 'object' && value !== null) {
return {
...res,
[key]: convertNode(value)
};
}
return {
...res,
[key]: value
};
}, {});
};
(0, walkers_1.simple)(node, sanitizers);
return convertNode(node);
}
exports.sanitizeAST = sanitizeAST;
//# sourceMappingURL=sanitizer.js.map
;