This repository has been archived by the owner on Jan 6, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Rajiv Tirumalareddy
committed
Feb 22, 2015
1 parent
5020e42
commit 98d8b2d
Showing
10 changed files
with
184 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* jshint ignore:start */ | ||
// copied and modified from https://github.com/jshint/fixmyjs/blob/v2.0/lib/index.js | ||
// because they don't allow custom rules to be specified | ||
|
||
var fu = require('fu'); | ||
var recast = require('recast'); | ||
var traverse = require('./traverse'); | ||
var SHEBANG = /^(\#\!.*)\n/; | ||
|
||
function getRules(options) { | ||
return require('./rules').map(function (rule) { | ||
return rule(options); | ||
}); | ||
} | ||
|
||
function fix(code, config) { | ||
config = config || {}; | ||
|
||
var shebang = SHEBANG.exec(code); | ||
var pureCode = code.replace(SHEBANG, ''); | ||
var ast; | ||
try { | ||
ast = recast.parse(pureCode); | ||
} catch (e) { | ||
if (e.message === 'AST contains no nodes at all?') { | ||
return code; | ||
} | ||
throw e; | ||
} | ||
var rules = getRules(config); | ||
var options = { wrapColumn: Infinity }; | ||
|
||
var modifiedTree = traverse(ast, function (node, parent) { | ||
return fu.foldl(function (node, f) { | ||
return node && f.hasOwnProperty(node.type) | ||
? f[node.type](node, parent) | ||
: node; | ||
}, rules, node); | ||
}); | ||
|
||
var generatedCode = recast.print(modifiedTree, options).code; | ||
|
||
return shebang === null | ||
? generatedCode | ||
: [shebang[1], generatedCode].join('\n'); | ||
} | ||
/* jshint ignore:end */ | ||
|
||
module.exports = fix; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
module.exports = [ | ||
require('./stripFnCall'), | ||
require('./stripFnDecl') | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
module.exports = function (opts) { | ||
return opts.strip ? { | ||
ExpressionStatement: stripFnCalls.bind(stripFnCalls, opts.strip) | ||
} : {}; | ||
}; | ||
|
||
function stripFnCalls (toStrip, node) { | ||
if (!node.expression.type || | ||
node.expression.type !== 'CallExpression' || | ||
!node.expression.callee || | ||
node.expression.callee.type !== 'Identifier' || | ||
toStrip.indexOf(node.expression.callee.name) === -1) { | ||
return node; | ||
} | ||
|
||
return []; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
module.exports = function (opts) { | ||
return opts.strip ? { | ||
VariableDeclarator: stripFnDecl.bind(stripFnDecl, opts.strip) | ||
} : {}; | ||
}; | ||
|
||
var recast = require('recast'); | ||
var b = recast.types.builders; | ||
|
||
var emptyFnNode = b.functionExpression( | ||
null, | ||
[], | ||
{ | ||
type: 'BlockStatement', | ||
body: [] | ||
}); | ||
|
||
function stripFnDecl (toStrip, node) { | ||
if (node.id.type !== 'Identifier' || | ||
toStrip.indexOf(node.id.name) === -1) { | ||
return node; | ||
} | ||
|
||
node.init = emptyFnNode; | ||
|
||
return node; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* jshint ignore:start */ | ||
/* globals toString */ | ||
/* from https://github.com/jshint/fixmyjs/blob/v2.0/lib/traverse.js*/ | ||
module.exports = traverse | ||
|
||
var fu = require('fu') | ||
|
||
function traverse(o, f, p) { | ||
var k | ||
|
||
function make(parent) { | ||
return function (node) { | ||
var next = traverse(node, f, parent) | ||
next === node || (next._fixmyjs = 1) | ||
return next | ||
} | ||
} | ||
|
||
if (o === undefined) { | ||
return o | ||
} | ||
|
||
for (k in o) { | ||
var call = make(o) | ||
|
||
if (toString.call(o[k]) == '[object Object]') { | ||
o[k] = call(o[k]) | ||
} else if (Array.isArray(o[k])) { | ||
o[k] = fu.concatMap(call, o[k]) | ||
} | ||
} | ||
|
||
return f(o, p) | ||
} | ||
/* jshint ignore:end */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters