Giter Club home page Giter Club logo

estree-util-module-to-function's Introduction

estree-util-module-to-function

github actions npm codecov prettier

Convert an estree module into a function body.

Installation

npm install estree-util-module-to-function

Usage

Typically you’ll get an AST from acorn, then process it.

import { parse } from 'acorn'
import { moduleToFunction } from 'estree-util-module-to-function'

const ast = parse(source, { ecmaVersion: 'latest', sourceType: 'module' })
moduleToFunction(ast)

console.dir(ast)

API

This module exports a single function named moduleToFunction.

moduleToFunction(ast, options?)

Convert an estree module into a function body. This modifies the input AST.

Options

  • importName: A custom name for the import. By default, import() expressions are used. If this option is given, import expressions and import meta properties are transformed into identifiers using this name. (type: string)

Examples

The following example shows how to read the home directory in Node.js by using ESM code from a string.

import { parse } from 'acorn'
import { generate } from 'astring'
import { moduleToFunction } from 'estree-util-module-to-function'

const AsyncFunction = Object.getPrototypeOf(async () => {}).constructor

const source = `
import { readdir } from 'fs/promises'
import { homedir } from 'os'

const home = homedir()
export default home
export const files = await readdir(homedir())
`

const ast = parse(source, { ecmaVersion: 'latest', sourceType: 'module' })
moduleToFunction(ast)
const transformed = generate(ast)
const fn = new AsyncFunction(transformed)

const result = await fn()
console.dir(result)

The following example is derived from the above. It injects a custom import function, which stubs actual import behaviour.

import { parse } from 'acorn'
import { generate } from 'astring'
import { moduleToFunction } from 'estree-util-module-to-function'

const AsyncFunction = Object.getPrototypeOf(async () => {}).constructor

function customImport(name) {
  if (name === 'fs/promises') {
    return {
      readdir: () => ['foo.txt']
    }
  }
  if (name === 'os') {
    return {
      homedir: () => '/home/fakeuser'
    }
  }
}

const source = `
import { readdir } from 'fs/promises'
import { homedir } from 'os'

const home = homedir()
export default home
export const files = await readdir(homedir())
`

const importName = '__import__'
const ast = parse(source, { ecmaVersion: 'latest', sourceType: 'module' })
moduleToFunction(ast, { importName })
const transformed = generate(ast)
const fn = new AsyncFunction(importName, transformed)

const result = await fn(customImport)
console.dir(result)

Security

Warning This package only transforms the AST input, which is safe to use on its own. However, it was created with the use case in mind to evaluate an ECMASscript module. Evaluating user input is dangerous and should be avoided whenever possible.

License

MIT @ Remco Haszing

estree-util-module-to-function's People

Contributors

remcohaszing avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.