Giter VIP home page Giter VIP logo

extract-cjs-static-bindings's Introduction

just run npm t and look in the fixtures folder... will have to write this all up later.

Design Goals

Static Only

Pick up "static" bindings on the module exports as found in CJS source texts. For some forms of code, export * from in particular, they will be a best effort and might not be picked up. A variety of workflows like module.exports = require(...) are not problematic even though they have similar effects.

Lenient

If a binding is a constant, but ambiguous if it is actually exported, include it. In reality this would mean

if (isTuesday()) {
  exports.taco = true;
}

Would always list taco as exported.

Conventional

Compiled output contains things like __esModule, a best effort will be made to pick up on those.

Algorithms

Static member expressions

When a member expression is found, if the property name is a non-computed value it is treated as a static member expression.

Export target references

When a reference to the static member expression module.exports is found and module is a free variable it will be treated as a valid exports target with the replacement flag set to true.

When a reference to exports is found and exports is a free variable it will be treated as a valid exports target with the replacement flag set to false.

Static Literals

String, number, boolean, and null literals will be treated as static literals.

Static property providers

When an object literal is found it is treated as a static property provider. Any property which has a non-computed name that is a static literal or identifier will be treated a static property provided by the literal.

When a call expression is found calling a free variable require with a static literal as its only argument. it is treated as a static property provider. Create a dependency on the result of resolving the argument against the current module path. The properties provided are the static exports the dependency. Note: this requires a complete graph traversal due to cycles.

Simple assignments

When an assignment expression is found:

  • if the left hand side is a static member expression that has a valid export target as the object. mark the property of the left hand side as exportable and hold onto the right hand side.

  • if the left hand side is a valid export target with the replacement flag set and the right hand side is a static property provider. mark the right hand side as a spread provider.

Defines

When a call expression is found calling either the static member expression Object.defineProperty or Reflect.defineProperty, the corresponding object identifier as a free variable, the first argument being a valid exports target, and the second argument being a static literal. Mark the value of the second argument as being exportable and hold onto the 3rd argument with the descriptor flag set to true.

When a call expression is found calling either the static member expression Object.defineProperties with the first argument being a valid exports target and the second argument being a static property provider. Mark the value of the second argument as a spread provider with the descriptor flag set to true.

IIFEs

If a call expression is found that has a function literal as its callee, it will be treated as an IIFE (Immediately Invoked Function Expression).

Functions

When a function is found if it is not an IIFE ignore anything exportable inside of the function.

Static exports

After completely parsing the current module source text and all dependencies. The static exports of the current module are the aggregation of the exportable names in the current module and the static exports of all dependencies.

extract-cjs-static-bindings's People

Contributors

bfarias-godaddy avatar

Watchers

 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.