Skip to content
Permalink
Browse files

Update: Make jsx-quotes fixable (refs #4377)

  • Loading branch information...
gabro committed Jan 13, 2016
1 parent f3dbbd9 commit a2ac359d1d0345cbdc71f6d28292c8160b493afa
Showing with 62 additions and 6 deletions.
  1. +1 −1 docs/rules/README.md
  2. +2 −0 docs/rules/jsx-quotes.md
  3. +15 −3 lib/rules/jsx-quotes.js
  4. +44 −2 tests/lib/rules/jsx-quotes.js
@@ -158,7 +158,7 @@ These rules are purely matters of style and are quite subjective.
* [id-length](id-length.md) - this option enforces minimum and maximum identifier lengths (variable names, property names etc.)
* [id-match](id-match.md) - require identifiers to match the provided regular expression
* [indent](indent.md) - specify tab or space width for your code (fixable)
* [jsx-quotes](jsx-quotes.md) - specify whether double or single quotes should be used in JSX attributes
* [jsx-quotes](jsx-quotes.md) - specify whether double or single quotes should be used in JSX attributes (fixable)
* [key-spacing](key-spacing.md) - enforce spacing between keys and values in object literal properties
* [keyword-spacing](keyword-spacing.md) - enforce spacing before and after keywords (fixable)
* [linebreak-style](linebreak-style.md) - disallow mixed 'LF' and 'CRLF' as linebreaks
@@ -15,6 +15,8 @@ If you want to have e.g. a double quote within a JSX attribute value, you have t
<a b='"' />
```

**Fixable:** This rule is automatically fixable using the `--fix` flag on the command line.

## Rule Details

This rule takes one argument.
@@ -19,11 +19,17 @@ var astUtils = require("../ast-utils");
var QUOTE_SETTINGS = {
"prefer-double": {
quote: "\"",
description: "singlequote"
description: "singlequote",
convert: function(str) {
return str.replace(/'/g, "\"");
}
},
"prefer-single": {
quote: "'",
description: "doublequote"
description: "doublequote",
convert: function(str) {
return str.replace(/"/g, "'");
}
}
};

@@ -50,7 +56,13 @@ module.exports = function(context) {
var attributeValue = node.value;

if (attributeValue && astUtils.isStringLiteral(attributeValue) && !usesExpectedQuotes(attributeValue)) {
context.report(attributeValue, "Unexpected usage of {{description}}.", setting);
context.report({
node: attributeValue,
message: "Unexpected usage of " + setting.description + ".",
fix: function(fixer) {
return fixer.replaceText(attributeValue, setting.convert(attributeValue.raw));
}
});
}
}
};
@@ -24,6 +24,11 @@ ruleTester.run("jsx-quotes", rule, {
code: "<foo bar='\"' />",
parserOptions: { ecmaVersion: 6, ecmaFeatures: { jsx: true } }
},
{
code: "<foo bar=\"'\" />",
options: [ "prefer-single" ],
parserOptions: { ecmaVersion: 6, ecmaFeatures: { jsx: true } }
},
{
code: "<foo bar='baz' />",
options: [ "prefer-single" ],
@@ -54,6 +59,24 @@ ruleTester.run("jsx-quotes", rule, {
{
code: "<foo bar />",
parserOptions: { ecmaVersion: 6, ecmaFeatures: { jsx: true } }
},
{
code: "<foo bar='&quot;' />",
options: [ "prefer-single" ],
parserOptions: { ecmaVersion: 6, ecmaFeatures: { jsx: true } }
},
{
code: "<foo bar=\"&quot;\" />",
parserOptions: { ecmaVersion: 6, ecmaFeatures: { jsx: true } }
},
{
code: "<foo bar='&#39;' />",
options: [ "prefer-single" ],
parserOptions: { ecmaVersion: 6, ecmaFeatures: { jsx: true } }
},
{
code: "<foo bar=\"&#39;\" />",
parserOptions: { ecmaVersion: 6, ecmaFeatures: { jsx: true } }
}
],
invalid: [
@@ -62,15 +85,34 @@ ruleTester.run("jsx-quotes", rule, {
parserOptions: { ecmaVersion: 6, ecmaFeatures: { jsx: true } },
errors: [
{ message: "Unexpected usage of singlequote.", line: 1, column: 10, type: "Literal" }
]
],
output: "<foo bar=\"baz\" />"
},
{
code: "<foo bar=\"baz\" />",
options: [ "prefer-single" ],
parserOptions: { ecmaVersion: 6, ecmaFeatures: { jsx: true } },
errors: [
{ message: "Unexpected usage of doublequote.", line: 1, column: 10, type: "Literal" }
]
],
output: "<foo bar='baz' />"
},
{
code: "<foo bar=\"&quot;\" />",
options: [ "prefer-single" ],
parserOptions: { ecmaVersion: 6, ecmaFeatures: { jsx: true } },
errors: [
{ message: "Unexpected usage of doublequote.", line: 1, column: 10, type: "Literal" }
],
output: "<foo bar='&quot;' />"
},
{
code: "<foo bar=\'&#39;\' />",
parserOptions: { ecmaVersion: 6, ecmaFeatures: { jsx: true } },
errors: [
{ message: "Unexpected usage of singlequote.", line: 1, column: 10, type: "Literal" }
],
output: "<foo bar=\"&#39;\" />"
}
]
});

0 comments on commit a2ac359

Please sign in to comment.
You can’t perform that action at this time.