Comparing version 0.1.0-rc1 to 0.1.0-rc2
/*jshint eqnull:true*/ | ||
// A "magic object". | ||
// If variable, property or function return value equals to this object, | ||
// inline cannot be done | ||
var UNCOMPUTABLE = {}; | ||
function _isPropname(memberExpression, identifier) { | ||
@@ -317,2 +322,5 @@ if (!memberExpression || memberExpression.type !== 'MemberExpression') { | ||
var value = method.apply(base, args); | ||
if (value === UNCOMPUTABLE){ | ||
return; | ||
} | ||
callExpression.buildtimeComputedValue = value; | ||
@@ -381,2 +389,5 @@ } catch (e) {} | ||
} | ||
if (value === UNCOMPUTABLE){ | ||
return; | ||
} | ||
memberExpression.buildtimeComputedValue = value; | ||
@@ -615,2 +626,5 @@ } | ||
root = root[path[depth]]; | ||
if (root === UNCOMPUTABLE){ | ||
return; // Value is uncomputable | ||
} | ||
} | ||
@@ -721,2 +735,3 @@ return { | ||
module.exports.inline = inline; | ||
module.exports.inline = inline; | ||
module.exports.UNCOMPUTABLE = UNCOMPUTABLE; |
{ | ||
"name": "rhubarb", | ||
"version": "0.1.0-rc1", | ||
"version": "0.1.0-rc2", | ||
"description": "Compile-time js constant inliner", | ||
@@ -5,0 +5,0 @@ "main": "lib/rhubarb.node.js", |
@@ -95,1 +95,13 @@ # rhubarb | ||
- `undeclared` - same as `global`, but if a global variable was defined (for example, with `var`), it is skipped | ||
## Uncomputables | ||
Sure, we cannot prceisely define all the state at the build time. | ||
There's a special object `UNCOMPUTABLE` exported from module. | ||
If any variable, property or function result equals to `UNCOMPUTABLE`, it will be ignored. | ||
There's no need to assign this value of all properties, if `state` or any its descendant properties was not defined | ||
(`hasOwnProperty` in JavaScript), it's assumed to be uncomputable. This approach doesn't work for function calls. | ||
If you're going to return an object, make sure all uncomputable properties are marked explicitly. |
107
test/test.js
@@ -140,2 +140,13 @@ var assert = require("assert"); | ||
}); | ||
it('Should get properties of return value', function(){ | ||
var code = "func().bar"; | ||
var expected = '42'; | ||
var result = rhubarb.inline(code, { | ||
func: function(){ | ||
return {bar: 42}; | ||
} | ||
}); | ||
assert.equal(expected, result); | ||
}); | ||
}); | ||
@@ -215,66 +226,2 @@ | ||
// describe('Conditional compilation', function(){ | ||
// it('Should remove if statement branches', function(){ | ||
// var code = [ | ||
// "if (foo){", | ||
// "\ttruthy1();", | ||
// "} else {", | ||
// "\tfalsy1();", | ||
// "}", | ||
// "if (!foo){", | ||
// "\ttruthy2();", | ||
// "} else {", | ||
// "\tfalsy2();", | ||
// "}" | ||
// ].join('\n'); | ||
// var expected = [ | ||
// "truthy1();", | ||
// "falsy2();" | ||
// ].join('\n'); | ||
// var result = rhubarb.inline(code, { | ||
// foo: 42 | ||
// }); | ||
// assert.equal(expected, result); | ||
// }); | ||
// it('Should work with nested ifs', function(){ | ||
// var code = [ | ||
// "if (foo){", | ||
// "truthy1();", | ||
// "if (bar){", | ||
// "truthy2();", | ||
// "}", | ||
// "}" | ||
// ].join('\n'); | ||
// var expected = [ | ||
// "truthy1();", | ||
// "truthy2();" | ||
// ].join('\n'); | ||
// var result = rhubarb.inline(code, { | ||
// foo: 42, | ||
// bar: 'whatever' | ||
// }); | ||
// assert.equal(expected, result); | ||
// }); | ||
// it('Should work with else if', function(){ | ||
// var code = [ | ||
// "if (foo) {", | ||
// "nope1();", | ||
// "} else if (bar) {", | ||
// "nope2();", | ||
// "} else {", | ||
// "fallback();", | ||
// "}" | ||
// ].join('\n'); | ||
// var expected = [ | ||
// "fallback();" | ||
// ].join('\n'); | ||
// var result = rhubarb.inline(code, { | ||
// foo: 0, | ||
// bar: null | ||
// }); | ||
// assert.equal(expected, result); | ||
// }); | ||
// }); | ||
describe("builtins", function(){ | ||
@@ -309,2 +256,34 @@ it('Should use builtins', function(){ | ||
}); | ||
}); | ||
describe("uncomputable", function(){ | ||
it('Should ignore uncomputable identifier', function(){ | ||
var code = 'foo.bar'; | ||
var expected = code; | ||
var result = rhubarb.inline(code, { | ||
foo: rhubarb.UNCOMPUTABLE | ||
}); | ||
assert.equal(expected, result); | ||
}); | ||
it('Should ignore uncomputable property', function(){ | ||
var code = 'foo.bar.baz'; | ||
var expected = code; | ||
var result = rhubarb.inline(code, { | ||
foo: { | ||
bar: rhubarb.UNCOMPUTABLE | ||
} | ||
}); | ||
assert.equal(expected, result); | ||
}); | ||
it('Should ignore uncomputable return value', function(){ | ||
var code = 'foo().bar'; | ||
var expected = code; | ||
var result = rhubarb.inline(code, { | ||
foo: function(){ | ||
return rhubarb.UNCOMPUTABLE; | ||
} | ||
}); | ||
assert.equal(expected, result); | ||
}); | ||
}); |
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
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
28435
107
941