Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

rhubarb

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rhubarb - npm Package Compare versions

Comparing version 0.1.0-a to 0.1.0-rc1

71

lib/rhubarb.node.js

@@ -539,50 +539,2 @@ /*jshint eqnull:true*/

function _getIfReplacement(ifStatement, stack) {
var ownRange = ifStatement.range;
var parentAstNode = null;
for (var i = 0; i < stack.length; i++) {
if (Array.isArray(stack[i])) {
continue;
}
if (stack[i] === ifStatement) {
break;
}
parentAstNode = stack[i];
}
var parentIsBlock = (parentAstNode && (parentAstNode.type === 'BlockStatement' || parentAstNode.type === 'Program'));
var branch;
if (ifStatement.test.buildtimeComputedValue) {
branch = ifStatement.consequent;
} else if (ifStatement.alternate) {
branch = ifStatement.alternate;
}
if (!branch || !branch.body.length) {
return [{
start: ownRange[0],
end: ownRange[1],
value: parentIsBlock ? '' : ';'
}];
}
var branchRange = branch.range;
if (parentIsBlock && branch.type === 'BlockStatement') {
branchRange = [branch.body[0].range[0], branch.body[branch.body.length - 1].range[1]];
}
return [{
start: ownRange[0],
end: branchRange[0],
value: ''
}, {
start: branchRange[1],
end: ownRange[1],
value: ''
}];
}
function _normalizeOptions(dirtyOptions) {

@@ -720,10 +672,2 @@ var options = {

// DEBUG
// try {
// require('fs').mkdirSync(require('path').resolve(__dirname, '..', 'debug'));
// } catch (e) {}
// try {
// require('fs').writeFileSync(require('path').resolve(__dirname, '..', 'debug', 'ast.json'), JSON.stringify(ast, null, '\t'));
// } catch (e) {}
var idNodes = extractIdNodes(ast, opts.scope);

@@ -760,12 +704,9 @@ var replacements = [];

// Special case: expression is wrapped into if statement
if (astNode && astNode.type === 'IfStatement' && astNode.test == modifiedNode) {
replacements = replacements.concat(_getIfReplacement(astNode, stack));
} else {
replacements.push({
'start': modifiedNode.range[0],
'end': modifiedNode.range[1],
'value': _serialize(modifiedNode.buildtimeComputedValue)
});
}
replacements.push({
'start': modifiedNode.range[0],
'end': modifiedNode.range[1],
'value': _serialize(modifiedNode.buildtimeComputedValue)
});
}

@@ -772,0 +713,0 @@

10

package.json
{
"name": "rhubarb",
"version": "0.1.0a",
"version": "0.1.0-rc1",
"description": "Compile-time js constant inliner",

@@ -20,3 +20,9 @@ "main": "lib/rhubarb.node.js",

"url": "https://github.com/subzey/rhubarb.git"
}
},
"keywords": [
"inline",
"constants",
"build",
"compile"
]
}

@@ -40,10 +40,25 @@ # rhubarb

```javascript
doFunctionFoo();
console.debug("I'm in debug environment");
if (true){
doFunctionFoo();
} else {
console.log("I haven't feature foo!");
}
if (true){
console.debug("I'm in debug environment");
}
```
## What this stuff doesn't do?
This is not a minifier/compressor. It just inlines constants.
This is not a dead code removal tool. There are some expression computations, but Rhubarb doesn't touch block
statements. I would like to drop `if` branches if the condition was calculated, but found that this
apparently simple feature is actually tricky and error-prone.
## Why not UglifyJS?
UglifyJS2 is a great compression tool with conditional compilation.
Unfortunately, it cannot always guess if expression have constant value.
Unfortunately, it cannot always guess if expression evaluates constant value.

@@ -70,3 +85,3 @@ This tool can. With your little help.

- `options` is an object with options:
- `options` is an optional object with options:

@@ -82,18 +97,1 @@ ### Options

- `undeclared` - same as `global`, but if a global variable was defined (for example, with `var`), it is skipped
## Status
Currently in development. Do not try to use it yet.
Roadmap:
- [x] Make builtin js objects usable (`Math`, `Object`, etc.)
- [x] Globals reference (`window`, `global`, `self`, custom?)
- [ ] Make a fake-global object accessor
- [ ] Add more options
- [x] Write tests
- [ ] Write docs
- [ ] Make a CLI
- [ ] Publish on NPM
- [ ] Show warnings

@@ -214,46 +214,66 @@ var assert = require("assert");

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);
});
});
// 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(){

@@ -260,0 +280,0 @@ it('Should use builtins', function(){

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc