node/tools/eslint/lib/rules/no-trailing-spaces.js
Yosuke Furukawa f9dd34d301 tools: replace closure-linter with eslint
PR-URL: https://github.com/iojs/io.js/pull/1539
Fixes: https://github.com/iojs/io.js/issues/1253
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Reviewed-By: Roman Reiss <me@silverwind.io>
Reviewed-By: Chris Dickinson <christopher.s.dickinson@gmail.com>
Reviewed-By: Johan Bergström <bugs@bergstroem.nu>
Reviewed-By: Fedor Indutny <fedor.indutny@gmail.com>
2015-05-09 12:09:52 +09:00

47 lines
1.6 KiB
JavaScript

/**
* @fileoverview Disallow trailing spaces at the end of lines.
* @author Nodeca Team <https://github.com/nodeca>
*/
"use strict";
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
module.exports = function(context) {
var TRAILER = "[ \t\u00a0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000]+$";
//--------------------------------------------------------------------------
// Public
//--------------------------------------------------------------------------
return {
"Program": function checkTrailingSpaces(node) {
// Let's hack. Since Esprima does not return whitespace nodes,
// fetch the source code and do black magic via regexps.
var src = context.getSource(),
re = new RegExp(TRAILER, "mg"),
match, lines, location;
while ((match = re.exec(src)) !== null) {
lines = src.slice(0, re.lastIndex).split(/\r?\n/g);
location = {
line: lines.length,
column: lines[lines.length - 1].length - match[0].length + 1
};
// Passing node is a bit dirty, because message data will contain
// big text in `source`. But... who cares :) ?
// One more kludge will not make worse the bloody wizardry of this plugin.
context.report(node, location, "Trailing spaces not allowed.");
}
}
};
};