-
-
Notifications
You must be signed in to change notification settings - Fork 418
Add prefer-string-raw rule
#2339
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 17 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
7f34d4f
Init
fisker c1ed90b
Test
fisker 78ceb51
More tests
fisker 898b6a5
More test
fisker 3114625
Linting
fisker 20a7fb1
Fix
fisker fea5a27
Lint
fisker f8ba679
Edge cases
fisker 94e5d90
Ignore String contains `${`
fisker 672ef7a
Linting
fisker 586eb4c
simplify
fisker a04c8a9
Disable `unicorn/escape-case`
fisker 6068974
Test
fisker 0257f7c
Disable `unicorn/no-hex-escape`
fisker 00c2a98
Turn off `reportUnusedDisableDirectives`
fisker d8a4978
Fix codebase
fisker 9bc7b93
Revert "Fix codebase"
fisker a298fc0
Update description
fisker 48426c6
Add docs
fisker 6127df9
Minor tweak
fisker 2207f95
Merge branch 'main' into prefer-string-raw
sindresorhus f590709
Update prefer-string-raw.md
sindresorhus 3fcf69d
Revert "Revert "Fix codebase""
fisker 977a9d8
Return earlier
fisker 297e874
Fix
fisker File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,22 @@ | ||
| # 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). | ||
|
|
||
| 🔧 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` --> | ||
|
|
||
| <!-- Remove this comment, add more detailed description. --> | ||
fisker marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ## Fail | ||
|
|
||
| ```js | ||
| const foo = 'unicorn'; | ||
| ``` | ||
|
|
||
| ## Pass | ||
|
|
||
| ```js | ||
| const foo = '🦄'; | ||
| ``` | ||
fisker marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,92 @@ | ||
| '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 = '\\'; | ||
sindresorhus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| 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.includes(BACKSLASH + BACKSLASH) | ||
| || raw.includes('`') | ||
| || raw.includes('${') | ||
| || node.loc.start.line !== node.loc.end.line | ||
| ) { | ||
| return; | ||
| } | ||
|
|
||
| const unescaped = unescapeBackslash(raw); | ||
| if (unescaped.endsWith(BACKSLASH) || 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 `String.raw` tag to avoid escaping `\\`.', | ||
| recommended: true, | ||
| }, | ||
| fixable: 'code', | ||
| messages, | ||
| }, | ||
| }; | ||
This file contains hidden or 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,40 @@ | ||
| import outdent from 'outdent'; | ||
| import {getTester} from './utils/test.mjs'; | ||
|
|
||
| const {test} = getTester(import.meta); | ||
|
|
||
| test.snapshot({ | ||
| valid: [ | ||
| String.raw`a = '\''`, | ||
| // Cannot use `String.raw` | ||
| String.raw`'a\\b'`, | ||
| String.raw`import foo from "./foo\\bar.js";`, | ||
| String.raw`export {foo} from "./foo\\bar.js";`, | ||
| String.raw`export * from "./foo\\bar.js";`, | ||
| String.raw`a = {'a\\b': ''}`, | ||
| outdent` | ||
| a = "\\\\a \\ | ||
| b" | ||
| `, | ||
| String.raw`a = 'a\\b\u{51}c'`, | ||
| 'a = "a\\\\b`"', | ||
| // eslint-disable-next-line no-template-curly-in-string | ||
| 'a = "a\\\\b${foo}"', | ||
| { | ||
| code: String.raw`<Component attribute="a\\b" />`, | ||
| languageOptions: { | ||
| parserOptions: { | ||
| ecmaFeatures: { | ||
| jsx: true, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| ], | ||
| invalid: [ | ||
| String.raw`a = 'a\\b'`, | ||
| String.raw`a = {['a\\b']: b}`, | ||
| String.raw`function a() {return'a\\b'}`, | ||
| String.raw`const foo = "foo \\x46";`, | ||
| ], | ||
| }); |
This file contains hidden or 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 hidden or 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,89 @@ | ||
| # Snapshot report for `test/prefer-string-raw.mjs` | ||
|
|
||
| The actual snapshot is saved in `prefer-string-raw.mjs.snap`. | ||
|
|
||
| Generated by [AVA](https://avajs.dev). | ||
|
|
||
| ## invalid(1): a = 'a\\b' | ||
|
|
||
| > Input | ||
|
|
||
| `␊ | ||
| 1 | a = 'a\\\\b'␊ | ||
| ` | ||
|
|
||
| > Output | ||
|
|
||
| `␊ | ||
| 1 | a = String.raw\`a\\b\`␊ | ||
| ` | ||
|
|
||
| > Error 1/1 | ||
|
|
||
| `␊ | ||
| > 1 | a = 'a\\\\b'␊ | ||
| | ^^^^^^ \`String.raw\` should be used to avoid escaping \`\\\`.␊ | ||
| ` | ||
|
|
||
| ## invalid(2): a = {['a\\b']: b} | ||
|
|
||
| > Input | ||
|
|
||
| `␊ | ||
| 1 | a = {['a\\\\b']: b}␊ | ||
| ` | ||
|
|
||
| > Output | ||
|
|
||
| `␊ | ||
| 1 | a = {[String.raw\`a\\b\`]: b}␊ | ||
| ` | ||
|
|
||
| > Error 1/1 | ||
|
|
||
| `␊ | ||
| > 1 | a = {['a\\\\b']: b}␊ | ||
| | ^^^^^^ \`String.raw\` should be used to avoid escaping \`\\\`.␊ | ||
| ` | ||
|
|
||
| ## invalid(3): function a() {return'a\\b'} | ||
|
|
||
| > Input | ||
|
|
||
| `␊ | ||
| 1 | function a() {return'a\\\\b'}␊ | ||
| ` | ||
|
|
||
| > Output | ||
|
|
||
| `␊ | ||
| 1 | function a() {return String.raw\`a\\b\`}␊ | ||
| ` | ||
|
|
||
| > Error 1/1 | ||
|
|
||
| `␊ | ||
| > 1 | function a() {return'a\\\\b'}␊ | ||
| | ^^^^^^ \`String.raw\` should be used to avoid escaping \`\\\`.␊ | ||
| ` | ||
|
|
||
| ## invalid(4): const foo = "foo \\x46"; | ||
|
|
||
| > Input | ||
|
|
||
| `␊ | ||
| 1 | const foo = "foo \\\\x46";␊ | ||
| ` | ||
|
|
||
| > Output | ||
|
|
||
| `␊ | ||
| 1 | const foo = String.raw\`foo \\x46\`;␊ | ||
| ` | ||
|
|
||
| > Error 1/1 | ||
|
|
||
| `␊ | ||
| > 1 | const foo = "foo \\\\x46";␊ | ||
| | ^^^^^^^^^^^ \`String.raw\` should be used to avoid escaping \`\\\`.␊ | ||
| ` |
Binary file not shown.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.