async-to-gen
Advanced tools
Comparing version 1.2.0 to 1.3.0
52
index.js
@@ -17,4 +17,2 @@ var babylon = require('babylon'); | ||
* | ||
* - sourceMap: (default: false) collects data to produce a correct source map. | ||
* provide true if you plan on calling generateMap(). | ||
* - fastSkip: (default: true) returns the source directly if it doesn't find | ||
@@ -26,2 +24,7 @@ * the word "async" in the source. | ||
function asyncToGen(source, options) { | ||
// Options | ||
var fastSkip = !(options && options.fastSkip === false); | ||
var includeHelper = !(options && options.includeHelper === false); | ||
// Create editor | ||
var editor = new MagicString(source); | ||
@@ -34,4 +37,3 @@ editor.isEdited = false; | ||
// Cheap trick for files that don't actually contain async functions | ||
if (!(options && options.fastSkip === false) && | ||
source.indexOf('async') === -1) { | ||
if (fastSkip && source.indexOf('async') === -1) { | ||
return editor; | ||
@@ -50,7 +52,7 @@ } | ||
ast.shouldIncludeHelper = !(options && options.includeHelper === false); | ||
var sourceMap = options && options.sourceMap === true; | ||
ast.shouldIncludeHelper = includeHelper; | ||
visit(ast, editor, asyncToGenVisitor, sourceMap); | ||
visit(ast, editor, asyncToGenVisitor); | ||
editor.isEdited = Boolean( | ||
@@ -279,3 +281,3 @@ ast.containsAsync || | ||
if (end) { | ||
editor.insertLeft(node.end, end); | ||
editor.appendLeft(node.end, end); | ||
} | ||
@@ -311,4 +313,4 @@ } | ||
var wrapping = createAsyncWrapping(node); | ||
editor.insertLeft(node.body.start + 1, 'return ' + wrapping[0]); | ||
editor.insertRight(node.body.end - 1, wrapping[1]); | ||
editor.appendLeft(node.body.start + 1, 'return ' + wrapping[0]); | ||
editor.prependRight(node.body.end - 1, wrapping[1]); | ||
} | ||
@@ -354,5 +356,5 @@ } | ||
} | ||
editor.insertRight(ast.tokens[idx].end, wrapping[0]); | ||
editor.insertLeft(node.body.start, 'return '); | ||
editor.insertRight(node.body.end, wrapping[1]); | ||
editor.appendLeft(ast.tokens[idx].end, wrapping[0]); | ||
editor.prependRight(node.body.start, 'return '); | ||
editor.appendLeft(node.body.end, wrapping[1]); | ||
} | ||
@@ -416,3 +418,3 @@ } | ||
editor.overwrite(node.object.start, node.object.end, '$uper('); | ||
editor.insertLeft(node.end, ')'); | ||
editor.appendLeft(node.end, ')'); | ||
@@ -444,3 +446,3 @@ // Ensure super.prop() use the current this binding. | ||
editor.overwrite(left.object.start, left.object.end, '$uperEq('); | ||
editor.insertLeft(node.end, ')') | ||
editor.appendLeft(node.end, ')') | ||
@@ -463,4 +465,4 @@ var idx = findTokenIndex(ast.tokens, left.end); | ||
} else { | ||
editor.insertRight(node.property.start, '"'); | ||
editor.insertLeft(node.property.end, '"'); | ||
editor.prependRight(node.property.start, '"'); | ||
editor.appendLeft(node.property.end, '"'); | ||
} | ||
@@ -506,8 +508,8 @@ } | ||
editor.insertRight(node.start, head); | ||
editor.prependRight(node.start, head); | ||
editor.move(node.left.start, node.left.end, node.body.start + 1); | ||
editor.insertLeft(node.left.end, '=' + step + '.value;'); | ||
editor.insertLeft(node.left.start, left); | ||
editor.insertRight(node.right.end, right); | ||
editor.insertLeft(node.end, tail); | ||
editor.appendLeft(node.left.end, '=' + step + '.value;'); | ||
editor.appendLeft(node.left.start, left); | ||
editor.prependRight(node.right.end, right); | ||
editor.appendLeft(node.end, tail); | ||
} | ||
@@ -526,3 +528,3 @@ | ||
// calling methods on the given visitor, providing editor as the first argument. | ||
function visit(ast, editor, visitor, sourceMap) { | ||
function visit(ast, editor, visitor) { | ||
var stack; | ||
@@ -553,6 +555,2 @@ var parent = ast; | ||
if (node.type) { | ||
if (sourceMap) { | ||
editor.addSourcemapLocation(node.start); | ||
editor.addSourcemapLocation(node.end); | ||
} | ||
var visitFn = visitor[node.type] && visitor[node.type].enter; | ||
@@ -559,0 +557,0 @@ visitFn && visitFn(editor, node, ast, stack); |
{ | ||
"name": "async-to-gen", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "Transform async functions to generator functions with speed and simplicity.", | ||
@@ -34,2 +34,4 @@ "author": "Lee Byron <lee@leebyron.com> (http://leebyron.com/)", | ||
"async-await", | ||
"for-await", | ||
"for-await-of", | ||
"generators", | ||
@@ -43,4 +45,4 @@ "compiler", | ||
"babylon": "^6.14.0", | ||
"magic-string": "^0.16.0" | ||
"magic-string": "^0.19.0" | ||
} | ||
} |
@@ -28,9 +28,12 @@ async-to-gen | ||
``` | ||
```sh | ||
npm install --global async-to-gen | ||
``` | ||
``` | ||
```sh | ||
async-to-gen --help | ||
async-to-gen input.js > output.js | ||
# source maps! | ||
async-to-gen input.js --sourcemaps inline > output.js | ||
``` | ||
@@ -40,3 +43,3 @@ | ||
``` | ||
```sh | ||
npm install async-to-gen | ||
@@ -54,4 +57,4 @@ ``` | ||
// source maps! | ||
var map = asyncToGen(input, { sourceMaps: true }).generateMap(); | ||
fs.writeFileSync('output.js.map', output); | ||
var map = asyncToGen(input).generateMap(); | ||
fs.writeFileSync('output.js.map', JSON.stringify(output)); | ||
``` | ||
@@ -65,3 +68,3 @@ | ||
``` | ||
```sh | ||
$ async-node | ||
@@ -94,3 +97,10 @@ > async function answer() { | ||
You can also provide options to the require hook: | ||
```js | ||
// Include inline source maps for use with development tools. | ||
require('flow-remove-types/register')({ sourceMaps: true }) | ||
``` | ||
## Use in Build Systems: | ||
@@ -250,7 +260,2 @@ | ||
NOTE: The behavior of `for-await-of` loops using this tool is not identical to the | ||
proposed spec addition. Where the proposed spec's `for-await-of` expects a | ||
`Symbol.asyncIterator` method for the iterated source, this tool expects | ||
`Symbol.iterator` instead since it transforms it to a `for-of` loop. | ||
## Dead-Simple Transforms | ||
@@ -257,0 +262,0 @@ |
@@ -1,13 +0,48 @@ | ||
var Module = require('module'); | ||
var asyncToGen = require('./index'); | ||
// Rather than use require.extensions, swizzle Module#_compile. Not only does | ||
// this typically leverage the existing behavior of require.extensions['.js'], | ||
// but allows for use alongside other "require extension hook" if necessary. | ||
var super_compile = Module.prototype._compile; | ||
Module.prototype._compile = function _compile(source, filename) { | ||
var transformedSource = filename.indexOf('node_modules/') === -1 | ||
? asyncToGen(source).toString() | ||
: source; | ||
super_compile.call(this, transformedSource, filename); | ||
}; | ||
// Supported options: | ||
// | ||
// - sourceMaps: Include inline source maps. (default: false) | ||
// - includes: A Regexp to determine which files should be transformed. | ||
// - excludes: A Regexp to determine which files should not be transformed, | ||
// defaults to ignoring /node_modules/, set to null to excludes nothing. | ||
var options; | ||
module.exports = function setOptions(newOptions) { | ||
options = newOptions; | ||
} | ||
// Swizzle Module#_compile on each applicable module instance. | ||
// NOTE: if using alongside Babel or another require-hook which simply | ||
// over-writes the require.extensions and does not continue execution, then | ||
// this require hook must come after it. Encourage those module authors to call | ||
// the prior loader in their require hooks. | ||
var jsLoader = require.extensions['.js']; | ||
var exts = [ '.js', '.jsx', '.flow', '.es6' ]; | ||
exts.forEach(function (ext) { | ||
var superLoader = require.extensions[ext] || jsLoader; | ||
require.extensions[ext] = function (module, filename) { | ||
if (shouldTransform(filename, options)) { | ||
var super_compile = module._compile; | ||
module._compile = function _compile(code, filename) { | ||
var sourceMaps = options && options.sourceMaps; | ||
var result = asyncToGen(code, options); | ||
var code = result.toString(); | ||
if (sourceMaps) { | ||
var map = result.generateMap(); | ||
delete map.file; | ||
delete map.sourcesContent; | ||
map.sources[0] = filename; | ||
code += '\n//# sourceMappingURL=' + map.toUrl(); | ||
} | ||
super_compile.call(this, code, filename); | ||
}; | ||
} | ||
superLoader(module, filename); | ||
}; | ||
}); | ||
function shouldTransform(filename, options) { | ||
var includes = options && options.includes; | ||
var excludes = options && 'excludes' in options ? options.excludes : /\/node_modules\//; | ||
return (!includes || include.test(filename)) && !(excludes && excludes.test(filename)); | ||
} |
Sorry, the diff of this file is not supported yet
Debug access
Supply chain riskUses debug, reflection and dynamic code execution features.
Found 1 instance in 1 package
40161
548
299
0
+ Addedmagic-string@0.19.1(transitive)
- Removedmagic-string@0.16.0(transitive)
Updatedmagic-string@^0.19.0