How to use the acorn.Parser.parse function in acorn

To help you get started, we’ve selected a few acorn examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github allenhwkim / ngentest / src / util.js View on Github external
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;
  }
github DavidWells / analytics / scripts / docs / parse / parse.js View on Github external
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,
github styleguidist / react-styleguidist / src / client / utils / getAst.ts View on Github external
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;
	}
}
github allenhwkim / ngentest / src3 / ng-class-writer.js View on Github external
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);
  }
github allenhwkim / ngentest / src3 / util.js View on Github external
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;
  }
github allenhwkim / ngentest / src3 / ng-func-writer.js View on Github external
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);
  }
github allenhwkim / ngentest / src / z-deleted.ng-class-writer.js View on Github external
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);
  }
github jrainlau / canjs / src / index.js View on Github external
constructor (code = '', extraDeclaration = {}) {
    this.code = code
    this.extraDeclaration = extraDeclaration
    this.ast = Parser.parse(code)
    this.nodeIterator = null
    this.init()
  }
github riot / compiler / src / utils / generate-ast.js View on Github external
parse(source, opts) {
        return Parser.parse(source, {
          ...opts,
          ecmaVersion: 2020
        })
      }
    },
github software-mansion / react-native-reanimated / codegen / codegen.js View on Github external
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