-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
214 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# Enforce JSX Quote Style (jsx-quotes) | ||
|
||
JSX attribute values can contain string literals, which are delimited with single or double quotes. | ||
|
||
```js | ||
<a b='c' /> | ||
<a b="c" /> | ||
``` | ||
|
||
Unlike string literals in JavaScript, string literals within JSX attributes can’t contain escaped quotes. | ||
If you want to have e.g. a double quote within a JSX attribute value, you have to use single quotes as string delimiter. | ||
|
||
```js | ||
<a b="'" /> | ||
<a b='"' /> | ||
``` | ||
|
||
## Rule Details | ||
|
||
This rule takes one argument. | ||
If it is `"prefer-double"` then the rule enforces the usage of double quotes for all JSX attribute values which doesn’t contain a double quote. | ||
If `"prefer-single"` is configured then the rule enforces the usage of single quotes for all JSX attribute values which doesn’t contain a single quote. | ||
|
||
The default is `"prefer-double"`. | ||
|
||
The following patterns are considered warnings when set to `"prefer-double"`: | ||
|
||
```js | ||
<a b='c' /> | ||
``` | ||
|
||
The following patterns are not considered warnings when set to `"prefer-double"`: | ||
|
||
```js | ||
<a b="c" /> | ||
<a b='"' /> | ||
``` | ||
|
||
The following patterns are considered warnings when set to `"prefer-single"`: | ||
|
||
```js | ||
<a b="c" /> | ||
``` | ||
|
||
The following patterns are not considered warnings when set to `"prefer-single"`: | ||
|
||
```js | ||
<a b='c' /> | ||
<a b="'" /> | ||
``` | ||
|
||
## When Not To Use It | ||
|
||
You can turn this rule off if you don’t use JSX or if you aren’t concerned with a consistent usage of quotes within JSX attributes. | ||
|
||
## Related Rules | ||
|
||
* [quotes](quotes.md) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/** | ||
* @fileoverview A rule to ensure consistent quotes used in jsx syntax. | ||
* @author Mathias Schreck <https://github.com/lo1tuma> | ||
* @copyright 2015 Mathias Schreck | ||
*/ | ||
|
||
"use strict"; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Requirements | ||
//------------------------------------------------------------------------------ | ||
|
||
var astUtils = require("../ast-utils"); | ||
|
||
//------------------------------------------------------------------------------ | ||
// Constants | ||
//------------------------------------------------------------------------------ | ||
|
||
var QUOTE_SETTINGS = { | ||
"prefer-double": { | ||
quote: "\"", | ||
description: "singlequote" | ||
}, | ||
"prefer-single": { | ||
quote: "'", | ||
description: "doublequote" | ||
} | ||
}; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
//------------------------------------------------------------------------------ | ||
|
||
module.exports = function(context) { | ||
var quoteOption = context.options[0] || "prefer-double", | ||
setting = QUOTE_SETTINGS[quoteOption]; | ||
|
||
/** | ||
* Checks if the given string literal node uses the expected quotes | ||
* @param {ASTNode} node - A string literal node. | ||
* @returns {boolean} Whether or not the string literal used the expected quotes. | ||
* @public | ||
*/ | ||
function usesExpectedQuotes(node) { | ||
return node.value.indexOf(setting.quote) !== -1 || astUtils.isSurroundedBy(node.raw, setting.quote); | ||
} | ||
|
||
return { | ||
"JSXAttribute": function(node) { | ||
var attributeValue = node.value; | ||
|
||
if (astUtils.isStringLiteral(attributeValue) && !usesExpectedQuotes(attributeValue)) { | ||
context.report(attributeValue, "Unexpected usage of {{description}}.", setting); | ||
} | ||
} | ||
}; | ||
}; | ||
|
||
module.exports.schema = [ | ||
{ | ||
"enum": [ "prefer-single", "prefer-double" ] | ||
} | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/** | ||
* @fileoverview Tests for jsx-quotes rule. | ||
* @author Mathias Schreck <https://github.com/lo1tuma> | ||
* @copyright 2015 Mathias Schreck | ||
*/ | ||
|
||
"use strict"; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Requirements | ||
//------------------------------------------------------------------------------ | ||
|
||
var rule = require("../../../lib/rules/jsx-quotes"), | ||
RuleTester = require("../../../lib/testers/rule-tester"); | ||
|
||
var ruleTester = new RuleTester(); | ||
ruleTester.run("jsx-quotes", rule, { | ||
valid: [ | ||
{ | ||
code: "<foo bar=\"baz\" />", | ||
ecmaFeatures: { jsx: true } | ||
}, | ||
{ | ||
code: "<foo bar='\"' />", | ||
ecmaFeatures: { jsx: true } | ||
}, | ||
{ | ||
code: "<foo bar='baz' />", | ||
options: [ "prefer-single" ], | ||
ecmaFeatures: { jsx: true } | ||
}, | ||
{ | ||
code: "<foo bar=\"baz\">\"</foo>", | ||
ecmaFeatures: { jsx: true } | ||
}, | ||
{ | ||
code: "<foo bar='baz'>'</foo>", | ||
options: [ "prefer-single" ], | ||
ecmaFeatures: { jsx: true } | ||
}, | ||
{ | ||
code: "<foo bar={'baz'} />", | ||
ecmaFeatures: { jsx: true } | ||
}, | ||
{ | ||
code: "<foo bar={\"baz\"} />", | ||
options: [ "prefer-single" ], | ||
ecmaFeatures: { jsx: true } | ||
}, | ||
{ | ||
code: "<foo bar={baz} />", | ||
ecmaFeatures: { jsx: true } | ||
} | ||
], | ||
invalid: [ | ||
{ | ||
code: "<foo bar=\'baz\' />", | ||
ecmaFeatures: { jsx: true }, | ||
errors: [ | ||
{ message: "Unexpected usage of singlequote.", line: 1, column: 10, type: "Literal" } | ||
] | ||
}, | ||
{ | ||
code: "<foo bar=\"baz\" />", | ||
options: [ "prefer-single" ], | ||
ecmaFeatures: { jsx: true }, | ||
errors: [ | ||
{ message: "Unexpected usage of doublequote.", line: 1, column: 10, type: "Literal" } | ||
] | ||
} | ||
] | ||
}); |