Skip to content

Commit

Permalink
New: wrong-quotes-for-template-string rules (fixes eslint#6186)
Browse files Browse the repository at this point in the history
  • Loading branch information
jfmengels committed Jul 26, 2016
1 parent 6f3faa4 commit 44e68ce
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 0 deletions.
1 change: 1 addition & 0 deletions conf/eslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@
"vars-on-top": "off",
"wrap-iife": "off",
"wrap-regex": "off",
"wrong-quotes-for-template-string": "off",
"yield-star-spacing": "off",
"yoda": "off"
}
Expand Down
33 changes: 33 additions & 0 deletions docs/rules/wrong-quotes-for-template-string.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Warn when using template string syntax in regular strings (wrong-quotes-for-template-string)

ECMAScript 6 allows programmers to create strings containing variable or expressions using template literals, instead of string concatenation, by writing expressions like `${variable}` between two backtick quotes (\`). It can be easy to use the wrong quotes when wanting to use template literals, and end up with the literal value `"${variable}"` instead of a string containing the value of the injected expressions.


## Rule Details

This rule aims to warn programmers of a potential quotes mismatch. It will warn when it finds a string containing the template literal place holder (`${something}`) that uses either `\"` or `\'` for the quotes.

## Examples

Examples of **incorrect** code for this rule:

```js
/*eslint wrong-quotes-for-template-string: "error"*/
"Hello ${name}!";
'Hello ${name}!';
"Time: ${12 * 60 * 60 * 1000}";
```

Examples of **correct** code for this rule:

```js
/*eslint wrong-quotes-for-template-string: "error"*/
`Hello ${name}!`;
`Time: ${12 * 60 * 60 * 1000}`;

templateFunction`Hello ${name}`;
```

## When Not To Use It

This rule should not be used in ES3/5 environments.
34 changes: 34 additions & 0 deletions lib/rules/wrong-quotes-for-template-string.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* @fileoverview Warn when using template string syntax in regular strings
* @author Jeroen Engels
*/
"use strict";

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------

module.exports = {
meta: {
docs: {
description: "Warn when using template string syntax in regular strings",
category: "Possible Errors",
recommended: true
},

schema: []
},

create: function(context) {
var regex = /\$\{.*\}/;

return {
Literal: function(node) {
if (typeof node.value === "string" && regex.test(node.value)) {
context.report(node, "Possible use of template string inside a regular string.");
}
}
};

}
};
79 changes: 79 additions & 0 deletions tests/lib/rules/wrong-quotes-for-template-string.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* @fileoverview Warn when using template string syntax in regular strings.
* @author Jeroen Engels
*/
"use strict";

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

var rule = require("../../../lib/rules/wrong-quotes-for-template-string"),
RuleTester = require("../../../lib/testers/rule-tester");


//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------

var ruleTester = new RuleTester();

var message = "Possible use of template string inside a regular string.";
var parserOptions = { ecmaVersion: 6 };

ruleTester.run("wrong-quotes-for-template-string", rule, {
valid: [
{ code: "`Hello, ${name}`;", parserOptions },
{ code: "templateFunction`Hello, ${name}`;", parserOptions },
{ code: "`Hello, name`;", parserOptions },
{ code: "'Hello, name';", parserOptions },
{ code: "'Hello, ' + name;", parserOptions },
{ code: "`Hello, ${index + 1}`", parserOptions },
{ code: "`Hello, ${name + \" foo\"}`", parserOptions },
{ code: "`Hello, ${name || \"foo\"}`", parserOptions },
{ code: "`Hello, ${{foo: \"bar\"}.foo}`", parserOptions },
{ code: "'$2'", parserOptions },
{ code: "'${'", parserOptions },
{ code: "'$}'", parserOptions },
{ code: "'{foo}'", parserOptions },
{ code: "'{foo: \"bar\"}'", parserOptions },
{ code: "const number = 3", parserOptions }
],
invalid: [
{
code: "'Hello, ${name}'",
parserOptions,
errors: [{ message }]
},
{
code: "\"Hello, ${name}\"",
parserOptions,
errors: [{ message }]
},
{
code: "'${greeting}, ${name}'",
parserOptions,
errors: [{ message }]
},
{
code: "'Hello, ${index + 1}'",
parserOptions,
errors: [{ message }]
},
{
code: "'Hello, ${name + \" foo\"}'",
parserOptions,
errors: [{ message }]
},
{
code: "'Hello, ${name || \"foo\"}'",
parserOptions,
errors: [{ message }]
},
{
code: "'Hello, ${{foo: \"bar\"}.foo}'",
parserOptions,
errors: [{ message }]
}
]
});

0 comments on commit 44e68ce

Please sign in to comment.