├── .DS_Store ├── .gitignore ├── .travis.yml ├── README.md ├── lib └── codesurgeon.js ├── package.json └── test ├── .DS_Store ├── fixtures ├── .DS_Store ├── debuggerStatements.js ├── functionDeclarations.js ├── functionExpressions.js └── variableDeclarations.js ├── test └── tests ├── debugger.js ├── declarations.js └── expressions.js /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/heapwolf/codesurgeon/c4fe37684dd24761e62f7ee8412feab915b3d5bc/.DS_Store -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | lib-cov 2 | *.seed 3 | *.log 4 | *.csv 5 | *.dat 6 | *.out 7 | *.pid 8 | *.gz 9 | 10 | pids 11 | logs 12 | 13 | node_modules/* 14 | 15 | npm-debug.log 16 | .DS_Store 17 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: "node_js" 2 | node_js: 3 | - 0.6 4 | - 0.7 5 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DEPRECATED 2 | 3 | You should use Facebook's module -- https://github.com/facebook/jscodeshift 4 | 5 | # SYNOPSIS 6 | Build a subset or superset of a code base. 7 | 8 | # DESCRIPTION 9 | Rather than breaking apart files at design-time for the purpose of targeted 10 | distribution, you can break them apart using a filter at build-time. 11 | 12 | 13 | # EXAMPLES 14 | As the Javascript file is traversed and syntax is discovered, you will have the 15 | opportunity to interject, for example: 16 | 17 | ### Input 18 | ```js 19 | var a = 10 20 | var b = 11 21 | var c = 12 22 | ``` 23 | 24 | ### Code 25 | 26 | ```js 27 | var cs = require('codesurgeon') 28 | 29 | var filter = cs.filter() 30 | 31 | filter.inclusive = true 32 | 33 | filter.syntax.VariableDeclaration = function(name, item) { 34 | if (name === 'b') { 35 | return true 36 | } 37 | } 38 | 39 | fstream 40 | .Reader('example.js') 41 | .pipe(filter) 42 | .pipe(process.stdout) 43 | ``` 44 | 45 | ### Output 46 | ```js 47 | var b = 11 48 | ``` 49 | 50 | A filter supports a streaming style API, but most operations block processing so 51 | this is purely for convenience. 52 | 53 | # Syntax Reference 54 | 55 | `AssignmentExpression` 56 | `ArrayExpression` 57 | `ArrayPattern` 58 | `BlockStatement` 59 | `BinaryExpression` 60 | `BreakStatement` 61 | `CallExpression` 62 | `CatchClause` 63 | `ComprehensionBlock` 64 | `ComprehensionExpression` 65 | `ConditionalExpression` 66 | `ContinueStatement` 67 | `DirectiveStatement` 68 | `DoWhileStatement` 69 | `DebuggerStatement` 70 | `EmptyStatement` 71 | `ExpressionStatement` 72 | `ForStatement` 73 | `ForInStatement` 74 | `FunctionDeclaration` 75 | `FunctionExpression` 76 | `Identifier` 77 | `IfStatement` 78 | `Literal` 79 | `LabeledStatement` 80 | `LogicalExpression` 81 | `MemberExpression` 82 | `NewExpression` 83 | `ObjectExpression` 84 | `ObjectPattern` 85 | `Program` 86 | `Property` 87 | `ReturnStatement` 88 | `SequenceExpression` 89 | `SwitchStatement` 90 | `SwitchCase` 91 | `ThisExpression` 92 | `ThrowStatement` 93 | `TryStatement` 94 | `UnaryExpression` 95 | `UpdateExpression` 96 | `VariableDeclaration` 97 | `VariableDeclarator` 98 | `WhileStatement` 99 | `WithStatement` 100 | `YieldExpression` 101 | 102 | # LICENSE 103 | (The MIT License) 104 | 105 | Copyright (c) 2010 hij1nx 106 | 107 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 108 | 109 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 110 | 111 | THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 112 | -------------------------------------------------------------------------------- /lib/codesurgeon.js: -------------------------------------------------------------------------------- 1 | var esprima = require('esprima'); 2 | var escodegen = require('escodegen'); 3 | var Stream = require('stream').Stream; 4 | 5 | exports.filter = function (options) { 6 | 7 | options = options || {}; 8 | 9 | var debug = true; 10 | 11 | if (typeof options.debug === 'boolean') { 12 | debug = options.debug; 13 | } 14 | 15 | var buffer = ''; 16 | var ast = null; 17 | var code = null; 18 | var stream = new Stream; 19 | 20 | stream.writable = true; 21 | stream.readable = true; 22 | 23 | stream.exclusive = false; 24 | 25 | stream.syntax = {}; 26 | 27 | stream.write = function(data) { 28 | 29 | buffer += data.toString(); 30 | }; 31 | 32 | stream.end = function (data) { 33 | 34 | if (arguments.length) { 35 | stream.write(data); 36 | } 37 | 38 | ast = esprima.parse(buffer); 39 | 40 | traverse(ast.body); 41 | 42 | code = escodegen.generate(ast); 43 | 44 | stream.emit('data', code); 45 | 46 | stream.writable = false; 47 | stream.emit('end', code); 48 | }; 49 | 50 | var traverse = function traverse(ast) { 51 | 52 | if (ast.body) { 53 | traverse(ast.body); 54 | } 55 | 56 | if (Array.isArray(ast)) { 57 | 58 | ast.forEach(function(item, index) { 59 | 60 | // 61 | // TODO: cant always traverse into the object 62 | // the same way, need to address a few cases. 63 | // 64 | if (item.body || item.declarations) { 65 | traverse(item.body || item.declarations); 66 | } 67 | 68 | var id = (item.id && item.id.name) || null; 69 | 70 | if (stream.syntax[item.type]) { 71 | 72 | var result = stream.syntax[item.type](id, item); 73 | 74 | if ((result && stream.exclusive === true) || 75 | !result && stream.exclusive === false) { 76 | return ast.splice(index, 1); 77 | } 78 | } 79 | }); 80 | } 81 | }; 82 | 83 | return stream; 84 | }; 85 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "codesurgeon", 3 | "description": "Build a subset or superset of a code base.", 4 | "version": "0.3.6", 5 | "author": "Paolo Fragomeni", 6 | "contributors": [ 7 | { 8 | "name": "Paolo Fragomeni", 9 | "email": "paolo@async.ly" 10 | } 11 | ], 12 | "repository": { 13 | "type": "git", 14 | "url": "http://github.com/hij1nx/codesurgeon.git" 15 | }, 16 | "keywords": [ 17 | "ast", 18 | "build" 19 | ], 20 | "dependencies": { 21 | "esprima": "1.0.2", 22 | "escodegen": "0.0.17" 23 | }, 24 | "devDependencies": { 25 | "async": "*", 26 | "tap": "*", 27 | "fstream": "*" 28 | }, 29 | "main": "./lib/codesurgeon", 30 | "scripts": { 31 | "test": "./test/test" 32 | }, 33 | "engines": { "node": ">= 0.8.0" } 34 | } 35 | -------------------------------------------------------------------------------- /test/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/heapwolf/codesurgeon/c4fe37684dd24761e62f7ee8412feab915b3d5bc/test/.DS_Store -------------------------------------------------------------------------------- /test/fixtures/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/heapwolf/codesurgeon/c4fe37684dd24761e62f7ee8412feab915b3d5bc/test/fixtures/.DS_Store -------------------------------------------------------------------------------- /test/fixtures/debuggerStatements.js: -------------------------------------------------------------------------------- 1 | function a() { 2 | debugger; 3 | } 4 | -------------------------------------------------------------------------------- /test/fixtures/functionDeclarations.js: -------------------------------------------------------------------------------- 1 | function a() { 2 | } 3 | 4 | function b() { 5 | } 6 | 7 | function c() { 8 | } -------------------------------------------------------------------------------- /test/fixtures/functionExpressions.js: -------------------------------------------------------------------------------- 1 | 2 | var a = function() { 3 | }; 4 | 5 | var b = function() { 6 | } 7 | 8 | var c = function() { 9 | } -------------------------------------------------------------------------------- /test/fixtures/variableDeclarations.js: -------------------------------------------------------------------------------- 1 | var a = 1; 2 | var b = 2; 3 | var c = 3; 4 | -------------------------------------------------------------------------------- /test/test: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | var fs = require('fs'); 4 | var path = require('path'); 5 | var async = require('async'); 6 | var tap = require('tap').test; 7 | 8 | var pathname = path.join(__dirname, 'tests'); 9 | 10 | var filter = process.argv[2] || null 11 | var caseFilter = process.argv[3] || null 12 | 13 | async.forEach( 14 | fs.readdirSync(pathname), 15 | function (name, nextfile) { 16 | 17 | if (filter && name.indexOf(filter) < 0) { return; } 18 | 19 | if (path.extname(name) === '.js') { 20 | 21 | var basename = path.basename(name, '.js'); 22 | var rawfile = path.join(pathname, basename); 23 | 24 | var tests = require(rawfile); 25 | 26 | async.forEach( 27 | Object.keys(tests), 28 | function(testname, nexttest) { 29 | if (!caseFilter || test.indexOf(caseFilter) > -1) { 30 | tap(testname, function(test) { 31 | tests[testname].call(null, test, nexttest); 32 | }); 33 | } 34 | }, 35 | function(err) { 36 | if (!err) { 37 | console.log(err); 38 | } 39 | nextfile(); 40 | } 41 | ); 42 | } 43 | }, 44 | function(err) { 45 | console.log(err); 46 | } 47 | ); -------------------------------------------------------------------------------- /test/tests/debugger.js: -------------------------------------------------------------------------------- 1 | var path = require('path') 2 | var fstream = require('fstream') 3 | var cs = require(path.join(__dirname, '..', '..', 'lib', 'codesurgeon')) 4 | 5 | module.exports = { 6 | 7 | 'Debugger statements should get removed': function(test, next) { 8 | 9 | test.plan(1) 10 | 11 | var input = path.join(__dirname, '..', 'fixtures', 'debuggerStatements.js') 12 | var output = 'function a() {\n}' 13 | 14 | var filter = cs.filter() 15 | 16 | filter.inclusive = true 17 | 18 | filter.syntax.DebuggerStatement = function(name, item) { 19 | return false 20 | } 21 | 22 | fstream 23 | .Reader(input) 24 | .pipe(filter) 25 | .on('end', function(data) { 26 | 27 | test.equal(data, output) 28 | next() 29 | }) 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /test/tests/declarations.js: -------------------------------------------------------------------------------- 1 | var path = require('path') 2 | var fstream = require('fstream') 3 | var cs = require(path.join(__dirname, '..', '..', 'lib', 'codesurgeon')) 4 | 5 | module.exports = { 6 | 7 | "Include a single variable declaration": function(test, next) { 8 | 9 | test.plan(1) 10 | 11 | var input = path.join(__dirname, '..', 'fixtures', 'variableDeclarations.js') 12 | var output = 'var b = 2;' 13 | 14 | var filter = cs.filter() 15 | 16 | filter.syntax.VariableDeclarator = function(name, item) { 17 | if (name === 'b') { 18 | return true 19 | } 20 | } 21 | 22 | fstream 23 | .Reader(input) 24 | .pipe(filter) 25 | .on('end', function(data) { 26 | test.equal(data, output) 27 | next() 28 | }) 29 | }, 30 | 31 | // "Exclude a single variable declaration": function(test, next) { 32 | 33 | // test.plan(1) 34 | 35 | // var input = path.join(__dirname, '..', 'fixtures', 'variableDeclarations.js') 36 | // var output = 'var a = 1;\nvar c = 3;' 37 | 38 | // var filter = cs.filter() 39 | 40 | // filter.exclusive = true 41 | 42 | // filter.syntax.VariableDeclarator = function(name, item) { 43 | // if (name === 'b') { 44 | // return false 45 | // } 46 | // } 47 | 48 | // fstream 49 | // .Reader(input) 50 | // .pipe(filter) 51 | // .on('end', function(data) { 52 | // test.equal(data, output) 53 | // next(); 54 | // }) 55 | // }, 56 | 57 | // "Include a single function declaration": function(test, next) { 58 | 59 | // test.plan(1) 60 | 61 | // var opts = { include: ['b'] } 62 | // var result = 'function b() {\n}'; 63 | // var file = path.join('fixtures', 'functionDeclarations.js') 64 | 65 | // fstream 66 | // .Reader(file) 67 | // .pipe(filter(opts)) 68 | // .on('end', function(data) { 69 | // test.equal(data, result) 70 | // next() 71 | // }) 72 | // } 73 | }; 74 | 75 | 76 | 77 | 78 | 79 | -------------------------------------------------------------------------------- /test/tests/expressions.js: -------------------------------------------------------------------------------- 1 | var path = require('path') 2 | var fstream = require('fstream') 3 | var filter = require(path.join('..', '..', 'lib', 'codesurgeon')) 4 | 5 | module.exports = { 6 | 7 | "Include a single variable expression": function(test, next) { 8 | 9 | test.plan(1) 10 | } 11 | }; 12 | 13 | 14 | 15 | 16 | 17 | --------------------------------------------------------------------------------