Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion rules/prefer-string-raw.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {isStringLiteral, isDirective} from './ast/index.js';
import {fixSpaceAroundKeyword, replaceTemplateElement} from './fix/index.js';
import isJestInlineSnapshot from './shared/is-jest-inline-snapshot.js';

const MESSAGE_ID = 'prefer-string-raw';
const messages = {
Expand Down Expand Up @@ -72,7 +73,7 @@ function isStringRawRestricted(node) {
/** @param {import('eslint').Rule.RuleContext} context */
const create = context => {
context.on('Literal', node => {
if (!isStringLiteral(node) || isStringRawRestricted(node)) {
if (!isStringLiteral(node) || isStringRawRestricted(node) || isJestInlineSnapshot(node)) {
return;
}

Expand Down Expand Up @@ -108,6 +109,7 @@ const create = context => {
(node.parent.type === 'TaggedTemplateExpression' && node.parent.quasi === node)
|| node.quasis.every(({value: {cooked, raw}}) => cooked === raw)
|| node.quasis.some(({value: {cooked, raw}}) => cooked.at(-1) === BACKSLASH || unescapeBackslash(raw) !== cooked)
|| isJestInlineSnapshot(node)
) {
return;
}
Expand Down
18 changes: 18 additions & 0 deletions rules/shared/is-jest-inline-snapshot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {isMethodCall, isCallExpression} from '../ast/index.js';

const isJestInlineSnapshot = node =>
isMethodCall(node.parent, {
method: 'toMatchInlineSnapshot',
argumentsLength: 1,
optionalCall: false,
optionalMember: false,
})
&& node.parent.arguments[0] === node
&& isCallExpression(node.parent.callee.object, {
name: 'expect',
argumentsLength: 1,
optionalCall: false,
optionalMember: false,
});

export default isJestInlineSnapshot;
18 changes: 2 additions & 16 deletions rules/template-indent.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,15 @@ import stripIndent from 'strip-indent';
import indentString from 'indent-string';
import esquery from 'esquery';
import {replaceTemplateElement} from './fix/index.js';
import {isMethodCall, isCallExpression, isTaggedTemplateLiteral} from './ast/index.js';
import {isTaggedTemplateLiteral} from './ast/index.js';
import {isNodeMatches} from './utils/index.js';
import isJestInlineSnapshot from './shared/is-jest-inline-snapshot.js';

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

const isJestInlineSnapshot = node =>
isMethodCall(node.parent, {
method: 'toMatchInlineSnapshot',
argumentsLength: 1,
optionalCall: false,
optionalMember: false,
})
&& node.parent.arguments[0] === node
&& isCallExpression(node.parent.callee.object, {
name: 'expect',
argumentsLength: 1,
optionalCall: false,
optionalMember: false,
});

const parsedEsquerySelectors = new Map();
const parseEsquerySelector = selector => {
if (!parsedEsquerySelectors.has(selector)) {
Expand Down
8 changes: 7 additions & 1 deletion test/prefer-string-raw.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,13 @@ test.snapshot({
String.raw`type T = '${TEST_STRING}';`,
...keyTestsComputedIsInvalid,
...keyTestsComputedIsValid.flatMap(code => [code, toComputed(code)]),
`expect(foo).toMatchInlineSnapshot('${TEST_STRING}')`,
`expect(foo).toMatchInlineSnapshot(\`${TEST_STRING}\`)`,
],
invalid: [
...keyTestsComputedIsInvalid.map(code => toComputed(code)),
`expect('${TEST_STRING}').toMatchInlineSnapshot("")`,
`expect(\`${TEST_STRING}\`).toMatchInlineSnapshot(\`\`)`,
],
invalid: keyTestsComputedIsInvalid.map(code => toComputed(code)),
});

38 changes: 38 additions & 0 deletions test/snapshots/prefer-string-raw.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -387,3 +387,41 @@ Generated by [AVA](https://avajs.dev).
Output:␊
1 | class C { accessor [String.raw\`a\\b\`] = 1 }␊
`

## invalid(5): expect('a\\b').toMatchInlineSnapshot("")

> Input

`␊
1 | expect('a\\\\b').toMatchInlineSnapshot("")␊
`

> Error 1/1

`␊
Message:␊
> 1 | expect('a\\\\b').toMatchInlineSnapshot("")␊
| ^^^^^^ \`String.raw\` should be used to avoid escaping \`\\\`.␊
Output:␊
1 | expect(String.raw\`a\\b\`).toMatchInlineSnapshot("")␊
`

## invalid(6): expect(`a\\b`).toMatchInlineSnapshot(``)

> Input

`␊
1 | expect(\`a\\\\b\`).toMatchInlineSnapshot(\`\`)␊
`

> Error 1/1

`␊
Message:␊
> 1 | expect(\`a\\\\b\`).toMatchInlineSnapshot(\`\`)␊
| ^^^^^^ \`String.raw\` should be used to avoid escaping \`\\\`.␊
Output:␊
1 | expect(String.raw\`a\\b\`).toMatchInlineSnapshot(\`\`)␊
`
Binary file modified test/snapshots/prefer-string-raw.js.snap
Binary file not shown.
Loading