Skip to content
This repository was archived by the owner on Mar 23, 2024. It is now read-only.

Commit 0318aa5

Browse files
committed
validateQuoteMarks: add "ignoreJSX" value
Fixes #1953 Closes gh-1962
1 parent 2665494 commit 0318aa5

2 files changed

Lines changed: 50 additions & 1 deletion

File tree

lib/rules/validate-quote-marks.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* - `Object`:
1111
* - `escape`: allow the "other" quote mark to be used, but only to avoid having to escape
1212
* - `mark`: the same effect as the non-object values
13+
* - `ignoreJSX`: ignore JSX nodes
1314
*
1415
* JSHint: [`quotmark`](http://jshint.com/docs/options/#quotmark)
1516
*
@@ -28,13 +29,20 @@
2829
* var x = "x";
2930
* var y = '"x"';
3031
* ```
32+
*
3133
* ##### Invalid example for mode `{ "mark": "\"", "escape": true }`
3234
*
3335
* ```js
3436
* var x = "x";
3537
* var y = 'x';
3638
* ```
3739
*
40+
* ##### Valid example for mode `{ "mark": "'", "escape": true, "ignoreJSX": true }`
41+
*
42+
* ```js
43+
* <div className="flex-card__header">{this.props.children}</div>;
44+
* ```
45+
*
3846
* ##### Valid example for mode `"\""` or mode `true`
3947
*
4048
* ```js
@@ -62,13 +70,19 @@ module.exports.prototype = {
6270

6371
configure: function(quoteMark) {
6472
this._allowEscape = false;
73+
this._ignoreJSX = false;
6574

6675
if (typeof quoteMark === 'object') {
6776
assert(
6877
typeof quoteMark.escape === 'boolean' && quoteMark.mark !== undefined,
6978
this.getOptionName() + ' option requires the "escape" and "mark" property to be defined'
7079
);
7180
this._allowEscape = quoteMark.escape;
81+
82+
if (quoteMark.ignoreJSX) {
83+
this._ignoreJSX = quoteMark.ignoreJSX;
84+
}
85+
7286
quoteMark = quoteMark.mark;
7387
}
7488

@@ -77,6 +91,11 @@ module.exports.prototype = {
7791
this.getOptionName() + ' option requires \'"\', "\'", or boolean true'
7892
);
7993

94+
assert(
95+
quoteMark === '"' || quoteMark === '\'' || quoteMark === true,
96+
this.getOptionName() + ' option requires \'"\', "\'", or boolean true'
97+
);
98+
8099
this._quoteMark = quoteMark;
81100
},
82101

@@ -87,12 +106,20 @@ module.exports.prototype = {
87106
check: function(file, errors) {
88107
var quoteMark = this._quoteMark;
89108
var allowEscape = this._allowEscape;
109+
var ignoreJSX = this._ignoreJSX;
110+
90111
var opposite = {
91112
'"': '\'',
92113
'\'': '"'
93114
};
94-
95115
file.iterateTokensByType('String', function(token) {
116+
if (
117+
ignoreJSX &&
118+
file.getNodeByRange(token.range[0]).parentNode.type === 'JSXAttribute'
119+
) {
120+
return;
121+
}
122+
96123
var str = token.value;
97124
var mark = str[0];
98125
var stripped = str.substring(1, str.length - 1);

test/specs/rules/validate-quote-marks.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,28 @@ describe('rules/validate-quote-marks', function() {
2424
});
2525
});
2626

27+
describe('JSX', function() {
28+
it('should not report any errors for JSX attribute', function() {
29+
checker.configure({
30+
validateQuoteMarks: { mark: '\'', ignoreJSX: true, escape: true },
31+
esnext: true
32+
});
33+
34+
var str = '<div className="flex-card__header">{this.props.children}</div>;';
35+
expect(checker.checkString(str)).to.not.have.errors();
36+
});
37+
38+
it('should report errors for JSX attribute', function() {
39+
checker.configure({
40+
validateQuoteMarks: { mark: '\'', escape: true },
41+
esnext: true
42+
});
43+
44+
var str = '<div className="flex-card__header">{this.props.children}</div>;';
45+
expect(checker.checkString(str)).to.have.errors.from('validateQuoteMarks');
46+
});
47+
});
48+
2749
describe('option value \' ', function() {
2850
beforeEach(function() {
2951
checker.configure({ validateQuoteMarks: '\'' });

0 commit comments

Comments
 (0)