Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
static getNode (code) {
let parsed;
if (code.replace(/\n/g,'').match(/^{.*}$/m)) {
code = `(${code})`;
}
try {
parsed = jsParser.parse(code);
} catch (e) {
throw new Error(`ERROR Util.getNode JS code is invalid, "${code}"`);
}
// const parsed = jsParser.parse(code);
const firstNode = parsed.body[0];
const node = firstNode.type === 'BlockStatement' ? firstNode.body[0] :
firstNode.type === 'ExpressionStatement' ? firstNode.expression : null;
return node;
}
function parseCode(code) {
let ast
try {
// Parse node code
ast = Parser.parse(code)
} catch (err) {
// on fail, try module
ast = Parser.parse(code, {
sourceType: 'module'
})
}
// console.log(ast)
const foundExports = getExports(ast.body)
// If parsing a window iife fallback and regex file
if (!foundExports.length) {
let m = getWindowExports(code)
return {
methodsByName: m.map((prop) => prop.name),
methodsAndValues: m.map((x) => {
return {
name: x.name,
type: x.statement,
export default function getAst(code: string): Program | undefined {
try {
return (Parser.parse(code, {
...ACORN_OPTIONS,
// types of acorn are too simplistic and we have to use the body
// eslint-disable-next-line @typescript-eslint/no-explicit-any
}) as any) as Program;
} catch (err) {
return undefined;
}
}
getJSParsed (klass, funcName) {
const jsCode = '' + klass.prototype.constructor;
const jsParsed = jsParser.parse(jsCode);
const jsKlass = jsParsed.body[0];
const jsMethods = jsKlass.body.body;
return jsMethods.find(node => node.key.name === funcName);
}
static getNode (code) {
const parsed = jsParser.parse(code);
const firstNode = parsed.body[0];
const node = firstNode.type === 'BlockStatement' ? firstNode.body[0] :
firstNode.type === 'ExpressionStatement' ? firstNode.expression : null;
return node;
}
constructor (Klass, funcName) {
this.Klass = Klass;
this.funcName = funcName;
this.classCode = '' + Klass.prototype.constructor;
this.klassDecl = jsParser.parse(this.classCode).body[0];
this.methodDefinition = this.klassDecl.body.body.find(node => node.key.name === this.funcName);
}
getJSParsed (klass, funcName) {
const jsCode = '' + klass.prototype.constructor;
const jsParsed = jsParser.parse(jsCode);
const jsKlass = jsParsed.body[0];
const jsMethods = jsKlass.body.body;
return jsMethods.find(node => node.key.name === funcName);
}
constructor (code = '', extraDeclaration = {}) {
this.code = code
this.extraDeclaration = extraDeclaration
this.ast = Parser.parse(code)
this.nodeIterator = null
this.init()
}
parse(source, opts) {
return Parser.parse(source, {
...opts,
ecmaVersion: 2020
})
}
},
module.exports = function codegen(input) {
const ast = Parser.parse(input, { sourceType: "module" })
const red =
ast.body
.filter(v => v.type === "VariableDeclaration")
.map(v => v.declarations[0])
.filter(v => v.init.type === 'CallExpression')
.filter(v => v.init.callee.callee.name === 'codegen')
.filter(v => v.init.callee.arguments[0].type === 'Literal')
.map(v => {
const args = v.init.arguments[0].params.map(v => v.name).reduce((curr, w, i) => ({ ...curr, [w]: i }), {})
return ({
ret: traverseNode(v.init.arguments[0].body.body.find(w => w.type === "ReturnStatement").argument, args),
name: 'CODEGEN' + jsUcfirst(v.init.callee.arguments[0].value),
id: v.init.callee.arguments[0].value
})
})
const objc = red