jsx-quotes.js
2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/**
* @fileoverview Enforce props quotes style
* @author Matt DuVall <http://www.mattduvall.com/>, Brandon Payton, Yannick Croissant
*/
'use strict';
// ------------------------------------------------------------------------------
// Constants
// ------------------------------------------------------------------------------
var QUOTE_SETTINGS = {
double: {
quote: '"',
alternateQuote: '\'',
description: 'doublequote'
},
single: {
quote: '\'',
alternateQuote: '"',
description: 'singlequote'
}
};
var AVOID_ESCAPE = 'avoid-escape';
var isWarnedForDeprecation = false;
// ------------------------------------------------------------------------------
// 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;
}
return {
Program: function() {
if (isWarnedForDeprecation || /\=-(f|-format)=/.test(process.argv.join('='))) {
return;
}
/* eslint-disable no-console */
console.log('The react/jsx-quotes rule is deprecated. Please use the jsx-quotes rule instead.');
/* eslint-enable no-console */
isWarnedForDeprecation = true;
},
Literal: function(node) {
if (node.parent.type !== 'JSXAttribute') {
return;
}
var val = node.value;
var rawVal = node.raw;
var quoteOption = context.options[0];
var settings = QUOTE_SETTINGS[quoteOption];
var avoidEscape = context.options[1] === AVOID_ESCAPE;
var isValid;
if (settings && typeof val === 'string') {
isValid = isSurroundedBy(rawVal, settings.quote);
if (!isValid && avoidEscape) {
isValid = isSurroundedBy(rawVal, settings.alternateQuote) && rawVal.indexOf(settings.quote) >= 0;
}
if (!isValid) {
context.report(node, 'JSX attributes must use ' + settings.description + '.');
}
}
}
};
};
module.exports.schema = [{
enum: ['single', 'double']
}, {
enum: ['avoid-escape']
}];