/** * @fileoverview Rule to check for max length on a line. * @author Matt DuVall * @copyright 2013 Matt DuVall. All rights reserved. */ "use strict"; //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ module.exports = function(context) { /** * Creates a string that is made up of repeating a given string a certain * number of times. This uses exponentiation of squares to achieve significant * performance gains over the more traditional implementation of such * functionality. * @param {string} str The string to repeat. * @param {int} num The number of times to repeat the string. * @returns {string} The created string. * @private */ function stringRepeat(str, num) { var result = ""; for (num |= 0; num > 0; num >>>= 1, str += str) { if (num & 1) { result += str; } } return result; } var tabWidth = context.options[1]; var maxLength = context.options[0], tabString = stringRepeat(" ", tabWidth); //-------------------------------------------------------------------------- // Helpers //-------------------------------------------------------------------------- function checkProgramForMaxLength(node) { var lines = context.getSourceLines(); // Replace the tabs // Split (honors line-ending) // Iterate lines.forEach(function(line, i) { if (line.replace(/\t/g, tabString).length > maxLength) { context.report(node, { line: i + 1, col: 1 }, "Line " + (i + 1) + " exceeds the maximum line length of " + maxLength + "."); } }); } //-------------------------------------------------------------------------- // Public API //-------------------------------------------------------------------------- return { "Program": checkProgramForMaxLength }; };