Comparing version 0.5.0 to 0.6.0
"use strict"; | ||
var esprima = require("esprima").parse; | ||
var assert = require("assert"); | ||
@@ -310,2 +309,5 @@ var is = require("simple-is"); | ||
}); | ||
// ast change const|let => var | ||
node.kind = "var"; | ||
} | ||
@@ -379,2 +381,4 @@ } | ||
error(getline(node), msg, node.name, "return", getline(n)); | ||
} else if (n.type === "YieldExpression") { | ||
error(getline(node), msg, node.name, "yield", getline(n)); | ||
} else if (n.type === "Identifier" && n.name === "arguments") { | ||
@@ -462,3 +466,3 @@ error(getline(node), msg, node.name, "arguments", getline(n)); | ||
function transformLoopClosures(root, ops) { | ||
function transformLoopClosures(root, ops, options) { | ||
function insertOp(pos, str, node) { | ||
@@ -495,3 +499,3 @@ var op = { | ||
// modify AST | ||
var iifeFragment = esprima(iifeHead + iifeTail); | ||
var iifeFragment = options.parse(iifeHead + iifeTail); | ||
var iifeExpressionStatement = iifeFragment.body[0]; | ||
@@ -587,6 +591,13 @@ var iifeBlockStatement = iifeExpressionStatement.expression.callee.object.body; | ||
var ast = esprima(src, { | ||
var gotAST = is.object(src); | ||
if (gotAST && !options.ast) { | ||
return { | ||
errors: ["Can't produce string output when input is an AST. Did you forget to set options.ast = true?"], | ||
} | ||
} | ||
var ast = (gotAST ? src : options.parse(src, { | ||
loc: true, | ||
range: true, | ||
}); | ||
})); | ||
@@ -604,3 +615,3 @@ // TODO detect unused variables (never read) | ||
var changes = []; | ||
transformLoopClosures(ast, changes); | ||
transformLoopClosures(ast, changes, options); | ||
@@ -607,0 +618,0 @@ //ast.$scope.print(); process.exit(-1); |
@@ -7,2 +7,3 @@ // default configuration | ||
disallowUnknownReferences: true, | ||
parse: require("esprima").parse, | ||
}; |
@@ -0,1 +1,6 @@ | ||
## v0.6.0 2013-12-01 | ||
* Accept a custom parse function | ||
* Accept AST input when used as a library | ||
* Bugfix AST modification (issue #19) | ||
## v0.5.0 2013-09-30 | ||
@@ -2,0 +7,0 @@ * Loop closure IIFE transformation support |
"use strict"; | ||
const esprima = require("esprima").parse; | ||
const assert = require("assert"); | ||
@@ -310,2 +309,5 @@ const is = require("simple-is"); | ||
}); | ||
// ast change const|let => var | ||
node.kind = "var"; | ||
} | ||
@@ -379,2 +381,4 @@ } | ||
error(getline(node), msg, node.name, "return", getline(n)); | ||
} else if (n.type === "YieldExpression") { | ||
error(getline(node), msg, node.name, "yield", getline(n)); | ||
} else if (n.type === "Identifier" && n.name === "arguments") { | ||
@@ -462,3 +466,3 @@ error(getline(node), msg, node.name, "arguments", getline(n)); | ||
function transformLoopClosures(root, ops) { | ||
function transformLoopClosures(root, ops, options) { | ||
function insertOp(pos, str, node) { | ||
@@ -495,3 +499,3 @@ const op = { | ||
// modify AST | ||
const iifeFragment = esprima(iifeHead + iifeTail); | ||
const iifeFragment = options.parse(iifeHead + iifeTail); | ||
const iifeExpressionStatement = iifeFragment.body[0]; | ||
@@ -587,6 +591,13 @@ const iifeBlockStatement = iifeExpressionStatement.expression.callee.object.body; | ||
const ast = esprima(src, { | ||
const gotAST = is.object(src); | ||
if (gotAST && !options.ast) { | ||
return { | ||
errors: ["Can't produce string output when input is an AST. Did you forget to set options.ast = true?"], | ||
} | ||
} | ||
const ast = (gotAST ? src : options.parse(src, { | ||
loc: true, | ||
range: true, | ||
}); | ||
})); | ||
@@ -604,3 +615,3 @@ // TODO detect unused variables (never read) | ||
const changes = []; | ||
transformLoopClosures(ast, changes); | ||
transformLoopClosures(ast, changes, options); | ||
@@ -607,0 +618,0 @@ //ast.$scope.print(); process.exit(-1); |
@@ -7,2 +7,3 @@ // default configuration | ||
disallowUnknownReferences: true, | ||
parse: require("esprima").parse, | ||
}; |
{ | ||
"name": "defs", | ||
"version": "0.5.0", | ||
"version": "0.6.0", | ||
"description": "Static scope analysis and transpilation of ES6 block scoped const and let variables, to ES3.", | ||
@@ -5,0 +5,0 @@ "main": "build/es5/defs-main.js", |
@@ -82,3 +82,6 @@ # defs.js | ||
`parse` (defaults to `null`) lets you provide a custom parse function if you | ||
use defs as an API. By default it will use `require("esprima").parse`. | ||
## Example | ||
@@ -123,4 +126,10 @@ | ||
const options = {}; | ||
const res = defs("const x = 1", options); | ||
const src = "const x = 1"; | ||
const res = defs(src, options); | ||
assert(res.src === "var x = 1"); | ||
// you can also pass an AST (with loc and range) instead of a string to defs | ||
const ast = require("esprima").parse(src, {loc: true, range: true}); | ||
const res = defs(ast, {ast: true}); // AST-in, AST-out | ||
// inspect res.ast | ||
``` | ||
@@ -127,0 +136,0 @@ |
119212
3093
154