summaryrefslogtreecommitdiff
path: root/tools/eslint/lib/rules/quotes.js
diff options
context:
space:
mode:
Diffstat (limited to 'tools/eslint/lib/rules/quotes.js')
-rw-r--r--tools/eslint/lib/rules/quotes.js83
1 files changed, 83 insertions, 0 deletions
diff --git a/tools/eslint/lib/rules/quotes.js b/tools/eslint/lib/rules/quotes.js
new file mode 100644
index 0000000000..2bb10ea5e4
--- /dev/null
+++ b/tools/eslint/lib/rules/quotes.js
@@ -0,0 +1,83 @@
+/**
+ * @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],
+ 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 + ".");
+ }
+ }
+ }
+ };
+
+};