Skip to content

Commit

Permalink
Linting
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed May 8, 2024
1 parent 33c01d2 commit 4dea3a1
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 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 escape ``
# Prefer `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 escape ``. || 🔧 | |
| [prefer-string-raw](docs/rules/prefer-string-raw.md) | Prefer `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
24 changes: 12 additions & 12 deletions rules/prefer-string-raw.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,39 @@ const {isStringLiteral, isDirective} = require('./ast/index.js');
const {fixSpaceAroundKeyword} = require('./fix/index.js');
const {} = require('./utils/index.js');

Check failure on line 4 in rules/prefer-string-raw.js

View workflow job for this annotation

GitHub Actions / lint-test (ubuntu-latest)

Unexpected empty object pattern.


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

const BACKSLASH = '\\'
const BACKSLASH = '\\';

function unescapeBackslash(raw) {
const quote = raw.charAt(0);

raw = raw.slice(1, -1);

let result = '';
for (let position = 0; position < raw.length; position ++) {
for (let position = 0; position < raw.length; position++) {
const character = raw[position];
if (character === BACKSLASH) {
const nextCharacter = raw[position + 1];
switch (nextCharacter) {
case BACKSLASH: {
result += nextCharacter
position++
result += nextCharacter;
position++;
continue;
}

case '\n':
case quote:
case quote: {
continue;
}
// No default
}
}

result += character
result += character;
}

return result;
Expand All @@ -52,7 +53,6 @@ const create = context => {
return;
}


const {raw} = node;
if (
!raw.includes(BACKSLASH + BACKSLASH)
Expand All @@ -64,7 +64,7 @@ const create = context => {

const unescaped = unescapeBackslash(raw);
if (unescaped.endsWith(BACKSLASH) || unescaped !== node.value) {
return
return;
}

return {
Expand All @@ -73,8 +73,8 @@ const create = context => {
* fix(fixer) {
yield fixer.replaceText(node, `String.raw\`${unescaped}\``);
yield * fixSpaceAroundKeyword(fixer, node, context.sourceCode);
}
}
},
};
});
};

Expand Down

0 comments on commit 4dea3a1

Please sign in to comment.