Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

238 lines (190 sloc) 6.51 kB
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>The source code</title>
<link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="../resources/prettify/prettify.js"></script>
<style type="text/css">
.highlight { display: block; background-color: #ddd; }
</style>
<script type="text/javascript">
function highlight() {
document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
}
</script>
</head>
<body onload="prettyPrint(); highlight();">
<pre class="prettyprint lang-js"><span id='LintRoller-util'>/**
</span> * @class LintRoller.util
* @singleton
*
* Some additional utilities for cleaning up JavaScript lint.
*/
util = {
<span id='LintRoller-util-method-init'> /**
</span> * @private
*/
init : function (parent) {
this.parent = parent;
return this;
},
<span id='LintRoller-util-method-utilInit'> /**
</span> * @private
*/
utilInit : function(config) {
this.parent.initConfigs(config);
this.parent.parseTree(config.filepaths);
this.parent.log('\nFilesystem has been parsed. Looping through available files...', true);
},
<span id='LintRoller-util-method-removeBOM'> /**
</span> * A utility that will remove all Byte-Order-Marks (BOM - \uFEFF) from a file
*/
removeBOM : function(config) {
this.utilInit(config);
var msg = 'Unsafe character.';
var offendingFiles = this.locateOffenders(msg);
//HOW MANY ERRORS?!?
this.parent.log(
'Found ' + offendingFiles.length +
' files matching the error &quot;' + msg +
'&quot; between ' + this.parent.linters.length + ' linters.\n',
true
);
if (offendingFiles.length === 0) {
this.parent.log(
'LintRoller has found 0 offending files. Your usage of tabs/spaces is acceptable!\n',
true
);
process.exit(0);
}
try {
this.fixBomFiles(offendingFiles);
}
catch (e) {
this.parent.log(
'An error has occurred: ' + e,
true
);
process.exit(1);
}
this.parent.log(
'LintRoller has attempted to remove all BOM.\n',
true
);
process.exit(0);
},
<span id='LintRoller-util-method-replaceTabsWithSpaces'> /**
</span> * A utility that will eliminate all &quot;Mixed spaces with tabs.&quot; warnings by replacing tab characters with spaces.
*
* @param {Object} config A configuration object
* @param {Number} spacingChars The number of spaces to replace a tab
*/
replaceTabsWithSpaces : function (config, spacingChars) {
this.utilInit(config);
var msg = 'Mixed spaces and tabs.';
var offendingFiles = this.locateOffenders(msg);
//HOW MANY ERRORS?!?
this.parent.log(
'Found ' + offendingFiles.length +
' files matching the error &quot;' + msg +
'&quot; between ' + this.parent.linters.length + ' linters.\n',
true
);
if (offendingFiles.length === 0) {
this.parent.log(
'LintRoller has found 0 offending files. Your usage of tabs/spaces is acceptable!\n',
true
);
process.exit(0);
}
try {
this.fixOffendingFiles(offendingFiles, spacingChars);
}
catch (e) {
this.parent.log(
'An error has occurred: ' + e,
true
);
process.exit(1);
}
this.parent.log(
'LintRoller has attempted to replace all tabs with ' + spacingChars + ' spaces.\n',
true
);
process.exit(0);
},
<span id='LintRoller-util-method-locateOffenders'> /**
</span> * @private
*/
locateOffenders : function (msg) {
var offendingFiles = [],
linter, newFiles, i;
//find offending files
for (i = 0; i &lt; this.parent.linters.length; i++) {
linter = this.parent.linters[i];
newFiles = this.findLintErrors(linter.lib, linter.options, msg);
offendingFiles = offendingFiles.concat(newFiles);
}
return offendingFiles;
},
<span id='LintRoller-util-method-fixOffendingFiles'> /**
</span> * @private
*/
fixOffendingFiles : function(offendingFiles, spacingChars) {
var i = 0,
spaces = '',
file, js;
for (i; i&lt;spacingChars; i++) {
spaces += ' ';
}
for (i=0; i &lt; offendingFiles.length; i++) {
file = offendingFiles[i];
js = this.parent.fs.readFileSync(file, 'utf8');
js = js.replace(/\t/g, spaces);
this.parent.fs.writeFileSync(file, js, 'utf8');
}
},
<span id='LintRoller-util-method-fixBomFiles'> /**
</span> * @private
*/
fixBomFiles : function(offendingFiles) {
var i = 0,
file, js;
for (i=0; i &lt; offendingFiles.length; i++) {
file = offendingFiles[i];
js = this.parent.fs.readFileSync(file, 'utf8');
js = js.replace(/\uFEFF/g, '');
this.parent.fs.writeFileSync(file, js, 'utf8');
}
},
<span id='LintRoller-util-method-findLintErrors'> /**
</span> * @private
*/
findLintErrors : function (linter, options, msg) {
var j = 0,
offendingFiles = [],
file, js;
for (j; j &lt; this.parent.files.length; j++) {
file = this.parent.files[j];
js = this.parent.fs.readFileSync(file, 'utf8');
var i = 0,
result = linter(js, options),
totalErrors = linter.errors.length,
error;
if (!result) {
for (i; i &lt; totalErrors; i++) {
error = linter.errors[i];
if (error &amp;&amp; error.reason === msg) {
offendingFiles.push(file);
break;
}
}
}
}
return offendingFiles;
}
};
module.exports = util;</pre>
</body>
</html>
Jump to Line
Something went wrong with that request. Please try again.