Skip to content

Commit 6e7f93c

Browse files
authored
prefer-string-raw: Exclude inline snapshot (#2811)
1 parent f39b02a commit 6e7f93c

File tree

6 files changed

+68
-18
lines changed

6 files changed

+68
-18
lines changed

rules/prefer-string-raw.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {isStringLiteral, isDirective} from './ast/index.js';
22
import {fixSpaceAroundKeyword, replaceTemplateElement} from './fix/index.js';
3+
import isJestInlineSnapshot from './shared/is-jest-inline-snapshot.js';
34

45
const MESSAGE_ID = 'prefer-string-raw';
56
const messages = {
@@ -72,7 +73,7 @@ function isStringRawRestricted(node) {
7273
/** @param {import('eslint').Rule.RuleContext} context */
7374
const create = context => {
7475
context.on('Literal', node => {
75-
if (!isStringLiteral(node) || isStringRawRestricted(node)) {
76+
if (!isStringLiteral(node) || isStringRawRestricted(node) || isJestInlineSnapshot(node)) {
7677
return;
7778
}
7879

@@ -108,6 +109,7 @@ const create = context => {
108109
(node.parent.type === 'TaggedTemplateExpression' && node.parent.quasi === node)
109110
|| node.quasis.every(({value: {cooked, raw}}) => cooked === raw)
110111
|| node.quasis.some(({value: {cooked, raw}}) => cooked.at(-1) === BACKSLASH || unescapeBackslash(raw) !== cooked)
112+
|| isJestInlineSnapshot(node)
111113
) {
112114
return;
113115
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import {isMethodCall, isCallExpression} from '../ast/index.js';
2+
3+
const isJestInlineSnapshot = node =>
4+
isMethodCall(node.parent, {
5+
method: 'toMatchInlineSnapshot',
6+
argumentsLength: 1,
7+
optionalCall: false,
8+
optionalMember: false,
9+
})
10+
&& node.parent.arguments[0] === node
11+
&& isCallExpression(node.parent.callee.object, {
12+
name: 'expect',
13+
argumentsLength: 1,
14+
optionalCall: false,
15+
optionalMember: false,
16+
});
17+
18+
export default isJestInlineSnapshot;

rules/template-indent.js

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,15 @@ import stripIndent from 'strip-indent';
22
import indentString from 'indent-string';
33
import esquery from 'esquery';
44
import {replaceTemplateElement} from './fix/index.js';
5-
import {isMethodCall, isCallExpression, isTaggedTemplateLiteral} from './ast/index.js';
5+
import {isTaggedTemplateLiteral} from './ast/index.js';
66
import {isNodeMatches} from './utils/index.js';
7+
import isJestInlineSnapshot from './shared/is-jest-inline-snapshot.js';
78

89
const MESSAGE_ID_IMPROPERLY_INDENTED_TEMPLATE = 'template-indent';
910
const messages = {
1011
[MESSAGE_ID_IMPROPERLY_INDENTED_TEMPLATE]: 'Templates should be properly indented.',
1112
};
1213

13-
const isJestInlineSnapshot = node =>
14-
isMethodCall(node.parent, {
15-
method: 'toMatchInlineSnapshot',
16-
argumentsLength: 1,
17-
optionalCall: false,
18-
optionalMember: false,
19-
})
20-
&& node.parent.arguments[0] === node
21-
&& isCallExpression(node.parent.callee.object, {
22-
name: 'expect',
23-
argumentsLength: 1,
24-
optionalCall: false,
25-
optionalMember: false,
26-
});
27-
2814
const parsedEsquerySelectors = new Map();
2915
const parseEsquerySelector = selector => {
3016
if (!parsedEsquerySelectors.has(selector)) {

test/prefer-string-raw.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,13 @@ test.snapshot({
141141
String.raw`type T = '${TEST_STRING}';`,
142142
...keyTestsComputedIsInvalid,
143143
...keyTestsComputedIsValid.flatMap(code => [code, toComputed(code)]),
144+
`expect(foo).toMatchInlineSnapshot('${TEST_STRING}')`,
145+
`expect(foo).toMatchInlineSnapshot(\`${TEST_STRING}\`)`,
146+
],
147+
invalid: [
148+
...keyTestsComputedIsInvalid.map(code => toComputed(code)),
149+
`expect('${TEST_STRING}').toMatchInlineSnapshot("")`,
150+
`expect(\`${TEST_STRING}\`).toMatchInlineSnapshot(\`\`)`,
144151
],
145-
invalid: keyTestsComputedIsInvalid.map(code => toComputed(code)),
146152
});
147153

test/snapshots/prefer-string-raw.js.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,3 +387,41 @@ Generated by [AVA](https://avajs.dev).
387387
Output:␊
388388
1 | class C { accessor [String.raw\`a\\b\`] = 1 }␊
389389
`
390+
391+
## invalid(5): expect('a\\b').toMatchInlineSnapshot("")
392+
393+
> Input
394+
395+
`␊
396+
1 | expect('a\\\\b').toMatchInlineSnapshot("")␊
397+
`
398+
399+
> Error 1/1
400+
401+
`␊
402+
Message:␊
403+
> 1 | expect('a\\\\b').toMatchInlineSnapshot("")␊
404+
| ^^^^^^ \`String.raw\` should be used to avoid escaping \`\\\`.␊
405+
406+
Output:␊
407+
1 | expect(String.raw\`a\\b\`).toMatchInlineSnapshot("")␊
408+
`
409+
410+
## invalid(6): expect(`a\\b`).toMatchInlineSnapshot(``)
411+
412+
> Input
413+
414+
`␊
415+
1 | expect(\`a\\\\b\`).toMatchInlineSnapshot(\`\`)␊
416+
`
417+
418+
> Error 1/1
419+
420+
`␊
421+
Message:␊
422+
> 1 | expect(\`a\\\\b\`).toMatchInlineSnapshot(\`\`)␊
423+
| ^^^^^^ \`String.raw\` should be used to avoid escaping \`\\\`.␊
424+
425+
Output:␊
426+
1 | expect(String.raw\`a\\b\`).toMatchInlineSnapshot(\`\`)␊
427+
`
90 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)