main.js
3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/**
* Copyright (c) 2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* https://raw.github.com/facebook/regenerator/master/LICENSE file. An
* additional grant of patent rights can be found in the PATENTS file in
* the same directory.
*/
var assert = require("assert");
var path = require("path");
var fs = require("fs");
var through = require("through");
var transform = require("./lib/visit").transform;
var utils = require("./lib/util");
var recast = require("recast");
var types = recast.types;
var genOrAsyncFunExp = /\bfunction\s*\*|\basync\b/;
var blockBindingExp = /\b(let|const)\s+/;
function exports(file, options) {
var data = [];
return through(write, end);
function write(buf) {
data.push(buf);
}
function end() {
this.queue(compile(data.join(""), options).code);
this.queue(null);
}
}
// To get a writable stream for use as a browserify transform, call
// require("regenerator")().
module.exports = exports;
// To include the runtime globally in the current node process, call
// require("regenerator").runtime().
function runtime() {
require("./runtime");
}
exports.runtime = runtime;
runtime.path = path.join(__dirname, "runtime.js");
function compile(source, options) {
options = normalizeOptions(options);
if (!genOrAsyncFunExp.test(source)) {
return {
// Shortcut: no generators or async functions to transform.
code: (options.includeRuntime === true ? fs.readFileSync(
path.join(__dirname, "runtime.js"), "utf-8"
) + "\n" : "") + source
};
}
var recastOptions = getRecastOptions(options);
var ast = recast.parse(source, recastOptions);
var nodePath = new types.NodePath(ast);
var programPath = nodePath.get("program");
if (shouldVarify(source, options)) {
// Transpile let/const into var declarations.
varifyAst(programPath.node);
}
transform(programPath, options);
return recast.print(nodePath, recastOptions);
}
function normalizeOptions(options) {
options = utils.defaults(options || {}, {
includeRuntime: false,
supportBlockBinding: true
});
if (!options.esprima) {
options.esprima = require("esprima-fb");
}
assert.ok(
/harmony/.test(options.esprima.version),
"Bad esprima version: " + options.esprima.version
);
return options;
}
function getRecastOptions(options) {
var recastOptions = {
range: true
};
function copy(name) {
if (name in options) {
recastOptions[name] = options[name];
}
}
copy("esprima");
copy("sourceFileName");
copy("sourceMapName");
copy("inputSourceMap");
copy("sourceRoot");
return recastOptions;
}
function shouldVarify(source, options) {
var supportBlockBinding = !!options.supportBlockBinding;
if (supportBlockBinding) {
if (!blockBindingExp.test(source)) {
supportBlockBinding = false;
}
}
return supportBlockBinding;
}
function varify(source, options) {
var recastOptions = getRecastOptions(normalizeOptions(options));
var ast = recast.parse(source, recastOptions);
varifyAst(ast.program);
return recast.print(ast, recastOptions).code;
}
function varifyAst(ast) {
types.namedTypes.Program.assert(ast);
var defsResult = require("defs")(ast, {
ast: true,
disallowUnknownReferences: false,
disallowDuplicated: false,
disallowVars: false,
loopClosures: "iife"
});
if (defsResult.errors) {
throw new Error(defsResult.errors.join("\n"))
}
return ast;
}
// Convenience for just translating let/const to var declarations.
exports.varify = varify;
// Allow packages that depend on Regenerator to use the same copy of
// ast-types, in case multiple versions are installed by NPM.
exports.types = types;
// Transforms a string of source code, returning the { code, map? } result
// from recast.print.
exports.compile = compile;
// To modify an AST directly, call require("regenerator").transform(ast).
exports.transform = transform;