Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed May 8, 2024
1 parent 4dea3a1 commit d48776b
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 9 deletions.
2 changes: 1 addition & 1 deletion docs/rules/prefer-string-raw.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Prefer `String.raw` tag to avoid escaping `\`
# Prefer using `String.raw` tag to avoid escaping `\`

💼 This rule is enabled in the ✅ `recommended` [config](https://github.com/sindresorhus/eslint-plugin-unicorn#preset-configs-eslintconfigjs).

Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ If you don't use the preset, ensure you use the same `env` and `parserOptions` c
| [prefer-set-has](docs/rules/prefer-set-has.md) | Prefer `Set#has()` over `Array#includes()` when checking for existence or non-existence. || 🔧 | 💡 |
| [prefer-set-size](docs/rules/prefer-set-size.md) | Prefer using `Set#size` instead of `Array#length`. || 🔧 | |
| [prefer-spread](docs/rules/prefer-spread.md) | Prefer the spread operator over `Array.from(…)`, `Array#concat(…)`, `Array#{slice,toSpliced}()` and `String#split('')`. || 🔧 | 💡 |
| [prefer-string-raw](docs/rules/prefer-string-raw.md) | Prefer `String.raw` tag to avoid escaping `\`. || 🔧 | |
| [prefer-string-raw](docs/rules/prefer-string-raw.md) | Prefer using `String.raw` tag to avoid escaping `\`. || 🔧 | |
| [prefer-string-replace-all](docs/rules/prefer-string-replace-all.md) | Prefer `String#replaceAll()` over regex searches with the global flag. || 🔧 | |
| [prefer-string-slice](docs/rules/prefer-string-slice.md) | Prefer `String#slice()` over `String#substr()` and `String#substring()`. || 🔧 | |
| [prefer-string-starts-ends-with](docs/rules/prefer-string-starts-ends-with.md) | Prefer `String#startsWith()` & `String#endsWith()` over `RegExp#test()`. || 🔧 | 💡 |
Expand Down
2 changes: 2 additions & 0 deletions rules/ast/is-directive.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

const isDirective = node =>
node.type === 'ExpressionStatement'
&& typeof node.directive === 'string';
Expand Down
7 changes: 3 additions & 4 deletions rules/prefer-string-raw.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
'use strict';
const {isStringLiteral, isDirective} = require('./ast/index.js');
const {fixSpaceAroundKeyword} = require('./fix/index.js');
const {} = require('./utils/index.js');

const MESSAGE_ID = 'prefer-string-raw';
const messages = {
[MESSAGE_ID]: 'Prefer `String.raw` tag to avoid escaping `\\`.',
[MESSAGE_ID]: '`String.raw` should be used to avoid escaping `\\`.',
};

const BACKSLASH = '\\';
Expand Down Expand Up @@ -69,7 +68,7 @@ const create = context => {

return {
node,
message: MESSAGE_ID,
messageId: MESSAGE_ID,
* fix(fixer) {
yield fixer.replaceText(node, `String.raw\`${unescaped}\``);
yield * fixSpaceAroundKeyword(fixer, node, context.sourceCode);
Expand All @@ -84,7 +83,7 @@ module.exports = {
meta: {
type: 'suggestion',
docs: {
description: 'Prefer `String.raw` tag to avoid escaping `\\`.',
description: 'Prefer using `String.raw` tag to avoid escaping `\\`.',
recommended: true,
},
fixable: 'code',
Expand Down
6 changes: 3 additions & 3 deletions test/snapshots/prefer-string-raw.mjs.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Generated by [AVA](https://avajs.dev).
`␊
> 1 | a = 'a\\\\b'␊
| ^^^^^^ prefer-string-raw␊
| ^^^^^^ \`String.raw\` should be used to avoid escaping \`\\\`.
`

## invalid(2): a = {['a\\b']: b}
Expand All @@ -43,7 +43,7 @@ Generated by [AVA](https://avajs.dev).
`␊
> 1 | a = {['a\\\\b']: b}␊
| ^^^^^^ prefer-string-raw␊
| ^^^^^^ \`String.raw\` should be used to avoid escaping \`\\\`.
`

## invalid(3): function a() {return'a\\b'}
Expand All @@ -64,5 +64,5 @@ Generated by [AVA](https://avajs.dev).
`␊
> 1 | function a() {return'a\\\\b'}␊
| ^^^^^^ prefer-string-raw␊
| ^^^^^^ \`String.raw\` should be used to avoid escaping \`\\\`.
`
Binary file modified test/snapshots/prefer-string-raw.mjs.snap
Binary file not shown.

0 comments on commit d48776b

Please sign in to comment.