node/tools/eslint/lib/rules/linebreak-style.js
Roman Reiss d91e10b3bd tools: update eslint to 0.24.0
PR-URL: https://github.com/nodejs/io.js/pull/2072
Reviewed-By: Yosuke Furukawa <yosuke.furukawa@gmail.com>
Reviewed-by: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Alex Kocharin <alex@kocharin.ru>
2015-06-29 19:02:17 +02:00

44 lines
1.4 KiB
JavaScript

/**
* @fileoverview Rule to forbid mixing LF and LFCR line breaks.
* @author Erik Mueller
* @copyright 2015 Varun Verma. All rights reserverd.
* @copyright 2015 James Whitney. All rights reserved.
* @copyright 2015 Erik Mueller. All rights reserved.
*/
"use strict";
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
module.exports = function (context) {
var EXPECTED_LF_MSG = "Expected linebreaks to be 'LF' but found 'CRLF'.",
EXPECTED_CRLF_MSG = "Expected linebreaks to be 'CRLF' but found 'LF'.";
return {
"Program": function checkForlinebreakStyle(node) {
var linebreakStyle = context.options[0] || "unix",
expectedLF = linebreakStyle === "unix",
linebreaks = context.getSource().match(/\r\n|\r|\n|\u2028|\u2029/g),
lineOfError = -1;
if (linebreaks !== null) {
lineOfError = linebreaks.indexOf(expectedLF ? "\r\n" : "\n");
}
if (lineOfError !== -1) {
context.report(node, {
line: lineOfError + 1,
column: context.getSourceLines()[lineOfError].length
}, expectedLF ? EXPECTED_LF_MSG : EXPECTED_CRLF_MSG);
}
}
};
};
module.exports.schema = [
{
"enum": ["unix", "windows"]
}
];