Comparing version 0.0.1 to 0.0.2
@@ -0,1 +1,3 @@ | ||
var fs = require('fs'); | ||
var es6arrowfn = require('es6-arrow-function'); | ||
@@ -5,2 +7,5 @@ var es6class = require('es6-class'); | ||
var esprima = require('esprima'); | ||
var recast = require('recast'); | ||
/** | ||
@@ -24,31 +29,11 @@ * Compile the given next-generation JavaScript into JavaScript usable in | ||
function compile(source, options) { | ||
var maps = []; | ||
var compiled; | ||
if (!options) { | ||
options = {}; | ||
} | ||
if (options.arrowFunction !== false) { | ||
compiled = es6arrowfn.compile(source); | ||
source = compiled.code; | ||
maps.push(compiled.map); | ||
} | ||
if (options['class'] !== false) { | ||
compiled = es6class.compile(source); | ||
source = compiled.code; | ||
maps.push(compiled.map); | ||
} | ||
if (options.generator !== false) { | ||
// TODO: get source maps from regenerator | ||
compiled = regenerator(source, { | ||
includeRuntime: options.includeRuntime | ||
}); | ||
source = compiled; | ||
} | ||
// TODO: figure out how to combine the source maps | ||
return { code: source, map: '' }; | ||
if (!options) { options = {}; } | ||
var ast = recast.parse(source, { | ||
esprima: esprima, | ||
sourceFileName: options.sourceFileName | ||
}); | ||
ast = transform(ast, options); | ||
return recast.print(ast, { | ||
sourceMapName: options.sourceMapName | ||
}); | ||
} | ||
@@ -72,5 +57,3 @@ | ||
function transform(ast, options) { | ||
if (!options) { | ||
options = {}; | ||
} | ||
if (!options) { options = {}; } | ||
@@ -86,8 +69,45 @@ if (options.arrowFunction !== false) { | ||
if (options.generator !== false) { | ||
ast = regenerator.ast(source); | ||
ast = regenerator.transform(ast); | ||
} | ||
if (options.includeRuntime) { | ||
var runtime = fs.readFileSync(regenerator.runtime.dev, 'utf8'); | ||
injectRuntime(runtime, regenerator.runtime.dev, ast.program); | ||
} | ||
return ast; | ||
} | ||
/** | ||
* Originally from http://facebook.github.io/regenerator/. | ||
* | ||
* 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. | ||
*/ | ||
function injectRuntime(runtime, filename, ast) { | ||
recast.types.builtInTypes.string.assert(runtime); | ||
recast.types.namedTypes.Program.assert(ast); | ||
// Include the runtime by modifying the AST rather than by concatenating | ||
// strings. This technique will allow for more accurate source mapping. | ||
if (runtime !== "") { | ||
var runtimeBody = recast.parse(runtime, { | ||
sourceFileName: filename | ||
}).program.body; | ||
var body = ast.body; | ||
body.unshift.apply(body, runtimeBody); | ||
} | ||
return ast; | ||
} | ||
/** | ||
* End Facebook Copyright. | ||
*/ | ||
exports.compile = compile; |
{ | ||
"name": "esnext", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Transform next-generation JavaScript to today's JavaScript.", | ||
@@ -23,3 +23,3 @@ "main": "lib/index.js", | ||
"devDependencies": { | ||
"example-runner": "0.0.2" | ||
"example-runner": "^0.0.3" | ||
}, | ||
@@ -29,4 +29,6 @@ "dependencies": { | ||
"es6-arrow-function": "^0.2.0", | ||
"regenerator": "^0.4.5" | ||
"regenerator": "^0.4.5", | ||
"esprima": "git://github.com/ariya/esprima.git#harmony", | ||
"recast": "^0.5.14" | ||
} | ||
} |
@@ -19,3 +19,2 @@ # esnext | ||
* Generate human-readable code. | ||
* TODO: Provide source maps from the original source to compiled source. | ||
* TODO: Integrate with build tools such as [broccoli][broccoli]. | ||
@@ -54,3 +53,5 @@ | ||
var compile = require('esnext').compile; | ||
console.log(compile(es6Source).code); | ||
var result = compile(es6Source); | ||
fs.writeFileSync('result.js', result.code, 'utf8'); | ||
fs.writeFileSync('result.js.map', JSON.stringify(result.map), 'utf8'); | ||
``` | ||
@@ -94,2 +95,10 @@ | ||
## Acknowledgements | ||
Huge thanks to [Ben Newman][benjamn] for [recast][recast] and | ||
[regenerator][regenerator]. Thanks also to [Thomas Boyt][thomasboyt] for his | ||
work on the [es6-module-transpiler][es6-module-transpiler], | ||
[es6-class][es6-class], [es6-arrow-function][es6-arrow-function], and others. | ||
[benjamn]: https://github.com/benjamn | ||
[broccoli]: https://github.com/joliss/broccoli | ||
@@ -99,3 +108,5 @@ [es6-arrow-function]: https://github.com/square/es6-arrow-function | ||
[es6-module-transpiler]: https://github.com/square/es6-module-transpiler | ||
[recast]: https://github.com/benjamn/recast | ||
[regenerator]: http://facebook.github.io/regenerator/ | ||
[thomasboyt]: http://www.thomasboyt.com/ | ||
[traceur]: https://github.com/google/traceur-compiler |
@@ -7,5 +7,22 @@ /** | ||
Error.stackTraceLimit = 20; | ||
var compile = require('../lib').compile; | ||
require('example-runner').runCLI(function(source) { | ||
return compile(source, { includeRuntime: true }).code; | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
var RESULTS = 'test/results'; | ||
if (!fs.existsSync(RESULTS)) { | ||
fs.mkdirSync(RESULTS); | ||
} | ||
require('example-runner').runCLI(function(source, testName, filename) { | ||
var result = compile(source, { | ||
includeRuntime: true, | ||
sourceFileName: filename, | ||
sourceMapName: filename + '.map' | ||
}); | ||
fs.writeFileSync(path.join(RESULTS, testName + '.js'), result.code, 'utf8'); | ||
fs.writeFileSync(path.join(RESULTS, testName + '.js.map'), JSON.stringify(result.map), 'utf8'); | ||
return result.code; | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Git dependency
Supply chain riskContains a dependency which resolves to a remote git URL. Dependencies fetched from git URLs are not immutable and can be used to inject untrusted code or reduce the likelihood of a reproducible install.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9630
10
173
109
5
1
2