node/tools/eslint/lib/rules/quotes.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

92 lines
2.6 KiB
JavaScript

/**
* @fileoverview A rule to choose between single and double quote marks
* @author Matt DuVall <http://www.mattduvall.com/>, Brandon Payton
*/
"use strict";
//------------------------------------------------------------------------------
// Constants
//------------------------------------------------------------------------------
var QUOTE_SETTINGS = {
"double": {
quote: "\"",
alternateQuote: "'",
description: "doublequote"
},
"single": {
quote: "'",
alternateQuote: "\"",
description: "singlequote"
},
"backtick": {
quote: "`",
alternateQuote: "\"",
description: "backtick"
}
};
var AVOID_ESCAPE = "avoid-escape";
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
module.exports = function(context) {
/**
* Validate that a string passed in is surrounded by the specified character
* @param {string} val The text to check.
* @param {string} character The character to see if it's surrounded by.
* @returns {boolean} True if the text is surrounded by the character, false if not.
* @private
*/
function isSurroundedBy(val, character) {
return val[0] === character && val[val.length - 1] === character;
}
/**
* Determines if a given node is part of JSX syntax.
* @param {ASTNode} node The node to check.
* @returns {boolean} True if the node is a JSX node, false if not.
* @private
*/
function isJSXElement(node) {
return node.type.indexOf("JSX") === 0;
}
return {
"Literal": function(node) {
var val = node.value,
rawVal = node.raw,
quoteOption = context.options[0],
settings = QUOTE_SETTINGS[quoteOption || "double"],
avoidEscape = context.options[1] === AVOID_ESCAPE,
isValid;
if (settings && typeof val === "string") {
isValid = isJSXElement(node.parent) || isSurroundedBy(rawVal, settings.quote);
if (!isValid && avoidEscape) {
isValid = isSurroundedBy(rawVal, settings.alternateQuote) && rawVal.indexOf(settings.quote) >= 0;
}
if (!isValid) {
context.report(node, "Strings must use " + settings.description + ".");
}
}
}
};
};
module.exports.schema = [
{
"enum": ["single", "double", "backtick"]
},
{
"enum": ["avoid-escape"]
}
];