All Projects → avajs → babel-plugin-detective

avajs / babel-plugin-detective

Licence: MIT license
Babel plugin that scans the AST for require calls and import statements

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to babel-plugin-detective

Astexplorer.app
https://astexplorer.net with ES Modules support and Hot Reloading
Stars: ✭ 65 (+150%)
Mutual labels:  babel-plugin, ast
Babel Plugin Ast
使用 babel 进行 AST 分析和处理
Stars: ✭ 44 (+69.23%)
Mutual labels:  babel-plugin, ast
I18nize React
Internationalize react apps within a lunch break
Stars: ✭ 389 (+1396.15%)
Mutual labels:  babel-plugin, ast
Babel Plugin React Persist
Automatically useCallback() & useMemo(); memoize inline functions
Stars: ✭ 91 (+250%)
Mutual labels:  babel-plugin, ast
predeclared
Find definitions and declarations in Go source code that shadow predeclared identifiers
Stars: ✭ 26 (+0%)
Mutual labels:  ast
c-compiler
A compiler that accepts any valid program written in C. It is made using Lex and Yacc. Returns a symbol table, parse tree, annotated syntax tree and intermediate code.
Stars: ✭ 37 (+42.31%)
Mutual labels:  ast
freAST
Fast, simple Free Monads using ScalaMeta macro annotations. Port of Freasy-Monad.
Stars: ✭ 14 (-46.15%)
Mutual labels:  ast
babel-plugin-solid-labels
Simple, reactive labels for SolidJS
Stars: ✭ 127 (+388.46%)
Mutual labels:  babel-plugin
ts-transform-react-constant-elements
A TypeScript AST Transformer that can speed up reconciliation and reduce garbage collection pressure by hoisting React elements to the highest possible scope.
Stars: ✭ 44 (+69.23%)
Mutual labels:  ast
babel-plugin-transform-amd-to-commonjs
✨ Babel plugin that transforms AMD to CommonJS
Stars: ✭ 44 (+69.23%)
Mutual labels:  babel-plugin
code summarization public
source code for 'Improving automatic source code summarization via deep reinforcement learning'
Stars: ✭ 71 (+173.08%)
Mutual labels:  ast
asmdot
[Unstable] Fast, zero-copy and lightweight (Arm | Mips | x86) assembler in (C | C++ | C# | Go | Haskell | Javascript | Nim | OCaml | Python | Rust).
Stars: ✭ 23 (-11.54%)
Mutual labels:  ast
babel-plugin-console-source
Add the file name and line numbers to all console logs.
Stars: ✭ 38 (+46.15%)
Mutual labels:  babel-plugin
babel-plugin-solid-undestructure
A Babel plugin for SolidJS that allows you to destructure component props without losing reactivity.
Stars: ✭ 45 (+73.08%)
Mutual labels:  babel-plugin
babel-plugin-transform-react-qa-classes
Add component's name in `data-qa` attributes to React Components
Stars: ✭ 41 (+57.69%)
Mutual labels:  babel-plugin
Python3Generator
A toolkit to generate Python 3 source code from Pharo.
Stars: ✭ 25 (-3.85%)
Mutual labels:  ast
babel-plugin-storybook-csf-title
A Babel plugin to generate titles for Storybook CSF stories at compile time, typically based on the story file's file name.
Stars: ✭ 17 (-34.62%)
Mutual labels:  babel-plugin
toast
Plugin-driven CLI utility for code generation using Go source as IDL
Stars: ✭ 52 (+100%)
Mutual labels:  ast
babel-plugin-transform-rename-properties
A Babel plugin for renaming JavaScript properties
Stars: ✭ 19 (-26.92%)
Mutual labels:  babel-plugin
titef
🌠 A tiny, lightning-fast, zero-dependecies JavaScript test framework 🌠
Stars: ✭ 19 (-26.92%)
Mutual labels:  ava

babel-plugin-detective Build Status Coverage Status

Babel 5/6 plugin that scans the AST for require calls and import statements

Install

$ npm install --save babel-plugin-detective babel-core

Usage

import babel from 'babel-core';

const detective = require('babel-plugin-detective');
const myModule = require('my' + 'module');

// See below for available options
const options = {};

// Babel 5
// `position` can be 'before' or 'after'
let result = babel.transformFileSync(path, {
	plugins: [{transformer: detective, position: position}],
	extra: {
			detective: options
	}
});

// Babel 6
result = babel.transformFileAsync('/path/to/file', {
	plugins:[['detective', options]]
});

// metadata will be stored on `result.metadata.requires`
// a convenience method is provided to extract it.
const metadata = detective.metadata(result);

console.log(metadata);
// {
//   strings: [
//     'babel-core',
//     'babel-plugin-detective'
//   ],
//   expressions: [
//     {start: 110, end: 125, loc: {...}} // loc of 'my' + 'module' expression
//   ]
// }

API

detective.metadata(previousParseResult)

During traversal, the plugin stores each discovered require/import on the Babel metadata object. It can be extracted manually from parseResult.metadata.requires, or you can pass the parse result to this convenience method.

Returned Metadata

After a babel traversal with this plugin, metadata will be attached at parseResult.metadata.requires

requires.strings

Type: Array<string>

Array of every module imported with an ES2015 import statement, and every module required using a string literal (it does not include dynamic requires).

requires.expressions

Type: Array<locationData>

Array of location data for expressions that are used as the first argument to require (i.e. dynamic requires). The source of the expression can be attached using the attachExpressionSource option. If you wish to disallow dynamic requires, you should throw if this has length greater than 0.

Options

options.generated

Type: boolean Default: false

If set to true, it will include require calls generated by previous plugins in the tool chain. This will lead to some duplicate entries if ES2015 import statements are present in the file. This plugin already scans for ES2015 import statements, so you only need to use this if there is some other type of generated require statement you want to know about.

Works on Babel 6 only

generated:true can be combined with import:false to get only the require statements of the post transform code.

options.import

Type: boolean
Default: true

Include ES2015 imports in the metadata. All ES2015 imports will be of type string.

options.export

Type: boolean
Default: true

Include ES2015 re-exports in the metadata. All ES2015 re-exports will be of type string.

export * from './foo';
export {bar as baz} from './quz';
export {hello} from './goodbye';

options.require

Type: boolean
Default: true

Include CommonJS style require(...) statements in the metadata. CommonJS require statements will be pushed on to requires.strings if the argument is a string literal. For dynamic expressions (i.e. require(foo + bar)), an object will be pushed on to requires.expressions. It will have start and end properties that can be used to extract the code directly from the original source, And a loc object that includes the line/column numbers (useful for creating error statements).

options.word

Type: string
Default: 'require'

The name of the require function. You most likely do not need to change this.

options.source

Type: boolean
Default: false

Attach the actual expression code to each member of requires.expressions.

expressions: [
	 {start: 110, end: 125, loc: {...} code: "'my' + 'module'"}
]

options.nodes

Type: boolean
Default: false

Return the actual nodes instead of extracting strings.

strings : [{type: 'StringLiteral', value: 'foo', ...}],
expressions: [
	 {type: 'BinaryExpression', ...}
]

Everything in strings will be a Literal (Babel 5), or StringLiteral (Babel 6). The path required will be on node.value.

The expressions array can contain any valid Expression node.

Manipulating Require Statements

Warning: Exploratory Support Only: The documentation here is intentionally sparse. While every attempt will be made to avoid breaking changes, it is a new feature so changes are a real possibility. You should look at the source of index.js and the test suite for a better idea on how to use this.

babel-detective/wrap-listener allows you to create your own plugin that can manipulate exports.

The following creates a plugin that upper-cases all require and import statements:

const wrapListener = require('babel-detective/wrap-listener');

module.exports = wrapListener(listener, 'uppercase');

function listener(path, file, opts) {
	if (path.isLiteral()) {
		path.node.value = path.node.value.toUpperCase();
	}
}

wrapListener(listener, name, options)

listener

Type: callback(nodePath, file, opts) Required

A listener that performs the actual manipulation. It is called with:

  • nodePath: The actual node in question can be accessed via nodePath.node. nodePath has other properties (parent, isLiteral(), etc.). A full description of that API is out of scope for this document.

  • file: The Babel file metadata object. This is what the main module uses to store metadata for required modules that it finds.

  • opts: The options that were passed to the plugin. This is done via the array syntax in Babel 6, or options.extra[name] in Babel 5.

name

Required
Type: string

This is the key used to locate opts in Babel 5 options.extra[name].

options

Optional

Accepts the import, require, and generated options as described above.

Related

  • node-detective Inspiration for this module. Used by browserify to analyze module dependencies.

License

MIT © James Talmage

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].