How to use the acorn.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 stdlib-js / stdlib / lib / node_modules / @stdlib / repl / lib / process_command.js View on Github external
debug( 'Attempting to process top-level `await` expression...' );
		tmp = processAwait( code );
		if ( typeof tmp === 'string' ) {
			debug( 'Successfully processed top-level `await` expression.' );
			code = tmp;
		} else {
			debug( 'Unable to process command as a top-level `await` expression.' );
			debug( 'Error: %s', tmp.message );
		}
	} else {
		debug( 'Unable to detect a top-level `await` expression.' );
	}
	// Attempt to strictly parse the provided code string:
	debug( 'Attempting to generate an AST...' );
	try {
		ast = parse( code );
		debug( 'Successfully generated an AST.' );
	} catch ( error ) {
		debug( 'Unable to generate an AST.' );
		debug( 'Error: %s', error.message );
		err = error; // cache the error message
	}
	if ( err === void 0 ) {
		// If the body is empty, assume that we have been given a multi-line comment...
		if ( ast.body.length === 0 ) {
			debug( 'Detected multi-line comment.' );
			return code;
		}
		// Check whether the code ends in an empty block statement, and, if so, interpret as an empty object literal...
		for ( i = ast.body.length-1; i >= 0; i-- ) {
			node = ast.body[ i ];
			if ( node.type !== 'EmptyStatement' ) {
github rollup / d3-jsnext / generate / src / Module.js View on Github external
constructor ( file ) {
		this.file = file;
		this.dir = dirname( file );

		let lastIndex = 0;

		this.src = readSource( file, this.included = {} );
		this.magicString = new MagicString( this.src );

		// Attempt to parse with acorn
		try {
			this.ast = parse( this.src );
		} catch ( err ) {
			console.log( this.src );
			console.log( `error parsing ${file}: ${err.message}` );
			throw err;
		}

		this.internalDependencies = {};
		this.exportDependencies = {};

		this.internalNameByExportName = {};
		this.exports = {};

		this.analyse();

		this.definitions = this.ast._scope.names.slice();
	}
github Youjingyu / vue-hap-tools / convert / utils / index.js View on Github external
function getVModelAst (vModels, e, keyToPolyfill) {
  // 将v-model监听的数据,添加到change的回调函数处理
  const jsStr = vModels.reduce((total, dataName) => {
    return total + `this.${dataName}=${e}.target.${keyToPolyfill};`
  }, '')
  return acorn.parse(jsStr, {sourceType: 'script'}).body
}
github daliwali / doc-tree / lib / index.js View on Github external
constructor (input, options) {
    if (options === void 0) options = {}

    let comments = []

    if (Buffer.isBuffer(input))
      input = input.toString(options.encoding || defaultEncoding)

    this.AST = acorn.parse(input, Object.assign({
      ecmaVersion,
      sourceType,
      locations: true,
      onComment: comments
    }, options))

    // Collapse contiguous single-line comments.
    for (let index = 0; index < comments.length; index++) {
      const comment = comments[index]
      const nextComment = comments[index + 1]

      if (nextComment &&
      comment.type === 'Line' &&
      nextComment.type === 'Line' &&
      nextComment.loc.start.line === comment.loc.end.line + 1) {
        comments[index + 1].value = comment.value + nextComment.value
github rollup / rollup / test / form / samples / external-import-alias-shadow / _expected / cjs.js View on Github external
function parse(source) {
	return acorn.parse(source, { ecmaVersion: 6 });
}
github Tangerine-Community / Tangerine / client-legacy / src / js / lib / bower_components / bluebird / tools / ast_passes.js View on Github external
fileName = opts;
        opts = void 0;
    }
    try {
        return jsp.parse(src, opts);
    }
    catch(e) {
        e.message = e.message + " " + fileName;
        e.scriptSrc = src;
        throw e;
    }
}

var inlinedFunctions = Object.create(null);

var lastElement = jsp.parse("___input.length").body[0].expression;
var firstElement = jsp.parse("0").body[0].expression;
inlinedFunctions.INLINE_SLICE = function( node ) {
    var statement = node;
    node = node.expression;
    var args = node.arguments;

    if( !(2 <= args.length && args.length <= 4 ) ) {
        throw new Error("INLINE_SLICE must have exactly 2, 3 or 4 arguments");
    }

    var varExpression = args[0];
    var collectionExpression = args[1];
    var startExpression = args.length < 3
        ? firstElement
        : args[2];
    var endExpression = args.length < 4
github dcloudio / uni-app / lib / rollup-plugin-require-context / src / helper / extract-args.js View on Github external
return new Promise((resolve, reject) => {
    const ast = parse(code, {
      sourceType: 'module'
    })
    const res = []
    walk.simple(ast, {
      CallExpression (node) {
        const {
          start,
          end,
          callee,
          arguments: argNodes
        } = node
        let args = []
        if (
          callee.type === 'MemberExpression' &&
          callee.object.name === 'require' &&
          callee.property.name === 'context'
github humhub / humhub / static / assets / e8190bfa / tools / ast_passes.js View on Github external
function parse( src, opts, fileName) {
    if( !fileName ) {
        fileName = opts;
        opts = void 0;
    }
    try {
        return jsp.parse(src, opts);
    }
    catch(e) {
        e.message = e.message + " " + fileName;
        e.scriptSrc = src;
        throw e;
    }
}
github Polyconseil / easygettext / src / extract.js View on Github external
const match = re.exec(context);
        if (match !== null) {
          expr = match[1];
          break;
        }
      }

      if (expr.startsWith('"')) {
        return [/^"(.*)"/.exec(expr)[1]];
      }

      if (!expr.match(/['"]/)) {
        continue;
      }
      try {
        return this._compileExpressions(acorn.parse(expr));
      } catch (exception) {
        if (i === (contexts.length - 1)) {
          throw exception;
        }
      }
    }
    return [];
  }