forked from eslint/eslint
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New: wrong-quotes-for-template-string rules (fixes eslint#6186)
- Loading branch information
Showing
4 changed files
with
147 additions
and
0 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
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. |
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,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."); | ||
} | ||
} | ||
}; | ||
|
||
} | ||
}; |
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,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 }] | ||
} | ||
] | ||
}); |