-
-
Notifications
You must be signed in to change notification settings - Fork 366
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Sindre Sorhus <[email protected]>
- Loading branch information
1 parent
aabcf1d
commit 4f1400a
Showing
24 changed files
with
571 additions
and
303 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Prefer using the `String.raw` tag to avoid escaping `\` | ||
|
||
💼 This rule is enabled in the ✅ `recommended` [config](https://github.com/sindresorhus/eslint-plugin-unicorn#preset-configs-eslintconfigjs). | ||
|
||
🔧 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). | ||
|
||
<!-- end auto-generated rule header --> | ||
<!-- Do not manually modify this header. Run: `npm run fix:eslint-docs` --> | ||
|
||
[`String.raw`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/raw) can be used to avoid escaping `\`. | ||
|
||
## Fail | ||
|
||
```js | ||
const file = "C:\\windows\\style\\path\\to\\file.js"; | ||
``` | ||
|
||
```js | ||
const regexp = new RegExp('foo\\.bar'); | ||
``` | ||
|
||
## Pass | ||
|
||
```js | ||
const file = String.raw`C:\windows\style\path\to\file.js`; | ||
``` | ||
|
||
```js | ||
const regexp = new RegExp(String.raw`foo\.bar`); | ||
``` |
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
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,7 @@ | ||
'use strict'; | ||
|
||
const isDirective = node => | ||
node.type === 'ExpressionStatement' | ||
&& typeof node.directive === 'string'; | ||
|
||
module.exports = isDirective; |
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
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,93 @@ | ||
'use strict'; | ||
const {isStringLiteral, isDirective} = require('./ast/index.js'); | ||
const {fixSpaceAroundKeyword} = require('./fix/index.js'); | ||
|
||
const MESSAGE_ID = 'prefer-string-raw'; | ||
const messages = { | ||
[MESSAGE_ID]: '`String.raw` should be used to avoid escaping `\\`.', | ||
}; | ||
|
||
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++) { | ||
const character = raw[position]; | ||
if (character === BACKSLASH) { | ||
const nextCharacter = raw[position + 1]; | ||
if (nextCharacter === BACKSLASH || nextCharacter === quote) { | ||
result += nextCharacter; | ||
position++; | ||
continue; | ||
} | ||
} | ||
|
||
result += character; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
/** @param {import('eslint').Rule.RuleContext} context */ | ||
const create = context => { | ||
context.on('Literal', node => { | ||
if ( | ||
!isStringLiteral(node) | ||
|| isDirective(node.parent) | ||
|| ( | ||
( | ||
node.parent.type === 'ImportDeclaration' | ||
|| node.parent.type === 'ExportNamedDeclaration' | ||
|| node.parent.type === 'ExportAllDeclaration' | ||
) && node.parent.source === node | ||
) | ||
|| (node.parent.type === 'Property' && !node.parent.computed && node.parent.key === node) | ||
|| (node.parent.type === 'JSXAttribute' && node.parent.value === node) | ||
) { | ||
return; | ||
} | ||
|
||
const {raw} = node; | ||
if ( | ||
raw.at(-2) === BACKSLASH | ||
|| !raw.includes(BACKSLASH + BACKSLASH) | ||
|| raw.includes('`') | ||
|| raw.includes('${') | ||
|| node.loc.start.line !== node.loc.end.line | ||
) { | ||
return; | ||
} | ||
|
||
const unescaped = unescapeBackslash(raw); | ||
if (unescaped !== node.value) { | ||
return; | ||
} | ||
|
||
return { | ||
node, | ||
messageId: MESSAGE_ID, | ||
* fix(fixer) { | ||
yield fixer.replaceText(node, `String.raw\`${unescaped}\``); | ||
yield * fixSpaceAroundKeyword(fixer, node, context.sourceCode); | ||
}, | ||
}; | ||
}); | ||
}; | ||
|
||
/** @type {import('eslint').Rule.RuleModule} */ | ||
module.exports = { | ||
create, | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: 'Prefer using the `String.raw` tag to avoid escaping `\\`.', | ||
recommended: true, | ||
}, | ||
fixable: 'code', | ||
messages, | ||
}, | ||
}; |
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
Oops, something went wrong.